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.modules.openide.util;
043:
044: import java.awt.event.ActionEvent;
045: import java.beans.PropertyChangeListener;
046: import javax.swing.Action;
047: import org.openide.util.Lookup;
048: import org.openide.util.RequestProcessor;
049: import org.openide.util.actions.CallableSystemAction;
050: import org.openide.util.actions.SystemAction;
051:
052: /** Allows Node action to get access to special tricks in CallableSystemAction.
053: */
054: public abstract class ActionsBridge extends Object {
055: /** thread to run actions in */
056: private static RequestProcessor RP = new RequestProcessor(
057: "Module-Actions", Integer.MAX_VALUE); // NOI18N
058:
059: /** Invokes an action.
060: */
061: protected abstract void invokeAction(Action action, ActionEvent ev);
062:
063: public static void doPerformAction(CallableSystemAction action,
064: final ActionsBridge.ActionRunnable r) {
065: assert java.awt.EventQueue.isDispatchThread() : "Action "
066: + action.getClass().getName()
067: + " may not be invoked from the thread "
068: + Thread.currentThread().getName()
069: + ", only the event queue: http://www.netbeans.org/download/4_1/javadoc/OpenAPIs/apichanges.html#actions-event-thread";
070:
071: if (r.async && !r.needsToBeSynchronous()) {
072: Runnable r2 = new Runnable() {
073: public void run() {
074: r.doRun();
075: }
076: };
077:
078: RP.post(r2);
079: } else {
080: r.run();
081: }
082: }
083:
084: /** Special class that can be passed to invokeAction and delegates
085: * to correct values
086: */
087: public static abstract class ActionRunnable implements Action {
088: final ActionEvent ev;
089: final SystemAction action;
090: final boolean async;
091:
092: public ActionRunnable(ActionEvent ev, SystemAction action,
093: boolean async) {
094: this .ev = ev;
095: this .action = action;
096: this .async = async;
097: }
098:
099: public final boolean needsToBeSynchronous() {
100: return "waitFinished".equals(ev.getActionCommand()); // NOI18N
101: }
102:
103: public final void doRun() {
104: ActionsBridge bridge = Lookup.getDefault().lookup(
105: ActionsBridge.class);
106: if (bridge != null) {
107: bridge.invokeAction(this , ev);
108: } else {
109: this .actionPerformed(ev);
110: }
111: }
112:
113: protected abstract void run();
114:
115: public final void actionPerformed(ActionEvent e) {
116: run();
117: }
118:
119: public final void addPropertyChangeListener(
120: PropertyChangeListener listener) {
121: throw new UnsupportedOperationException();
122: }
123:
124: public final Object getValue(String key) {
125: return action.getValue(key);
126: }
127:
128: public final boolean isEnabled() {
129: return action.isEnabled();
130: }
131:
132: public final void putValue(String key, Object value) {
133: throw new UnsupportedOperationException();
134: }
135:
136: public final void removePropertyChangeListener(
137: PropertyChangeListener listener) {
138: throw new UnsupportedOperationException();
139: }
140:
141: public final void setEnabled(boolean b) {
142: throw new UnsupportedOperationException();
143: }
144: }
145: // end of ActionRunnable
146: }
|