//----------------------------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.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.huihoo.god4j.util.BarFactory;
import org.huihoo.god4j.util.StringManager;
public class MenuBarEx extends JMenuBar
{
	private static StringManager sm = StringManager.getManager(GOD4JConstants.PACKAGE);
	
	private Hashtable menuItems = new Hashtable();
	private Hashtable menus = new Hashtable();
	
	public MenuBarEx()
	{
		createMenubar();
	}

	public void switchGrid(boolean selected)
	{
		JMenuItem gridMenuItem=getMenuItem("grid");
		if(gridMenuItem!=null)
		{
			gridMenuItem.setSelected(selected);
		}
	}
	public void switchRuler(boolean selected)
	{
		JMenuItem rulerMenuItem=getMenuItem("ruler");
		if(rulerMenuItem !=null)
		{
			rulerMenuItem.setSelected(selected);
		}
	}

	public JMenuItem getMenuItem(String cmd)
	{
		return (JMenuItem) menuItems.get(cmd);
	}
	
	public JMenu getMenu(String cmd)
	{
		return (JMenu) menus.get(cmd);
	}
	
	protected void createMenubar()
	{
		String[] menuKeys = BarFactory.tokenize(sm.getString(BarFactory.MENUTOOLBAR_PREFIX+"menubar"));
		
		for (int i = 0; i < menuKeys.length; i++)
		{
			JMenu m = createMenu(menuKeys[i]);
			if (m != null)
			{			
				add(m);
			}
		}
	}

	/**
	 * Create a menu for the app.  By default this pulls the
	 * definition of the menu from the associated resource file.
	 */
	protected JMenu createMenu(String key)
	{
		JMenu menu = BarFactory.createMenu(sm,key);
		menus.put(key, menu);
		
		String[] itemKeys = BarFactory.tokenize(sm.getString(BarFactory.MENUTOOLBAR_PREFIX+key));
		for (int i = 0; i < itemKeys.length; i++)
		{
			if (itemKeys[i].equals("-"))
			{
				menu.addSeparator();
			}
			else
			{
				JMenuItem mi = BarFactory.createMenuItem(sm,itemKeys[i]);
				menuItems.put(itemKeys[i], mi);
				menu.add(mi);
			}
		}
			
		return menu;
	}
}
