/*
 * SGDataAdditionWizardFrame.java
 *
 * Created on 2004/06/18, 14:44
 */

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

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import jp.riken.brain.ni.samuraigraph.base.*;
import jp.riken.brain.ni.samuraigraph.data.*;
import jp.riken.brain.ni.samuraigraph.figure.*;
import jp.riken.brain.ni.samuraigraph.figure.java2d.*;


/**
 *
 * @author  kuromaru
 */
public class SGXYDataTypeSelectionWizardDialog extends SGWizardDialog
{
	
	/**
	 * 
	 */
	public static final String TITLE = "Select the data type.";
    
    
	/**
	 * 
	 * @param title
	 */
	public SGXYDataTypeSelectionWizardDialog( final Frame owner, final boolean modal )
	{
		super(owner,modal);
		this.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;

        mButtonPanel = new javax.swing.JPanel();
        mOKButton = new javax.swing.JButton();
        mCancelButton = new javax.swing.JButton();
        mPreviousButton = new javax.swing.JButton();
        mPanel = new javax.swing.JPanel();
        mScalarXYRadioButton = new javax.swing.JRadioButton();
        mVectorXYRadioButton = new javax.swing.JRadioButton();
        mMultipleXYRadioButton = new javax.swing.JRadioButton();
        mSamplingXYRadioButton = new javax.swing.JRadioButton();
        mSamplingRateField = new javax.swing.JTextField();

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

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

        mButtonPanel.setBorder(new javax.swing.border.EtchedBorder());
        mOKButton.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        mOKButton.setText("OK");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        mButtonPanel.add(mOKButton, gridBagConstraints);

        mCancelButton.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        mCancelButton.setText("Cancel");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 2);
        mButtonPanel.add(mCancelButton, gridBagConstraints);

        mPreviousButton.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        mPreviousButton.setText("<Prev");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        mButtonPanel.add(mPreviousButton, gridBagConstraints);

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

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

        mPanel.setBorder(new javax.swing.border.TitledBorder("Data Type"));
        mScalarXYRadioButton.setText("Scalar-XY");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mPanel.add(mScalarXYRadioButton, gridBagConstraints);

        mVectorXYRadioButton.setText("Vector-XY");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mPanel.add(mVectorXYRadioButton, gridBagConstraints);

        mMultipleXYRadioButton.setText("Multiple-XY");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        mPanel.add(mMultipleXYRadioButton, gridBagConstraints);

        mSamplingXYRadioButton.setText("Sampling-XY");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        mPanel.add(mSamplingXYRadioButton, gridBagConstraints);

        mSamplingRateField.setText("0.0");
        mSamplingRateField.setPreferredSize(new java.awt.Dimension(50, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        mPanel.add(mSamplingRateField, gridBagConstraints);

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

        pack();
    }//GEN-END:initComponents
    
    
    // 変数宣言 - 編集不可//GEN-BEGIN:variables
    private javax.swing.JPanel mButtonPanel;
    protected javax.swing.JButton mCancelButton;
    private javax.swing.JRadioButton mMultipleXYRadioButton;
    protected javax.swing.JButton mOKButton;
    private javax.swing.JPanel mPanel;
    protected javax.swing.JButton mPreviousButton;
    private javax.swing.JTextField mSamplingRateField;
    private javax.swing.JRadioButton mSamplingXYRadioButton;
    private javax.swing.JRadioButton mScalarXYRadioButton;
    private javax.swing.JRadioButton mVectorXYRadioButton;
    // 変数宣言の終わり//GEN-END:variables



	/**
	 * 
	 */
	private TreeMap mDataTypeMap = new TreeMap();



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

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


		// create a button-group
		ButtonGroup group = new ButtonGroup();
		group.add( this.mScalarXYRadioButton );
		group.add( this.mVectorXYRadioButton );
		group.add( this.mMultipleXYRadioButton );
		group.add( this.mSamplingXYRadioButton );


		// initialize components
		this.mScalarXYRadioButton.setSelected(true);
		this.mSamplingRateField.setEnabled(false);
		this.mSamplingRateField.setText( new Double(1.0).toString() );


		// add itself as an action lister
		this.mScalarXYRadioButton.addActionListener(this);
		this.mVectorXYRadioButton.addActionListener(this);
		this.mMultipleXYRadioButton.addActionListener(this);
		this.mSamplingXYRadioButton.addActionListener(this);


		// initialize the map
		this.mDataTypeMap.put( SGDataTypeConstants.SXY_DATA, this.mScalarXYRadioButton );
		this.mDataTypeMap.put( SGDataTypeConstants.VXY_DATA, this.mVectorXYRadioButton );
		this.mDataTypeMap.put( SGDataTypeConstants.SXY_MULTIPLE_DATA, this.mMultipleXYRadioButton );
		this.mDataTypeMap.put( SGDataTypeConstants.SXY_SAMPLING_DATA, this.mSamplingXYRadioButton );



		//
		this.setResizable(false);


       	// add itself as an action lister
		this.mOKButton.addActionListener(this);
		this.mCancelButton.addActionListener(this);
		this.mPreviousButton.addActionListener(this);
//		this.mNextButton.addActionListener(this);

		return true;

	}



	/**
	 * 
	 */
	public boolean setActionListener( final ActionListener listener )
	{
//System.out.println("add:"+listener);

		this.mOKButton.addActionListener(listener);
		this.mCancelButton.addActionListener(listener);

		return true;
	}


	/**
	 * 
	 */
	public boolean removeActionListener()
	{
		ActionListener[] array = this.mOKButton.getActionListeners();
		for( int ii=0; ii<array.length; ii++ )
		{
			this.mOKButton.removeActionListener( array[ii] );
			this.mCancelButton.removeActionListener( array[ii] );
		}

		return true;
	}



	/**
	 * 
	 */
	public String getSelectedDataType()
	{
		String str = null;

		Iterator itr = this.mDataTypeMap.keySet().iterator();
		while( itr.hasNext() )
		{
			Object key = itr.next();
			JRadioButton btn = (JRadioButton)this.mDataTypeMap.get(key);
			if( btn.isSelected() )
			{
				str = (String)key;
				break;
			}
		}

		return str;

	}



	/**
	 * 
	 */
	public boolean setEffectiveDataType( final ArrayList list )
	{
		if( list==null )
		{
			return false;
		}

		if( list.size()==0 )
		{
			return false;
		}

		boolean b = false;
		Iterator itr = this.mDataTypeMap.keySet().iterator();
		while( itr.hasNext() )
		{
			Object key = itr.next();
			JRadioButton btn = (JRadioButton)this.mDataTypeMap.get(key);
			boolean flag = list.contains( key );
			if( btn.isSelected() )
			{
				if( !flag )
				{
					b = true;
				}
			}
			btn.setEnabled(flag);
		}

		// 使用不可能なデータ型のボタンが選択されている状態を回避する処理
		if( b )
		{
			itr = this.mDataTypeMap.keySet().iterator();
			while( itr.hasNext() )
			{
				Object key = itr.next();
				JRadioButton btn = (JRadioButton)this.mDataTypeMap.get(key);
				boolean flag = list.contains( key );
				btn.setSelected(flag);
				break;
			}
			
		}
	
		return true;

	}


	/**
	 * 
	 */
	public Double getSamplingRate()
	{
		String str = this.mSamplingRateField.getText();
		Double rate = null;
		try
		{
			rate = Double.valueOf(str);
		}
		catch( NumberFormatException ex )
		{
			ex.printStackTrace();
		}
		return rate;
	}


	/**
	 * 
	 */
	public void actionPerformed( final ActionEvent e )
	{
		Object source = e.getSource();
        
		if( source.equals( this.mOKButton ) )
		{
			this.onOK();
		}
		else if( source.equals( this.mCancelButton ) )
		{
			this.onCanceled();
		}

		if( source.equals( this.mPreviousButton ) )
		{
			this.onPrevious();
		}

		if( source.equals( this.mSamplingXYRadioButton ) )
		{
			this.mSamplingRateField.setEnabled(true);
		}
		else
		{
			if( source instanceof JRadioButton )
			{
				this.mSamplingRateField.setEnabled(false);
			}
		}


		repaint();

	}


}
