1 19 20 package org.netbeans.modules.debugger.jpda.ui.models; 21 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.util.Vector ; 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 52 public class VariablesNodeModel implements NodeModel { 53 54 public static final String FIELD = 55 "org/netbeans/modules/debugger/resources/watchesView/Field"; 56 public static final String LOCAL = 57 "org/netbeans/modules/debugger/resources/localsView/LocalVariable"; 58 public static final String FIXED_WATCH = 59 "org/netbeans/modules/debugger/resources/watchesView/FixedWatch"; 60 public static final String STATIC_FIELD = 61 "org/netbeans/modules/debugger/resources/watchesView/StaticField"; 62 public static final String SUPER = 63 "org/netbeans/modules/debugger/resources/watchesView/SuperVariable"; 64 public static final String STATIC = 65 "org/netbeans/modules/debugger/resources/watchesView/SuperVariable"; 66 public static final String 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 modelListeners = new HashSet (); 74 75 76 public VariablesNodeModel (ContextProvider lookupProvider) { 77 debugger = (JPDADebugger) lookupProvider. 78 lookupFirst (null, JPDADebugger.class); 79 } 80 81 82 public String getDisplayName (Object 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"; if (o instanceof This) 93 return "this"; if (o == "NoInfo") return NbBundle.getMessage(VariablesNodeModel.class, "CTL_No_Info"); 96 if (o == "No current thread") { return NbBundle.getMessage(VariablesNodeModel.class, "NoCurrentThreadVar"); 98 } 99 if (o instanceof JPDAClassType) { 100 return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_StaticNode"); } 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") { return NbBundle.getMessage(VariablesNodeModel.class, "lastOperationsNode"); 110 } 111 String str = o.toString(); 112 if (str.startsWith("SubArray")) { int index = str.indexOf('-'); 114 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 shortDescriptionMap = new HashMap (); 124 125 public String getShortDescription (final Object o) throws UnknownTypeException { 126 synchronized (shortDescriptionMap) { 127 Object shortDescription = shortDescriptionMap.remove(o); 128 if (shortDescription instanceof String ) { 129 return (String ) shortDescription; 130 } else if (shortDescription instanceof UnknownTypeException) { 131 throw (UnknownTypeException) shortDescription; 132 } 133 } 134 testKnown(o); 135 evaluationRP.post(new Runnable () { 137 public void run() { 138 Object 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 getShortDescriptionSynch (Object 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 type = ((ObjectVariable) o).getType (); 157 String 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 type = ((ObjectVariable) o).getType (); 179 String 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 str = o.toString(); 208 if (str.startsWith("SubArray")) { 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") return NbBundle.getMessage(VariablesNodeModel.class, "CTL_No_Info_descr"); 216 if (o == "No current thread") { return NbBundle.getMessage(VariablesNodeModel.class, "NoCurrentThreadVar"); 218 } 219 if (o instanceof JPDAClassType) { 220 return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_StaticNode_descr"); } 222 if (o instanceof ClassVariable) { 223 return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_Class_descr"); } 225 if (o instanceof ReturnVariable) { 226 return NbBundle.getMessage(VariablesNodeModel.class, "MSG_VariablesFilter_Return_descr", ((ReturnVariable) o).methodName()+"()"); } 228 if (o == "lastOperations") { return NbBundle.getMessage(VariablesNodeModel.class, "MSG_LastOperations_descr"); 230 } 231 return null; 232 } 234 235 private void testKnown(Object 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 str = o.toString(); 242 if (str.startsWith("SubArray")) return ; if (o == "NoInfo") return ; if (o == "No current thread") return ; if (o == "lastOperations") return ; 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 getIconBase (Object 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")) return LOCAL; 278 if (o == "NoInfo" || o == "No current thread") 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 [] 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 |