001: /*
002: * ActionContext.java - For code sharing between jEdit and VFSBrowser
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit;
024:
025: import java.util.*;
026:
027: /**
028: * Manages a collection of action sets. There are two instances of this class
029: * in jEdit:
030: * <ul>
031: * <li>{@link org.gjt.sp.jedit.jEdit#getActionContext()} - editor actions
032: * <li>{@link org.gjt.sp.jedit.browser.VFSBrowser#getActionContext()} - browser
033: * actions
034: * </ul>
035: *
036: * @since jEdit 4.2pre1
037: * @author Slava Pestov
038: * @version $Id: ActionContext.java 6884 2006-09-06 02:38:55Z ezust $
039: */
040: public abstract class ActionContext {
041: //{{{ invokeAction() method
042: /**
043: * Invokes the given action in response to a user-generated event.
044: * @param evt The event
045: * @param action The action
046: * @since jEdit 4.2pre1
047: */
048: public abstract void invokeAction(EventObject evt, EditAction action);
049:
050: //}}}
051:
052: //{{{ addActionSet() method
053: /**
054: * Adds a new action set to the context.
055: * @since jEdit 4.2pre1
056: */
057: public void addActionSet(ActionSet actionSet) {
058: actionNames = null;
059: actionSets.addElement(actionSet);
060: actionSet.context = this ;
061: String[] actions = actionSet.getActionNames();
062: for (int i = 0; i < actions.length; i++) {
063: /* Is it already there? */
064: if (actionHash.containsKey(actions[i])) {
065: /* Save it for plugin unloading time */
066: ActionSet oldAction = actionHash.get(actions[i]);
067: overriddenActions.put(actions[i], oldAction);
068: }
069: actionHash.put(actions[i], actionSet);
070: }
071: } //}}}
072:
073: //{{{ removeActionSet() method
074: /**
075: * Removes an action set from the context.
076: * @since jEdit 4.2pre1
077: */
078: public void removeActionSet(ActionSet actionSet) {
079: actionNames = null;
080: actionSets.removeElement(actionSet);
081: actionSet.context = null;
082: String[] actions = actionSet.getActionNames();
083: for (int i = 0; i < actions.length; i++) {
084: actionHash.remove(actions[i]);
085: if (overriddenActions.containsKey(actions[i])) {
086: ActionSet oldAction = overriddenActions
087: .remove(actions[i]);
088: actionHash.put(actions[i], oldAction);
089: }
090: }
091: } //}}}
092:
093: //{{{ getActionSets() method
094: /**
095: * Returns all registered action sets.
096: * @since jEdit 4.2pre1
097: */
098: public ActionSet[] getActionSets() {
099: ActionSet[] retVal = new ActionSet[actionSets.size()];
100: actionSets.copyInto(retVal);
101: return retVal;
102: } //}}}
103:
104: //{{{ getAction() method
105: /**
106: * Returns the specified action.
107: * @param name The action name
108: * @since jEdit 4.2pre1
109: */
110: public EditAction getAction(String name) {
111: ActionSet set = (ActionSet) actionHash.get(name);
112: if (set == null)
113: return null;
114: else
115: return set.getAction(name);
116: } //}}}
117:
118: //{{{ getActionSetForAction() method
119: /**
120: * Returns the action set that contains the specified action.
121: *
122: * @param action The action
123: * @since jEdit 4.2pre1
124: */
125: public ActionSet getActionSetForAction(String action) {
126: return (ActionSet) actionHash.get(action);
127: } //}}}
128:
129: //{{{ getActionNames() method
130: /**
131: * Returns all registered action names.
132: */
133: public String[] getActionNames() {
134: if (actionNames == null) {
135: List vec = new LinkedList();
136: for (int i = 0; i < actionSets.size(); i++)
137: (actionSets.elementAt(i)).getActionNames(vec);
138:
139: actionNames = (String[]) vec
140: .toArray(new String[vec.size()]);
141: Arrays.sort(actionNames,
142: new MiscUtilities.StringICaseCompare());
143: }
144:
145: return actionNames;
146: } //}}}
147:
148: //{{{ Package-private members
149: String[] actionNames;
150: Hashtable<String, ActionSet> actionHash = new Hashtable<String, ActionSet>();
151:
152: /** A map of built-in actions that were overridden by plugins. */
153: Hashtable<String, ActionSet> overriddenActions = new Hashtable<String, ActionSet>();
154: //}}}
155:
156: //{{{ Private members
157: private Vector<ActionSet> actionSets = new Vector<ActionSet>();
158: //}}}
159: }
|