KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.jdi.AbsentInformationException;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.netbeans.api.debugger.DebuggerEngine;
29 import org.netbeans.spi.debugger.ContextProvider;
30 import org.netbeans.api.debugger.Session;
31 import org.netbeans.api.debugger.jpda.CallStackFrame;
32 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
33 import org.netbeans.api.debugger.jpda.JPDADebugger;
34 import org.netbeans.api.debugger.jpda.JPDAThread;
35 import org.netbeans.api.debugger.jpda.JPDAThreadGroup;
36 import org.netbeans.api.debugger.jpda.ObjectVariable;
37 import org.netbeans.spi.viewmodel.NodeModel;
38 import org.netbeans.spi.viewmodel.TreeModel;
39 import org.netbeans.spi.viewmodel.ModelListener;
40 import org.netbeans.spi.viewmodel.UnknownTypeException;
41 import org.openide.util.NbBundle;
42
43
44 /**
45  * @author Jan Jancura
46  */

47 public class ThreadsNodeModel implements NodeModel {
48     
49     public static final String JavaDoc CURRENT_THREAD =
50         "org/netbeans/modules/debugger/resources/threadsView/CurrentThread"; // NOI18N
51
public static final String JavaDoc RUNNING_THREAD =
52         "org/netbeans/modules/debugger/resources/threadsView/RunningThread"; // NOI18N
53
public static final String JavaDoc SUSPENDED_THREAD =
54         "org/netbeans/modules/debugger/resources/threadsView/SuspendedThread"; // NOI18N
55
public static final String JavaDoc THREAD_GROUP =
56         "org/netbeans/modules/debugger/resources/threadsView/ThreadGroup"; // NOI18N
57
public static final String JavaDoc CURRENT_THREAD_GROUP =
58         "org/netbeans/modules/debugger/resources/threadsView/CurrentThreadGroup"; // NOI18N
59

60     
61     private JPDADebugger debugger;
62     private Session session;
63     private Vector JavaDoc listeners = new Vector JavaDoc ();
64     
65     
66     public ThreadsNodeModel (ContextProvider lookupProvider) {
67         debugger = (JPDADebugger) lookupProvider.
68             lookupFirst (null, JPDADebugger.class);
69         session = (Session) lookupProvider.
70             lookupFirst (null, Session.class);
71         new Listener JavaDoc (this, debugger);
72     }
73     
74     public String JavaDoc getDisplayName (Object JavaDoc o) throws UnknownTypeException {
75         if (o == TreeModel.ROOT) {
76             return NbBundle.getBundle (ThreadsNodeModel.class).getString
77                 ("CTL_ThreadsModel_Column_Name_Name");
78         } else
79         if (o instanceof JPDAThread) {
80             if (debugger.getCurrentThread () == o)
81                 return BoldVariablesTableModelFilterFirst.toHTML (
82                     ((JPDAThread) o).getName (),
83                     true,
84                     false,
85                     null
86                 );
87             else
88                 return ((JPDAThread) o).getName ();
89         } else
90         if (o instanceof JPDAThreadGroup) {
91             if (isCurrent ((JPDAThreadGroup) o))
92                 return BoldVariablesTableModelFilterFirst.toHTML (
93                     ((JPDAThreadGroup) o).getName (),
94                     true,
95                     false,
96                     null
97                 );
98             else
99                 return ((JPDAThreadGroup) o).getName ();
100         } else
101         throw new UnknownTypeException (o);
102     }
103     
104     public String JavaDoc getShortDescription (Object JavaDoc o) throws UnknownTypeException {
105         if (o == TreeModel.ROOT) {
106             return NbBundle.getBundle (ThreadsNodeModel.class).getString
107                 ("CTL_ThreadsModel_Column_Name_Desc");
108         } else
109         if (o instanceof JPDAThread) {
110             JPDAThread t = (JPDAThread) o;
111             int i = t.getState ();
112             String JavaDoc s = "";
113             switch (i) {
114                 case JPDAThread.STATE_UNKNOWN:
115                     s = NbBundle.getBundle (ThreadsNodeModel.class).getString
116                         ("CTL_ThreadsModel_State_Unknown");
117                     break;
118                 case JPDAThread.STATE_MONITOR:
119                     ObjectVariable ov = t.getContendedMonitor ();
120                     if (ov == null)
121                         s = NbBundle.getBundle (ThreadsNodeModel.class).
122                             getString ("CTL_ThreadsModel_State_Monitor");
123                     else
124                         try {
125                             s = java.text.MessageFormat.
126                                 format (
127                                     NbBundle.getBundle (ThreadsNodeModel.class).
128                                         getString (
129                                     "CTL_ThreadsModel_State_ConcreteMonitor"),
130                                     new Object JavaDoc [] { ov.getToStringValue () });
131                         } catch (InvalidExpressionException ex) {
132                             s = ex.getLocalizedMessage ();
133                         }
134                     break;
135                 case JPDAThread.STATE_NOT_STARTED:
136                     s = NbBundle.getBundle (ThreadsNodeModel.class).getString
137                         ("CTL_ThreadsModel_State_NotStarted");
138                     break;
139                 case JPDAThread.STATE_RUNNING:
140                     s = NbBundle.getBundle (ThreadsNodeModel.class).getString
141                         ("CTL_ThreadsModel_State_Running");
142                     break;
143                 case JPDAThread.STATE_SLEEPING:
144                     s = NbBundle.getBundle (ThreadsNodeModel.class).getString
145                         ("CTL_ThreadsModel_State_Sleeping");
146                     break;
147                 case JPDAThread.STATE_WAIT:
148                     ov = t.getContendedMonitor ();
149                     if (ov == null)
150                         s = NbBundle.getBundle (ThreadsNodeModel.class).
151                             getString ("CTL_ThreadsModel_State_Waiting");
152                     else
153                         try {
154                             s = java.text.MessageFormat.format
155                                 (NbBundle.getBundle (ThreadsNodeModel.class).
156                                 getString ("CTL_ThreadsModel_State_WaitingOn"),
157                                 new Object JavaDoc [] { ov.getToStringValue () });
158                         } catch (InvalidExpressionException ex) {
159                             s = ex.getLocalizedMessage ();
160                         }
161                     break;
162                 case JPDAThread.STATE_ZOMBIE:
163                     s = NbBundle.getBundle (ThreadsNodeModel.class).getString
164                         ("CTL_ThreadsModel_State_Zombie");
165                     break;
166             }
167             if (t.isSuspended () && (t.getStackDepth () > 0)) {
168                 try {
169                     CallStackFrame sf = t.getCallStack (0, 1) [0];
170                     s += " " + CallStackNodeModel.getCSFName (session, sf, true);
171                 } catch (AbsentInformationException e) {
172                 }
173             }
174             return s;
175         } else
176         if (o instanceof JPDAThreadGroup) {
177             return ((JPDAThreadGroup) o).getName ();
178         } else
179         throw new UnknownTypeException (o);
180     }
181     
182     public String JavaDoc getIconBase (Object JavaDoc o) throws UnknownTypeException {
183         if (o instanceof String JavaDoc) {
184             return THREAD_GROUP;
185         } else
186         if (o instanceof JPDAThread) {
187             if (o == debugger.getCurrentThread ())
188                 return CURRENT_THREAD;
189             return ((JPDAThread) o).isSuspended () ?
190                 SUSPENDED_THREAD : RUNNING_THREAD;
191             
192         } else
193         if (o instanceof JPDAThreadGroup) {
194             if (isCurrent ((JPDAThreadGroup) o))
195                 return CURRENT_THREAD_GROUP;
196             else
197                 return THREAD_GROUP;
198         } else
199         throw new UnknownTypeException (o);
200     }
201
202     /**
203      *
204      * @param l the listener to add
205      */

206     public void addModelListener (ModelListener l) {
207         listeners.add (l);
208     }
209
210     /**
211      *
212      * @param l the listener to remove
213      */

214     public void removeModelListener (ModelListener l) {
215         listeners.remove (l);
216     }
217     
218     private void fireTreeChanged () {
219         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
220         int i, k = v.size ();
221         for (i = 0; i < k; i++)
222             ((ModelListener) v.get (i)).modelChanged (null);
223     }
224     
225     private boolean isCurrent (JPDAThreadGroup tg) {
226         JPDAThread t = debugger.getCurrentThread ();
227         if (t == null)
228             return false;
229         JPDAThreadGroup ctg = t.getParentThreadGroup ();
230         while (ctg != null) {
231             if (ctg == tg) return true;
232             ctg = ctg.getParentThreadGroup ();
233         }
234         return false;
235     }
236
237     
238     // innerclasses ............................................................
239

240     /**
241      * Listens on DebuggerManager on PROP_CURRENT_ENGINE, and on
242      * currentTreeModel.
243      */

244     private static class Listener implements PropertyChangeListener JavaDoc {
245         
246         private WeakReference JavaDoc ref;
247         private JPDADebugger debugger;
248         
249         private Listener (
250             ThreadsNodeModel rm,
251             JPDADebugger debugger
252         ) {
253             ref = new WeakReference JavaDoc (rm);
254             this.debugger = debugger;
255             debugger.addPropertyChangeListener (
256                 debugger.PROP_CURRENT_THREAD,
257                 this
258             );
259         }
260         
261         private ThreadsNodeModel getModel () {
262             ThreadsNodeModel rm = (ThreadsNodeModel) ref.get ();
263             if (rm == null) {
264                 debugger.removePropertyChangeListener (
265                     debugger.PROP_CURRENT_THREAD,
266                     this
267                 );
268             }
269             return rm;
270         }
271         
272         public void propertyChange (PropertyChangeEvent JavaDoc e) {
273             ThreadsNodeModel rm = getModel ();
274             if (rm == null) return;
275             rm.fireTreeChanged ();
276         }
277     }
278 }
279
Popular Tags