//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.util;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.io.FileInputStream;

import javax.swing.JFrame;

import org.huihoo.god4j.JGraphFrame;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowProcess;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ToolUtils
{
	public static void centerDesktop(Window w)
	{
		centerDesktop(w, 0, 0);
	}

	public static void centerDesktop(Window w, int minXDiffFromMax, int minYDiffFromMax)
	{
		w.pack();

		// make dialog smaller if needed, and center it
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension windowSize = w.getPreferredSize();
		if (windowSize.width > screenSize.width - minXDiffFromMax)
		{
			windowSize.width = screenSize.width - minXDiffFromMax;
		}
		if (windowSize.height > screenSize.height - minYDiffFromMax)
		{
			windowSize.height = screenSize.height - minYDiffFromMax;
		}
		w.setSize(windowSize);
		w.setLocation(screenSize.width / 2 - (windowSize.width / 2), screenSize.height / 2 - (windowSize.height / 2));
	}

	public static JGraphFrame createJGraph(WorkflowProcess workflowProcess)
	{
		JGraphFrame graphFrame = new JGraphFrame(workflowProcess, workflowProcess.getName(), true, true, true, true);
		graphFrame.setBounds(100, 100, 400, 400);
		graphFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		return graphFrame;
	}

	/**
	 * Converts a file specified by the path, to the String.
	 */
	public static String fileToString(String fileName)
	{
		if (fileName != null)
		{
			//String sLine;
			byte[] utf8Bytes;
			String sFile = new String();
			// Reading input by lines:
			try
			{
				FileInputStream fis = new FileInputStream(fileName);
				int noOfBytes = fis.available();
				if (noOfBytes > 0)
				{
					utf8Bytes = new byte[noOfBytes];
					fis.read(utf8Bytes);
					sFile = new String(utf8Bytes, "UTF8");
				}
			}
			catch (Exception ex)
			{
				return null;
			}
			return sFile;
		}
		return null;
	}

	/** Returns the class name without package. */
	public static String getUnqualifiedClassName(Class cls)
	{
		String name = cls.getName();
		int lastDot = name.lastIndexOf(".");
		if (lastDot >= 0)
		{
			name = name.substring(lastDot + 1, name.length());
		}
		
		return name;
	}
	
	public static boolean validateWorkflowPackage(WorkflowPackage workflowPackage)
	{
		return true;
	}
}
