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-2007 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: * Utilities_RAVE.java
043: *
044: * Created on August 24, 2004, 10:58 AM
045: */
046:
047: package org.netbeans.modules.visualweb.extension.openide.util;
048:
049: import java.awt.Component;
050: import java.awt.Cursor;
051: import java.awt.Frame;
052: import java.util.HashSet;
053: import javax.swing.Action;
054: import javax.swing.ActionMap;
055: import javax.swing.JComponent;
056: import javax.swing.JFrame;
057: import javax.swing.JMenuItem;
058: import javax.swing.RepaintManager;
059: import javax.swing.SwingUtilities;
060:
061: import org.openide.ErrorManager;
062: import org.openide.util.ContextAwareAction;
063: import org.openide.util.Lookup;
064: import org.openide.windows.WindowManager;
065:
066: /**
067: *
068: * @author Peter Zavadsky
069: */
070: public final class Utilities_RAVE {
071:
072: /** Creates a new instance of Utilities_RAVE */
073: private Utilities_RAVE() {
074: }
075:
076: // <rave>
077: /** Showing/hiding busy cursor, before this funcionality was in Rave winsys,
078: * the code is copied from that module.
079: * It needs to be called from event-dispatching thread to work synch,
080: * otherwise it is scheduled into that thread. */
081: public static void showBusyCursor(final boolean busy) {
082: if (SwingUtilities.isEventDispatchThread()) {
083: doShowBusyCursor(busy);
084: } else {
085: SwingUtilities.invokeLater(new Runnable() {
086: public void run() {
087: doShowBusyCursor(busy);
088: }
089: });
090: }
091: }
092:
093: private static void doShowBusyCursor(boolean busy) {
094: JFrame mainWindow = (JFrame) WindowManager.getDefault()
095: .getMainWindow();
096: if (busy) {
097: RepaintManager.currentManager(mainWindow)
098: .paintDirtyRegions();
099: mainWindow.getGlassPane().setCursor(
100: Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
101: mainWindow.getGlassPane().setVisible(true);
102: mainWindow.repaint();
103: } else {
104: mainWindow.getGlassPane().setVisible(false);
105: mainWindow.getGlassPane().setCursor(null);
106: mainWindow.repaint();
107: }
108: }
109:
110: // </rave>
111:
112: // <RAVE>
113: // Some rave code uses this method (designer, navigation),
114: // revise.. and try to get rid of it.
115: public static javax.swing.JPopupMenu actionsToPopup(
116: Action[] actions, Component component,
117: javax.swing.JPopupMenu menu) {
118: // TEMP>> The next lines are copied from #atcionsToPopup(Action[], Component);
119: Lookup context = null;
120: for (Component c = component; c != null; c = c.getParent()) {
121: if (c instanceof Lookup.Provider) {
122: context = ((Lookup.Provider) c).getLookup();
123: if (context != null) {
124: break;
125: }
126: }
127: }
128:
129: if (context == null) {
130: // Fallback to composite action map, even it is questionable,
131: // whether we should support component which is not (nor
132: // none of its parents) lookup provider.
133: Object map = createUtilitiesCompositeMap(component);
134: if (map != null) {
135: context = org.openide.util.lookup.Lookups
136: .singleton(map);
137: } else {
138: // If the above fails, use the copy as a fallback.
139: context = org.openide.util.lookup.Lookups
140: .singleton(new UtilitiesCompositeActionMap(
141: component));
142: }
143: }
144:
145: // TEMP>> The next lines are copied from #atcionsToPopup(Action[], Lookup);
146: // keeps actions for which was menu item created already
147: HashSet counted = new HashSet();
148: boolean canSep = false;
149: for (int i = 0; i < actions.length; i++) {
150: boolean addSep = true;
151:
152: Action action = actions[i];
153:
154: if (action != null) {
155: // if this action has menu item already, skip to next iteration
156: if (counted.contains(action))
157: continue;
158:
159: counted.add(action);
160:
161: // switch to replacement action if there is some
162: if (action instanceof ContextAwareAction) {
163: action = ((ContextAwareAction) action)
164: .createContextAwareInstance(context);
165: }
166:
167: addSep = false;
168: canSep = true;
169: javax.swing.JMenuItem item;
170: if (action instanceof org.openide.util.actions.Presenter.Popup) {
171: item = ((org.openide.util.actions.Presenter.Popup) action)
172: .getPopupPresenter();
173: if (item == null) {
174: NullPointerException npe = new NullPointerException(
175: "findContextMenuImpl, getPopupPresenter returning null for "
176: + action); // NOI18N
177: ErrorManager.getDefault().notify(
178: ErrorManager.INFORMATIONAL, npe);
179: }
180: menu.add(item);
181: } else {
182: // We need to correctly handle mnemonics with '&' etc.
183: // TODO: Since org.netbeans.modules.openide.util.AWTBridge is not a public API, using the default
184: // implementation instead; this may not handle '&' correctly
185: JMenuItem mi = new javax.swing.JMenuItem(action);
186: menu.add(mi);
187: }
188: }
189:
190: if (addSep && canSep) {
191: menu.addSeparator();
192: canSep = false;
193: }
194: }
195:
196: return menu;
197: }
198:
199: private static Object createUtilitiesCompositeMap(
200: Component component) {
201: try {
202: // XXX Reflection usage, again better then modifying NB code.
203: ClassLoader cl = org.openide.util.Utilities.class
204: .getClassLoader();
205: Class utilitiesCompositeMapClass = Class.forName(
206: "org.openide.util.UtilitiesCompositeMap", true, cl);
207: java.lang.reflect.Constructor constructor = utilitiesCompositeMapClass
208: .getDeclaredConstructor(new Class[] { Component.class });
209: constructor.setAccessible(true);
210: return constructor.newInstance(new Object[] { component });
211: } catch (ClassNotFoundException cnfe) {
212: ErrorManager.getDefault().notify(
213: ErrorManager.INFORMATIONAL, cnfe);
214: } catch (NoSuchMethodException nsme) {
215: ErrorManager.getDefault().notify(
216: ErrorManager.INFORMATIONAL, nsme);
217: } catch (InstantiationException ie) {
218: ErrorManager.getDefault().notify(
219: ErrorManager.INFORMATIONAL, ie);
220: } catch (IllegalAccessException iae) {
221: ErrorManager.getDefault().notify(
222: ErrorManager.INFORMATIONAL, iae);
223: } catch (java.lang.reflect.InvocationTargetException ite) {
224: ErrorManager.getDefault().notify(
225: ErrorManager.INFORMATIONAL, ite);
226: }
227:
228: return null;
229: }
230:
231: // </RAVE>
232:
233: // Copy of openide/src/org/openide/util/UtilitiesCompositeActionMap as a fallback.
234: private static class UtilitiesCompositeActionMap extends ActionMap {
235: private Component component;
236:
237: public UtilitiesCompositeActionMap(Component c) {
238: this .component = c;
239: }
240:
241: public int size() {
242: return keys().length;
243: }
244:
245: public Action get(Object key) {
246: Component c = component;
247: for (;;) {
248: if (c instanceof JComponent) {
249: javax.swing.ActionMap m = ((JComponent) c)
250: .getActionMap();
251: if (m != null) {
252: Action a = m.get(key);
253: if (a != null) {
254: return a;
255: }
256: }
257: }
258:
259: if (c instanceof Lookup.Provider) {
260: break;
261: }
262:
263: c = c.getParent();
264:
265: if (c == null) {
266: break;
267: }
268: }
269:
270: return null;
271: }
272:
273: public Object[] allKeys() {
274: return keys(true);
275: }
276:
277: public Object[] keys() {
278: return keys(false);
279: }
280:
281: private Object[] keys(boolean all) {
282: java.util.HashSet keys = new java.util.HashSet();
283:
284: Component c = component;
285: for (;;) {
286: if (c instanceof JComponent) {
287: javax.swing.ActionMap m = ((JComponent) c)
288: .getActionMap();
289: if (m != null) {
290: java.util.List l;
291:
292: if (all) {
293: l = java.util.Arrays.asList(m.allKeys());
294: } else {
295: l = java.util.Arrays.asList(m.keys());
296: }
297:
298: keys.addAll(l);
299: }
300: }
301:
302: if (c instanceof Lookup.Provider) {
303: break;
304: }
305:
306: c = c.getParent();
307:
308: if (c == null) {
309: break;
310: }
311: }
312:
313: return keys.toArray();
314: }
315:
316: //
317: // Not implemented
318: //
319:
320: public void remove(Object key) {
321: }
322:
323: public void setParent(ActionMap map) {
324: }
325:
326: public void clear() {
327: }
328:
329: public void put(Object key, Action action) {
330: }
331:
332: public ActionMap getParent() {
333: return null;
334: }
335:
336: } // End of UtilitiesCompositeActionMap.
337:
338: }
|