/*
 * Created on 2004/10/28
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package jp.riken.brain.ni.samuraigraph.base;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.text.JTextComponent;


/**
 * @author kuromaru
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public abstract class SGDialog extends JDialog
	implements ActionListener, WindowListener, ComponentListener, SGIConstants
{

	/**
	 * Text for the OK button.
	 */
	public static final String OK_BUTTON_TEXT = "OK";


	/**
	 * Text for the Cancel button.
	 */
	public static final String CANCEL_BUTTON_TEXT = "Cancel";


	/**
	 * Text for the Yes button.
	 */
	public static final String YES_BUTTON_TEXT = "Yes";


	/**
	 * Text for the No button.
	 */
	public static final String NO_BUTTON_TEXT = "No";


	/**
	 * 
	 */
	public static final int OK_OPTION = 0;


	/**
	 * 
	 */
	public static final int CANCEL_OPTION = 1;


	/**
	 * A list of action listeners.
	 */
	private ArrayList mActionListenerList = new ArrayList();


	/**
	 * 
	 */
	private int mCloseOption = -1;


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


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


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


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


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


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



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


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


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


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


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



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

		//
		this.addComponentListener(this);

		//
		this.addWindowListener(this);


		return true;

	}



	/**
	 * コンポーネントが可視になると呼び出されます。 
	 */
	public void componentShown(final ComponentEvent e)
	{
	}


	/**
	 * コンポーネントが不可視になると呼び出されます。 
	 */
	public void componentHidden(final ComponentEvent e)
	{
	}


	/**
	 * コンポーネントの位置が変わると呼び出されます。 
	 */
	public void componentMoved(final ComponentEvent e)
	{
		Object owner = this.getOwner();
		if( owner instanceof Window )
		{
			Window wnd = (Window)owner;
			wnd.repaint();
		}
	}


	/**
	 * コンポーネントのサイズが変わると呼び出されます。 
	 */
	public void componentResized(final ComponentEvent e)
	{
	}



	/**
	 * 
	 */
	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)
	{
	}


	/**
	 * 
	 */
	public void windowClosing(final WindowEvent e)
	{
		this.mCloseOption = CANCEL_OPTION;
	}



	/**
	 * 
	 */
	protected ActionEvent getActionEvent( final String command )
	{
		return new ActionEvent( this, 0, command );
	}


	/**
	 * 
	 */
	public void addActionListener( final ActionListener listener )
	{
		for( int ii=0; ii<mActionListenerList.size(); ii++ )
		{
			final ActionListener el = (ActionListener)mActionListenerList.get(ii);
			if( el.equals(listener) )
			{
				return;
			}
		}
		mActionListenerList.add(listener);
	}


	/**
	 * 
	 */
	public void removeActionListener( ActionListener listener )
	{
		for( int ii=mActionListenerList.size()-1; ii>=0; ii-- )
		{
			final ActionListener el = (ActionListener)mActionListenerList.get(ii);
			if( el.equals(listener) )
			{
				mActionListenerList.remove(listener);
			}
		}
	}


	/**
	 * 
	 */
	protected void notifyToListener( final String command )
	{
		for( int ii=0; ii<mActionListenerList.size(); ii++ )
		{
			final ActionListener el = (ActionListener)mActionListenerList.get(ii);
			el.actionPerformed( this.getActionEvent( command ) );
		}
	}



	/**
	 * 
	 * @param wnd
	 * @return
	 */
	public void setCenter( final Window owner )
	{
		SGUtility.setCenter( this, owner );
	}
	
	
	
	/**
	 * 
	 * @param com
	 * @return
	 */
	protected Number getNumber( final JTextComponent com )
	{
		if( com==null )
		{
			return null;
		}
		String str = com.getText();
		Number num = null;
		try
		{
			NumberFormat nf = NumberFormat.getInstance();
			num = nf.parse(str);
		}
		catch( ParseException ex )
		{
		}
		return num;
	}


	/**
	 * 
	 * @return
	 */
	public int getCloseOption()
	{
		return this.mCloseOption;
	}
	
	
	/**
	 * 
	 * @param num
	 */
	protected void setCloseOption( final int num )
	{
		if( num!=OK_OPTION & num!=CANCEL_OPTION )
		{
			throw new IllegalArgumentException("num!=OK_OPTION & num!=CANCEL_OPTION");
		}
		this.mCloseOption = num;
	}


}


