/*
 * SGArrowDialog.java
 *
 * Created on 2005/05/23, 17:17
 */

package jp.riken.brain.ni.samuraigraph.figure.java2d;

import java.util.ArrayList;

import javax.swing.JRadioButton;

import jp.riken.brain.ni.samuraigraph.base.SGIPropertyDialogObserver;
import jp.riken.brain.ni.samuraigraph.base.SGPropertyDialog;
import jp.riken.brain.ni.samuraigraph.base.SGTwoAxesRadioButtonManager;

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

	/**
	 * 
	 */
	public static final String TITLE = "Arrow";

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

    
    /** 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;

        mCommonPanel = new javax.swing.JPanel();
        mLeftTopRadioButton = new javax.swing.JRadioButton();
        mLeftBottomRadioButton = new javax.swing.JRadioButton();
        mRightTopRadioButton = new javax.swing.JRadioButton();
        mRightBottomRadioButton = new javax.swing.JRadioButton();
        mScaleReferenceLabel2 = new javax.swing.JLabel();
        mScaleReferenceLabel1 = new javax.swing.JLabel();
        mButtonPanel = new javax.swing.JPanel();
        mOKButton = new javax.swing.JButton();
        mCancelButton = new javax.swing.JButton();
        mPreviewButton = new javax.swing.JButton();
        mPanel = new javax.swing.JPanel();
        mFormLabel = new javax.swing.JLabel();
        mFormSeparator = new javax.swing.JSeparator();
        mLeftSpaceLabel = new javax.swing.JLabel();
        mLocationLabel = new javax.swing.JLabel();
        mLocationSeparator = new javax.swing.JSeparator();
        mArrowPanelContainer = new javax.swing.JPanel();
        mLocationPanel = new javax.swing.JPanel();
        mLeftXValueLabel = new javax.swing.JLabel();
        mStartXValueTextField = new jp.riken.brain.ni.samuraigraph.base.SGTextField();
        mRightXValueLabel = new javax.swing.JLabel();
        mEndXValueTextField = new jp.riken.brain.ni.samuraigraph.base.SGTextField();
        mLeftYValueLabel = new javax.swing.JLabel();
        mStartYValueTextField = new jp.riken.brain.ni.samuraigraph.base.SGTextField();
        mRightYValueLabel = new javax.swing.JLabel();
        mEndYValueTextField = new jp.riken.brain.ni.samuraigraph.base.SGTextField();
        mRightSpaceLabel = new javax.swing.JLabel();

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

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        mCommonPanel.setLayout(new java.awt.GridBagLayout());

        mLeftTopRadioButton.setFont(new java.awt.Font("Dialog", 1, 12));
        mLeftTopRadioButton.setText("Left/Top");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mCommonPanel.add(mLeftTopRadioButton, gridBagConstraints);

        mLeftBottomRadioButton.setFont(new java.awt.Font("Dialog", 1, 12));
        mLeftBottomRadioButton.setText("Left/Bottom");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mCommonPanel.add(mLeftBottomRadioButton, gridBagConstraints);

        mRightTopRadioButton.setFont(new java.awt.Font("Dialog", 1, 12));
        mRightTopRadioButton.setText("Right/Top");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mCommonPanel.add(mRightTopRadioButton, gridBagConstraints);

        mRightBottomRadioButton.setFont(new java.awt.Font("Dialog", 1, 12));
        mRightBottomRadioButton.setText("Right/Bottom");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mCommonPanel.add(mRightBottomRadioButton, gridBagConstraints);

        mScaleReferenceLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
        mScaleReferenceLabel2.setText("(Value <-> Scale)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        mCommonPanel.add(mScaleReferenceLabel2, gridBagConstraints);

        mScaleReferenceLabel1.setFont(new java.awt.Font("Dialog", 1, 12));
        mScaleReferenceLabel1.setText("Scale Reference");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 7;
        mCommonPanel.add(mScaleReferenceLabel1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(mCommonPanel, gridBagConstraints);

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

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

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

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

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

        mPanel.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        mFormLabel.setFont(new java.awt.Font("Dialog", 3, 12));
        mFormLabel.setText("Form");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mPanel.add(mFormLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        mPanel.add(mFormSeparator, gridBagConstraints);

        mLeftSpaceLabel.setText("    ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        mPanel.add(mLeftSpaceLabel, gridBagConstraints);

        mLocationLabel.setFont(new java.awt.Font("Dialog", 3, 12));
        mLocationLabel.setText("Location");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 5);
        mPanel.add(mLocationLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 4, 5, 5);
        mPanel.add(mLocationSeparator, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        mPanel.add(mArrowPanelContainer, gridBagConstraints);

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

        mLeftXValueLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mLeftXValueLabel.setText("Start X");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        mLocationPanel.add(mLeftXValueLabel, gridBagConstraints);

        mStartXValueTextField.setColumns(6);
        mStartXValueTextField.setFont(new java.awt.Font("Dialog", 0, 12));
        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);
        mLocationPanel.add(mStartXValueTextField, gridBagConstraints);

        mRightXValueLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mRightXValueLabel.setText("End X");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 5, 5);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        mLocationPanel.add(mRightXValueLabel, gridBagConstraints);

        mEndXValueTextField.setColumns(6);
        mEndXValueTextField.setFont(new java.awt.Font("Dialog", 0, 12));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mLocationPanel.add(mEndXValueTextField, gridBagConstraints);

        mLeftYValueLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mLeftYValueLabel.setText("Start Y");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        mLocationPanel.add(mLeftYValueLabel, gridBagConstraints);

        mStartYValueTextField.setColumns(6);
        mStartYValueTextField.setFont(new java.awt.Font("Dialog", 0, 12));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mLocationPanel.add(mStartYValueTextField, gridBagConstraints);

        mRightYValueLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        mRightYValueLabel.setText("End Y");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 5, 5);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        mLocationPanel.add(mRightYValueLabel, gridBagConstraints);

        mEndYValueTextField.setColumns(6);
        mEndYValueTextField.setFont(new java.awt.Font("Dialog", 0, 12));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mLocationPanel.add(mEndYValueTextField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mPanel.add(mLocationPanel, gridBagConstraints);

        mRightSpaceLabel.setText("    ");
        mPanel.add(mRightSpaceLabel, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(mPanel, gridBagConstraints);

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

    // 変数宣言 - 編集不可//GEN-BEGIN:variables
    private javax.swing.JPanel mArrowPanelContainer;
    private javax.swing.JPanel mButtonPanel;
    private javax.swing.JButton mCancelButton;
    private javax.swing.JPanel mCommonPanel;
    private jp.riken.brain.ni.samuraigraph.base.SGTextField mEndXValueTextField;
    private jp.riken.brain.ni.samuraigraph.base.SGTextField mEndYValueTextField;
    private javax.swing.JLabel mFormLabel;
    private javax.swing.JSeparator mFormSeparator;
    private javax.swing.JRadioButton mLeftBottomRadioButton;
    private javax.swing.JLabel mLeftSpaceLabel;
    private javax.swing.JRadioButton mLeftTopRadioButton;
    private javax.swing.JLabel mLeftXValueLabel;
    private javax.swing.JLabel mLeftYValueLabel;
    private javax.swing.JLabel mLocationLabel;
    private javax.swing.JPanel mLocationPanel;
    private javax.swing.JSeparator mLocationSeparator;
    private javax.swing.JButton mOKButton;
    private javax.swing.JPanel mPanel;
    private javax.swing.JButton mPreviewButton;
    private javax.swing.JRadioButton mRightBottomRadioButton;
    private javax.swing.JLabel mRightSpaceLabel;
    private javax.swing.JRadioButton mRightTopRadioButton;
    private javax.swing.JLabel mRightXValueLabel;
    private javax.swing.JLabel mRightYValueLabel;
    private javax.swing.JLabel mScaleReferenceLabel1;
    private javax.swing.JLabel mScaleReferenceLabel2;
    private jp.riken.brain.ni.samuraigraph.base.SGTextField mStartXValueTextField;
    private jp.riken.brain.ni.samuraigraph.base.SGTextField mStartYValueTextField;
    // 変数宣言の終わり//GEN-END:variables



	private SGArrowPanel mArrowPanel = null;


	private void initProperty()
	{

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

		this.mArrowPanel = new SGArrowPanel();
		this.mArrowPanelContainer.add( this.mArrowPanel );

		this.pack();

		
		//
		// create a button group
		//
		
		this.mAxesButtonGroup = new SGTwoAxesRadioButtonManager(
			this.mLeftBottomRadioButton, this.mLeftTopRadioButton,
			this.mRightBottomRadioButton, this.mRightTopRadioButton,
			new JRadioButton() );
	
		this.mOKButton.addActionListener(this);
		this.mCancelButton.addActionListener(this);
		this.mPreviewButton.addActionListener(this);
	
	}


	/**
	 * 
	 */
	public ArrayList getColorSelectionButtonsList()
	{
		final ArrayList list = new ArrayList();
		list.addAll( this.mArrowPanel.getColorSelectionButtonsList() );
		return list;
	}


	/**
	 * 
	 */
	public ArrayList getTextFieldComponentsList()
	{
		final ArrayList list = this.getFormattedTextFieldsListFromSpinners();
		list.addAll( this.getNumberTextFieldList() );
		return list;
	}


	/**
	 * Returns a list of text fields to set number.
	 * @return
	 */
	public ArrayList getNumberTextFieldList()
	{
		final ArrayList list = new ArrayList();
		list.add( this.mStartXValueTextField );
		list.add( this.mEndXValueTextField );
		list.add( this.mStartYValueTextField );
		list.add( this.mEndYValueTextField );
		return list;
	}


	/**
	 * 
	 */
	public ArrayList getSpinnerList()
	{
		ArrayList list = new ArrayList();
		list.addAll( this.mArrowPanel.getSpinnerList() );
		return list;
	}


	private SGTwoAxesRadioButtonManager mAxesButtonGroup = null;



	/**
	 * 
	 * @param l
	 * @return
	 */
	public boolean addPropertyDialogObserver(
		final SGIPropertyDialogObserver l )
	{
		super.addPropertyDialogObserver(l);
		if( l instanceof SGIArrowPanelObserver )
		{
			this.mArrowPanel.addObserver( (SGIArrowPanelObserver)l );
		}
		return true;
	}


	/**
	 * 
	 * @param l
	 * @return
	 */
	public boolean removePropertyDialogObserver(
		final SGIPropertyDialogObserver l )
	{
		super.removePropertyDialogObserver(l);
		if( l instanceof SGIArrowPanelObserver )
		{
			this.mArrowPanel.removeObserver( (SGIArrowPanelObserver)l );
		}
		return true;
	}



	/**
	 * 
	 * @return
	 */
	public boolean removeAllPropertyDialogObserver()
	{
		super.removeAllPropertyDialogObserver();
		this.mArrowPanel.removeAllObserver();
		return true;
	}



	/**
	 * 
	 * @param value
	 */
	public boolean setStartXValue( final Object obj )
	{
		return this.setValue( this.mStartXValueTextField, obj );
	}


	/**
	 * 
	 * @param value
	 */
	public boolean setStartYValue( final Object obj )
	{
		return this.setValue( this.mStartYValueTextField, obj );
	}


	/**
	 * 
	 * @param value
	 */
	public boolean setEndXValue( final Object obj )
	{
		return this.setValue( this.mEndXValueTextField, obj );
	}


	/**
	 * 
	 * @param value
	 */
	public boolean setEndYValue( final Object obj )
	{
		return this.setValue( this.mEndYValueTextField, obj );
	}



	/**
	 * 
	 * @return
	 */
	public Number getStartXValue()
	{
		return this.getNumber( this.mStartXValueTextField );
	}

	
	/**
	 * 
	 * @return
	 */
	public Number getStartYValue()
	{
		return this.getNumber( this.mStartYValueTextField );
	}


	/**
	 * 
	 * @return
	 */
	public Number getEndXValue()
	{
		return this.getNumber( this.mEndXValueTextField );
	}

	
	/**
	 * 
	 * @return
	 */
	public Number getEndYValue()
	{
		return this.getNumber( this.mEndYValueTextField );
	}



	/**
	 * 
	 * @return
	 */
	public boolean setDialogProperty()
	{
		ArrayList list = this.mPropertyDialogObserverList;

		if( this.mAxesButtonGroup.setRadioButton( list ) == false )
		{
			return false;
		}

		// set to the arrow panel
		if( this.mArrowPanel.setComponentsProperty() == false )
		{
			return false;
		}


		SGIArrowDialogObserver arrow0
			= (SGIArrowDialogObserver)list.get(0);

		final double startXValue0 = arrow0.getStartXValue();
		final double startYValue0 = arrow0.getStartYValue();
		final double endXValue0 = arrow0.getEndXValue();
		final double endYValue0 = arrow0.getEndYValue();

		Double startXValue = new Double( startXValue0 );
		Double startYValue = new Double( startYValue0 );
		Double endXValue = new Double( endXValue0 );
		Double endYValue = new Double( endYValue0 );

		if( list.size() > 1 )
		{
			for( int ii=1; ii<list.size(); ii++ )
			{
				SGIArrowDialogObserver arrow1
					= (SGIArrowDialogObserver)list.get(ii);
				final double xValue1 = arrow1.getStartXValue();
				if( startXValue0!=xValue1 )
				{
					startXValue = null;
					break;
				}
			}

			for( int ii=1; ii<list.size(); ii++ )
			{
				SGIArrowDialogObserver arrow1
					= (SGIArrowDialogObserver)list.get(ii);
				final double yValue1 = arrow1.getStartYValue();
				if( startYValue0!=yValue1 )
				{
					startYValue = null;
					break;
				}
			}

			for( int ii=1; ii<list.size(); ii++ )
			{
				SGIArrowDialogObserver arrow1
					= (SGIArrowDialogObserver)list.get(ii);
				final double xValue1 = arrow1.getEndXValue();
				if( endXValue0!=xValue1 )
				{
					endXValue = null;
					break;
				}
			}

			for( int ii=1; ii<list.size(); ii++ )
			{
				SGIArrowDialogObserver arrow1
					= (SGIArrowDialogObserver)list.get(ii);
				final double yValue1 = arrow1.getEndYValue();
				if( endYValue0!=yValue1 )
				{
					endYValue = null;
					break;
				}
			}
		}


		//
		this.setStartXValue( startXValue );
		this.setStartYValue( startYValue );
		this.setEndXValue( endXValue );
		this.setEndYValue( endYValue );
		

		return true;
	}



	/**
	 * 
	 */
	public boolean setPropertiesToListeners( SGIPropertyDialogObserver l )
	{
		SGIArrowDialogObserver arrow = (SGIArrowDialogObserver)l;


		// get values
		final int xConfig = this.mAxesButtonGroup.getXAxisConfiguration();
		final int yConfig = this.mAxesButtonGroup.getYAxisConfiguration();
		final Number startX = this.getStartXValue();
		final Number startY = this.getStartYValue();
		final Number endX = this.getEndXValue();
		final Number endY = this.getEndYValue();


		if( arrow.hasValidStartXValue( xConfig, startX ) == false )
		{
			return false;
		}

		if( arrow.hasValidStartYValue( yConfig, startY ) == false )
		{
			return false;
		}

		if( arrow.hasValidEndXValue( xConfig, endX ) == false )
		{
			return false;
		}

		if( arrow.hasValidEndYValue( yConfig, endY ) == false )
		{
			return false;
		}


		// set the related axes
		if( xConfig!=-1 & yConfig!=-1 )
		{
			arrow.setAxisConfiguration( xConfig, yConfig );
		}


		if( startX!=null )
		{
			arrow.setStartXValue( startX.doubleValue() );
		}

		if( startY!=null )
		{
			arrow.setStartYValue( startY.doubleValue() );
		}

		if( endX!=null )
		{
			arrow.setEndXValue( endX.doubleValue() );
		}

		if( endY!=null )
		{
			arrow.setEndYValue( endY.doubleValue() );
		}


		// set with the arrow panel
		if( this.mArrowPanel.setPropertiesToListeners( (SGIArrowPanelObserver)arrow ) == false )
		{
			return false;
		}

		return true;
	}


}

