//----------------------------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.util.Hashtable;

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

import org.huihoo.god4j.util.BarFactory;
import org.huihoo.god4j.util.StringManager;
public class ToolBarEx extends JToolBar
{
	private static StringManager sm = StringManager.getManager(GOD4JConstants.PACKAGE);

	private Hashtable compHash = new Hashtable();

	public ToolBarEx()
	{
		setBorderPainted(false);
		setRollover(true);
		createToolbar();
	}

	public void switchGrid(boolean selected)
	{
		AbstractButton gridButton = this.getToolbarButton("grid");
		if (gridButton != null)
		{
			gridButton.setSelected(selected);
		}
	}
	public void switchRuler(boolean selected)
	{
		AbstractButton rulerButton = this.getToolbarButton("ruler");
		if (rulerButton != null)
		{
			rulerButton.setSelected(selected);
		}
	}

	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);
		compHash.put(key, b);

		return b;
	}

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