2
* JEditActionContext.java - For code sharing between jEdit and VFSBrowser
3
* :tabSize=8:indentSize=8:noTabs=false:
4
* :folding=explicit:collapseFolds=1:
6
* Copyright (C) 1998, 2003 Slava Pestov
7
* Portions copyright (C) 2007 Matthieu Casanova
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.
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.
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.
24
package org.gjt.sp.jedit;
26
import java.lang.reflect.Array;
30
* Manages a collection of action sets. There are two instances of this class
33
* <li>{@link org.gjt.sp.jedit.jEdit#getActionContext()} - editor actions
34
* <li>{@link org.gjt.sp.jedit.browser.VFSBrowser#getActionContext()} - browser
38
* @since jEdit 4.3pre13
39
* @author Slava Pestov
40
* @version $Id: ActionContext.java 6884 2006-09-06 02:38:55Z ezust $
42
public abstract class JEditActionContext<F extends JEditAbstractEditAction, E extends JEditActionSet<F>>
44
//{{{ invokeAction() method
46
* Invokes the given action in response to a user-generated event.
47
* @param evt The event
48
* @param action The action
49
* @since jEdit 4.3pre13
51
public abstract void invokeAction(EventObject evt, F action);
54
//{{{ addActionSet() method
56
* Adds a new action set to the context.
57
* @since jEdit 4.3pre13
59
public void addActionSet(E actionSet)
62
actionSets.addElement(actionSet);
63
actionSet.context = this;
64
String[] actions = actionSet.getActionNames();
65
for(int i = 0; i < actions.length; i++)
67
/* Is it already there? */
68
if (actionHash.containsKey(actions[i]))
70
/* Save it for plugin unloading time */
71
E oldAction = actionHash.get(actions[i]);
72
overriddenActions.put(actions[i], oldAction);
74
actionHash.put(actions[i],actionSet);
78
//{{{ removeActionSet() method
80
* Removes an action set from the context.
81
* @since jEdit 4.23pre13
83
public void removeActionSet(E actionSet)
86
actionSets.removeElement(actionSet);
87
actionSet.context = null;
88
String[] actions = actionSet.getActionNames();
89
for(int i = 0; i < actions.length; i++)
91
actionHash.remove(actions[i]);
92
if (overriddenActions.containsKey(actions[i]))
94
E oldAction = overriddenActions.remove(actions[i]);
95
actionHash.put(actions[i], oldAction);
100
//{{{ getActionSets() method
102
* Returns all registered action sets.
103
* @since jEdit 4.3pre13
105
public E[] getActionSets()
107
if (actionSets.isEmpty())
109
Class clazz = actionSets.get(0).getClass();
110
E[] retVal =(E[]) Array.newInstance(clazz, actionSets.size());
111
actionSets.copyInto(retVal);
115
//{{{ getAction() method
117
* Returns the specified action.
118
* @param name The action name
119
* @return a JEditAbstractEditAction or null if it doesn't exists
120
* @since jEdit 4.3pre13
122
public F getAction(String name)
124
E set = actionHash.get(name);
128
return set.getAction(name);
131
//{{{ getActionSetForAction() method
133
* Returns the action set that contains the specified action.
135
* @param action The action
136
* @return the actionSet that contains the given action
137
* @since jEdit 4.3pre13
139
public E getActionSetForAction(String action)
141
return actionHash.get(action);
144
//{{{ getActionNames() method
146
* Returns all registered action names.
148
public String[] getActionNames()
150
if(actionNames == null)
152
List<String> vec = new LinkedList<String>();
153
for(int i = 0; i < actionSets.size(); i++)
154
(actionSets.elementAt(i)).getActionNames(vec);
156
actionNames = vec.toArray(new String[vec.size()]);
157
Arrays.sort(actionNames,
158
new MiscUtilities.StringICaseCompare());
164
//{{{ Package-private members
165
String[] actionNames;
167
* This map contains as key an action name,
168
* and as value the JEditActionSet that contains this action
170
Hashtable<String, E> actionHash = new Hashtable<String, E>();
172
/** A map of built-in actions that were overridden by plugins. */
173
Hashtable<String, E> overriddenActions = new Hashtable<String, E>();
176
//{{{ Private members
177
private final Vector<E> actionSets = new Vector<E>();