KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > ModuleActions


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.util.*;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.swing.Action JavaDoc;
27
28 import org.openide.actions.ActionManager;
29 import org.openide.util.actions.SystemAction;
30 import org.openide.util.Lookup;
31
32 import org.netbeans.core.startup.ManifestSection;
33
34
35 /**
36  * Holds list of all actions added by modules.
37  * @author Jaroslav Tulach, Jesse Glick
38  */

39 public class ModuleActions extends ActionManager
40 /*implements PropertyChangeListener*/ {
41
42     /** array of all actions added by modules */
43     private static SystemAction[] array;
44     /** of (ModuleItem, List (ManifestSection.ActionSection)) */
45     private static Map<Object JavaDoc,List<ManifestSection.ActionSection>> map = new HashMap<Object JavaDoc,List<ManifestSection.ActionSection>> (8);
46     /** current module */
47     private static Object JavaDoc module = null;
48     /** Map of currently running actions */
49     private Map<ActionEvent JavaDoc,Action JavaDoc> runningActions = new HashMap<ActionEvent JavaDoc,Action JavaDoc>();
50
51     public static ModuleActions getDefaultInstance() {
52         ActionManager mgr = ActionManager.getDefault();
53         assert mgr instanceof ModuleActions : "Got wrong ActionManager instance: " + mgr + " from " + Lookup.getDefault();
54         return (ModuleActions)mgr;
55     }
56
57     /** Array with all activated actions.
58     * Can contain null that will be replaced by separators.
59     */

60     public SystemAction[] getContextActions () {
61         SystemAction[] a = array;
62         if (a != null) {
63             return a;
64         }
65         array = a = createActions ();
66         return a;
67     }
68     
69     /** Invokes action in a RequestPrecessor dedicated to performing
70      * actions.
71      */

72     @SuppressWarnings JavaDoc("deprecation")
73     public void invokeAction(final Action JavaDoc a, final ActionEvent JavaDoc e) {
74         try {
75             org.openide.util.Mutex.EVENT.readAccess (new Runnable JavaDoc() {
76                 public void run() {
77                     showWaitCursor(e);
78                 }
79             });
80             addRunningAction(a, e);
81             
82             a.actionPerformed (e);
83         } finally {
84             removeRunningAction(e);
85             org.openide.util.Mutex.EVENT.readAccess (new Runnable JavaDoc() {
86                 public void run() {
87                     hideWaitCursor(e);
88                 }
89             });
90         }
91     }
92     
93     /** Listens on change of modules and if changed,
94     * fires change to all listeners.
95     */

96     private void fireChange () {
97         firePropertyChange(PROP_CONTEXT_ACTIONS, null, null);
98     }
99     
100     /** Adds action to <code>runningAction</code> map using event as a key.
101      * @param rp <code>RequestProcessor</code> which runs the actio task
102      * @param action action to put in map
103      * @param evt action event used as key in the map */

104     private void addRunningAction(Action JavaDoc action, ActionEvent JavaDoc evt) {
105         synchronized(runningActions) {
106             runningActions.put(evt, action);
107         }
108     }
109     
110     /** Removes action from <code>runningAction</code> map for key.
111      * @param evt action event used as a key in map */

112     private void removeRunningAction(ActionEvent JavaDoc evt) {
113         synchronized(runningActions) {
114             runningActions.remove(evt);
115         }
116     }
117
118     /** Gets collection of currently running actions. */
119     public Collection<Action JavaDoc> getRunningActions() {
120         synchronized(runningActions) {
121             return new ArrayList<Action JavaDoc>(runningActions.values());
122         }
123     }
124      
125     /** Change enabled property of an action
126     *
127     public void propertyChange (PropertyChangeEvent ev) {
128         if (SystemAction.PROP_ENABLED.equals (ev.getPropertyName ())) {
129             fireChange ();
130         }
131     }
132     */

133
134     /** Attaches to processing of a module.
135      * The actual object passed is arbitrary, so long as
136      * it is different for every installed modules (as this
137      * controls the grouping of actions with separators).
138      * Passing null means stop processing a given module.
139      */

140     public static synchronized void attachTo (Object JavaDoc m) {
141         module = m;
142     }
143
144     /** Adds new action to the list.
145     */

146     public synchronized static void add (ManifestSection.ActionSection a) {
147         List<ManifestSection.ActionSection> list = map.get (module);
148         if (list == null) {
149             list = new ArrayList<ManifestSection.ActionSection> ();
150             map.put (module, list);
151         }
152         list.add (a);
153         //a.addPropertyChangeListener (INSTANCE);
154

155         array = null;
156         getDefaultInstance().fireChange (); // PENDING this is too often
157
}
158
159     /** Removes new action from the list.
160     */

161     public synchronized static void remove (ManifestSection.ActionSection a) {
162         List<ManifestSection.ActionSection> list = map.get (module);
163         if (list == null) {
164             return;
165         }
166         list.remove (a);
167         //a.removePropertyChangeListener (INSTANCE);
168

169         if (list.isEmpty ()) {
170             map.remove (module);
171         }
172
173         array = null;
174         getDefaultInstance().fireChange (); // PENDING this is too often
175
}
176
177     /** Creates the actions.
178     */

179     private synchronized static SystemAction[] createActions () {
180         Iterator<List<ManifestSection.ActionSection>> it = map.values ().iterator ();
181
182         ArrayList<Object JavaDoc> arr = new ArrayList<Object JavaDoc> (map.size () * 5);
183
184         while (it.hasNext ()) {
185             List<ManifestSection.ActionSection> l = it.next ();
186
187             Iterator<ManifestSection.ActionSection> actions = l.iterator ();
188             while (actions.hasNext()) {
189                 ManifestSection.ActionSection s = actions.next();
190                 
191                 try {
192                     arr.add (s.getInstance ());
193                 } catch (Exception JavaDoc ex) {
194                     Logger.getLogger(ModuleActions.class.getName()).log(Level.WARNING, null, ex);
195                 }
196             }
197             
198             
199             if (it.hasNext ()) {
200                 // add separator between modules
201
arr.add (null);
202             }
203
204         }
205
206         return (SystemAction[])arr.toArray (new SystemAction[arr.size ()]);
207     }
208
209     
210     private static final Logger JavaDoc err = Logger.getLogger("org.openide.util.actions.MouseCursorUtils"); // NOI18N
211

212     /**
213      * Running show/hide count for glass panes in use.
214      * Maps arbitrary keys to glass panes.
215      * Several keys may map to the same glass pane - the wait cursor is shown
216      * so long as there are any.
217      */

218     private static final Map<Object JavaDoc,java.awt.Component JavaDoc> glassPaneUses = new HashMap<Object JavaDoc,java.awt.Component JavaDoc>();
219     
220     /**
221      * Try to find the active window's glass pane.
222      * @return a glass pane, or null
223      */

224     private static java.awt.Component JavaDoc activeGlassPane() {
225         java.awt.Window JavaDoc w = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
226         if (w instanceof javax.swing.RootPaneContainer JavaDoc) {
227             return ((javax.swing.RootPaneContainer JavaDoc)w).getGlassPane();
228         } else {
229             return null;
230         }
231     }
232     
233     /**
234      * Sets wait cursor visible on the window associated with an event, if any.
235      * @param key something to pass to {@link #hideWaitCursor} to turn it off
236      */

237     public static void showWaitCursor(Object JavaDoc key) {
238         assert java.awt.EventQueue.isDispatchThread();
239         assert !glassPaneUses.containsKey(key);
240         java.awt.Component JavaDoc c = activeGlassPane();
241         if (c == null) {
242             err.warning("showWaitCursor could not find a suitable glass pane; key=" + key);
243             return;
244         }
245         if (glassPaneUses.values().contains(c)) {
246             err.fine("wait cursor already displayed on " + c);
247         } else {
248             err.fine("wait cursor will be displayed on " + c);
249             c.setCursor(org.openide.util.Utilities.createProgressCursor(c));
250             c.setVisible(true);
251         }
252         glassPaneUses.put(key, c);
253     }
254     
255     /**
256      * Resets cursor to default.
257      * @param key the same key passed to {@link #showWaitCursor}
258      */

259     public static void hideWaitCursor(Object JavaDoc key) {
260         assert java.awt.EventQueue.isDispatchThread();
261         java.awt.Component JavaDoc c = glassPaneUses.get(key);
262         if (c == null) {
263             return;
264         }
265         glassPaneUses.remove(key);
266         if (glassPaneUses.values().contains(c)) {
267             err.fine("wait cursor still displayed on " + c);
268         } else {
269             err.fine("wait cursor will be hidden on " + c);
270             c.setVisible(false);
271             c.setCursor(null);
272         }
273     }
274     
275     
276 }
277
278
Popular Tags