1 19 20 package org.netbeans.modules.debugger.ui.models; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.lang.ref.WeakReference ; 24 import java.util.Vector ; 25 26 import org.netbeans.api.debugger.DebuggerManager; 27 import org.netbeans.api.debugger.DebuggerManagerAdapter; 28 import org.netbeans.api.debugger.Session; 29 import org.netbeans.spi.viewmodel.ModelEvent; 30 import org.netbeans.spi.viewmodel.NodeModel; 31 import org.netbeans.spi.viewmodel.TreeModel; 32 import org.netbeans.spi.viewmodel.ModelListener; 33 import org.netbeans.spi.viewmodel.UnknownTypeException; 34 import org.openide.util.NbBundle; 35 36 37 40 public class SesionsNodeModel implements NodeModel { 41 42 public static final String SESSION = 43 "org/netbeans/modules/debugger/resources/sessionsView/Session"; 44 public static final String CURRENT_SESSION = 45 "org/netbeans/modules/debugger/resources/sessionsView/CurrentSession"; 46 47 private Vector listeners = new Vector (); 48 private Listener listener; 49 50 51 public String getDisplayName (Object o) throws UnknownTypeException { 52 if (listener == null) 53 listener = new Listener (this); 54 if (o == TreeModel.ROOT) { 55 return NbBundle.getBundle(SesionsNodeModel.class).getString("CTL_SessionsModel_Column_Name_Name"); 56 } else 57 if (o instanceof Session) { 58 return ((Session) o).getName (); 59 } else 60 throw new UnknownTypeException (o); 61 } 62 63 public String getShortDescription (Object o) throws UnknownTypeException { 64 if (listener == null) 65 listener = new Listener (this); 66 if (o == TreeModel.ROOT) { 67 return TreeModel.ROOT; 68 } else 69 if (o instanceof Session) { 70 return null; 71 } else 72 throw new UnknownTypeException (o); 73 } 74 75 public String getIconBase (Object o) throws UnknownTypeException { 76 if (listener == null) 77 listener = new Listener (this); 78 if (o == TreeModel.ROOT) { 79 return SESSION; 80 } else 81 if (o instanceof Session) { 82 if (o == DebuggerManager.getDebuggerManager ().getCurrentSession ()) 83 return CURRENT_SESSION; 84 else 85 return SESSION; 86 } else 87 throw new UnknownTypeException (o); 88 } 89 90 94 public void addModelListener (ModelListener l) { 95 listeners.add (l); 96 } 97 98 102 public void removeModelListener (ModelListener l) { 103 listeners.remove (l); 104 } 105 106 private void fireTreeChanged () { 107 Vector v = (Vector ) listeners.clone (); 108 int i, k = v.size (); 109 for (i = 0; i < k; i++) 110 ((ModelListener) v.get (i)).modelChanged ( 111 new ModelEvent.TreeChanged (this) 112 ); 113 } 114 115 116 118 119 122 private static class Listener extends DebuggerManagerAdapter { 123 124 private WeakReference ref; 125 126 public Listener ( 127 SesionsNodeModel rm 128 ) { 129 ref = new WeakReference (rm); 130 DebuggerManager.getDebuggerManager ().addDebuggerListener ( 131 DebuggerManager.PROP_CURRENT_SESSION, 132 this 133 ); 134 } 135 136 private SesionsNodeModel getModel () { 137 SesionsNodeModel rm = (SesionsNodeModel) ref.get (); 138 if (rm == null) { 139 DebuggerManager.getDebuggerManager (). 140 removeDebuggerListener ( 141 DebuggerManager.PROP_CURRENT_SESSION, 142 this 143 ); 144 } 145 return rm; 146 } 147 148 public void propertyChange (PropertyChangeEvent e) { 149 SesionsNodeModel rm = getModel (); 150 if (rm == null) return; 151 rm.fireTreeChanged (); 152 } 153 } 154 } 155 | Popular Tags |