KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > ui > actions > DebuggerAction


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.debugger.ui.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
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 /**
39  *
40  * @author Jan Jancura
41  */

42 public class DebuggerAction extends AbstractAction JavaDoc {
43     
44     private Object JavaDoc action;
45
46     private DebuggerAction (Object JavaDoc action) {
47         this.action = action;
48         new Listener (this);
49         setEnabled (isEnabled (getAction ()));
50     }
51     
52     public Object JavaDoc getAction () {
53         return action;
54     }
55     
56     public Object JavaDoc getValue(String JavaDoc key) {
57         if (key == Action.NAME) {
58             return NbBundle.getMessage (DebuggerAction.class, (String JavaDoc) super.getValue(key));
59         }
60         Object JavaDoc value = super.getValue(key);
61         if (key == Action.SMALL_ICON) {
62             if (value instanceof String JavaDoc) {
63                 value = Utils.getIcon ((String JavaDoc) value);
64             }
65         }
66         return value;
67     }
68     
69     public void actionPerformed (ActionEvent JavaDoc evt) {
70         // Post the action asynchronously, since we're on AWT
71
getActionsManager(action).postAction(action);
72     }
73     
74     /**
75      * Get the actions manager of the current engine (if any).
76      * @return The actions manager or <code>null</code>.
77      */

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     /**
88      * Test whether the given action is enabled in either the current engine's
89      * action manager, or the default action manager.
90      * We need to take the default actions into account so that actions provided
91      * by other debuggers are not ignored.
92      */

93     private static boolean isEnabled(Object JavaDoc 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     /**
104      * Get the actions manager for which the action is enabled.
105      * It returns either the current engine's manager, or the default one.
106      * @param the action
107      * @return the actions manager
108      */

109     private static ActionsManager getActionsManager(Object JavaDoc 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" // NOI18N
124
);
125         action.putValue (
126             "iconBase", // NOI18N
127
"org/netbeans/modules/debugger/resources/actions/Continue.gif" // NOI18N
128
);
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" // NOI18N
137
);
138         action.putValue (
139             "iconBase",
140             "org/netbeans/modules/debugger/resources/actions/Fix.gif" // NOI18N
141
);
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" // NOI18N
150
);
151         action.putValue (
152             "iconBase", // NOI18N
153
"org/netbeans/modules/debugger/resources/actions/Kill.gif" // NOI18N
154
);
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" // NOI18N
164
);
165         action.putValue (
166             "iconBase", // NOI18N
167
"org/netbeans/modules/debugger/resources/actions/GoToCalledMethod.gif" // NOI18N
168
);
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" // NOI18N
177
);
178         action.putValue (
179             "iconBase", // NOI18N
180
"org/netbeans/modules/debugger/resources/actions/GoToCallingMethod.gif" // NOI18N
181
);
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" // NOI18N
190
);
191         action.putValue (
192             "iconBase", // NOI18N
193
"org/netbeans/modules/debugger/resources/actions/Pause.gif" // NOI18N
194
);
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" // NOI18N
215
);
216         action.putValue (
217             "iconBase", // NOI18N
218
"org/netbeans/modules/debugger/resources/actions/RunToCursor.gif" // NOI18N
219
);
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" // NOI18N
228
);
229         action.putValue (
230             "iconBase", // NOI18N
231
"org/netbeans/modules/debugger/resources/actions/StepInto.gif" // NOI18N
232
);
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" // NOI18N
241
);
242         action.putValue (
243             "iconBase", // NOI18N
244
"org/netbeans/modules/debugger/resources/actions/StepOut.gif" // NOI18N
245
);
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" // NOI18N
254
);
255         action.putValue (
256             "iconBase", // NOI18N
257
"org/netbeans/modules/debugger/resources/actions/StepOver.gif" // NOI18N
258
);
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" // NOI18N
267
);
268         action.putValue (
269             "iconBase", // NOI18N
270
"org/netbeans/modules/debugger/resources/actions/StepOverOperation.gif" // NOI18N
271
);
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     // innerclasses ............................................................
283

284     /**
285      * Listens on DebuggerManager on PROP_CURRENT_ENGINE and on current engine
286      * on PROP_ACTION_STATE and updates state of this action instance.
287      */

288     static class Listener extends DebuggerManagerAdapter
289     implements ActionsManagerListener {
290         
291         private ActionsManager currentActionsManager;
292         private WeakReference JavaDoc ref;
293
294         
295         Listener (DebuggerAction da) {
296             ref = new WeakReference JavaDoc (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 JavaDoc 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 JavaDoc () {
314                 public void run () {
315                     da.setEnabled (en);
316                 }
317             });
318         }
319         
320         public void actionPerformed (Object JavaDoc action) {
321         }
322         public void actionStateChanged (
323             final Object JavaDoc action,
324             final boolean enabled
325         ) {
326             final DebuggerAction da = getDebuggerAction ();
327             if (da == null) return;
328             if (action != da.getAction ()) return;
329             // ignore the enabled argument, check it with respect to the proper
330
// actions manager.
331
final boolean en = DebuggerAction.isEnabled (da.getAction ());
332             if (SwingUtilities.isEventDispatchThread()) {
333                 da.setEnabled(en);
334             } else {
335                 SwingUtilities.invokeLater (new Runnable JavaDoc () {
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