/*
 * 作成日: 2004/05/08
 *
 * この生成されたコメントの挿入されるテンプレートを変更するため
 * ウィンドウ > 設定 > Java > コード生成 > コードとコメント
 */
package jp.riken.brain.ni.samuraigraph.base;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;


/**
 * @author LANDAU
 *
 * この生成されたコメントの挿入されるテンプレートを変更するため
 * ウィンドウ > 設定 > Java > コード生成 > コードとコメント
 */
public class SGUtility implements SGIConstants
{


	/**
	 * 
	 */
	public static String[] getAvailableFontNames()
	{
		final String fontArray[]
			= GraphicsEnvironment.getLocalGraphicsEnvironment().
			  getAvailableFontFamilyNames();

		return fontArray;
	}


	/**
	 * Get the canonical path.
	 */
	public static String getCanonicalPath( final String str )
	{
		File file = new File( str );
		String pathName = null;
		try
		{
			pathName = file.getCanonicalPath();
		}
		catch( IOException ex )
		{
			return null;
		}

		return pathName;
	}



	/**
	 * 
	 */
	public static Rectangle2D createUnion(
		final ArrayList rectList )
	{

		if( rectList==null )
		{
			throw new IllegalArgumentException("rectList==null");
		}

		Rectangle2D rectAll = null;
		for( int ii=0; ii<rectList.size(); ii++ )
		{
			Rectangle2D rect = (Rectangle2D)rectList.get(ii);
			if( rect==null )
			{
				throw new IllegalArgumentException("rect==null");
			}

			if( rectAll == null )
			{
				rectAll = rect;
				continue;
			}

			rectAll = rect.createUnion(rectAll);
		}

		return rectAll;		
	}



	/**
	 * 
	 * @param rect
	 * @param direction
	 * @return
	 */
	public static double getRectStart(
		final Rectangle2D rect,
		final boolean direction )
	{
		if( direction )
		{
			return rect.getX();
		}
		else
		{
			return rect.getY();
		}
	}



	/**
	 * 
	 * @param rect
	 * @param direction
	 * @return
	 */
	public static double getRectSize(
		final Rectangle2D rect,
		final boolean direction )
	{
		if( direction )
		{
			return rect.getWidth();
		}
		else
		{
			return rect.getHeight();
		}
	}



	/**
	 * 
	 * @param rect
	 * @param direction
	 * @return
	 */
	public static double getRectEnd(
		final Rectangle2D rect,
		final boolean direction )
	{
		return getRectStart(rect,direction) + getRectSize(rect,direction);
	}



	/**
	 * 
	 * @param rect
	 * @param value
	 * @param direction
	 * @return
	 */
	public static boolean setRectStart(
		final Rectangle2D rect,
		final double value,
		final boolean direction )
	{

		if( direction )
		{
			rect.setRect(
				value,
				rect.getY(),
				rect.getWidth(),
				rect.getHeight()
			);
		}
		else
		{
			rect.setRect(
				rect.getX(),
				value,
				rect.getWidth(),
				rect.getHeight()
			);
		}

		return true;
	}



	/**
	 * 
	 * @param rect
	 * @param value
	 * @param direction
	 * @return
	 */
	public static boolean setRectEnd(
		final Rectangle2D rect,
		final double value,
		final boolean direction )
	{

		if( direction )
		{
			rect.setRect(
				value - rect.getWidth(),
				rect.getY(),
				rect.getWidth(),
				rect.getHeight()
			);
		}
		else
		{
			rect.setRect(
				rect.getX(),
				value - rect.getHeight(),
				rect.getWidth(),
				rect.getHeight()
			);
		}

		return true;
	}



	/**
	 * 
	 * @param rect
	 * @param value
	 * @param direction
	 * @return
	 */
	public static boolean setRectSize(
		final Rectangle2D rect,
		final double value,
		final boolean direction )
	{

		if( direction )
		{
			rect.setRect(
				rect.getX(),
				rect.getY(),
				value,
				rect.getHeight()
			);
		}
		else
		{
			rect.setRect(
				rect.getX(),
				rect.getY(),
				rect.getWidth(),
				value
			);
		}

		return true;
	}



	/**
	 * 
	 * @param rect
	 * @param value
	 * @param direction
	 * @return
	 */
	public static boolean isRectContains(
		final Rectangle2D rect,
		final double value,
		final boolean direction )
	{
		return SGUtilityNumber.contains(
			getRectStart(rect,direction), getRectEnd(rect,direction), value );
	}



	/**
	 * 
	 * @param rect1
	 * @param rect2
	 * @param direction
	 * @return
	 */
	public static boolean isRectContains(
		final Rectangle2D rect1,
		final Rectangle2D rect2,
		final boolean direction )
	{
		return SGUtilityNumber.contains(
			getRectStart(rect1,direction), getRectEnd(rect1,direction),
			getRectStart(rect2,direction), getRectEnd(rect2,direction) );
	}



	/**
	 * 
	 * @param rect1
	 * @param rect2
	 * @param direction
	 * @return
	 */
	public static double getOverlapping(
		final Rectangle2D rect1,
		final Rectangle2D rect2,
		final boolean direction )
	{
		return SGUtilityNumber.getOverlap(
			getRectStart(rect1,direction), getRectEnd(rect1,direction),
			getRectStart(rect2,direction), getRectEnd(rect2,direction) );
	}



	/**
	 * 
	 */
	public static boolean showMessageDialog( Component parentComponent, Object message, String title, int messageType )
	{
		JOptionPane.showMessageDialog(
			parentComponent, message, title, messageType
		);
		return true;
	}


	/**
	 * 
	 * @param parentComponent
	 * @return
	 */
	public static boolean showErrorMessageDialog(
		Component parentComponent, String msg, String title )
	{
		Toolkit.getDefaultToolkit().beep();
		SGUtility.showMessageDialog(
			parentComponent,
			msg,
			title, 
			JOptionPane.ERROR_MESSAGE );
		return true;
	}


	/**
	 * 
	 * @param parentComponent
	 * @return
	 */
	public static boolean showIllegalInputErrorMessageDialog(
		Component parentComponent )
	{
		return showIllegalInputErrorMessageDialog(
			parentComponent,
			"The input value is illegal." );
	}


	/**
	 * 
	 * @param parentComponent
	 * @return
	 */
	public static boolean showIllegalInputErrorMessageDialog(
		Component parentComponent, String msg )
	{
		return showErrorMessageDialog(
			parentComponent,
			msg,
			"Illegal input" );
	}

	/**
	 * 
	 * @param parentComponent
	 * @return
	 */
	public static boolean showFileNotFoundMessageDialog( Component parentComponent )
	{
		SGUtility.showMessageDialog(
			parentComponent,
			"Failed to open the file denoted by a specified pathname.",
			"File not found", 
			JOptionPane.ERROR_MESSAGE );
		return true;
	}



	/**
	 * 
	 * @param wnd
	 * @return
	 */
	public static void setCenter( final Window wnd, final Window owner )
	{
		Dimension dim = owner.getSize();
		final int width = wnd.getWidth();
		final int height = wnd.getHeight();
		final int x = owner.getX() + ( dim.width - width )/2;
		final int y = owner.getY() + ( dim.height - height )/2;
		wnd.setLocation(x,y);
	}



	/**
	 * Copy the list of copied objects.
	 * @param list1 - a list which contains copiable objects to be copied
	 * @param list2 - a list to set the copied objects
	 * @return
	 */
	public static boolean copyObjects( final ArrayList list1, final ArrayList list2 )
	{
		if( list1==null | list2==null )
		{
			throw new IllegalArgumentException("list1==null | list2==null");
		}
		
		for( int ii=0; ii<list1.size(); ii++ )
		{
			Object obj = list1.get(ii);
			if( ( obj instanceof SGICopiable ) == false )
			{
				throw new IllegalArgumentException("Not copiable objects are included.");
			}
			SGICopiable cp = (SGICopiable)obj;
			list2.add( cp.copy() );
		}
		
		return true;
	}



	/**
	 * Add a menu command to the pop-up menu.
	 * @param p - a pop-up menu
	 * @param l - an action listener of the command
	 * @param cmd - a command to be added
	 * @return added menu item
	 */
	public static JMenuItem addItem(
		JPopupMenu p,
		ActionListener l,
		String cmd )
	{
		final JMenuItem item = new JMenuItem( cmd );
		p.add(item);
		item.addActionListener(l);
		return item;
	}



	/**
	 * Add a menu command with the check box to the pop-up menu.
	 * @param p - a pop-up menu
	 * @param l - an action listener of the command
	 * @param cmd - a command to be added
	 * @return added menu item
	 */
	public static JCheckBoxMenuItem addCheckBoxItem(
		JPopupMenu p,
		ActionListener l,
		String cmd )
	{
		final JCheckBoxMenuItem item = new JCheckBoxMenuItem( cmd );
		p.add(item);
		item.addActionListener(l);
		return item;
	}



	/**
	 * Identify the OS name.
	 * @param prefix - the prefix of OS name
	 * @return whether the OS name starts with the prefix
	 */
	public static boolean identifyOS( final String prefix )
	{
		if( prefix==null )
		{
			throw new IllegalArgumentException("prefix==null");
		}

		return OS_NAME.toLowerCase().startsWith(prefix);
	}



	/**
	 * 
	 * @return
	 */
	public static String getLookAndFeelID()
	{
		LookAndFeel laf = UIManager.getLookAndFeel();
		if(laf!=null)
		{
			return laf.getID();
		}
		else
		{
			return null;
		}
	}



	/**
	 * Copy an array of a String array.
	 * @param src - an array to be copied
	 * @return a copy of an array
	 */
	public static String[] copyStringArray( String[] src )
	{
		final String[] dest = new String[src.length];
		System.arraycopy( src, 0, dest, 0, src.length );
		return dest;
	}



	/**
	 * Copy an array of a primitive double array.
	 * @param src - an array to be copied
	 * @return a copy of an array
	 */
	public static double[] copyDoubleArray( double[] src )
	{
		final double[] dest = new double[src.length];
		System.arraycopy( src, 0, dest, 0, src.length );
		return dest;
	}



	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean setValue(
		final SGTextField tf, final Object obj )
	{
		if( obj==null )
		{
			tf.setText(null);
		}
		else
		{
			String str = obj.toString();
			Double d = SGUtilityText.getDouble(str);
			if( d==null )
			{
				return false;
			}
			tf.setText( str );
		}

		return true;
	}



	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean setIntValue(
		final SGTextField tf, final Object obj )
	{
		if( obj==null )
		{
			tf.setText(null);
		}
		else
		{
			String str = obj.toString();
			Integer n = SGUtilityText.getInteger(str);
			if( n==null )
			{
				return false;
			}
			tf.setText( str );
		}

		return true;
	}



	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean setValue(
		final SGSpinner sp, final Object obj )
	{
		if( obj==null )
		{
			sp.setText("");
			return false;
		}

		String str = obj.toString();
		Double d = SGUtilityText.getDouble(str);
		if( d==null )
		{
			return false;
		}
		sp.setText( str );
		sp.setValue( d );

		try
		{
			sp.commitEditByDefault();
		}
		catch( ParseException e )
		{
		}

		return true;
	}


	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean setIntValue(
		final SGSpinner sp, final Object obj )
	{
		if( obj==null )
		{
			sp.setText("");
			return false;
		}

		String str = obj.toString();
		Integer n = SGUtilityText.getInteger(str);
		if( n==null )
		{
			return false;
		}
		sp.setText( str );
		sp.setValue( n );

		try
		{
			sp.commitEditByDefault();
		}
		catch( ParseException e )
		{
		}

		return true;
	}


	/**
	 * Clear the focus.
	 *
	 */
	public static void clearGlobalFocusOwner()
	{
		// clear focus in the tool bar
		KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
	}

}

