//----------------------------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.jgraph;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JToolBar;

import org.huihoo.god4j.GOD4JFactory;
import org.huihoo.god4j.JGraphFrame;
import org.huihoo.god4j.util.BarFactory;
import org.huihoo.god4j.util.StringManager;

public class XPDLJGraphToolBar extends javax.swing.JToolBar implements ActionListener
{
  private static  StringManager sm =StringManager.getManager(JGraphConstants.PACKAGE); 

	public final static int INSERT_BEGIN = 99;
	public final static int INSERT_END = 100;	
	public final static int SELECT = 0;
	public final static int INSERT_BUSINESS = 1;
	public final static int INSERT_ROUTE = 2;
	public final static int INSERT_SUBFLOW = 3;
	public final static int INSERT_TRANSITION = 4;
	
	protected ButtonGroup buttonGrp = new ButtonGroup();
	protected Hashtable   compHash = new Hashtable();
	
	protected int actionType = 0;
	
	public XPDLJGraphToolBar()
	{
		this.setRollover(true);
		this.setBorderPainted(true);
		this.setOrientation(JToolBar.VERTICAL);
		
		this.createToolbar();
	}

	public int getActionType()
	{
		return this.actionType;
	}
	
	public void setActionType(int actionType)
	{
		this.actionType=actionType;
		
		if(this.actionType==SELECT)
		{
			buttonGrp.setSelected(getToolbarButton("select").getModel(), true);
		}
		
		JGraphFrame frame = GOD4JFactory.getGOD4J().getControlPanel().getSelectedJGraph();
		if(frame!=null)
		{
			if(this.actionType == INSERT_TRANSITION)
			{
				frame.getJGraph().setPortsVisible(true);
			}
			else
			{
				frame.getJGraph().setPortsVisible(false);
			}
		}
	}


	public void actionPerformed(ActionEvent actionEvent)
	{		
		Object source = actionEvent.getSource();
		if (source == getToolbarButton("select"))
		{
			setActionType(SELECT);
		}
		else if (source == getToolbarButton("transition"))
		{
			setActionType(INSERT_TRANSITION);		
		}
		else if (source == getToolbarButton("businessActivity"))
		{
			setActionType(INSERT_BUSINESS);
		}
		else if (source == getToolbarButton("routeActivity"))
		{
			setActionType(INSERT_ROUTE);
		}
		else if (source == getToolbarButton("subFlow"))
		{
			setActionType(INSERT_SUBFLOW);
		}
		else if (source == getToolbarButton("begin"))
		{
			setActionType(INSERT_BEGIN);
		}
		else if (source == getToolbarButton("end"))
		{
			setActionType(INSERT_END);
		}
	}
	
	protected void createToolbar()
	{
		String[] toolKeys = BarFactory.tokenize(sm.getString(BarFactory.MENUTOOLBAR_PREFIX + "toolbar"));
		for (int i = 0; i < toolKeys.length; i++)
		{
			if (toolKeys[i].equals("-"))
			{
				add(Box.createHorizontalStrut(5));
			}
			else
			{
				add(createToolbarButton(toolKeys[i]));
			}
		}
		add(Box.createHorizontalGlue());
	}

	protected AbstractButton createToolbarButton(String key)
	{	
		
		AbstractButton b = BarFactory.createToolbarComponent(sm,key,this);
		buttonGrp.add(b);
		compHash.put(key, b);

		return b;
	}

	protected AbstractButton getToolbarButton(String key)
	{
		return (AbstractButton) compHash.get(key);
	}
}