KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.netbeans.api.debugger.DebuggerEngine;
29 import org.netbeans.api.debugger.jpda.ClassVariable;
30 import org.netbeans.api.debugger.jpda.JPDAClassType;
31 import org.netbeans.api.debugger.jpda.ReturnVariable;
32 import org.netbeans.spi.debugger.ContextProvider;
33 import org.netbeans.api.debugger.jpda.Field;
34 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
35 import org.netbeans.api.debugger.jpda.JPDADebugger;
36 import org.netbeans.api.debugger.jpda.LocalVariable;
37 import org.netbeans.api.debugger.jpda.ObjectVariable;
38 import org.netbeans.api.debugger.jpda.Super;
39 import org.netbeans.api.debugger.jpda.This;
40 import org.netbeans.spi.viewmodel.ModelEvent;
41 import org.netbeans.spi.viewmodel.NodeModel;
42 import org.netbeans.spi.viewmodel.TreeModel;
43 import org.netbeans.spi.viewmodel.ModelListener;
44 import org.netbeans.spi.viewmodel.UnknownTypeException;
45 import org.openide.util.NbBundle;
46 import org.openide.util.RequestProcessor;
47
48
49 /**
50  * @author Jan Jancura
51  */

52 public class VariablesNodeModel implements NodeModel {
53
54     public static final String JavaDoc FIELD =
55         "org/netbeans/modules/debugger/resources/watchesView/Field";
56     public static final String JavaDoc LOCAL =
57         "org/netbeans/modules/debugger/resources/localsView/LocalVariable";
58     public static final String JavaDoc FIXED_WATCH =
59         "org/netbeans/modules/debugger/resources/watchesView/FixedWatch";
60     public static final String JavaDoc STATIC_FIELD =
61         "org/netbeans/modules/debugger/resources/watchesView/StaticField";
62     public static final String JavaDoc SUPER =
63         "org/netbeans/modules/debugger/resources/watchesView/SuperVariable";
64     public static final String JavaDoc STATIC =
65         "org/netbeans/modules/debugger/resources/watchesView/SuperVariable";
66     public static final String JavaDoc RETURN =
67         "org/netbeans/modules/debugger/jpda/resources/Filter";
68
69     
70     private JPDADebugger debugger;
71     
72     private RequestProcessor evaluationRP = new RequestProcessor();
73     private final Collection JavaDoc modelListeners = new HashSet JavaDoc();
74     
75     
76     public VariablesNodeModel (ContextProvider lookupProvider) {
77         debugger = (JPDADebugger) lookupProvider.
78             lookupFirst (null, JPDADebugger.class);
79     }
80     
81     
82     public String JavaDoc getDisplayName (Object JavaDoc o) throws UnknownTypeException {
83         if (o == TreeModel.ROOT)
84             return NbBundle.getBundle (VariablesNodeModel.class).getString
85                 ("CTL_LocalsModel_Column_Name_Name");
86         if (o instanceof Field)
87             return ((Field) o).getName ();
88         if (o instanceof LocalVariable)
89             return ((LocalVariable) o).getName ();
90         if (o instanceof Super)
91             return "super"; // NOI18N
92
if (o instanceof This)
93             return "this"; // NOI18N
94
if (o == "NoInfo") // NOI18N
95
return NbBundle.getMessage(VariablesNodeModel.class, "CTL_No_Info");
96         if (o == "No current thread") { // NOI18N
97
return NbBundle.getMessage(VariablesNodeModel.class, "NoCurrentThreadVar");
98         }
99         if (o instanceof JPDAClassType) {
100             return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_StaticNode"); // NOI18N
101
}
102         if (o instanceof ClassVariable) {
103             return "class";
104         }
105         if (o instanceof ReturnVariable) {
106             return "return "+((ReturnVariable) o).methodName()+"()";
107         }
108         if (o == "lastOperations") { // NOI18N
109
return NbBundle.getMessage(VariablesNodeModel.class, "lastOperationsNode");
110         }
111         String JavaDoc str = o.toString();
112         if (str.startsWith("SubArray")) { // NOI18N
113
int index = str.indexOf('-');
114             //int from = Integer.parseInt(str.substring(8, index));
115
//int to = Integer.parseInt(str.substring(index + 1));
116
return NbBundle.getMessage (VariablesNodeModel.class,
117                     "CTL_LocalsModel_Column_Name_SubArray",
118                     str.substring(8, index), str.substring(index + 1));
119         }
120         throw new UnknownTypeException (o);
121     }
122     
123     private Map JavaDoc shortDescriptionMap = new HashMap JavaDoc();
124     
125     public String JavaDoc getShortDescription (final Object JavaDoc o) throws UnknownTypeException {
126         synchronized (shortDescriptionMap) {
127             Object JavaDoc shortDescription = shortDescriptionMap.remove(o);
128             if (shortDescription instanceof String JavaDoc) {
129                 return (String JavaDoc) shortDescription;
130             } else if (shortDescription instanceof UnknownTypeException) {
131                 throw (UnknownTypeException) shortDescription;
132             }
133         }
134         testKnown(o);
135         // Called from AWT - we need to postpone the work...
136
evaluationRP.post(new Runnable JavaDoc() {
137             public void run() {
138                 Object JavaDoc shortDescription = getShortDescriptionSynch(o);
139                 if (shortDescription != null && !"".equals(shortDescription)) {
140                     synchronized (shortDescriptionMap) {
141                         shortDescriptionMap.put(o, shortDescription);
142                     }
143                     fireModelChange(new ModelEvent.NodeChanged(VariablesNodeModel.this,
144                         o, ModelEvent.NodeChanged.SHORT_DESCRIPTION_MASK));
145                 }
146             }
147         });
148         return "";
149     }
150     
151     private String JavaDoc getShortDescriptionSynch (Object JavaDoc o) {
152         if (o == TreeModel.ROOT)
153             return NbBundle.getBundle(VariablesNodeModel.class).getString("CTL_LocalsModel_Column_Name_Desc");
154         if (o instanceof Field) {
155             if (o instanceof ObjectVariable) {
156                 String JavaDoc type = ((ObjectVariable) o).getType ();
157                 String JavaDoc declaredType = ((Field) o).getDeclaredType ();
158                 if (type.equals (declaredType))
159                     try {
160                         return "(" + type + ") " +
161                             ((ObjectVariable) o).getToStringValue ();
162                     } catch (InvalidExpressionException ex) {
163                         return ex.getLocalizedMessage ();
164                     }
165                 else
166                     try {
167                         return "(" + declaredType + ") " + "(" + type + ") " +
168                             ((ObjectVariable) o).getToStringValue ();
169                     } catch (InvalidExpressionException ex) {
170                         return ex.getLocalizedMessage ();
171                     }
172             } else
173                 return "(" + ((Field) o).getDeclaredType () + ") " +
174                     ((Field) o).getValue ();
175         }
176         if (o instanceof LocalVariable) {
177             if (o instanceof ObjectVariable) {
178                 String JavaDoc type = ((ObjectVariable) o).getType ();
179                 String JavaDoc declaredType = ((LocalVariable) o).getDeclaredType ();
180                 if (type.equals (declaredType))
181                     try {
182                         return "(" + type + ") " +
183                             ((ObjectVariable) o).getToStringValue ();
184                     } catch (InvalidExpressionException ex) {
185                         return ex.getLocalizedMessage ();
186                     }
187                 else
188                     try {
189                         return "(" + declaredType + ") " + "(" + type + ") " +
190                             ((ObjectVariable) o).getToStringValue ();
191                     } catch (InvalidExpressionException ex) {
192                         return ex.getLocalizedMessage ();
193                     }
194             } else
195                 return "(" + ((LocalVariable) o).getDeclaredType () + ") " +
196                     ((LocalVariable) o).getValue ();
197         }
198         if (o instanceof Super)
199             return ((Super) o).getType ();
200         if (o instanceof This)
201             try {
202                 return "(" + ((This) o).getType () + ") " +
203                     ((This) o).getToStringValue ();
204             } catch (InvalidExpressionException ex) {
205                 return ex.getLocalizedMessage ();
206             }
207         String JavaDoc str = o.toString();
208         if (str.startsWith("SubArray")) { // NOI18N
209
int index = str.indexOf('-');
210             return NbBundle.getMessage (VariablesNodeModel.class,
211                     "CTL_LocalsModel_Column_Descr_SubArray",
212                     str.substring(8, index), str.substring(index + 1));
213         }
214         if (o == "NoInfo") // NOI18N
215
return NbBundle.getMessage(VariablesNodeModel.class, "CTL_No_Info_descr");
216         if (o == "No current thread") { // NOI18N
217
return NbBundle.getMessage(VariablesNodeModel.class, "NoCurrentThreadVar");
218         }
219         if (o instanceof JPDAClassType) {
220             return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_StaticNode_descr"); // NOI18N
221
}
222         if (o instanceof ClassVariable) {
223             return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_Class_descr"); // NOI18N
224
}
225         if (o instanceof ReturnVariable) {
226             return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_Return_descr", ((ReturnVariable) o).methodName()+"()"); // NOI18N
227
}
228         if (o == "lastOperations") { // NOI18N
229
return NbBundle.getMessage(VariablesNodeModel.class, "MSG_LastOperations_descr");
230         }
231         return null;
232         //throw new UnknownTypeException (o);
233
}
234     
235     private void testKnown(Object JavaDoc o) throws UnknownTypeException {
236         if (o == TreeModel.ROOT) return ;
237         if (o instanceof Field) return ;
238         if (o instanceof LocalVariable) return ;
239         if (o instanceof Super) return ;
240         if (o instanceof This) return ;
241         String JavaDoc str = o.toString();
242         if (str.startsWith("SubArray")) return ; // NOI18N
243
if (o == "NoInfo") return ; // NOI18N
244
if (o == "No current thread") return ; // NOI18N
245
if (o == "lastOperations") return ; // NOI18N
246
if (o instanceof JPDAClassType) return ;
247         if (o instanceof ClassVariable) return ;
248         if (o instanceof ReturnVariable) return ;
249         throw new UnknownTypeException (o);
250     }
251     
252     public String JavaDoc getIconBase (Object JavaDoc o) throws UnknownTypeException {
253         if (o == TreeModel.ROOT)
254             return FIELD;
255         if (o instanceof Field) {
256             if (((Field) o).isStatic ())
257                 return STATIC_FIELD;
258             else
259                 return FIELD;
260         }
261         if (o instanceof LocalVariable)
262             return LOCAL;
263         if (o instanceof Super)
264             return SUPER;
265         if (o instanceof This)
266             return FIELD;
267         if (o instanceof JPDAClassType) {
268             return STATIC;
269         }
270         if (o instanceof ClassVariable) {
271             return STATIC;
272         }
273         if (o instanceof ReturnVariable || o == "lastOperations") {
274             return RETURN;
275         }
276         if (o.toString().startsWith("SubArray")) // NOI18N
277
return LOCAL;
278         if (o == "NoInfo" || o == "No current thread") // NOI18N
279
return null;
280         throw new UnknownTypeException (o);
281     }
282
283     public void addModelListener (ModelListener l) {
284         synchronized (modelListeners) {
285             modelListeners.add(l);
286         }
287     }
288
289     public void removeModelListener (ModelListener l) {
290         synchronized (modelListeners) {
291             modelListeners.remove(l);
292         }
293     }
294     
295     private void fireModelChange(ModelEvent me) {
296         Object JavaDoc[] listeners;
297         synchronized (modelListeners) {
298             listeners = modelListeners.toArray();
299         }
300         for (int i = 0; i < listeners.length; i++) {
301             ((ModelListener) listeners[i]).modelChanged(me);
302         }
303     }
304 }
305
Popular Tags