//----------------------------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/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE----------------------------
package org.huihoo.god4j.actions;

import java.util.Hashtable;

import org.huihoo.god4j.GOD4JFactory;
import org.huihoo.god4j.util.ToolUtils;

/**
 * @author zosatapo
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ActionMap extends Hashtable
{
	public ActionMap()
	{
		super();

		init();
	}

	public ActionBase getAction(String key)
	{
		//System.err.println("getAction : [key] = "+key);

		ActionBase action = (ActionBase) get(key);

		if (action == null)
		{
			String otherKey = key;

			if (otherKey.length() > 1)
			{
				otherKey = otherKey.substring(0, 1).toUpperCase() + otherKey.substring(1);
			}
			else if (otherKey.length() == 1)
			{
				otherKey = otherKey.toUpperCase();
			}

			//System.err.println("getAction : [otherKey] = "+otherKey);

			action = (ActionBase) get(otherKey);
		}

		return action;
	}

	public void putAction(String key, ActionBase action)
	{
		if (!containsKey(key))
		{
			put(key, action);
		}
		putAction(action);
	}

	public void putAction(ActionBase action)
	{
		String key = ToolUtils.getUnqualifiedClassName(action.getClass());
		if (!containsKey(key))
		{
			put(key, action);
		}
	}

	protected void init()
	{
		putAction(new New());
		putAction(new Open());
		putAction(new Close());
		putAction(new Save());
		putAction(new SaveAs());
		putAction(new SaveAsJPG());
		putAction(new Quit());

		putAction(new EditProperties());
		putAction(new Delete());
		putAction(new Undo());
		putAction(new Redo());
		putAction(new Cut());
		putAction(new Copy());
		putAction(new Paste());
		
		putAction(new ZoomNormal());
		putAction(new ZoomIn());
		putAction(new ZoomOut());
		putAction(new Grid());
		putAction(new Ruler());

		putAction(new HelpManual());
		putAction(new HelpAbout());
		
		putAction(new PasteAt());
	}

	public void update4Document()
	{
		GOD4JFactory.getGOD4J().getActionMap().getAction("save").setEnabled(GOD4JFactory.getGOD4J().isDirty());	
		
		if(GOD4JFactory.getGOD4J().getDocument() == null)
		{
			GOD4JFactory.getGOD4J().getActionMap().getAction("close").setEnabled(false);
			GOD4JFactory.getGOD4J().getActionMap().getAction("saveAs").setEnabled(false);
			GOD4JFactory.getGOD4J().getActionMap().getAction("saveAsJPG").setEnabled(false);	
		}
		else
		{
			GOD4JFactory.getGOD4J().getActionMap().getAction("close").setEnabled(true);	
			GOD4JFactory.getGOD4J().getActionMap().getAction("saveAs").setEnabled(true);
			GOD4JFactory.getGOD4J().getActionMap().getAction("saveAsJPG").setEnabled(true);				
		}
	}

}

