//----------------------------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 java.awt.BorderLayout;
import java.beans.PropertyVetoException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.swing.JDesktopPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.huihoo.god4j.jgraph.XPDLJGraphToolBar;
import org.huihoo.god4j.util.ToolUtils;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowProcess;

public class ControlPanel extends JPanel 
{ 
	private JDesktopPane desktop = null;
	private XPDLJGraphToolBar toolBar = null;
	
	private boolean showRuler = false;
	private boolean showGrid = false;
	
	private Hashtable jgraphs=new Hashtable();
	
	public ControlPanel()
	{	
		this.desktop = new JDesktopPane();
		this.toolBar=new XPDLJGraphToolBar();
		
		this.setLayout(new BorderLayout());
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());	
		panel.add(desktop,BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane(panel);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);	
		
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(toolBar, BorderLayout.WEST);	
	}
	
	public boolean isRulerVisible()
	{
		return showRuler;
	}
	
	public void setRulerVisible(boolean showRuler)
	{
		this.showRuler=showRuler;
		JGraphFrame processFrame = getSelectedJGraph();
		if(processFrame!=null)
		{
			processFrame.setDisplayRuler(showRuler);
		}
	}
	
	public boolean isGridVisible()
	{
		return showGrid;
	}
	
	public void setGridVisible(boolean showGrid)
	{
		this.showGrid=showGrid;
		JGraphFrame processFrame = getSelectedJGraph();
		if(processFrame!=null)
		{
			processFrame.setDisplayGrid(showGrid);
		}
	}
	
	public int getActionType()
	{
		return toolBar.getActionType();
	}
	
	public void setActionType(int actionType)
	{
		toolBar.setActionType(actionType);
	}
	
	public void zoomNormal()
	{
		JGraphFrame graphFrame = getSelectedJGraph();
		if(graphFrame!=null)
		{
			graphFrame.zoomNormal();
		}
	}
	public void zoomOut()
	{
		JGraphFrame processFrame = getSelectedJGraph();
		if(processFrame!=null)
		{
			processFrame.zoomOut();
		}
	}
	public void zoomIn()
	{
		JGraphFrame graphFrame = getSelectedJGraph();
		if(graphFrame!=null)
		{
			graphFrame.zoomIn();
		}
	}
	
	public JGraphFrame findJGraph(String uuid)
	{
		return (JGraphFrame)jgraphs.get(uuid);
	}
	
	public Iterator findJGraphs()
	{
		return jgraphs.values().iterator();
	}
	
	public  void addJGraph(JGraphFrame graphFrame)
	{
		if(graphFrame!=null)
		{
			jgraphs.put(graphFrame.getWorkflowProcess().getUUID(),graphFrame);
			desktop.add(graphFrame,JGraphFrame.JGRAPH_FRAME_LAYER);
		}
	}
	
	public void removeJGraph(String uuid)
	{
		JGraphFrame graphFame=(JGraphFrame)jgraphs.remove(uuid);
		if(graphFame!=null)
		{
			graphFame.dispose();
			graphFame=null;
		}
	}
	
	public JGraphFrame getSelectedJGraph() 
	{
		return (JGraphFrame)desktop.getSelectedFrame();
	}

	public void setSelectedJGraph(JGraphFrame graphFrame) 
	{
		if(graphFrame!=null)
		{
			graphFrame.show();			
			
			try
			{
				graphFrame.setSelected(true);
			}
			catch (PropertyVetoException e)
			{
			}	
			
			desktop.setSelectedFrame(graphFrame);
		}
	}
	
	public void loadDocument()
	{		
		cleanControlPanel();
		
		WorkflowPackage document = GOD4JFactory.getGOD4J().getDocument();
		
		if(document != null)
		{
			List processList =document.getWorkflowProcesses();
			
			for(int i=0;i<processList.size();++i)
			{
				JGraphFrame graphFrame = ToolUtils.createJGraph((WorkflowProcess)processList.get(i));
				addJGraph(graphFrame);			
			}
			
			
			WorkflowProcess mainProcess=GOD4JFactory.getGOD4J().getDocument().getMainProcess();
			if(mainProcess!=null)
			{
				this.setSelectedJGraph(this.findJGraph(mainProcess.getUUID()));
			}	
		}
		
		this.updateUI();
		
	}
	
	protected void cleanControlPanel()
	{
		desktop.removeAll();
		
		Iterator iterator=jgraphs.values().iterator();
		while(iterator.hasNext())
		{
			JGraphFrame frame=(JGraphFrame)iterator.next();
			frame.dispose();
		}
		
		jgraphs.clear();		
	}
}
