KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectCollectedException;
23 import com.sun.jdi.VMDisconnectedException;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.swing.Action JavaDoc;
30 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
31
32 import org.netbeans.api.debugger.jpda.JPDAThread;
33 import org.netbeans.api.debugger.jpda.JPDAThreadGroup;
34 import org.netbeans.api.debugger.jpda.ObjectVariable;
35 import org.netbeans.spi.debugger.ui.Constants;
36 import org.netbeans.spi.viewmodel.ModelEvent;
37 import org.netbeans.spi.viewmodel.NodeActionsProvider;
38 import org.netbeans.spi.viewmodel.NodeActionsProviderFilter;
39 import org.netbeans.spi.viewmodel.NodeModel;
40 import org.netbeans.spi.viewmodel.NodeModelFilter;
41 import org.netbeans.spi.viewmodel.TableModel;
42 import org.netbeans.spi.viewmodel.TreeModel;
43 import org.netbeans.spi.viewmodel.TreeModelFilter;
44 import org.netbeans.spi.viewmodel.ModelListener;
45 import org.netbeans.spi.viewmodel.UnknownTypeException;
46 import org.openide.util.NbBundle;
47 import org.openide.util.RequestProcessor;
48
49
50 /**
51  * @author Jan Jancura
52  */

53 public class MonitorModel implements TreeModelFilter, NodeModelFilter,
54 NodeActionsProviderFilter, TableModel, Constants {
55
56     public static final String JavaDoc CONTENDED_MONITOR =
57         "org/netbeans/modules/debugger/resources/allInOneView/ContendedMonitor"; // NOI18N
58
public static final String JavaDoc OWNED_MONITORS =
59         "org/netbeans/modules/debugger/resources/allInOneView/OwnedMonitors"; // NOI18N
60
public static final String JavaDoc MONITOR =
61         "org/netbeans/modules/debugger/resources/allInOneView/Monitor"; // NOI18N
62

63     private RequestProcessor evaluationRP = new RequestProcessor();
64     private final Collection JavaDoc modelListeners = new HashSet JavaDoc();
65     
66     // TreeView impl............................................................
67

68     public Object JavaDoc getRoot (TreeModel model) {
69         return model.getRoot ();
70     }
71     
72     public Object JavaDoc[] getChildren (
73         TreeModel model,
74         Object JavaDoc o,
75         int from,
76         int to
77     ) throws UnknownTypeException {
78         if (o instanceof ThreadWithBordel) {
79             try {
80                 JPDAThread t = ((ThreadWithBordel) o).originalThread;
81                 ObjectVariable contended = t.getContendedMonitor ();
82                 ObjectVariable[] owned = t.getOwnedMonitors ();
83                 Object JavaDoc cm = null;
84                 Object JavaDoc om = null;
85                 if ( (contended != null) &&
86                      (from == 0) && (to > 0)
87                 ) cm = new ContendedMonitor (contended);
88                 if ( (owned.length > 0) &&
89                      ( ((contended != null) && (from < 2) && (to > 1)) ||
90                        ((contended == null) && (from == 0) && (to > 0))
91                      )
92                 ) om = new OwnedMonitors (owned);
93                 int i = 0;
94                 if (cm != null) i++;
95                 if (om != null) i++;
96                 Object JavaDoc[] os = new Object JavaDoc [i];
97                 i = 0;
98                 if (cm != null) os[i++] = cm;
99                 if (om != null) os[i++] = om;
100                 return os;
101             } catch (ObjectCollectedException e) {
102             } catch (VMDisconnectedException e) {
103             }
104             return new Object JavaDoc [0];
105         }
106         if (o instanceof JPDAThreadGroup) {
107             JPDAThreadGroup tg = (JPDAThreadGroup) o;
108             Object JavaDoc[] ch = model.getChildren (o, from, to);
109             int i, k = ch.length;
110             for (i = 0; i < k; i++) {
111                 if (!(ch [i] instanceof JPDAThread)) continue;
112                 try {
113                     JPDAThread t = (JPDAThread) ch [i];
114                     if (t.getContendedMonitor () == null &&
115                         t.getOwnedMonitors ().length == 0
116                     ) continue;
117                     ThreadWithBordel twb = new ThreadWithBordel ();
118                     twb.originalThread = t;
119                     ch [i] = twb;
120                 } catch (ObjectCollectedException e) {
121                 } catch (VMDisconnectedException e) {
122                 }
123             }
124             return ch;
125         }
126         if (o instanceof OwnedMonitors) {
127             OwnedMonitors om = (OwnedMonitors) o;
128             Object JavaDoc[] fo = new Object JavaDoc [to - from];
129             System.arraycopy (om.variables, from, fo, 0, to - from);
130             return fo;
131         }
132         return model.getChildren (o, from, to);
133     }
134     
135     public int getChildrenCount (
136         TreeModel model,
137         Object JavaDoc o
138     ) throws UnknownTypeException {
139         if (o instanceof ThreadWithBordel) {
140             // Performance, see issue #59058.
141
return Integer.MAX_VALUE;
142             /*
143             try {
144                 JPDAThread t = ((ThreadWithBordel) o).originalThread;
145                 ObjectVariable contended = t.getContendedMonitor ();
146                 ObjectVariable[] owned = t.getOwnedMonitors ();
147                 int i = 0;
148                 if (contended != null) i++;
149                 if (owned.length > 0) i++;
150                 return i;
151             } catch (ObjectCollectedException e) {
152             } catch (VMDisconnectedException e) {
153             }
154             return 0;
155              */

156         }
157         if (o instanceof ThreadWithBordel) {
158             return model.getChildrenCount (
159                 ((ThreadWithBordel) o).originalThread
160             );
161         }
162         if (o instanceof OwnedMonitors) {
163             return ((OwnedMonitors) o).variables.length;
164         }
165         return model.getChildrenCount (o);
166     }
167     
168     public boolean isLeaf (TreeModel model, Object JavaDoc o)
169     throws UnknownTypeException {
170         if (o instanceof ThreadWithBordel) {
171             return false;
172         }
173         if (o instanceof OwnedMonitors)
174             return false;
175         if (o instanceof ContendedMonitor)
176             return true;
177         if (o instanceof ObjectVariable)
178             return true;
179         return model.isLeaf (o);
180     }
181     
182     
183     // NodeModel impl...........................................................
184

185     public String JavaDoc getDisplayName (NodeModel model, Object JavaDoc o) throws
186     UnknownTypeException {
187         if (o instanceof ContendedMonitor) {
188             ObjectVariable v = ((ContendedMonitor) o).variable;
189             return java.text.MessageFormat.format(NbBundle.getBundle(MonitorModel.class).getString(
190                     "CTL_MonitorModel_Column_ContendedMonitor"), new Object JavaDoc [] { v.getType(), v.getValue() });
191         } else
192         if (o instanceof ThreadWithBordel) {
193             return model.getDisplayName (
194                 ((ThreadWithBordel) o).originalThread
195             );
196         }
197         if (o instanceof OwnedMonitors) {
198             return NbBundle.getBundle(MonitorModel.class).getString("CTL_MonitorModel_Column_OwnedMonitors");
199         } else
200         if (o instanceof ObjectVariable) {
201             ObjectVariable v = (ObjectVariable) o;
202             return java.text.MessageFormat.format(NbBundle.getBundle(MonitorModel.class).getString(
203                     "CTL_MonitorModel_Column_Monitor"), new Object JavaDoc [] { v.getType(), v.getValue() });
204         } else
205         return model.getDisplayName (o);
206     }
207     
208     private Map JavaDoc shortDescriptionMap = new HashMap JavaDoc();
209     
210     public String JavaDoc getShortDescription (final NodeModel model, final Object JavaDoc o) throws
211     UnknownTypeException {
212         
213         synchronized (shortDescriptionMap) {
214             Object JavaDoc shortDescription = shortDescriptionMap.remove(o);
215             if (shortDescription instanceof String JavaDoc) {
216                 return (String JavaDoc) shortDescription;
217             } else if (shortDescription instanceof UnknownTypeException) {
218                 throw (UnknownTypeException) shortDescription;
219             }
220         }
221         
222         // Called from AWT - we need to postpone the work...
223
evaluationRP.post(new Runnable JavaDoc() {
224             public void run() {
225                 Object JavaDoc shortDescription;
226                 if (o instanceof ContendedMonitor) {
227                     ObjectVariable v = ((ContendedMonitor) o).variable;
228                     try {
229                         shortDescription = "(" + v.getType () + ") " + v.getToStringValue ();
230                     } catch (InvalidExpressionException ex) {
231                         shortDescription = ex.getLocalizedMessage ();
232                     }
233                 } else
234                 if (o instanceof ThreadWithBordel) {
235                     try {
236                         shortDescription = model.getShortDescription (
237                             ((ThreadWithBordel) o).originalThread
238                         );
239                     } catch (UnknownTypeException utex) {
240                         shortDescription = utex;
241                     }
242                 } else
243                 if (o instanceof OwnedMonitors) {
244                     shortDescription = "";
245                 } else
246                 if (o instanceof ObjectVariable) {
247                     ObjectVariable v = (ObjectVariable) o;
248                     try {
249                         shortDescription = "(" + v.getType () + ") " + v.getToStringValue ();
250                     } catch (InvalidExpressionException ex) {
251                         shortDescription = ex.getLocalizedMessage ();
252                     }
253                 } else {
254                     try {
255                         shortDescription = model.getShortDescription (o);
256                     } catch (UnknownTypeException utex) {
257                         shortDescription = utex;
258                     }
259                 }
260                 
261                 if (shortDescription != null && !"".equals(shortDescription)) {
262                     synchronized (shortDescriptionMap) {
263                         shortDescriptionMap.put(o, shortDescription);
264                     }
265                     fireModelChange(new ModelEvent.NodeChanged(MonitorModel.this,
266                         o, ModelEvent.NodeChanged.SHORT_DESCRIPTION_MASK));
267                 }
268             }
269         });
270         
271         return ""; // NOI18N
272
}
273     
274     public String JavaDoc getIconBase (NodeModel model, Object JavaDoc o) throws
275     UnknownTypeException {
276         if (o instanceof ContendedMonitor) {
277             return CONTENDED_MONITOR;
278         } else
279         if (o instanceof ThreadWithBordel) {
280             return model.getIconBase (
281                 ((ThreadWithBordel) o).originalThread
282             );
283         }
284         if (o instanceof OwnedMonitors) {
285             return OWNED_MONITORS;
286         } else
287         if (o instanceof ObjectVariable) {
288             return MONITOR;
289         } else
290         return model.getIconBase (o);
291     }
292
293     public void addModelListener (ModelListener l) {
294         synchronized (modelListeners) {
295             modelListeners.add(l);
296         }
297     }
298
299     public void removeModelListener (ModelListener l) {
300         synchronized (modelListeners) {
301             modelListeners.remove(l);
302         }
303     }
304     
305     private void fireModelChange(ModelEvent me) {
306         Object JavaDoc[] listeners;
307         synchronized (modelListeners) {
308             listeners = modelListeners.toArray();
309         }
310         for (int i = 0; i < listeners.length; i++) {
311             ((ModelListener) listeners[i]).modelChanged(me);
312         }
313     }
314     
315     
316     // NodeActionsProvider impl.................................................
317

318     public Action JavaDoc[] getActions (NodeActionsProvider model, Object JavaDoc o) throws
319     UnknownTypeException {
320         if (o instanceof ContendedMonitor) {
321             return new Action JavaDoc [0];
322         } else
323         if (o instanceof OwnedMonitors) {
324             return new Action JavaDoc [0];
325         } else
326         if (o instanceof ThreadWithBordel) {
327             return model.getActions (
328                 ((ThreadWithBordel) o).originalThread
329             );
330         }
331         if (o instanceof ObjectVariable) {
332             return new Action JavaDoc [0];
333         } else
334         return model.getActions (o);
335     }
336     
337     public void performDefaultAction (NodeActionsProvider model, Object JavaDoc o)
338     throws UnknownTypeException {
339         if (o instanceof ContendedMonitor) {
340             return;
341         } else
342         if (o instanceof OwnedMonitors) {
343             return;
344         } else
345         if (o instanceof ThreadWithBordel) {
346             model.performDefaultAction (
347                 ((ThreadWithBordel) o).originalThread
348             );
349             return;
350         }
351         if (o instanceof ObjectVariable) {
352             return;
353         } else
354         model.performDefaultAction (o);
355     }
356     
357     
358     // TableModel ..............................................................
359

360     public Object JavaDoc getValueAt (Object JavaDoc node, String JavaDoc columnID) throws
361     UnknownTypeException {
362         if (node instanceof OwnedMonitors ||
363             node instanceof ContendedMonitor ||
364             node instanceof ObjectVariable) {
365             
366             if (columnID == THREAD_STATE_COLUMN_ID)
367                 return "";
368             if (columnID == THREAD_SUSPENDED_COLUMN_ID)
369                 return null;
370         }
371         throw new UnknownTypeException (node);
372     }
373     
374     public boolean isReadOnly (Object JavaDoc node, String JavaDoc columnID) throws
375     UnknownTypeException {
376         if (node instanceof OwnedMonitors ||
377             node instanceof ContendedMonitor ||
378             node instanceof ObjectVariable) {
379             
380             if (columnID == THREAD_STATE_COLUMN_ID ||
381                 columnID == THREAD_SUSPENDED_COLUMN_ID) {
382                 
383                 return true;
384             }
385         }
386         throw new UnknownTypeException (node);
387     }
388     
389     public void setValueAt (Object JavaDoc node, String JavaDoc columnID, Object JavaDoc value)
390     throws UnknownTypeException {
391     }
392     
393     
394     // innerclasses ............................................................
395

396     private static class OwnedMonitors {
397         ObjectVariable[] variables;
398         
399         OwnedMonitors (ObjectVariable[] vs) {
400             variables = vs;
401         }
402     }
403     
404     private static class ContendedMonitor {
405         ObjectVariable variable;
406         
407         ContendedMonitor (ObjectVariable v) {
408             variable = v;
409         }
410     }
411     
412     static class ThreadWithBordel {
413         JPDAThread originalThread;
414     }
415 }
416
Popular Tags