/*
 * 作成日: 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.Toolkit;
import java.awt.Window;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;


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


	/**
	 * 
	 */
	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 );

		FileInputStream fis = null;
		try
		{
			fis = new FileInputStream( str );
		}
		catch( FileNotFoundException ex )
		{
			return null;
		}
		finally
		{
			try
			{
				fis.close();
			}
			catch( IOException ex )
			{
				
			}
		}

		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 showIllegalInputErrorMessageDialog( Component parentComponent )
	{
		Toolkit.getDefaultToolkit().beep();
		SGUtility.showMessageDialog(
			parentComponent,
			"不正な入力です。",
			"Illegal input", 
			JOptionPane.ERROR_MESSAGE );
		return true;
	}



	/**
	 * 
	 * @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.PLAIN_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);
	}


}
