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

import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import org.huihoo.god4j.jgraph.JGraphActivity;
import org.huihoo.god4j.jgraph.JGraphTransition;
import org.huihoo.god4j.jgraph.XPDLJGraph;
import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.huihoo.workflow.xpdl.WorkflowTransition;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JGraphFrameListener implements InternalFrameListener
{
	protected JGraphFrame frameGraph;

	public JGraphFrameListener(JGraphFrame frameGraph)
	{
		this.frameGraph = frameGraph;
	}
	/**
	 * Invoked when a internal frame has been opened.
	 * @see javax.swing.JInternalFrame#show
	 */
	public void internalFrameOpened(InternalFrameEvent e)
	{
	}

	/**
	 * Invoked when an internal frame is in the process of being closed.
	 * The close operation can be overridden at this point.
	 * @see javax.swing.JInternalFrame#setDefaultCloseOperation
	 */
	public void internalFrameClosing(InternalFrameEvent e)
	{
	}

	/**
	 * Invoked when an internal frame has been closed.
	 * @see javax.swing.JInternalFrame#setClosed
	 */
	public void internalFrameClosed(InternalFrameEvent e)
	{
	}

	/**
	 * Invoked when an internal frame is iconified.
	 * @see javax.swing.JInternalFrame#setIcon
	 */
	public void internalFrameIconified(InternalFrameEvent e)
	{

	}

	/**
	 * Invoked when an internal frame is de-iconified.
	 * @see javax.swing.JInternalFrame#setIcon
	 */
	public void internalFrameDeiconified(InternalFrameEvent e)
	{

	}

	/**
	 * Invoked when an internal frame is activated.
	 * @see javax.swing.JInternalFrame#setSelected
	 */
	public void internalFrameActivated(InternalFrameEvent e)
	{
		//System.err.println("-------internalFrameActivated-------["+frameGraph.getTitle()+"]");

		GOD4JFactory.getGOD4J().getControlPanel().setSelectedJGraph(frameGraph);

		GOD4JFactory.getGOD4J().getActionMap().getAction("undo").update();
		GOD4JFactory.getGOD4J().getActionMap().getAction("redo").update();
		GOD4JFactory.getGOD4J().getActionMap().getAction("grid").update();
		GOD4JFactory.getGOD4J().getActionMap().getAction("ruler").update();

		XPDLJGraph graph = frameGraph.getJGraph();
		Object cell = graph.getSelectionCell();
		if (cell instanceof JGraphTransition)
		{
			JGraphTransition temp = (JGraphTransition) cell;
			WorkflowTransition transition = (WorkflowTransition) (temp.getUserObject());
			GOD4JFactory.getGOD4J().getExplorerPanel().getNodeExplorer().setSelectionNode(transition.getUUID());
		}
		else if (cell instanceof JGraphActivity)
		{
			JGraphActivity temp = (JGraphActivity) cell;
			WorkflowActivity activity = (WorkflowActivity) (temp.getUserObject());
			GOD4JFactory.getGOD4J().getExplorerPanel().getNodeExplorer().setSelectionNode(activity.getUUID());
		}
	}

	/**
	 * Invoked when an internal frame is de-activated.
	 * @see javax.swing.JInternalFrame#setSelected
	 */
	public void internalFrameDeactivated(InternalFrameEvent e)
	{
		//System.err.println("-------internalFrameDeactivated-------["+frameGraph.getTitle()+"]");
		GOD4JFactory.getGOD4J().getActionMap().getAction("undo").update();
		GOD4JFactory.getGOD4J().getActionMap().getAction("redo").update();
		GOD4JFactory.getGOD4J().getActionMap().getAction("grid").update();
		GOD4JFactory.getGOD4J().getActionMap().getAction("ruler").update();

	}
}
