1 19 20 package org.netbeans.modules.debugger.jpda.ui.models; 21 22 import com.sun.jdi.AbsentInformationException; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Vector ; 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 47 public class ThreadsNodeModel implements NodeModel { 48 49 public static final String CURRENT_THREAD = 50 "org/netbeans/modules/debugger/resources/threadsView/CurrentThread"; public static final String RUNNING_THREAD = 52 "org/netbeans/modules/debugger/resources/threadsView/RunningThread"; public static final String SUSPENDED_THREAD = 54 "org/netbeans/modules/debugger/resources/threadsView/SuspendedThread"; public static final String THREAD_GROUP = 56 "org/netbeans/modules/debugger/resources/threadsView/ThreadGroup"; public static final String CURRENT_THREAD_GROUP = 58 "org/netbeans/modules/debugger/resources/threadsView/CurrentThreadGroup"; 60 61 private JPDADebugger debugger; 62 private Session session; 63 private Vector listeners = new Vector (); 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 (this, debugger); 72 } 73 74 public String getDisplayName (Object 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 getShortDescription (Object 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 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 [] { 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 [] { 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 getIconBase (Object o) throws UnknownTypeException { 183 if (o instanceof String ) { 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 206 public void addModelListener (ModelListener l) { 207 listeners.add (l); 208 } 209 210 214 public void removeModelListener (ModelListener l) { 215 listeners.remove (l); 216 } 217 218 private void fireTreeChanged () { 219 Vector v = (Vector ) 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 240 244 private static class Listener implements PropertyChangeListener { 245 246 private WeakReference ref; 247 private JPDADebugger debugger; 248 249 private Listener ( 250 ThreadsNodeModel rm, 251 JPDADebugger debugger 252 ) { 253 ref = new WeakReference (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 e) { 273 ThreadsNodeModel rm = getModel (); 274 if (rm == null) return; 275 rm.fireTreeChanged (); 276 } 277 } 278 } 279 | Popular Tags |