001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.awt.event.ActionEvent;
045: import java.util.*;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.Action;
049:
050: import org.openide.actions.ActionManager;
051: import org.openide.util.actions.SystemAction;
052: import org.openide.util.Lookup;
053:
054: import org.netbeans.core.startup.ManifestSection;
055:
056: /**
057: * Holds list of all actions added by modules.
058: * @author Jaroslav Tulach, Jesse Glick
059: */
060: public class ModuleActions extends ActionManager
061: /*implements PropertyChangeListener*/{
062:
063: /** array of all actions added by modules */
064: private static SystemAction[] array;
065: /** of (ModuleItem, List (ManifestSection.ActionSection)) */
066: private static Map<Object, List<ManifestSection.ActionSection>> map = new HashMap<Object, List<ManifestSection.ActionSection>>(
067: 8);
068: /** current module */
069: private static Object module = null;
070: /** Map of currently running actions */
071: private Map<ActionEvent, Action> runningActions = new HashMap<ActionEvent, Action>();
072:
073: public static ModuleActions getDefaultInstance() {
074: ActionManager mgr = ActionManager.getDefault();
075: assert mgr instanceof ModuleActions : "Got wrong ActionManager instance: "
076: + mgr + " from " + Lookup.getDefault();
077: return (ModuleActions) mgr;
078: }
079:
080: /** Array with all activated actions.
081: * Can contain null that will be replaced by separators.
082: */
083: public SystemAction[] getContextActions() {
084: SystemAction[] a = array;
085: if (a != null) {
086: return a;
087: }
088: array = a = createActions();
089: return a;
090: }
091:
092: /** Invokes action in a RequestPrecessor dedicated to performing
093: * actions.
094: */
095: @SuppressWarnings("deprecation")
096: public void invokeAction(final Action a, final ActionEvent e) {
097: try {
098: org.openide.util.Mutex.EVENT.readAccess(new Runnable() {
099: public void run() {
100: showWaitCursor(e);
101: }
102: });
103: addRunningAction(a, e);
104:
105: a.actionPerformed(e);
106: } finally {
107: removeRunningAction(e);
108: org.openide.util.Mutex.EVENT.readAccess(new Runnable() {
109: public void run() {
110: hideWaitCursor(e);
111: }
112: });
113: }
114: }
115:
116: /** Listens on change of modules and if changed,
117: * fires change to all listeners.
118: */
119: private void fireChange() {
120: firePropertyChange(PROP_CONTEXT_ACTIONS, null, null);
121: }
122:
123: /** Adds action to <code>runningAction</code> map using event as a key.
124: * @param rp <code>RequestProcessor</code> which runs the actio task
125: * @param action action to put in map
126: * @param evt action event used as key in the map */
127: private void addRunningAction(Action action, ActionEvent evt) {
128: synchronized (runningActions) {
129: runningActions.put(evt, action);
130: }
131: }
132:
133: /** Removes action from <code>runningAction</code> map for key.
134: * @param evt action event used as a key in map */
135: private void removeRunningAction(ActionEvent evt) {
136: synchronized (runningActions) {
137: runningActions.remove(evt);
138: }
139: }
140:
141: /** Gets collection of currently running actions. */
142: public Collection<Action> getRunningActions() {
143: synchronized (runningActions) {
144: return new ArrayList<Action>(runningActions.values());
145: }
146: }
147:
148: /** Change enabled property of an action
149: *
150: public void propertyChange (PropertyChangeEvent ev) {
151: if (SystemAction.PROP_ENABLED.equals (ev.getPropertyName ())) {
152: fireChange ();
153: }
154: }
155: */
156:
157: /** Attaches to processing of a module.
158: * The actual object passed is arbitrary, so long as
159: * it is different for every installed modules (as this
160: * controls the grouping of actions with separators).
161: * Passing null means stop processing a given module.
162: */
163: public static synchronized void attachTo(Object m) {
164: module = m;
165: }
166:
167: /** Adds new action to the list.
168: */
169: public synchronized static void add(ManifestSection.ActionSection a) {
170: List<ManifestSection.ActionSection> list = map.get(module);
171: if (list == null) {
172: list = new ArrayList<ManifestSection.ActionSection>();
173: map.put(module, list);
174: }
175: list.add(a);
176: //a.addPropertyChangeListener (INSTANCE);
177:
178: array = null;
179: getDefaultInstance().fireChange(); // PENDING this is too often
180: }
181:
182: /** Removes new action from the list.
183: */
184: public synchronized static void remove(
185: ManifestSection.ActionSection a) {
186: List<ManifestSection.ActionSection> list = map.get(module);
187: if (list == null) {
188: return;
189: }
190: list.remove(a);
191: //a.removePropertyChangeListener (INSTANCE);
192:
193: if (list.isEmpty()) {
194: map.remove(module);
195: }
196:
197: array = null;
198: getDefaultInstance().fireChange(); // PENDING this is too often
199: }
200:
201: /** Creates the actions.
202: */
203: private synchronized static SystemAction[] createActions() {
204: Iterator<List<ManifestSection.ActionSection>> it = map.values()
205: .iterator();
206:
207: ArrayList<Object> arr = new ArrayList<Object>(map.size() * 5);
208:
209: while (it.hasNext()) {
210: List<ManifestSection.ActionSection> l = it.next();
211:
212: Iterator<ManifestSection.ActionSection> actions = l
213: .iterator();
214: while (actions.hasNext()) {
215: ManifestSection.ActionSection s = actions.next();
216:
217: try {
218: arr.add(s.getInstance());
219: } catch (Exception ex) {
220: Logger.getLogger(ModuleActions.class.getName())
221: .log(Level.WARNING, null, ex);
222: }
223: }
224:
225: if (it.hasNext()) {
226: // add separator between modules
227: arr.add(null);
228: }
229:
230: }
231:
232: return (SystemAction[]) arr
233: .toArray(new SystemAction[arr.size()]);
234: }
235:
236: private static final Logger err = Logger
237: .getLogger("org.openide.util.actions.MouseCursorUtils"); // NOI18N
238:
239: /**
240: * Running show/hide count for glass panes in use.
241: * Maps arbitrary keys to glass panes.
242: * Several keys may map to the same glass pane - the wait cursor is shown
243: * so long as there are any.
244: */
245: private static final Map<Object, java.awt.Component> glassPaneUses = new HashMap<Object, java.awt.Component>();
246:
247: /**
248: * Try to find the active window's glass pane.
249: * @return a glass pane, or null
250: */
251: private static java.awt.Component activeGlassPane() {
252: java.awt.Window w = java.awt.KeyboardFocusManager
253: .getCurrentKeyboardFocusManager().getActiveWindow();
254: if (w instanceof javax.swing.RootPaneContainer) {
255: return ((javax.swing.RootPaneContainer) w).getGlassPane();
256: } else {
257: return null;
258: }
259: }
260:
261: /**
262: * Sets wait cursor visible on the window associated with an event, if any.
263: * @param key something to pass to {@link #hideWaitCursor} to turn it off
264: */
265: public static void showWaitCursor(Object key) {
266: assert java.awt.EventQueue.isDispatchThread();
267: assert !glassPaneUses.containsKey(key);
268: java.awt.Component c = activeGlassPane();
269: if (c == null) {
270: err
271: .warning("showWaitCursor could not find a suitable glass pane; key="
272: + key);
273: return;
274: }
275: if (glassPaneUses.values().contains(c)) {
276: err.fine("wait cursor already displayed on " + c);
277: } else {
278: err.fine("wait cursor will be displayed on " + c);
279: c.setCursor(org.openide.util.Utilities
280: .createProgressCursor(c));
281: c.setVisible(true);
282: }
283: glassPaneUses.put(key, c);
284: }
285:
286: /**
287: * Resets cursor to default.
288: * @param key the same key passed to {@link #showWaitCursor}
289: */
290: public static void hideWaitCursor(Object key) {
291: assert java.awt.EventQueue.isDispatchThread();
292: java.awt.Component c = glassPaneUses.get(key);
293: if (c == null) {
294: return;
295: }
296: glassPaneUses.remove(key);
297: if (glassPaneUses.values().contains(c)) {
298: err.fine("wait cursor still displayed on " + c);
299: } else {
300: err.fine("wait cursor will be hidden on " + c);
301: c.setVisible(false);
302: c.setCursor(null);
303: }
304: }
305:
306: }
|