PageRenderTime 30ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/ActionContext.java

#
Java | 154 lines | 63 code | 13 blank | 78 comment | 7 complexity | 8a8647c594db2446d56e2be186e1dc96 MD5 | raw file
  1/*
  2 * ActionContext.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 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit;
 24
 25import java.util.*;
 26
 27/**
 28 * Manages a collection of action sets. There are two instances of this class
 29 * in jEdit:
 30 * <ul>
 31 * <li>{@link org.gjt.sp.jedit.jEdit#getActionContext()} - editor actions
 32 * <li>{@link org.gjt.sp.jedit.browser.VFSBrowser#getActionContext()} - browser
 33 * actions
 34 * </ul>
 35 *
 36 * @since jEdit 4.2pre1
 37 * @author Slava Pestov
 38 * @version $Id: ActionContext.java 4998 2004-03-20 06:08:50Z spestov $
 39 */
 40public abstract class ActionContext
 41{
 42	//{{{ invokeAction() method
 43	/**
 44	 * Invokes the given action in response to a user-generated event.
 45	 * @param evt The event
 46	 * @param action The action
 47	 * @since jEdit 4.2pre1
 48	 */
 49	public abstract void invokeAction(EventObject evt, EditAction action);
 50	//}}}
 51
 52	//{{{ addActionSet() method
 53	/**
 54	 * Adds a new action set to the context.
 55	 * @since jEdit 4.2pre1
 56	 */
 57	public void addActionSet(ActionSet actionSet)
 58	{
 59		actionNames = null;
 60		actionSets.addElement(actionSet);
 61		actionSet.context = this;
 62		String[] actions = actionSet.getActionNames();
 63		for(int i = 0; i < actions.length; i++)
 64		{
 65			actionHash.put(actions[i],actionSet);
 66		}
 67	} //}}}
 68
 69	//{{{ removeActionSet() method
 70	/**
 71	 * Removes an action set from the context.
 72	 * @since jEdit 4.2pre1
 73	 */
 74	public void removeActionSet(ActionSet actionSet)
 75	{
 76		actionNames = null;
 77		actionSets.removeElement(actionSet);
 78		actionSet.context = null;
 79		String[] actions = actionSet.getActionNames();
 80		for(int i = 0; i < actions.length; i++)
 81		{
 82			actionHash.remove(actions[i]);
 83		}
 84	} //}}}
 85
 86	//{{{ getActionSets() method
 87	/**
 88	 * Returns all registered action sets.
 89	 * @since jEdit 4.2pre1
 90	 */
 91	public ActionSet[] getActionSets()
 92	{
 93		ActionSet[] retVal = new ActionSet[actionSets.size()];
 94		actionSets.copyInto(retVal);
 95		return retVal;
 96	} //}}}
 97
 98	//{{{ getAction() method
 99	/**
100	 * Returns the specified action.
101	 * @param name The action name
102	 * @since jEdit 4.2pre1
103	 */
104	public EditAction getAction(String name)
105	{
106		ActionSet set = (ActionSet)actionHash.get(name);
107		if(set == null)
108			return null;
109		else
110			return set.getAction(name);
111	} //}}}
112
113	//{{{ getActionSetForAction() method
114	/**
115	 * Returns the action set that contains the specified action.
116	 *
117	 * @param action The action
118	 * @since jEdit 4.2pre1
119	 */
120	public ActionSet getActionSetForAction(String action)
121	{
122		return (ActionSet)actionHash.get(action);
123	} //}}}
124
125	//{{{ getActionNames() method
126	/**
127	 * Returns all registered action names.
128	 */
129	public String[] getActionNames()
130	{
131		if(actionNames == null)
132		{
133			List vec = new LinkedList();
134			for(int i = 0; i < actionSets.size(); i++)
135				((ActionSet)actionSets.elementAt(i)).getActionNames(vec);
136
137			actionNames = (String[])vec.toArray(
138				new String[vec.size()]);
139			Arrays.sort(actionNames,
140				new MiscUtilities.StringICaseCompare());
141		}
142
143		return actionNames;
144	} //}}}
145
146	//{{{ Package-private members
147	String[] actionNames;
148	Hashtable actionHash = new Hashtable();
149	//}}}
150
151	//{{{ Private members
152	private Vector actionSets = new Vector();
153	//}}}
154}