PageRenderTime 95ms CodeModel.GetById 84ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/JEditActionContext.java

#
Java | 181 lines | 78 code | 15 blank | 88 comment | 10 complexity | 9adae4e2e716b5895c5ba967d9299994 MD5 | raw file
  1/*
  2 * JEditActionContext.java - For code sharing between jEdit and VFSBrowser
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1998, 2003 Slava Pestov
  7 * Portions copyright (C) 2007 Matthieu Casanova
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24package org.gjt.sp.jedit;
 25
 26import org.gjt.sp.util.StandardUtilities;
 27
 28import java.lang.reflect.Array;
 29import java.util.*;
 30
 31/**
 32 * Manages a collection of action sets. There are two instances of this class
 33 * in jEdit:
 34 * <ul>
 35 * <li>{@link org.gjt.sp.jedit.jEdit#getActionContext()} - editor actions
 36 * <li>{@link org.gjt.sp.jedit.browser.VFSBrowser#getActionContext()} - browser
 37 * actions
 38 * </ul>
 39 *
 40 * @since jEdit 4.3pre13
 41 * @author Slava Pestov
 42 * @version $Id: ActionContext.java 6884 2006-09-06 02:38:55Z ezust $
 43 */
 44public abstract class JEditActionContext<F extends JEditAbstractEditAction, E extends JEditActionSet<F>>
 45{
 46	//{{{ invokeAction() method
 47	/**
 48	 * Invokes the given action in response to a user-generated event.
 49	 * @param evt The event
 50	 * @param action The action
 51	 * @since jEdit 4.3pre13
 52	 */
 53	public abstract void invokeAction(EventObject evt, F action);
 54	//}}}
 55
 56	//{{{ addActionSet() method
 57	/**
 58	 * Adds a new action set to the context.
 59	 * @since jEdit 4.3pre13
 60	 */
 61	public void addActionSet(E actionSet)
 62	{
 63		actionNames = null;
 64		actionSets.addElement(actionSet);
 65		actionSet.context = this;
 66		String[] actions = actionSet.getActionNames();
 67		for(int i = 0; i < actions.length; i++)
 68		{
 69			/* Is it already there? */
 70			if (actionHash.containsKey(actions[i])) 
 71			{
 72				/* Save it for plugin unloading time */
 73				E oldAction = actionHash.get(actions[i]);
 74				overriddenActions.put(actions[i], oldAction);
 75			}
 76			actionHash.put(actions[i],actionSet);
 77		}
 78	} //}}}
 79
 80	//{{{ removeActionSet() method
 81	/**
 82	 * Removes an action set from the context.
 83	 * @since jEdit 4.23pre13
 84	 */
 85	public void removeActionSet(E actionSet)
 86	{
 87		actionNames = null;
 88		actionSets.removeElement(actionSet);
 89		actionSet.context = null;
 90		String[] actions = actionSet.getActionNames();
 91		for(int i = 0; i < actions.length; i++)
 92		{
 93			actionHash.remove(actions[i]);
 94			if (overriddenActions.containsKey(actions[i])) 
 95			{
 96				E oldAction = overriddenActions.remove(actions[i]);
 97				actionHash.put(actions[i], oldAction);
 98			}
 99		}
100	} //}}}
101
102	//{{{ getActionSets() method
103	/**
104	 * Returns all registered action sets.
105	 * @since jEdit 4.3pre13
106	 */
107	public E[] getActionSets()
108	{
109		if (actionSets.isEmpty())
110			return null;
111		Class clazz = actionSets.get(0).getClass();
112		E[] retVal =(E[]) Array.newInstance(clazz, actionSets.size());
113		actionSets.copyInto(retVal);
114		return retVal;
115	} //}}}
116
117	//{{{ getAction() method
118	/**
119	 * Returns the specified action.
120	 * @param name The action name
121	 * @return a JEditAbstractEditAction or null if it doesn't exist
122	 * @since jEdit 4.3pre13
123	 */
124	public F getAction(String name)
125	{
126		E set = actionHash.get(name);
127		if(set == null)
128			return null;
129		else
130			return set.getAction(name);
131	} //}}}
132
133	//{{{ getActionSetForAction() method
134	/**
135	 * Returns the action set that contains the specified action.
136	 *
137	 * @param action The action
138	 * @return the actionSet that contains the given action
139	 * @since jEdit 4.3pre13
140	 */
141	public E getActionSetForAction(String action)
142	{
143		return actionHash.get(action);
144	} //}}}
145
146	//{{{ getActionNames() method
147	/**
148	 * Returns all registered action names.
149	 */
150	public String[] getActionNames()
151	{
152		if(actionNames == null)
153		{
154			List<String> vec = new LinkedList<String>();
155			for(int i = 0; i < actionSets.size(); i++)
156				(actionSets.elementAt(i)).getActionNames(vec);
157
158			actionNames = vec.toArray(new String[vec.size()]);
159			Arrays.sort(actionNames,
160				new StandardUtilities.StringCompare<String>(true));
161		}
162
163		return actionNames;
164	} //}}}
165
166	//{{{ Package-private members
167	String[] actionNames;
168	/** 
169	 * This map contains as key an action name, 
170	 * and as value the JEditActionSet that contains this action
171	 */
172	Hashtable<String, E> actionHash = new Hashtable<String, E>();
173	
174	/** A map of built-in actions that were overridden by plugins. */
175	Hashtable<String, E> overriddenActions = new Hashtable<String, E>(); 
176	//}}}
177
178	//{{{ Private members
179	private final Vector<E> actionSets = new Vector<E>();
180	//}}}
181}