/*
 * Created on 2005-5-16
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.god4j.util;

import java.awt.Event;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToggleButton;
import javax.swing.KeyStroke;

import org.huihoo.god4j.GOD4JConstants;
import org.huihoo.god4j.GOD4JFactory;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class BarFactory
{
	/**
	 * prefix applied to the key used in resource file
	 * lookups for a key.
	 */
	public static final String MENUTOOLBAR_PREFIX = "menu_toolbar.";

	/**
	 * Suffix applied to the key used in resource file
	 * lookups for a label.
	 */
	public static final String LABEL_SUFFIX = "Label";

	/**
	 * Suffix applied to the key used in resource file
	 * lookups for an image.
	 */
	public static final String IMAGE_SUFFIX = "Image";

	/**
	 * Suffix applied to the key used in resource file
	 * lookups for tooltip text.
	 */
	public static final String TOOLTIP_SUFFIX = "Tooltip";

	/**
	 * Mnemonic applied to the key used in resource file
	 * lookups for a mnemonic.
	 */
	public static final String MNEMONIC_SUFFIX = "Mnemonic";

	/**
	 * Action applied to the key used in resource file
	 * lookups for an action.
	 */
	public static final String ACTION_SUFFIX = "Action";

	/**
	 * Accelerator applied to the key used in resource file
	 * lookups for an Accelerator.
	 */
	public static final String ACCELERATOR_SUFFIX = "Accelerator";
	public static final String ACCELERATOR_KEYCHAR_SUFFIX = "Accelerator.keyChar";
	public static final String ACCELERATOR_MODIFIERS_SUFFIX = "Accelerator.modifiers";

	/**
	 * IsCheckBox applied to the key used in resource file
	 * lookups for an isCheckBox.
	 */
	public static final String ISCHECKBOX_SUFFIX = "IsCheckBox";

	/**
	 * IsToggle applied to the key used in resource file
	 * lookups for an IsToggle.
	 */
	public static final String ISTOGGLE_SUFFIX = "IsToggle";

	/**
	 * IsSubMenu applied to the key used in resource file
	 * lookups for an IsSubMenu.
	 */
	public static final String ISSUBMENU_SUFFIX = "IsSubMenu";

	public static AbstractButton createToolbarComponent(StringManager sm, String key)
	{
		String actionKey = sm.getString(MENUTOOLBAR_PREFIX + key + ACTION_SUFFIX);
		if (actionKey == null)
		{
			actionKey = key;
		}
		Action action = GOD4JFactory.getGOD4J().getActionMap().getAction(actionKey);

		return createToolbarComponent(sm, key, action);
	}

	public static AbstractButton createToolbarComponent(StringManager sm, String key, Action action)
	{
		AbstractButton b = null;

		if (action != null)
		{
			if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + key + ISTOGGLE_SUFFIX)))
			{
				b = new JToggleButton(action);
			}
			else
			{
				b = new JButton(action);
			}
		}
		else
		{
			if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + key + ISTOGGLE_SUFFIX)))
			{
				b = new JToggleButton();
			}
			else
			{
				b = new JButton();
			}

			b.setEnabled(false);
		}

		setToolbarComponents(b,sm,key);
		
		return b;
	}

	public static AbstractButton createToolbarComponent(StringManager sm, String key, ActionListener actionListener)
	{
		AbstractButton b = null;

		if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + key + ISTOGGLE_SUFFIX)))
		{
			b = new JToggleButton();
		}
		else
		{
			b = new JButton();
		}

		if (actionListener != null)
		{
			b.addActionListener(actionListener);
		}
		else
		{
			b.setEnabled(false);
		}


		setToolbarComponents(b,sm,key);

		return b;
	}

	public static JMenu createMenu(StringManager sm, String key)
	{
		JMenu menu = new JMenu(sm.getString(MENUTOOLBAR_PREFIX + key + LABEL_SUFFIX));
		menu.setFont(GOD4JConstants.WINDOWS_UI_FONT);

		String mnemonic = sm.getString(MENUTOOLBAR_PREFIX + key + MNEMONIC_SUFFIX);
		if (mnemonic != null)
		{
			menu.setMnemonic(mnemonic.charAt(0));
		}

		String keyChar = sm.getString(MENUTOOLBAR_PREFIX + key + ACCELERATOR_KEYCHAR_SUFFIX);
		if (keyChar != null)
		{
			menu.setAccelerator(
				KeyStroke.getKeyStroke(
					new Character(keyChar.charAt(0)),
					createAcceleratorModifiers(sm.getString(MENUTOOLBAR_PREFIX + key + ACCELERATOR_MODIFIERS_SUFFIX))));
		}

		return menu;
	}

	public static JMenuItem createMenuItem(StringManager sm, String cmd)
	{
		String actionKey = sm.getString(MENUTOOLBAR_PREFIX + cmd + ACTION_SUFFIX);
		if (actionKey == null)
		{
			actionKey = cmd;
		}
		Action action = GOD4JFactory.getGOD4J().getActionMap().getAction(actionKey);

		return createMenuItem(sm, cmd, action);
	}

	public static JMenuItem createMenuItem(StringManager sm, String cmd, Action action)
	{
		JMenuItem mi = null;

		if (action != null)
		{
			if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISCHECKBOX_SUFFIX)))
			{
				mi = new JCheckBoxMenuItem(action);
			}
			else
			{
				if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISSUBMENU_SUFFIX)))
				{
					mi = createMenu(sm, cmd);
				}
				else
				{
					mi = new JMenuItem(action);
				}
			}
		}
		else
		{
			if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISCHECKBOX_SUFFIX)))
			{
				mi = new JCheckBoxMenuItem();
			}
			else
			{
				if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISSUBMENU_SUFFIX)))
				{
					mi = createMenu(sm, cmd);
				}
				else
				{
					mi = new JMenuItem();
				}
			}

			mi.setEnabled(false);
		}

		setMenuItems(mi, sm, cmd);

		return mi;
	}

	public static JMenuItem createMenuItem(StringManager sm, String cmd, ActionListener actionLisntener)
	{
		JMenuItem mi = null;

		if (actionLisntener != null)
		{
			if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISCHECKBOX_SUFFIX)))
			{
				mi = new JCheckBoxMenuItem();
			}
			else
			{
				if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISSUBMENU_SUFFIX)))
				{
					mi = createMenu(sm, cmd);
				}
				else
				{
					mi = new JMenuItem();
				}
			}
			mi.addActionListener(actionLisntener);
		}
		else
		{
			if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISCHECKBOX_SUFFIX)))
			{
				mi = new JCheckBoxMenuItem();
			}
			else
			{
				if ("true".equalsIgnoreCase(sm.getString(MENUTOOLBAR_PREFIX + cmd + ISSUBMENU_SUFFIX)))
				{
					mi = createMenu(sm, cmd);
				}
				else
				{
					mi = new JMenuItem();
				}
			}

			mi.setEnabled(false);
		}

		setMenuItems(mi, sm, cmd);

		return mi;
	}

	public static String[] tokenize(String input)
	{
		Vector v = new Vector();

		String cmd[];

		if (input != null)
		{
			StringTokenizer t = new StringTokenizer(input);
			while (t.hasMoreTokens())
			{
				v.addElement(t.nextToken());
			}
		}
		cmd = new String[v.size()];
		for (int i = 0; i < cmd.length; i++)
		{
			cmd[i] = (String) v.elementAt(i);
		}

		return cmd;
	}

	protected static void setMenuItems(JMenuItem item, StringManager sm, String cmd)
	{
		String label = sm.getString(MENUTOOLBAR_PREFIX + cmd + LABEL_SUFFIX);
		if (label != null)
		{
			item.setText(label);
		}

		item.setFont(GOD4JConstants.WINDOWS_UI_FONT);

		String mnemonic = sm.getString(MENUTOOLBAR_PREFIX + cmd + MNEMONIC_SUFFIX);
		if (mnemonic != null)
		{
			item.setMnemonic(mnemonic.charAt(0));
		}

		String keyChar = sm.getString(MENUTOOLBAR_PREFIX + cmd + ACCELERATOR_KEYCHAR_SUFFIX);		

		if (keyChar != null)
		{
//			System.err.println("cmd : " + cmd + " , keyChar : " + keyChar);
//			
//			System.err.println(
//				"keyChar.charAt(0) : "
//					+ (int)keyChar.charAt(0)
//					+ " , modifiers : "
//					+ createAcceleratorModifiers(sm.getString(MENUTOOLBAR_PREFIX + cmd + ACCELERATOR_MODIFIERS_SUFFIX)));
//			System.err.println("keyChar.charAt(0) : " + KeyEvent.VK_N + " , modifiers : " + Event.CTRL_MASK);

			item.setAccelerator(
				KeyStroke.getKeyStroke(
					keyChar.charAt(0),
					createAcceleratorModifiers(sm.getString(MENUTOOLBAR_PREFIX + cmd + ACCELERATOR_MODIFIERS_SUFFIX))));
		}

		ImageIcon imageIcon = ImageLoader.getImageIcon(sm.getString(MENUTOOLBAR_PREFIX + cmd + IMAGE_SUFFIX));
		if (imageIcon != null)
		{
			item.setHorizontalTextPosition(JButton.RIGHT);
			item.setIcon(imageIcon);
		}
	}

	protected static void setToolbarComponents(AbstractButton item, StringManager sm, String key)
	{
		item.setFont(GOD4JConstants.WINDOWS_UI_FONT);
		item.setText("");

		ImageIcon imageIcon = ImageLoader.getImageIcon(sm.getString(MENUTOOLBAR_PREFIX + key + IMAGE_SUFFIX));
		if (imageIcon != null)
		{
			item.setIcon(imageIcon);
		}

		item.setRequestFocusEnabled(false);
		item.setMargin(new Insets(1, 1, 1, 1));

		String tip = sm.getString(MENUTOOLBAR_PREFIX + key + TOOLTIP_SUFFIX);
		if (tip != null)
		{
			item.setToolTipText(tip);
		}
	}
	
	protected static int createAcceleratorModifiers(String input)
	{
		if (input == null)
		{
			return 0;
		}

		int int_modifiers = 0;
		String[] modifierItems = tokenize(input);
		for (int i = 0; i < modifierItems.length; i++)
		{
			int_modifiers += createAcceleratorModifier(modifierItems[i]);
		}

		return int_modifiers;
	}

	protected static int createAcceleratorModifier(String input)
	{
		if ("SHIFT_MASK".equalsIgnoreCase(input))
		{
			return Event.SHIFT_MASK;
		}
		else if ("CTRL_MASK".equalsIgnoreCase(input))
		{
			return Event.CTRL_MASK;
		}
		else if ("ALT_MASK".equalsIgnoreCase(input))
		{
			return Event.ALT_MASK;
		}

		return 0;
	}
}
