1 19 20 package org.netbeans.modules.debugger.ui.models; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.Vector ; 24 25 import org.netbeans.api.debugger.Session; 26 import org.netbeans.api.debugger.DebuggerManager; 27 import org.netbeans.api.debugger.DebuggerManagerAdapter; 28 import org.netbeans.spi.viewmodel.TreeModel; 29 import org.netbeans.spi.viewmodel.ModelListener; 30 import org.netbeans.spi.viewmodel.UnknownTypeException; 31 32 33 36 public class SessionsTreeModel implements TreeModel { 37 38 private Listener listener; 39 private Vector listeners = new Vector (); 40 41 42 46 public Object getRoot () { 47 return ROOT; 48 } 49 50 54 public Object [] getChildren (Object parent, int from, int to) 55 throws UnknownTypeException { 56 if (parent == ROOT) { 57 Session[] ss = DebuggerManager.getDebuggerManager (). 58 getSessions (); 59 if (listener == null) 60 listener = new Listener (this); 61 to = Math.min(ss.length, to); 62 from = Math.min(ss.length, from); 63 Session[] fss = new Session [to - from]; 64 System.arraycopy (ss, from, fss, 0, to - from); 65 return fss; 66 } else 67 throw new UnknownTypeException (parent); 68 } 69 70 79 public int getChildrenCount (Object node) throws UnknownTypeException { 80 if (node == ROOT) { 81 if (listener == null) 82 listener = new Listener (this); 83 return Integer.MAX_VALUE; 85 } else 88 throw new UnknownTypeException (node); 89 } 90 91 public boolean isLeaf (Object node) 92 throws UnknownTypeException { 93 if (node == ROOT) return false; 94 if (node instanceof Session) return true; 95 throw new UnknownTypeException (node); 96 } 97 98 public void addModelListener (ModelListener l) { 99 listeners.add (l); 100 } 101 102 public void removeModelListener (ModelListener l) { 103 listeners.remove (l); 104 } 105 106 public 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 (null); 111 } 112 113 114 116 private static class Listener extends DebuggerManagerAdapter { 117 118 private WeakReference model; 119 120 public Listener ( 121 SessionsTreeModel tm 122 ) { 123 model = new WeakReference (tm); 124 DebuggerManager.getDebuggerManager ().addDebuggerListener ( 125 DebuggerManager.PROP_SESSIONS, 126 this 127 ); 128 } 129 130 private SessionsTreeModel getModel () { 131 SessionsTreeModel m = (SessionsTreeModel) model.get (); 132 if (m == null) { 133 DebuggerManager.getDebuggerManager ().removeDebuggerListener ( 134 DebuggerManager.PROP_SESSIONS, 135 this 136 ); 137 } 138 return m; 139 } 140 141 public void sessionAdded (Session s) { 142 SessionsTreeModel m = getModel (); 143 if (m == null) return; 144 m.fireTreeChanged (); 145 } 146 147 public void sessionRemoved (Session s) { 148 SessionsTreeModel m = getModel (); 149 if (m == null) return; 150 m.fireTreeChanged (); 151 } 152 } 153 } 154 | Popular Tags |