KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openide > util > ActionsBridge


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.modules.openide.util;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import org.openide.util.Lookup;
26 import org.openide.util.RequestProcessor;
27 import org.openide.util.actions.CallableSystemAction;
28 import org.openide.util.actions.SystemAction;
29
30 /** Allows Node action to get access to special tricks in CallableSystemAction.
31  */

32 public abstract class ActionsBridge extends Object JavaDoc {
33     /** thread to run actions in */
34     private static RequestProcessor RP = new RequestProcessor("Module-Actions", Integer.MAX_VALUE); // NOI18N
35

36     
37     /** Invokes an action.
38      */

39     protected abstract void invokeAction(Action JavaDoc action, ActionEvent JavaDoc ev);
40
41     public static void doPerformAction(CallableSystemAction action, final ActionsBridge.ActionRunnable r) {
42         assert java.awt.EventQueue.isDispatchThread() : "Action " + action.getClass().getName() +
43         " may not be invoked from the thread " + Thread.currentThread().getName() +
44         ", only the event queue: http://www.netbeans.org/download/4_1/javadoc/OpenAPIs/apichanges.html#actions-event-thread";
45
46         if (r.async && !r.needsToBeSynchronous()) {
47             Runnable JavaDoc r2 = new Runnable JavaDoc() {
48                     public void run() {
49                         r.doRun();
50                     }
51                 };
52
53             RP.post(r2);
54         } else {
55             r.run();
56         }
57     }
58     
59     /** Special class that can be passed to invokeAction and delegates
60      * to correct values
61      */

62     public static abstract class ActionRunnable implements Action JavaDoc {
63         final ActionEvent JavaDoc ev;
64         final SystemAction action;
65         final boolean async;
66
67         public ActionRunnable(ActionEvent JavaDoc ev, SystemAction action, boolean async) {
68             this.ev = ev;
69             this.action = action;
70             this.async = async;
71         }
72
73         public final boolean needsToBeSynchronous() {
74             return "waitFinished".equals(ev.getActionCommand()); // NOI18N
75
}
76
77         public final void doRun() {
78             ActionsBridge bridge = Lookup.getDefault().lookup(ActionsBridge.class);
79             if (bridge != null) {
80                 bridge.invokeAction (this, ev);
81             } else {
82                 this.actionPerformed(ev);
83             }
84         }
85
86         protected abstract void run();
87
88         public final void actionPerformed(ActionEvent JavaDoc e) {
89             run();
90         }
91
92         public final void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
93             throw new UnsupportedOperationException JavaDoc();
94         }
95
96         public final Object JavaDoc getValue(String JavaDoc key) {
97             return action.getValue(key);
98         }
99
100         public final boolean isEnabled() {
101             return action.isEnabled();
102         }
103
104         public final void putValue(String JavaDoc key, Object JavaDoc value) {
105             throw new UnsupportedOperationException JavaDoc();
106         }
107
108         public final void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
109             throw new UnsupportedOperationException JavaDoc();
110         }
111
112         public final void setEnabled(boolean b) {
113             throw new UnsupportedOperationException JavaDoc();
114         }
115     }
116     // end of ActionRunnable
117
}
118
Popular Tags