1 19 20 package org.netbeans.modules.debugger.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeEvent ; 24 import java.lang.ref.WeakReference ; 25 import javax.swing.AbstractAction ; 26 import javax.swing.Action ; 27 import javax.swing.SwingUtilities ; 28 import org.netbeans.api.debugger.ActionsManager; 29 30 import org.netbeans.api.debugger.ActionsManagerListener; 31 import org.netbeans.api.debugger.DebuggerEngine; 32 import org.netbeans.api.debugger.DebuggerManager; 33 import org.netbeans.api.debugger.DebuggerManagerAdapter; 34 import org.netbeans.modules.debugger.ui.Utils; 35 import org.openide.util.NbBundle; 36 37 38 42 public class DebuggerAction extends AbstractAction { 43 44 private Object action; 45 46 private DebuggerAction (Object action) { 47 this.action = action; 48 new Listener (this); 49 setEnabled (isEnabled (getAction ())); 50 } 51 52 public Object getAction () { 53 return action; 54 } 55 56 public Object getValue(String key) { 57 if (key == Action.NAME) { 58 return NbBundle.getMessage (DebuggerAction.class, (String ) super.getValue(key)); 59 } 60 Object value = super.getValue(key); 61 if (key == Action.SMALL_ICON) { 62 if (value instanceof String ) { 63 value = Utils.getIcon ((String ) value); 64 } 65 } 66 return value; 67 } 68 69 public void actionPerformed (ActionEvent evt) { 70 getActionsManager(action).postAction(action); 72 } 73 74 78 private static ActionsManager getCurrentEngineActionsManager() { 79 DebuggerEngine engine = DebuggerManager.getDebuggerManager().getCurrentEngine(); 80 if (engine != null) { 81 return engine.getActionsManager(); 82 } else { 83 return null; 84 } 85 } 86 87 93 private static boolean isEnabled(Object action) { 94 ActionsManager manager = getCurrentEngineActionsManager(); 95 if (manager != null) { 96 if (manager.isEnabled(action)) { 97 return true; 98 } 99 } 100 return DebuggerManager.getDebuggerManager().getActionsManager().isEnabled(action); 101 } 102 103 109 private static ActionsManager getActionsManager(Object action) { 110 ActionsManager manager = getCurrentEngineActionsManager(); 111 if (manager != null) { 112 if (manager.isEnabled(action)) { 113 return manager; 114 } 115 } 116 return DebuggerManager.getDebuggerManager().getActionsManager(); 117 } 118 119 public static DebuggerAction createContinueAction() { 120 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_CONTINUE); 121 action.putValue (Action.NAME, "CTL_Continue_action_name"); 122 action.putValue (Action.SMALL_ICON, 123 "org/netbeans/modules/debugger/resources/actions/Continue" ); 125 action.putValue ( 126 "iconBase", "org/netbeans/modules/debugger/resources/actions/Continue.gif" ); 129 return action; 130 } 131 132 public static DebuggerAction createFixAction() { 133 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_FIX); 134 action.putValue (Action.NAME, "CTL_Fix_action_name"); 135 action.putValue (Action.SMALL_ICON, 136 "org/netbeans/modules/debugger/resources/actions/Fix" ); 138 action.putValue ( 139 "iconBase", 140 "org/netbeans/modules/debugger/resources/actions/Fix.gif" ); 142 return action; 143 } 144 145 public static DebuggerAction createKillAction() { 146 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_KILL); 147 action.putValue (Action.NAME, "CTL_KillAction_name"); 148 action.putValue (Action.SMALL_ICON, 149 "org/netbeans/modules/debugger/resources/actions/Kill" ); 151 action.putValue ( 152 "iconBase", "org/netbeans/modules/debugger/resources/actions/Kill.gif" ); 155 action.setEnabled (false); 156 return action; 157 } 158 159 public static DebuggerAction createMakeCalleeCurrentAction() { 160 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_MAKE_CALLEE_CURRENT); 161 action.putValue (Action.NAME, "CTL_MakeCalleeCurrentAction_name"); 162 action.putValue (Action.SMALL_ICON, 163 "org/netbeans/modules/debugger/resources/actions/GoToCalledMethod" ); 165 action.putValue ( 166 "iconBase", "org/netbeans/modules/debugger/resources/actions/GoToCalledMethod.gif" ); 169 return action; 170 } 171 172 public static DebuggerAction createMakeCallerCurrentAction() { 173 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_MAKE_CALLER_CURRENT); 174 action.putValue (Action.NAME, "CTL_MakeCallerCurrentAction_name"); 175 action.putValue (Action.SMALL_ICON, 176 "org/netbeans/modules/debugger/resources/actions/GoToCallingMethod" ); 178 action.putValue ( 179 "iconBase", "org/netbeans/modules/debugger/resources/actions/GoToCallingMethod.gif" ); 182 return action; 183 } 184 185 public static DebuggerAction createPauseAction () { 186 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_PAUSE); 187 action.putValue (Action.NAME, "CTL_Pause_action_name"); 188 action.putValue (Action.SMALL_ICON, 189 "org/netbeans/modules/debugger/resources/actions/Pause" ); 191 action.putValue ( 192 "iconBase", "org/netbeans/modules/debugger/resources/actions/Pause.gif" ); 195 return action; 196 } 197 198 public static DebuggerAction createPopTopmostCallAction () { 199 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_POP_TOPMOST_CALL); 200 action.putValue (Action.NAME, "CTL_PopTopmostCallAction_name"); 201 return action; 202 } 203 204 public static DebuggerAction createRunIntoMethodAction () { 205 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_RUN_INTO_METHOD); 206 action.putValue (Action.NAME, "CTL_Run_into_method_action_name"); 207 return action; 208 } 209 210 public static DebuggerAction createRunToCursorAction () { 211 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_RUN_TO_CURSOR); 212 action.putValue (Action.NAME, "CTL_Run_to_cursor_action_name"); 213 action.putValue (Action.SMALL_ICON, 214 "org/netbeans/modules/debugger/resources/actions/RunToCursor" ); 216 action.putValue ( 217 "iconBase", "org/netbeans/modules/debugger/resources/actions/RunToCursor.gif" ); 220 return action; 221 } 222 223 public static DebuggerAction createStepIntoAction () { 224 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_STEP_INTO); 225 action.putValue (Action.NAME, "CTL_Step_into_action_name"); 226 action.putValue (Action.SMALL_ICON, 227 "org/netbeans/modules/debugger/resources/actions/StepInto" ); 229 action.putValue ( 230 "iconBase", "org/netbeans/modules/debugger/resources/actions/StepInto.gif" ); 233 return action; 234 } 235 236 public static DebuggerAction createStepOutAction () { 237 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_STEP_OUT); 238 action.putValue (Action.NAME, "CTL_Step_out_action_name"); 239 action.putValue (Action.SMALL_ICON, 240 "org/netbeans/modules/debugger/resources/actions/StepOut" ); 242 action.putValue ( 243 "iconBase", "org/netbeans/modules/debugger/resources/actions/StepOut.gif" ); 246 return action; 247 } 248 249 public static DebuggerAction createStepOverAction () { 250 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_STEP_OVER); 251 action.putValue (Action.NAME, "CTL_Step_over_action_name"); 252 action.putValue (Action.SMALL_ICON, 253 "org/netbeans/modules/debugger/resources/actions/StepOver" ); 255 action.putValue ( 256 "iconBase", "org/netbeans/modules/debugger/resources/actions/StepOver.gif" ); 259 return action; 260 } 261 262 public static DebuggerAction createStepOperationAction () { 263 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_STEP_OPERATION); 264 action.putValue (Action.NAME, "CTL_Step_operation_action_name"); 265 action.putValue (Action.SMALL_ICON, 266 "org/netbeans/modules/debugger/resources/actions/StepOverOperation" ); 268 action.putValue ( 269 "iconBase", "org/netbeans/modules/debugger/resources/actions/StepOverOperation.gif" ); 272 return action; 273 } 274 275 public static DebuggerAction createToggleBreakpointAction () { 276 DebuggerAction action = new DebuggerAction(ActionsManager.ACTION_TOGGLE_BREAKPOINT); 277 action.putValue (Action.NAME, "CTL_Toggle_breakpoint"); 278 return action; 279 } 280 281 282 284 288 static class Listener extends DebuggerManagerAdapter 289 implements ActionsManagerListener { 290 291 private ActionsManager currentActionsManager; 292 private WeakReference ref; 293 294 295 Listener (DebuggerAction da) { 296 ref = new WeakReference (da); 297 DebuggerManager.getDebuggerManager ().addDebuggerListener ( 298 DebuggerManager.PROP_CURRENT_ENGINE, 299 this 300 ); 301 DebuggerManager.getDebuggerManager ().getActionsManager().addActionsManagerListener( 302 ActionsManagerListener.PROP_ACTION_STATE_CHANGED, 303 this 304 ); 305 updateCurrentActionsManager (); 306 } 307 308 public void propertyChange (PropertyChangeEvent evt) { 309 final DebuggerAction da = getDebuggerAction (); 310 if (da == null) return; 311 updateCurrentActionsManager (); 312 final boolean en = DebuggerAction.isEnabled (da.getAction ()); 313 SwingUtilities.invokeLater (new Runnable () { 314 public void run () { 315 da.setEnabled (en); 316 } 317 }); 318 } 319 320 public void actionPerformed (Object action) { 321 } 322 public void actionStateChanged ( 323 final Object action, 324 final boolean enabled 325 ) { 326 final DebuggerAction da = getDebuggerAction (); 327 if (da == null) return; 328 if (action != da.getAction ()) return; 329 final boolean en = DebuggerAction.isEnabled (da.getAction ()); 332 if (SwingUtilities.isEventDispatchThread()) { 333 da.setEnabled(en); 334 } else { 335 SwingUtilities.invokeLater (new Runnable () { 336 public void run () { 337 da.setEnabled (en); 338 } 339 }); 340 } 341 } 342 343 private void updateCurrentActionsManager () { 344 ActionsManager newActionsManager = getCurrentEngineActionsManager (); 345 if (currentActionsManager == newActionsManager) return; 346 347 if (currentActionsManager != null) 348 currentActionsManager.removeActionsManagerListener 349 (ActionsManagerListener.PROP_ACTION_STATE_CHANGED, this); 350 if (newActionsManager != null) 351 newActionsManager.addActionsManagerListener 352 (ActionsManagerListener.PROP_ACTION_STATE_CHANGED, this); 353 currentActionsManager = newActionsManager; 354 } 355 356 private DebuggerAction getDebuggerAction () { 357 DebuggerAction da = (DebuggerAction) ref.get (); 358 if (da == null) { 359 DebuggerManager.getDebuggerManager ().removeDebuggerListener ( 360 DebuggerManager.PROP_CURRENT_ENGINE, 361 this 362 ); 363 DebuggerManager.getDebuggerManager ().getActionsManager().removeActionsManagerListener( 364 ActionsManagerListener.PROP_ACTION_STATE_CHANGED, 365 this 366 ); 367 if (currentActionsManager != null) 368 currentActionsManager.removeActionsManagerListener 369 (ActionsManagerListener.PROP_ACTION_STATE_CHANGED, this); 370 currentActionsManager = null; 371 return null; 372 } 373 return da; 374 } 375 } 376 } 377 378 | Popular Tags |