1 19 20 package org.netbeans.modules.openide.util; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeListener ; 24 import javax.swing.Action ; 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 32 public abstract class ActionsBridge extends Object { 33 34 private static RequestProcessor RP = new RequestProcessor("Module-Actions", Integer.MAX_VALUE); 36 37 39 protected abstract void invokeAction(Action action, ActionEvent 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 r2 = new Runnable () { 48 public void run() { 49 r.doRun(); 50 } 51 }; 52 53 RP.post(r2); 54 } else { 55 r.run(); 56 } 57 } 58 59 62 public static abstract class ActionRunnable implements Action { 63 final ActionEvent ev; 64 final SystemAction action; 65 final boolean async; 66 67 public ActionRunnable(ActionEvent 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()); } 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 e) { 89 run(); 90 } 91 92 public final void addPropertyChangeListener(PropertyChangeListener listener) { 93 throw new UnsupportedOperationException (); 94 } 95 96 public final Object getValue(String 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 key, Object value) { 105 throw new UnsupportedOperationException (); 106 } 107 108 public final void removePropertyChangeListener(PropertyChangeListener listener) { 109 throw new UnsupportedOperationException (); 110 } 111 112 public final void setEnabled(boolean b) { 113 throw new UnsupportedOperationException (); 114 } 115 } 116 } 118 | Popular Tags |