1 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 ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 29 import javax.swing.Action ; 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 53 public class MonitorModel implements TreeModelFilter, NodeModelFilter, 54 NodeActionsProviderFilter, TableModel, Constants { 55 56 public static final String CONTENDED_MONITOR = 57 "org/netbeans/modules/debugger/resources/allInOneView/ContendedMonitor"; public static final String OWNED_MONITORS = 59 "org/netbeans/modules/debugger/resources/allInOneView/OwnedMonitors"; public static final String MONITOR = 61 "org/netbeans/modules/debugger/resources/allInOneView/Monitor"; 63 private RequestProcessor evaluationRP = new RequestProcessor(); 64 private final Collection modelListeners = new HashSet (); 65 66 68 public Object getRoot (TreeModel model) { 69 return model.getRoot (); 70 } 71 72 public Object [] getChildren ( 73 TreeModel model, 74 Object 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 cm = null; 84 Object 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 [] os = new Object [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 [0]; 105 } 106 if (o instanceof JPDAThreadGroup) { 107 JPDAThreadGroup tg = (JPDAThreadGroup) o; 108 Object [] 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 [] fo = new Object [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 o 138 ) throws UnknownTypeException { 139 if (o instanceof ThreadWithBordel) { 140 return Integer.MAX_VALUE; 142 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 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 185 public String getDisplayName (NodeModel model, Object 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 [] { 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 [] { v.getType(), v.getValue() }); 204 } else 205 return model.getDisplayName (o); 206 } 207 208 private Map shortDescriptionMap = new HashMap (); 209 210 public String getShortDescription (final NodeModel model, final Object o) throws 211 UnknownTypeException { 212 213 synchronized (shortDescriptionMap) { 214 Object shortDescription = shortDescriptionMap.remove(o); 215 if (shortDescription instanceof String ) { 216 return (String ) shortDescription; 217 } else if (shortDescription instanceof UnknownTypeException) { 218 throw (UnknownTypeException) shortDescription; 219 } 220 } 221 222 evaluationRP.post(new Runnable () { 224 public void run() { 225 Object 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 ""; } 273 274 public String getIconBase (NodeModel model, Object 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 [] 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 318 public Action [] getActions (NodeActionsProvider model, Object o) throws 319 UnknownTypeException { 320 if (o instanceof ContendedMonitor) { 321 return new Action [0]; 322 } else 323 if (o instanceof OwnedMonitors) { 324 return new Action [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 [0]; 333 } else 334 return model.getActions (o); 335 } 336 337 public void performDefaultAction (NodeActionsProvider model, Object 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 360 public Object getValueAt (Object node, String 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 node, String 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 node, String columnID, Object value) 390 throws UnknownTypeException { 391 } 392 393 394 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 |