KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > ui > models > BreakpointsActionsProvider


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.models;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.AbstractAction JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.KeyStroke JavaDoc;
26
27 import org.netbeans.api.debugger.Breakpoint;
28 import org.netbeans.api.debugger.DebuggerManager;
29 import org.netbeans.modules.debugger.ui.actions.AddBreakpointAction;
30 import org.netbeans.spi.viewmodel.Models;
31 import org.netbeans.spi.viewmodel.TreeModel;
32 import org.netbeans.spi.viewmodel.NodeActionsProvider;
33 import org.netbeans.spi.viewmodel.ModelListener;
34 import org.netbeans.spi.viewmodel.UnknownTypeException;
35 import org.openide.DialogDisplayer;
36 import org.openide.NotifyDescriptor;
37 import org.openide.util.NbBundle;
38 import org.openide.util.RequestProcessor;
39
40
41 /**
42  * @author Jan Jancura
43  */

44 public class BreakpointsActionsProvider implements NodeActionsProvider {
45     
46     
47     private static final Action JavaDoc NEW_BREEAKPOINT_ACTION = new AbstractAction JavaDoc
48         (NbBundle.getBundle (BreakpointsActionsProvider.class).getString
49             ("CTL_BreakpointAction_New_Label")) {
50             public void actionPerformed (ActionEvent JavaDoc e) {
51                 new AddBreakpointAction ().actionPerformed (null);
52             }
53     };
54     private static final Action JavaDoc ENABLE_ALL_ACTION = new AbstractAction JavaDoc
55         (NbBundle.getBundle (BreakpointsActionsProvider.class).getString
56             ("CTL_BreakpointAction_EnableAll_Label")) {
57             public boolean isEnabled () {
58                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
59                 Breakpoint[] bs = dm.getBreakpoints ();
60                 int i, k = bs.length;
61                 for (i = 0; i < k; i++) {
62                     if (!bs[i].isEnabled()) {
63                         return true;
64                     }
65                 }
66                 return false;
67             }
68             public void actionPerformed (ActionEvent JavaDoc e) {
69                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
70                 Breakpoint[] bs = dm.getBreakpoints ();
71                 int i, k = bs.length;
72                 for (i = 0; i < k; i++)
73                     bs [i].enable ();
74             }
75     };
76     private static final Action JavaDoc DISABLE_ALL_ACTION = new AbstractAction JavaDoc
77         (NbBundle.getBundle (BreakpointsActionsProvider.class).getString
78             ("CTL_BreakpointAction_DisableAll_Label")) {
79             public boolean isEnabled () {
80                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
81                 Breakpoint[] bs = dm.getBreakpoints ();
82                 int i, k = bs.length;
83                 for (i = 0; i < k; i++) {
84                     if (bs[i].isEnabled()) {
85                         return true;
86                     }
87                 }
88                 return false;
89             }
90             public void actionPerformed (ActionEvent JavaDoc e) {
91                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
92                 Breakpoint[] bs = dm.getBreakpoints ();
93                 int i, k = bs.length;
94                 for (i = 0; i < k; i++)
95                     bs [i].disable ();
96             }
97     };
98     private static final Action JavaDoc DELETE_ALL_ACTION = new AbstractAction JavaDoc
99         (NbBundle.getBundle (BreakpointsActionsProvider.class).getString
100             ("CTL_BreakpointAction_DeleteAll_Label")) {
101             public boolean isEnabled () {
102                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
103                 Breakpoint[] bs = dm.getBreakpoints ();
104                 return bs.length > 0;
105             }
106             public void actionPerformed (ActionEvent JavaDoc e) {
107                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
108                 Breakpoint[] bs = dm.getBreakpoints ();
109                 int i, k = bs.length;
110                 for (i = 0; i < k; i++)
111                     dm.removeBreakpoint (bs [i]);
112             }
113     };
114     private static final Action JavaDoc ENABLE_ACTION = Models.createAction (
115         NbBundle.getBundle (BreakpointsActionsProvider.class).getString
116             ("CTL_BreakpointAction_Enable_Label"),
117         new Models.ActionPerformer () {
118             public boolean isEnabled (Object JavaDoc node) {
119                 return true;
120             }
121             public void perform (Object JavaDoc[] nodes) {
122                 int i, k = nodes.length;
123                 for (i = 0; i < k; i++)
124                     ((Breakpoint) nodes [i]).enable ();
125             }
126         },
127         Models.MULTISELECTION_TYPE_ANY
128     );
129     private static final Action JavaDoc DISABLE_ACTION = Models.createAction (
130         NbBundle.getBundle (BreakpointsActionsProvider.class).getString
131             ("CTL_BreakpointAction_Disable_Label"),
132         new Models.ActionPerformer () {
133             public boolean isEnabled (Object JavaDoc node) {
134                 return true;
135             }
136             public void perform (Object JavaDoc[] nodes) {
137                 int i, k = nodes.length;
138                 for (i = 0; i < k; i++)
139                     ((Breakpoint) nodes [i]).disable ();
140             }
141         },
142         Models.MULTISELECTION_TYPE_ANY
143     );
144     private static final Action JavaDoc DELETE_ACTION = Models.createAction (
145         NbBundle.getBundle (BreakpointsActionsProvider.class).getString
146             ("CTL_BreakpointAction_Delete_Label"),
147         new Models.ActionPerformer () {
148             public boolean isEnabled (Object JavaDoc node) {
149                 return true;
150             }
151             public void perform (final Object JavaDoc[] nodes) {
152                 RequestProcessor.getDefault().post(new Runnable JavaDoc() {
153                     public void run() {
154                         DebuggerManager dm = DebuggerManager.getDebuggerManager ();
155                         int i, k = nodes.length;
156                         for (i = 0; i < k; i++)
157                             dm.removeBreakpoint ((Breakpoint) nodes [i]);
158                     }
159                 });
160             }
161         },
162         Models.MULTISELECTION_TYPE_ANY
163     );
164     static {
165         DELETE_ACTION.putValue (
166             Action.ACCELERATOR_KEY,
167             KeyStroke.getKeyStroke ("DELETE")
168         );
169     };
170     private static final Action JavaDoc SET_GROUP_NAME_ACTION = Models.createAction (
171         NbBundle.getBundle (BreakpointsActionsProvider.class).getString
172             ("CTL_BreakpointAction_SetGroupName_Label"),
173         new Models.ActionPerformer () {
174             public boolean isEnabled (Object JavaDoc node) {
175                 return true;
176             }
177             public void perform (Object JavaDoc[] nodes) {
178                 setGroupName (nodes);
179             }
180         },
181         Models.MULTISELECTION_TYPE_ALL
182     );
183     private static final Action JavaDoc DELETE_ALL_ACTION_S = Models.createAction (
184         NbBundle.getBundle (BreakpointsActionsProvider.class).getString
185             ("CTL_BreakpointAction_DeleteAll_Label"),
186         new Models.ActionPerformer () {
187             public boolean isEnabled (Object JavaDoc node) {
188                 return true;
189             }
190             public void perform (Object JavaDoc[] nodes) {
191                 String JavaDoc groupName = (String JavaDoc) nodes [0];
192                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
193                 Breakpoint[] bs = dm.getBreakpoints ();
194                 int i, k = bs.length;
195                 for (i = 0; i < k; i++)
196                     if (bs [i].getGroupName ().equals (groupName))
197                         dm.removeBreakpoint (bs [i]);
198             }
199         },
200         Models.MULTISELECTION_TYPE_EXACTLY_ONE
201     );
202     private static final Action JavaDoc ENABLE_ALL_ACTION_S = Models.createAction (
203         NbBundle.getBundle (BreakpointsActionsProvider.class).getString
204             ("CTL_BreakpointAction_EnableAll_Label"),
205         new Models.ActionPerformer () {
206             public boolean isEnabled (Object JavaDoc node) {
207                 String JavaDoc groupName = (String JavaDoc) node;
208                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
209                 Breakpoint[] bs = dm.getBreakpoints ();
210                 int i, k = bs.length;
211                 for (i = 0; i < k; i++) {
212                     if (bs [i].getGroupName ().equals (groupName)) {
213                         if (!bs[i].isEnabled()) {
214                             return true;
215                         }
216                     }
217                 }
218                 return false;
219             }
220             public void perform (Object JavaDoc[] nodes) {
221                 String JavaDoc groupName = (String JavaDoc) nodes [0];
222                 Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
223                     getBreakpoints ();
224                 int i, k = bs.length;
225                 for (i = 0; i < k; i++)
226                     if (bs [i].getGroupName ().equals (groupName))
227                         bs [i].enable ();
228             }
229         },
230         Models.MULTISELECTION_TYPE_EXACTLY_ONE
231     );
232     private static final Action JavaDoc DISABLE_ALL_ACTION_S = Models.createAction (
233         NbBundle.getBundle (BreakpointsActionsProvider.class).getString
234             ("CTL_BreakpointAction_DisableAll_Label"),
235         new Models.ActionPerformer () {
236             public boolean isEnabled (Object JavaDoc node) {
237                 String JavaDoc groupName = (String JavaDoc) node;
238                 DebuggerManager dm = DebuggerManager.getDebuggerManager ();
239                 Breakpoint[] bs = dm.getBreakpoints ();
240                 int i, k = bs.length;
241                 for (i = 0; i < k; i++) {
242                     if (bs [i].getGroupName ().equals (groupName)) {
243                         if (bs[i].isEnabled()) {
244                             return true;
245                         }
246                     }
247                 }
248                 return false;
249             }
250             public void perform (Object JavaDoc[] nodes) {
251                 String JavaDoc groupName = (String JavaDoc) nodes [0];
252                 Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
253                     getBreakpoints ();
254                 int i, k = bs.length;
255                 for (i = 0; i < k; i++)
256                     if (bs [i].getGroupName ().equals (groupName))
257                         bs [i].disable ();
258             }
259         },
260         Models.MULTISELECTION_TYPE_EXACTLY_ONE
261     );
262     
263     
264     //private Vector listeners = new Vector ();
265

266     
267     public Action JavaDoc[] getActions (Object JavaDoc node) throws UnknownTypeException {
268         if (node == TreeModel.ROOT)
269             return new Action JavaDoc [] {
270                 NEW_BREEAKPOINT_ACTION,
271                 null,
272                 ENABLE_ALL_ACTION,
273                 DISABLE_ALL_ACTION,
274                 DELETE_ALL_ACTION,
275                 null
276             };
277         if (node instanceof String JavaDoc)
278             return new Action JavaDoc [] {
279                 SET_GROUP_NAME_ACTION,
280                 null,
281                 ENABLE_ALL_ACTION_S,
282                 DISABLE_ALL_ACTION_S,
283                 DELETE_ALL_ACTION_S,
284                 null
285             };
286         if (node instanceof Breakpoint)
287             if (((Breakpoint) node).isEnabled ())
288                 return new Action JavaDoc [] {
289                     DISABLE_ACTION,
290                     DELETE_ACTION,
291                     SET_GROUP_NAME_ACTION,
292                     null,
293                     NEW_BREEAKPOINT_ACTION,
294                     null,
295                     ENABLE_ALL_ACTION,
296                     DISABLE_ALL_ACTION,
297                     DELETE_ALL_ACTION,
298                     null
299                 };
300             else
301                 return new Action JavaDoc [] {
302                     ENABLE_ACTION,
303                     DELETE_ACTION,
304                     SET_GROUP_NAME_ACTION,
305                     null,
306                     NEW_BREEAKPOINT_ACTION,
307                     null,
308                     ENABLE_ALL_ACTION,
309                     DISABLE_ALL_ACTION,
310                     DELETE_ALL_ACTION,
311                     null
312                 };
313         throw new UnknownTypeException (node);
314     }
315     
316     public void performDefaultAction (Object JavaDoc node) throws UnknownTypeException {
317         if (node == TreeModel.ROOT)
318             return;
319         if (node instanceof String JavaDoc)
320             return;
321         if (node instanceof Breakpoint)
322             return;
323         throw new UnknownTypeException (node);
324     }
325
326     public void addModelListener (ModelListener l) {
327         //listeners.add (l);
328
}
329
330     public void removeModelListener (ModelListener l) {
331         //listeners.remove (l);
332
}
333     
334 // public void fireTreeChanged () {
335
// Vector v = (Vector) listeners.clone ();
336
// int i, k = v.size ();
337
// for (i = 0; i < k; i++)
338
// ((TreeModelListener) v.get (i)).treeChanged ();
339
// }
340

341     private static void setGroupName (Object JavaDoc[] nodes) {
342         NotifyDescriptor.InputLine descriptor = new NotifyDescriptor.InputLine (
343             NbBundle.getBundle (BreakpointsActionsProvider.class).getString
344                 ("CTL_BreakpointAction_GroupDialog_NameLabel"),
345             NbBundle.getBundle (BreakpointsActionsProvider.class).getString
346                 ("CTL_BreakpointAction_GroupDialog_Title")
347         );
348         if (DialogDisplayer.getDefault ().notify (descriptor) ==
349             NotifyDescriptor.OK_OPTION
350         ) {
351             if (nodes [0] instanceof String JavaDoc) {
352                 String JavaDoc oldName = (String JavaDoc) nodes [0];
353                 Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
354                     getBreakpoints ();
355                 int j, jj = bs.length;
356                 for (j = 0; j < jj; j++)
357                     if ( ((Breakpoint) bs [j]).getGroupName ().
358                          equals (oldName)
359                     )
360                         ((Breakpoint) bs [j]).setGroupName (
361                             descriptor.getInputText ()
362                         );
363                 return;
364             }
365             int i, k = nodes.length;
366             for (i = 0; i < k; i++)
367                 ((Breakpoint) nodes [i]).setGroupName (
368                     descriptor.getInputText ()
369                 );
370            // fireTreeChanged ();
371
}
372     }
373 }
374
Popular Tags