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

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;


/**
 * プロパティ設定用ダイアログ
 */
public abstract class SGPropertyDialog extends JDialog
	implements ActionListener, ChangeListener, WindowListener
{

	/**
	 *
	 */
//	protected JButton mOKButton = new JButton("OK");


	/**
	 *
	 */
//	protected JButton mCancelButton = new JButton("Cancel");


	/**
	 *
	 */
//	protected JButton mPreviewButton = new JButton("Preview");


	/**
	 * 
	 */
//	protected JPanel mButtonPanel = new JPanel();



	/**
	 * List of the property setting listeners.
	 */
	protected ArrayList mPropertySettingListenerList = new ArrayList();



	/**
	 * 
	 */
	public SGPropertyDialog()
	{
		super();
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Dialog owner )
	{
		super( owner );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Dialog owner, boolean modal )
	{
		super( owner, modal );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Dialog owner, String title )
	{
		super( owner, title );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Dialog owner, String title, boolean modal )
	{
		super( owner, title, modal );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Dialog owner, String title, boolean modal, GraphicsConfiguration gc )
	{
		super( owner, title, modal, gc );
		this.initialize();
	}



	/**
	 * 
	 */
	public SGPropertyDialog( Frame owner )
	{
		super( owner );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Frame owner, boolean modal )
	{
		super( owner, modal );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Frame owner, String title )
	{
		super( owner, title );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Frame owner, String title, boolean modal )
	{
		super( owner, title, modal );
		this.initialize();
	}


	/**
	 * 
	 */
	public SGPropertyDialog( Frame owner, String title, boolean modal, GraphicsConfiguration gc )
	{
		super( owner, title, modal, gc );
		this.initialize();
	}



	/**
	 * 
	 */
	private Icon mColorSelectionButtonEmphasisIcon = null;



	/**
	 * 
	 */
	private boolean initialize()
	{
		// set unresizable
		this.setResizable(false);

		// create a color dialog
		this.mColorDialog = new SGColorDialog(
			this, "Color Dialog", false );
		this.mColorDialog.setResizable(false);

		// set this as a ChangeListener of the color selection model
		JColorChooser cc = this.mColorDialog.getColorChooser();
		ColorSelectionModel cModel = cc.getSelectionModel();
		cModel.addChangeListener(this);

		// create background icon for the button
		this.mColorSelectionButtonEmphasisIcon
			= this.createIcon(SGConstants.RESOURCES_DIRNAME+"Lines.gif");

/*
		final ArrayList sList = this.getSpinnerList();
		for( int ii=0; ii<sList.size(); ii++ )
		{
			SGSpinner spinner = (SGSpinner)sList.get(ii);
			spinner.getFormattedTextField().addActionListener( this );
		}
*/


		//
		this.addWindowListener(this);


		return true;

	}



	/**
	 * name : name of resource file
	 */
	private ImageIcon createIcon( final String name )
	{
		if( name==null )
		{
			return null;
		}

		Class inClass = getClass();
		URL url = inClass.getResource(name);

//System.out.println("URL:"+url);

		ImageIcon icon;
		if( url!=null )
		{
			icon = new ImageIcon(url);
		}
		else
		{
			icon = new ImageIcon(name);
		}

//System.out.println("icon:"+icon);		
//System.out.println();

		return icon;
	}
	


	/**
	 * 
	 */
	protected String getString( final String key )
	{
		ResourceBundle bundle = ResourceBundle.getBundle("res",Locale.JAPAN);
//		ResourceBundle bundle = ResourceBundle.getBundle("res",Locale.ENGLISH);
		String val = bundle.getString(key);

		return val;
	}




	/**
	 * 
	 */
	public boolean setActionListener( final ActionListener listener )
	{
		return true;
	}


	/**
	 * 
	 */
	public boolean removeActionListener()
	{
		return true;
	}



	/**
	 * 
	 */
	protected SGColorDialog mColorDialog = null;


	/**
	 * 
	 */
	protected JButton mFocusedColorSetButton = null;


	/**
	 * 
	 */
	public SGColorDialog getColorDialog()
	{
		return this.mColorDialog;
	}


	/**
	 * 
	 */
	public void stateChanged( final ChangeEvent e )
	{
//System.out.println(e);

		Object source = e.getSource();

//System.out.println(source);

		if( this.mFocusedColorSetButton!=null
			&& ( source instanceof ColorSelectionModel ) )
		{
			Color cl = this.mColorDialog.getColorChooser().getColor();
			this.mFocusedColorSetButton.setBackground( cl );
//System.out.println(cl);
			this.mColorDialog.getColorChooser().getSelectionModel().setSelectedColor( null );
//System.out.println(this.mColorDialog.mColorChooser.getSelectionModel().getSelectedColor());
//System.out.println();
		}

//		repaint();

	}


	/**
	 * 
	 */
	public void hide()
	{
		super.hide();
		this.mColorDialog.hide();
	}


	/**
	 * 
	 */
	public abstract ArrayList getColorSelectionComponentList();


	/**
	 * 
	 */
	public abstract ArrayList getActiveComponentList();


	/**
	 * 
	 */
	public abstract ArrayList getSpinnerList();


	/**
	 * 
	 */
	protected ArrayList getActiveComponentListFromSpinners()
	{
		final ArrayList list = new ArrayList();

		final ArrayList sList = this.getSpinnerList();
		for( int ii=0; ii<sList.size(); ii++ )
		{
			SGSpinner spinner = (SGSpinner)sList.get(ii);
			list.add( spinner.getFormattedTextField() );
		}

		return list;
	}



	/**
	 * 
	 */
	public boolean setColorButtonBorder( final JButton btn, final boolean flag )
	{

		ArrayList list = this.getColorSelectionComponentList();
		for( int ii=0; ii<list.size(); ii++ )
		{
			JComponent com = (JComponent)list.get(ii);
			if( com.equals(btn) && flag )
			{
				// ボタンを斜線で囲む
				Border border = new MatteBorder(this.mColorSelectionButtonEmphasisIcon);
				btn.setBorder(border);
			}
			else
			{
				com.setBorder( new LineBorder( Color.BLACK, 1 ) );
			}
		}

		return true;
	}


	/**
	 * 
	 */
	public boolean setColorButtonBorder( final boolean flag )
	{
		this.setColorButtonBorder( this.mFocusedColorSetButton, flag );

		return true;
	}


	/**
	 * 
	 */
	public void actionPerformed( final ActionEvent e )
	{
		Object source = e.getSource();
		String command = e.getActionCommand();

//System.out.println(source);

		if( this.getColorSelectionComponentList().contains(source) )
		{
			JButton btn = (JButton)source;

			// 属性に設定
			this.mFocusedColorSetButton = btn;

			// ボタンに装飾を施す
			this.setColorButtonBorder( true );

			// 色選択ダイアログを出す
			this.getColorDialog().show();
		}


		ArrayList comList = this.getActiveComponentList();
		if( command.equals("OK") || comList.contains(source) )
		{
			this.onOK();
		}
		else if( command.equals("Cancel") )
		{
			this.onCanceled();
		}
		else if( command.equals("Preview") )
		{
			this.onPreviewed();
		}

	}



	/**
	 * 
	 */
	public SpinnerNumberModel getLineWidthSpinnerNumberModel()
	{
		return new SpinnerNumberModel( 0.5, 0.25, 6.0, 0.25 );
	}


	/**
	 * 
	 */
	public SpinnerNumberModel getFontSizeSpinnerNumberModel()
	{
		return new SpinnerNumberModel( 8.0, 8.0, 96.0, 1.0 );
	}



	/**
	 * 
	 * @param l
	 * @return
	 */
	public boolean addPropertySettingListener(
		final SGIPropertySettingListener l )
	{
		this.mPropertySettingListenerList.add(l);
		return true;
	}


	/**
	 * 
	 * @param l
	 * @return
	 */
	public boolean removePropertySettingListener(
		final SGIPropertySettingListener l )
	{
		return this.mPropertySettingListenerList.remove(l);
	}



	/**
	 * 
	 * @param l
	 * @return
	 */
	public boolean removeAllPropertySettingListeners()
	{
		this.mPropertySettingListenerList.clear();
		return true;
	}



	/**
	 * 
	 * @return
	 */
	public boolean removeAllWindowListener()
	{
		WindowListener[] array = this.getWindowListeners();
		for( int ii=0; ii<array.length; ii++ )
		{
			this.removeWindowListener(array[ii]);
		}
		return true;
	}



	/**
	 * 
	 * @return
	 */
	protected boolean onOK()
	{
		if( this.commit()==false )
		{
			return false;
		}

		this.removeAllPropertySettingListeners();
		this.hide();

		return true;
	}



	/**
	 * 
	 * @return
	 */
	protected boolean onCanceled()
	{
		if( this.cancel() == false )
		{
			return false;
		}
		
		this.removeAllPropertySettingListeners();
		this.hide();

		return true;
	}



	/**
	 * 
	 * @return
	 */
	protected boolean onPreviewed()
	{
		if( this.preview()==false )
		{
			return false;
		}
		return true;
	}



	/**
	 * 
	 * @return
	 */
	public boolean commit()
	{
		for( int ii=0; ii<this.mPropertySettingListenerList.size(); ii++ )
		{
			SGIPropertySettingListener l
				= (SGIPropertySettingListener)this.mPropertySettingListenerList.get(ii);
			if( l.commit() == false )
			{
				return false;
			}
		}

		this.hide();

		return true;
	}



	/**
	 * 
	 * @return
	 */
	public boolean cancel()
	{
//System.out.println("<< onCanceled >>");
		for( int ii=0; ii<this.mPropertySettingListenerList.size(); ii++ )
		{
//System.out.println("ii="+ii);
			SGIPropertySettingListener l
				= (SGIPropertySettingListener)this.mPropertySettingListenerList.get(ii);
			if( l.cancel() == false )
			{
//System.out.println("false");
				return false;
			}
		}
//System.out.println();

		return true;
	}


	/**
	 * 
	 * @return
	 */
	protected boolean preview()
	{
		for( int ii=0; ii<this.mPropertySettingListenerList.size(); ii++ )
		{
			SGIPropertySettingListener l
				= (SGIPropertySettingListener)this.mPropertySettingListenerList.get(ii);
			if( l.preview() == false )
			{
				return false;
			}
		}
		return true;
	}


	/**
	 * 
	 * @return
	 */
	public boolean setDialogProperty()
	{
		return true;
	}



	/**
	 * 
	 */
	public void windowActivated(final WindowEvent e)
	{
	}


	/**
	 * 
	 */
	public void windowDeactivated(final WindowEvent e)
	{
	}


	/**
	 * 
	 */
	public void windowIconified(final WindowEvent e)
	{
	}


	/**
	 * 
	 */
	public void windowDeiconified(final WindowEvent e)
	{
	}


	/**
	 * 
	 */
	public void windowOpened(final WindowEvent e)
	{
	}


	/**
	 * 
	 */
	public void windowClosed(final WindowEvent e)
	{
//System.out.println("windowClosed");
	}


	/**
	 * 
	 */
	public void windowClosing(final WindowEvent e)
	{
//System.out.println("windowClosing");
		this.onCanceled();
	}


}
