KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > models > ThreadsActionsProvider


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.jpda.ui.models;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.util.Vector JavaDoc;
24 import javax.swing.Action JavaDoc;
25
26 import org.netbeans.api.debugger.DebuggerEngine;
27 import org.netbeans.api.debugger.DebuggerManager;
28 import org.netbeans.spi.debugger.ContextProvider;
29 import org.netbeans.api.debugger.jpda.JPDADebugger;
30 import org.netbeans.api.debugger.jpda.JPDAThread;
31 import org.netbeans.api.debugger.jpda.JPDAThreadGroup;
32 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
33 import org.netbeans.modules.debugger.jpda.ui.SourcePath;
34 import org.netbeans.spi.viewmodel.NodeActionsProvider;
35 import org.netbeans.spi.viewmodel.ModelListener;
36 import org.netbeans.spi.viewmodel.UnknownTypeException;
37 import org.netbeans.spi.viewmodel.Models;
38 import org.netbeans.spi.viewmodel.TreeModel;
39 import org.openide.util.NbBundle;
40
41
42 /**
43  * @author Jan Jancura
44  */

45 public class ThreadsActionsProvider implements NodeActionsProvider {
46
47     private Action JavaDoc MAKE_CURRENT_ACTION = Models.createAction (
48         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_MakeCurrent_Label"),
49         new Models.ActionPerformer () {
50             public boolean isEnabled (Object JavaDoc node) {
51                 if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
52                 return debugger.getCurrentThread () != node;
53             }
54             
55             public void perform (Object JavaDoc[] nodes) {
56                 if (nodes[0] instanceof MonitorModel.ThreadWithBordel) nodes[0] = ((MonitorModel.ThreadWithBordel) nodes[0]).originalThread;
57                 ((JPDAThread) nodes [0]).makeCurrent ();
58             }
59         },
60         Models.MULTISELECTION_TYPE_EXACTLY_ONE
61     );
62
63     private static Action JavaDoc GO_TO_SOURCE_ACTION = Models.createAction (
64         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_GoToSource_Label"),
65         new Models.ActionPerformer () {
66             public boolean isEnabled (Object JavaDoc node) {
67                 if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
68                 return isGoToSourceSupported ((JPDAThread) node);
69             }
70             
71             public void perform (Object JavaDoc[] nodes) {
72                 if (nodes[0] instanceof MonitorModel.ThreadWithBordel) nodes[0] = ((MonitorModel.ThreadWithBordel) nodes[0]).originalThread;
73                 String JavaDoc language = DebuggerManager.getDebuggerManager ().
74                     getCurrentSession ().getCurrentLanguage ();
75                 SourcePath sp = (SourcePath) DebuggerManager.
76                     getDebuggerManager ().getCurrentEngine ().lookupFirst
77                     (null, SourcePath.class);
78                 sp.showSource ((JPDAThread) nodes [0], language);
79             }
80         },
81         Models.MULTISELECTION_TYPE_EXACTLY_ONE
82     );
83
84     private Action JavaDoc SUSPEND_ACTION = Models.createAction (
85         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_Suspend_Label"),
86         new Models.ActionPerformer () {
87             public boolean isEnabled (Object JavaDoc node) {
88                 if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
89                 if (node instanceof JPDAThread)
90                     return !((JPDAThread) node).isSuspended ();
91                 else
92                     return true;
93             }
94             
95             public void perform (Object JavaDoc[] nodes) {
96                 int i, k = nodes.length;
97                 for (i = 0; i < k; i++) {
98                     Object JavaDoc node = (nodes[i] instanceof MonitorModel.ThreadWithBordel) ?
99                             ((MonitorModel.ThreadWithBordel) nodes[i]).originalThread : nodes[i];
100                     if (node instanceof JPDAThread)
101                         ((JPDAThread) node).suspend ();
102                     else
103                         ((JPDAThreadGroup) node).suspend ();
104                 }
105             }
106         },
107         Models.MULTISELECTION_TYPE_ALL
108     );
109
110     private Action JavaDoc RESUME_ACTION = Models.createAction (
111         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_Resume_Label"),
112         new Models.ActionPerformer () {
113             public boolean isEnabled (Object JavaDoc node) {
114                 if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
115                 if (node instanceof JPDAThread)
116                     return ((JPDAThread) node).isSuspended ();
117                 else
118                     return true;
119             }
120             
121             public void perform (Object JavaDoc[] nodes) {
122                 int i, k = nodes.length;
123                 for (i = 0; i < k; i++) {
124                     Object JavaDoc node = (nodes[i] instanceof MonitorModel.ThreadWithBordel) ?
125                             ((MonitorModel.ThreadWithBordel) nodes[i]).originalThread : nodes[i];
126                     if (node instanceof JPDAThread)
127                         ((JPDAThread) node).resume ();
128                     else
129                         ((JPDAThreadGroup) node).resume ();
130                 }
131             }
132         },
133         Models.MULTISELECTION_TYPE_ALL
134     );
135         
136     private Action JavaDoc INTERRUPT_ACTION = Models.createAction (
137         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_ThreadAction_Interrupt_Label"),
138         new Models.ActionPerformer () {
139             public boolean isEnabled (Object JavaDoc node) {
140                 if (node instanceof MonitorModel.ThreadWithBordel) node = ((MonitorModel.ThreadWithBordel) node).originalThread;
141                 if (node instanceof JPDAThread)
142                     return !((JPDAThread) node).isSuspended ();
143                 else
144                     return false;
145             }
146             
147             public void perform (Object JavaDoc[] nodes) {
148                 int i, k = nodes.length;
149                 for (i = 0; i < k; i++) {
150                     Object JavaDoc node = (nodes[i] instanceof MonitorModel.ThreadWithBordel) ?
151                             ((MonitorModel.ThreadWithBordel) nodes[i]).originalThread : nodes[i];
152                     if (node instanceof JPDAThread) {
153                         ((JPDAThread) node).interrupt();
154                     }
155                 }
156             }
157         },
158         Models.MULTISELECTION_TYPE_ALL
159     );
160         
161     private JPDADebugger debugger;
162     
163     
164     public ThreadsActionsProvider (ContextProvider lookupProvider) {
165         debugger = (JPDADebugger) lookupProvider.
166             lookupFirst (null, JPDADebugger.class);
167     }
168     
169     public Action JavaDoc[] getActions (Object JavaDoc node) throws UnknownTypeException {
170         if (node == TreeModel.ROOT)
171             return new Action JavaDoc [0];
172         if (node instanceof JPDAThreadGroup) {
173             return new Action JavaDoc [] {
174                 RESUME_ACTION,
175                 SUSPEND_ACTION,
176             };
177         } else
178         if (node instanceof JPDAThread) {
179             JPDAThread t = (JPDAThread) node;
180             boolean suspended = t.isSuspended ();
181             Action JavaDoc a = null;
182             if (suspended)
183                 a = RESUME_ACTION;
184             else
185                 a = SUSPEND_ACTION;
186             return new Action JavaDoc [] {
187                 MAKE_CURRENT_ACTION,
188                 a,
189                 INTERRUPT_ACTION,
190                 GO_TO_SOURCE_ACTION,
191             };
192         } else
193         throw new UnknownTypeException (node);
194     }
195     
196     public void performDefaultAction (Object JavaDoc node) throws UnknownTypeException {
197         if (node == TreeModel.ROOT)
198             return;
199         if (node instanceof JPDAThread)
200             ((JPDAThread) node).makeCurrent ();
201         else
202         if (node instanceof JPDAThreadGroup)
203             return;
204         else
205         throw new UnknownTypeException (node);
206     }
207
208     /**
209      *
210      * @param l the listener to add
211      */

212     public void addModelListener (ModelListener l) {
213     }
214
215     /**
216      *
217      * @param l the listener to remove
218      */

219     public void removeModelListener (ModelListener l) {
220     }
221
222     private static boolean isGoToSourceSupported (JPDAThread t) {
223         String JavaDoc language = DebuggerManager.getDebuggerManager ().
224             getCurrentSession ().getCurrentLanguage ();
225         if (!t.isSuspended ())
226             return false;
227         String JavaDoc className = t.getClassName ();
228         SourcePath sp = (SourcePath) DebuggerManager.
229             getDebuggerManager ().getCurrentEngine ().lookupFirst
230             (null, SourcePath.class);
231         return sp.sourceAvailable (t, language, true);
232     }
233 }
234
Popular Tags