1 19 20 package org.netbeans.modules.debugger.jpda.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.lang.ref.Reference ; 26 import java.lang.ref.WeakReference ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import javax.swing.SwingUtilities ; 30 import org.netbeans.api.debugger.DebuggerEngine; 31 import org.netbeans.api.debugger.DebuggerManager; 32 import org.netbeans.api.debugger.DebuggerManagerAdapter; 33 import org.netbeans.api.debugger.jpda.JPDADebugger; 34 import org.netbeans.modules.debugger.jpda.ui.Evaluator; 35 import org.openide.util.HelpCtx; 36 import org.openide.util.NbBundle; 37 import org.openide.util.actions.CallableSystemAction; 38 39 44 public class EvaluateAction extends AbstractAction implements PropertyChangeListener , 45 Runnable { 46 47 private EnableListener listener; 48 private transient Reference lastDebuggerRef = new WeakReference (null); 49 50 public EvaluateAction () { 51 listener = new EnableListener (this); 52 DebuggerManager.getDebuggerManager().addDebuggerListener( 53 DebuggerManager.PROP_CURRENT_ENGINE, 54 listener); 55 putValue ( 56 Action.NAME, 57 NbBundle.getMessage(EvaluateAction.class, "CTL_Evaluate") ); 59 checkEnabled(); 60 } 61 62 private synchronized boolean canBeEnabled() { 63 DebuggerEngine de = DebuggerManager.getDebuggerManager().getCurrentEngine(); 64 if (de == null) return false; 65 JPDADebugger debugger = (JPDADebugger) de.lookupFirst(null, JPDADebugger.class); 66 JPDADebugger lastDebugger = (JPDADebugger) lastDebuggerRef.get(); 67 if (lastDebugger != null && debugger != lastDebugger) { 68 lastDebugger.removePropertyChangeListener( 69 JPDADebugger.PROP_CURRENT_THREAD, 70 this); 71 lastDebuggerRef = new WeakReference (null); 72 } 73 if (debugger != null) { 74 lastDebuggerRef = new WeakReference (debugger); 75 debugger.addPropertyChangeListener( 76 JPDADebugger.PROP_CURRENT_THREAD, 77 this); 78 return (debugger.getCurrentThread() != null); 79 } else { 80 return false; 81 } 82 } 83 84 private void checkEnabled() { 85 SwingUtilities.invokeLater(this); 86 } 87 88 public void run() { 89 setEnabled(canBeEnabled()); 90 } 91 92 public void actionPerformed (ActionEvent evt) { 93 DebuggerEngine de = DebuggerManager.getDebuggerManager().getCurrentEngine(); 94 if (de == null) return ; 95 JPDADebugger debugger = (JPDADebugger) de.lookupFirst(null, JPDADebugger.class); 96 if (debugger == null) return ; 97 Evaluator.open(debugger); 98 } 99 100 public void propertyChange(PropertyChangeEvent propertyChangeEvent) { 101 SwingUtilities.invokeLater(new Runnable () { 102 public void run() { 103 synchronized (this) { 104 JPDADebugger lastDebugger = (JPDADebugger) lastDebuggerRef.get(); 105 if (lastDebugger != null) { 106 setEnabled(lastDebugger.getCurrentThread() != null); 107 } 108 } 109 } 110 }); 111 } 112 113 protected void finalize() throws Throwable { 114 DebuggerManager.getDebuggerManager().removeDebuggerListener( 115 DebuggerManager.PROP_CURRENT_ENGINE, 116 listener); 117 } 118 119 120 private static class EnableListener extends DebuggerManagerAdapter { 121 122 private Reference actionRef; 123 124 public EnableListener(EvaluateAction action) { 125 actionRef = new WeakReference (action); 126 } 127 128 public void propertyChange(PropertyChangeEvent evt) { 129 EvaluateAction action = (EvaluateAction) actionRef.get(); 130 if (action != null) { 131 action.checkEnabled(); 132 } 133 } 134 135 } 136 137 } 138 | Popular Tags |