/*
 * SGDataFileChooseWizardDialog.java
 *
 * Created on 2004/06/25, 13:49
 */

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

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;
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 SGMultiDataFileChooserWizardDialog extends SGFileChooserWizardDialog
{

	/**
	 * 
	 */
	public static final String TITLE = "Choose data files.";


    /** Creates new form SGDataFileChooseWizardDialog */
    public SGMultiDataFileChooserWizardDialog(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;

        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();
        mScrollPane = new javax.swing.JScrollPane();
        mTable = new javax.swing.JTable();
        mFileChooserButton = new javax.swing.JButton();

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

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        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 = 3;
        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("Choose Data Files"));
        mScrollPane.setPreferredSize(new java.awt.Dimension(454, 200));
        mTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "File Name"
            }
        ));
        mScrollPane.setViewportView(mTable);

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

        mFileChooserButton.setFont(new java.awt.Font("MS Sans Serif", 1, 12));
        mFileChooserButton.setText("Choose");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        mPanel.add(mFileChooserButton, gridBagConstraints);

        getContentPane().add(mPanel, new java.awt.GridBagConstraints());

        pack();
    }//GEN-END:initComponents
    
    
    // 変数宣言 - 編集不可//GEN-BEGIN:variables
    private javax.swing.JPanel mButtonPanel;
    protected javax.swing.JButton mCancelButton;
    private javax.swing.JButton mFileChooserButton;
    protected javax.swing.JButton mOKButton;
    private javax.swing.JPanel mPanel;
    protected javax.swing.JButton mPreviousButton;
    private javax.swing.JScrollPane mScrollPane;
    private javax.swing.JTable mTable;
    // 変数宣言の終わり//GEN-END:variables



	/**
	 * 
	 */
	private int mFileNumber;



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

		//
		this.setTitle( SGMultiDataFileChooserWizardDialog.TITLE );


		//
		this.setResizable(false);


		//
		this.mOKButton.setEnabled(false);


		// button
		this.mFileChooserButton.addActionListener(this);


		// file-chooser
		this.mFileChooser.setMultiSelectionEnabled(true);


		//
		this.mOKButton.addActionListener(this);
		this.mCancelButton.addActionListener(this);
		this.mPreviousButton.addActionListener(this);


		return true;
	}



	/**
	 * 
	 */
	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();
		}
		else if( source.equals( this.mPreviousButton ) )
		{
			this.onPrevious();
		}

		if( source.equals(this.mFileChooserButton) )
		{
			if( this.showFileChooser() == false )
			{
				throw new Error();
			}

			if( this.getSelectedFiles().length==this.mFileNumber )
			{
				this.mOKButton.setEnabled(true);
			}
		}

	}



	/**
	 * 
	 */
	public JTable getTable()
	{
		return this.mTable;
	}


	/**
	 * 
	 */
	public boolean setTable( final JTable table )
	{
		this.mTable = table;
		return true;
	}


	/**
	 * 
	 */
	public TableModel getTableModel()
	{
		return this.mTable.getModel();
	}



	/**
	 * 
	 */
	public boolean setTableModel( final TableModel model )
	{
		this.mTable.setModel( model );
		return true;
	}


	/**
	 * 
	 */
	private boolean showFileChooser()
	{
		ArrayList list = this.openFileChooser();
		if( list==null )
		{
			return false;
		}
		if( list.size()==0 )
		{
			return true;
		}

		File[] files = new File[list.size()];
		for( int ii=0; ii<files.length; ii++ )
		{
			files[ii] = (File)list.get(ii);
		}

		boolean flag = this.setSelectedFiles(files);

		return flag;
	}



	/**
	 * 
	 */
	public File[] getSelectedFiles()
	{

		ArrayList list = new ArrayList();
		for( int ii=0; ii<this.mTable.getRowCount(); ii++ )
		{
			Object obj = this.mTable.getValueAt(ii,1);
			if( obj!=null )
			{
				list.add(obj);
			}
		}


		File[] files = new File[list.size()];
		for( int ii=0; ii<files.length; ii++ )
		{
			String pathName = (String)list.get(ii);
			files[ii] = new File( pathName );
		}
		

		return files;
	}



	/**
	 * 
	 */
	public boolean setSelectedFiles( final File[] files )
	{

		// ファイル名だけを更新
		for( int ii=0; ii<this.mTable.getRowCount(); ii++ )
		{
			this.mTable.setValueAt(null,ii,1);
		}

		for( int ii=0; ii<files.length; ii++ )
		{
			String path = files[ii].getPath();
			this.mTable.setValueAt(path,ii,1);
		}

		return true;
	}


	/**
	 * 
	 * @return
	 */
	public int getFileNumber()
	{
		return this.mFileNumber;
	}


	/**
	 * 
	 * @param num
	 */
	public void setFileNumber( final int num )
	{
		if( num<0 )
		{
			new IllegalArgumentException("num<0");
		}
		this.mFileNumber = num;
	}


	/**
	 * 
	 */
	public void show()
	{
		// if the property file does not have data information, do as follows
		final boolean flag = ( this.mFileNumber==0 );
		this.mOKButton.setEnabled(flag);
		this.mFileChooserButton.setEnabled(!flag);

		super.show();
	}


}
