KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > ActionContext


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
23 package org.gjt.sp.jedit;
24
25 import 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 6884 2006-09-06 02:38:55Z ezust $
39  */

40 public 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 JavaDoc[] actions = actionSet.getActionNames();
63         for(int i = 0; i < actions.length; i++)
64         {
65             /* Is it already there? */
66             if (actionHash.containsKey(actions[i]))
67             {
68                 /* Save it for plugin unloading time */
69                 ActionSet oldAction = actionHash.get(actions[i]);
70                 overriddenActions.put(actions[i], oldAction);
71             }
72             actionHash.put(actions[i],actionSet);
73         }
74     } //}}}
75

76     //{{{ removeActionSet() method
77
/**
78      * Removes an action set from the context.
79      * @since jEdit 4.2pre1
80      */

81     public void removeActionSet(ActionSet actionSet)
82     {
83         actionNames = null;
84         actionSets.removeElement(actionSet);
85         actionSet.context = null;
86         String JavaDoc[] actions = actionSet.getActionNames();
87         for(int i = 0; i < actions.length; i++)
88         {
89             actionHash.remove(actions[i]);
90             if (overriddenActions.containsKey(actions[i]))
91             {
92                 ActionSet oldAction = overriddenActions.remove(actions[i]);
93                 actionHash.put(actions[i], oldAction);
94             }
95         }
96     } //}}}
97

98     //{{{ getActionSets() method
99
/**
100      * Returns all registered action sets.
101      * @since jEdit 4.2pre1
102      */

103     public ActionSet[] getActionSets()
104     {
105         ActionSet[] retVal = new ActionSet[actionSets.size()];
106         actionSets.copyInto(retVal);
107         return retVal;
108     } //}}}
109

110     //{{{ getAction() method
111
/**
112      * Returns the specified action.
113      * @param name The action name
114      * @since jEdit 4.2pre1
115      */

116     public EditAction getAction(String JavaDoc name)
117     {
118         ActionSet set = (ActionSet)actionHash.get(name);
119         if(set == null)
120             return null;
121         else
122             return set.getAction(name);
123     } //}}}
124

125     //{{{ getActionSetForAction() method
126
/**
127      * Returns the action set that contains the specified action.
128      *
129      * @param action The action
130      * @since jEdit 4.2pre1
131      */

132     public ActionSet getActionSetForAction(String JavaDoc action)
133     {
134         return (ActionSet)actionHash.get(action);
135     } //}}}
136

137     //{{{ getActionNames() method
138
/**
139      * Returns all registered action names.
140      */

141     public String JavaDoc[] getActionNames()
142     {
143         if(actionNames == null)
144         {
145             List vec = new LinkedList();
146             for(int i = 0; i < actionSets.size(); i++)
147                 (actionSets.elementAt(i)).getActionNames(vec);
148
149             actionNames = (String JavaDoc[])vec.toArray(
150                 new String JavaDoc[vec.size()]);
151             Arrays.sort(actionNames,
152                 new MiscUtilities.StringICaseCompare());
153         }
154
155         return actionNames;
156     } //}}}
157

158     //{{{ Package-private members
159
String JavaDoc[] actionNames;
160     Hashtable<String JavaDoc, ActionSet> actionHash = new Hashtable<String JavaDoc, ActionSet>();
161     
162     /** A map of built-in actions that were overridden by plugins. */
163     Hashtable<String JavaDoc, ActionSet> overriddenActions = new Hashtable<String JavaDoc, ActionSet>();
164     //}}}
165

166     //{{{ Private members
167
private Vector<ActionSet> actionSets = new Vector<ActionSet>();
168     //}}}
169
}
170
Popular Tags