/*
 * SGWindowDialog.java
 *
 * Created on 2004/06/09, 16:23
 */

package jp.riken.brain.ni.samuraigraph.base;

import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;


/**
 *
 * @author  kuromaru
 */
public class SGWindowDialog  extends SGPropertyDialog
{

	/**
	 * 
	 */
	public static final String TITLE = "Window Dialog";


    
    /** Creates new form SGWindowDialog */
    public SGWindowDialog(java.awt.Frame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();
        this.initProperty();
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        mWindowPanel = new javax.swing.JPanel();
        mWidthLabel = new javax.swing.JLabel();
        mHeightLabel = new javax.swing.JLabel();
        mWidthSpinner = new jp.riken.brain.ni.samuraigraph.base.SGSpinner();
        mHeightSpinner = new jp.riken.brain.ni.samuraigraph.base.SGSpinner();
        mSizeLabel = new javax.swing.JLabel();
        mColorLabel = new javax.swing.JLabel();
        mIntervalLabel = new javax.swing.JLabel();
        mGridLinesIntervalSpinner = new jp.riken.brain.ni.samuraigraph.base.SGSpinner();
        mBottomLabel = new javax.swing.JLabel();
        mGridLinesWidthSpinner = new jp.riken.brain.ni.samuraigraph.base.SGSpinner();
        mGridLabel = new javax.swing.JLabel();
        mGridLinesColorButton = new javax.swing.JButton();
        mBackgroundColorLabel1 = new javax.swing.JLabel();
        mGridLinesColorLabel = new javax.swing.JLabel();
        mBackgroundColorButton = new javax.swing.JButton();
        mSizeSeparator = new javax.swing.JSeparator();
        mGridSeparator = new javax.swing.JSeparator();
        mColorSeparator = new javax.swing.JSeparator();
        mLeftSpaceLabel = new javax.swing.JLabel();
        mRightSpaceLabel = new javax.swing.JLabel();
        mButtonPanel = new javax.swing.JPanel();
        mOKButton = new javax.swing.JButton();
        mCancelButton = new javax.swing.JButton();
        mPreviewButton = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        mWindowPanel.setLayout(new java.awt.GridBagLayout());

        mWindowPanel.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        mWidthLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mWidthLabel.setText("Width");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        mWindowPanel.add(mWidthLabel, gridBagConstraints);

        mHeightLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mHeightLabel.setText("Height");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 5, 5);
        mWindowPanel.add(mHeightLabel, gridBagConstraints);

        mWidthSpinner.setPreferredSize(new java.awt.Dimension(65, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mWindowPanel.add(mWidthSpinner, gridBagConstraints);

        mHeightSpinner.setPreferredSize(new java.awt.Dimension(65, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mWindowPanel.add(mHeightSpinner, gridBagConstraints);

        mSizeLabel.setFont(new java.awt.Font("Dialog", 3, 12));
        mSizeLabel.setText("Size");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 5);
        mWindowPanel.add(mSizeLabel, gridBagConstraints);

        mColorLabel.setFont(new java.awt.Font("Dialog", 3, 12));
        mColorLabel.setText("Color");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        mWindowPanel.add(mColorLabel, gridBagConstraints);

        mIntervalLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mIntervalLabel.setText("Interval");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        mWindowPanel.add(mIntervalLabel, gridBagConstraints);

        mGridLinesIntervalSpinner.setPreferredSize(new java.awt.Dimension(65, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mWindowPanel.add(mGridLinesIntervalSpinner, gridBagConstraints);

        mBottomLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mBottomLabel.setText("Line width");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 5, 5);
        mWindowPanel.add(mBottomLabel, gridBagConstraints);

        mGridLinesWidthSpinner.setPreferredSize(new java.awt.Dimension(65, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mWindowPanel.add(mGridLinesWidthSpinner, gridBagConstraints);

        mGridLabel.setFont(new java.awt.Font("Dialog", 3, 12));
        mGridLabel.setText("Grid");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        mWindowPanel.add(mGridLabel, gridBagConstraints);

        mGridLinesColorButton.setPreferredSize(new java.awt.Dimension(65, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 5);
        mWindowPanel.add(mGridLinesColorButton, gridBagConstraints);

        mBackgroundColorLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
        mBackgroundColorLabel1.setText("Background");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 10, 5);
        mWindowPanel.add(mBackgroundColorLabel1, gridBagConstraints);

        mGridLinesColorLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mGridLinesColorLabel.setText("Grid");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 10, 5);
        mWindowPanel.add(mGridLinesColorLabel, gridBagConstraints);

        mBackgroundColorButton.setPreferredSize(new java.awt.Dimension(65, 20));
        mBackgroundColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mBackgroundColorButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 5);
        mWindowPanel.add(mBackgroundColorButton, gridBagConstraints);

        mSizeSeparator.setRequestFocusEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(10, 35, 0, 0);
        mWindowPanel.add(mSizeSeparator, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 35, 0, 0);
        mWindowPanel.add(mGridSeparator, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 42, 0, 0);
        mWindowPanel.add(mColorSeparator, gridBagConstraints);

        mLeftSpaceLabel.setText("    ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        mWindowPanel.add(mLeftSpaceLabel, gridBagConstraints);

        mRightSpaceLabel.setText("    ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        mWindowPanel.add(mRightSpaceLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(mWindowPanel, gridBagConstraints);

        mOKButton.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        mOKButton.setText("OK");
        mButtonPanel.add(mOKButton);

        mCancelButton.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        mCancelButton.setText("Cancel");
        mButtonPanel.add(mCancelButton);

        mPreviewButton.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        mPreviewButton.setText("Preview");
        mButtonPanel.add(mPreviewButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(mButtonPanel, gridBagConstraints);

        pack();
    }//GEN-END:initComponents

    private void mBackgroundColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mBackgroundColorButtonActionPerformed
        // ここに処理コードを追加:
    }//GEN-LAST:event_mBackgroundColorButtonActionPerformed

    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        // ここに処理コードを追加:
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new SGWindowDialog(new javax.swing.JFrame(), true).show();
    }
    
    // 変数宣言 - 編集不可//GEN-BEGIN:variables
    private javax.swing.JButton mBackgroundColorButton;
    private javax.swing.JLabel mBackgroundColorLabel1;
    private javax.swing.JLabel mBottomLabel;
    private javax.swing.JPanel mButtonPanel;
    private javax.swing.JButton mCancelButton;
    private javax.swing.JLabel mColorLabel;
    private javax.swing.JSeparator mColorSeparator;
    private javax.swing.JLabel mGridLabel;
    private javax.swing.JButton mGridLinesColorButton;
    private javax.swing.JLabel mGridLinesColorLabel;
    private jp.riken.brain.ni.samuraigraph.base.SGSpinner mGridLinesIntervalSpinner;
    private jp.riken.brain.ni.samuraigraph.base.SGSpinner mGridLinesWidthSpinner;
    private javax.swing.JSeparator mGridSeparator;
    private javax.swing.JLabel mHeightLabel;
    private jp.riken.brain.ni.samuraigraph.base.SGSpinner mHeightSpinner;
    private javax.swing.JLabel mIntervalLabel;
    private javax.swing.JLabel mLeftSpaceLabel;
    private javax.swing.JButton mOKButton;
    private javax.swing.JButton mPreviewButton;
    private javax.swing.JLabel mRightSpaceLabel;
    private javax.swing.JLabel mSizeLabel;
    private javax.swing.JSeparator mSizeSeparator;
    private javax.swing.JLabel mWidthLabel;
    private jp.riken.brain.ni.samuraigraph.base.SGSpinner mWidthSpinner;
    private javax.swing.JPanel mWindowPanel;
    // 変数宣言の終わり//GEN-END:variables
    


    /**
	 * 
	 */
	private boolean initProperty()
	{

		// set the title
		this.setTitle( SGWindowDialog.TITLE );


		//
		// spinner
		//

		this.mWidthSpinner.setDigitForRoundingOut(-2);
		this.mWidthSpinner.initProperties(
			new SpinnerNumberModel( 0.0, 0.0, 50.0, 1.0 ),
			SGSpinner.SPINNER_UNIT_CM	);
		this.mWidthSpinner.setMinimumFractionDigits(1);
		this.mWidthSpinner.setMaximumFractionDigits(1);

		this.mHeightSpinner.setDigitForRoundingOut(-2);
		this.mHeightSpinner.initProperties(
			new SpinnerNumberModel( 0.0, 0.0, 50.0, 1.0 ),
			SGSpinner.SPINNER_UNIT_CM	);
		this.mHeightSpinner.setMinimumFractionDigits(1);
		this.mHeightSpinner.setMaximumFractionDigits(1);

		this.mGridLinesIntervalSpinner.initProperties(
			new SpinnerNumberModel( 1.0, 0.50, 10.0, 0.50 ),
			SGSpinner.SPINNER_UNIT_CM	);

		this.mGridLinesWidthSpinner.initProperties(
			this.getLineWidthSpinnerNumberModel(),
			SGSpinner.SPINNER_UNIT_PT	);


		//
		// add action listeners
		//

{
	this.mOKButton.addActionListener(this);
	this.mCancelButton.addActionListener(this);
	this.mPreviewButton.addActionListener(this);
}

		this.mBackgroundColorButton.addActionListener(this);
		this.mGridLinesColorButton.addActionListener(this);


		// フォーカスされた色属性ボタンの初期化
		this.mFocusedColorSetButton = this.mBackgroundColorButton;


		return true;
	}



	/**
	 * 
	 */
	public Number getWindowWidth()
	{
		return this.mWidthSpinner.getNumber();
	}

	/**
	 * 
	 */
	public Number getWindowHeight()
	{
		return this.mHeightSpinner.getNumber();
	}

	/**
	 * 
	 */
	public Color getWindowBackgroundColor()
	{
		return this.mBackgroundColorButton.getBackground();
	}

	/**
	 * 
	 */    
	public Color getWindowGridLinesColor()
	{
		return this.mGridLinesColorButton.getBackground();
	}


	/**
	 * 
	 */
	public Number getWindowGridLinesInterval()
	{
		return this.mGridLinesIntervalSpinner.getNumber();
	}


	/**
	 * 
	 */
	public Number getWindowGridLinesWidth()
	{
		return this.mGridLinesWidthSpinner.getNumber();
	}

	/**
	 * 
	 */
	public boolean setWindowWidth( final float w )
	{
		this.mWidthSpinner.setValue( new Double( w ) );
		return true;
	}

	/**
	 * 
	 */
	public boolean setWindowHeight( final float h )
	{
		this.mHeightSpinner.setValue( new Double( h ) );
		return true;
	}

	/**
	 * 
	 */
	public boolean setWindowGridLinesInterval( final float interval )
	{
		this.mGridLinesIntervalSpinner.setValue( new Double( interval ) );
		return true;
	}

	/**
	 * 
	 */
	public boolean setWindowGridLinesWidth( final float width )
	{
		this.mGridLinesWidthSpinner.setValue( new Double( width ) );
		return true;
	}

	/**
	 * 
	 */
	public boolean setWindowBackgroundColor( final Color cl )
	{
		this.mBackgroundColorButton.setBackground( cl );
		return true;
	}

	/**
	 * 
	 */
	public boolean setWindowGridLinesColor( final Color cl )
	{
		this.mGridLinesColorButton.setBackground( cl );
		return true;
	}


	/**
	 * 
	 */
	public ArrayList getColorSelectionComponentList()
	{
		final ArrayList list = new ArrayList();
		list.add(this.mBackgroundColorButton);
		list.add(this.mGridLinesColorButton);
		return list;
	}


	/**
	 * 
	 */
	public ArrayList getActiveComponentList()
	{
		final ArrayList list = this.getActiveComponentListFromSpinners();
		return list;
	}


	/**
	 * 
	 */
	public ArrayList getSpinnerList()
	{
		ArrayList list = new ArrayList();

		list.add( this.mWidthSpinner );
		list.add( this.mHeightSpinner );
		list.add( this.mGridLinesIntervalSpinner );
		list.add( this.mGridLinesWidthSpinner );

		return list;
	}



	/**
	 * 
	 */
	public SGDrawingWindow.WindowProperties getProperties()
	{

		SGDrawingWindow.WindowProperties p
			= new SGDrawingWindow.WindowProperties();


		// paper width
		final Number width = this.getWindowWidth();
		if( width==null )
		{
			SGUtility.showIllegalInputErrorMessageDialog( this );
			return null;
		}
		else
		{
			p.setPaperWidth( width.floatValue()/SGConstants.CM_POINT_RATIO );
		}


		// paper height
		final Number height = this.getWindowHeight();
		if( height==null )
		{
			SGUtility.showIllegalInputErrorMessageDialog( this );
			return null;
		}
		else
		{
			p.setPaperHeight( height.floatValue()/SGConstants.CM_POINT_RATIO );
		}


		// grind line interval
		final Number gridLineInterval = this.getWindowGridLinesInterval();
		if( gridLineInterval==null )
		{
			SGUtility.showIllegalInputErrorMessageDialog( this );
			return null;
		}
		else
		{
			p.setGridInterval( gridLineInterval.floatValue()/SGConstants.CM_POINT_RATIO );
		}


		// grid line width
		final Number gridLineWidth = this.getWindowGridLinesWidth();
		if( gridLineWidth==null )
		{
			SGUtility.showIllegalInputErrorMessageDialog( this );
			return null;
		}
		else
		{
			p.setGridLineWidth( gridLineWidth.floatValue() );
		}


		// grid line color
		Color gridLineColor = this.getWindowGridLinesColor();
		p.setGridColor( gridLineColor );


		//
		Color bgColor = this.getWindowBackgroundColor();
		p.setBackGroundColor( bgColor );


		return p;
	}




}
