1 19 20 package org.netbeans.modules.debugger.jpda.ui; 21 22 import javax.swing.KeyStroke ; 23 import org.netbeans.spi.viewmodel.*; 24 import org.netbeans.spi.debugger.ContextProvider; 25 import org.netbeans.api.debugger.jpda.*; 26 import org.netbeans.spi.viewmodel.Models; 27 import org.netbeans.spi.viewmodel.NodeModel; 28 import org.openide.util.NbBundle; 29 30 import javax.swing.*; 31 import java.util.*; 32 33 40 public class FixedWatchesManager implements TreeModelFilter, 41 NodeActionsProviderFilter, NodeModelFilter { 42 43 public static final String FIXED_WATCH = 44 "org/netbeans/modules/debugger/resources/watchesView/FixedWatch"; 45 private final Action DELETE_ACTION = Models.createAction ( 46 NbBundle.getBundle (FixedWatchesManager.class).getString 47 ("CTL_DeleteFixedWatch_Label"), 48 new Models.ActionPerformer () { 49 public boolean isEnabled (Object node) { 50 return true; 51 } 52 public void perform (Object [] nodes) { 53 int i, k = nodes.length; 54 for (i = 0; i < k; i++) 55 fixedWatches.remove (nodes [i]); 56 fireModelChanged(new ModelEvent.NodeChanged( 57 FixedWatchesManager.this, 58 TreeModel.ROOT, 59 ModelEvent.NodeChanged.CHILDREN_MASK)); 60 } 61 }, 62 Models.MULTISELECTION_TYPE_ANY 63 ); 64 { 65 DELETE_ACTION.putValue ( 66 Action.ACCELERATOR_KEY, 67 KeyStroke.getKeyStroke ("DELETE") 68 ); 69 }; 70 private final Action CREATE_FIXED_WATCH_ACTION = Models.createAction ( 71 NbBundle.getBundle (FixedWatchesManager.class).getString 72 ("CTL_CreateFixedWatch_Label"), 73 new Models.ActionPerformer () { 74 public boolean isEnabled (Object node) { 75 return true; 76 } 77 public void perform (Object [] nodes) { 78 int i, k = nodes.length; 79 for (i = 0; i < k; i++) 80 createFixedWatch (nodes [i]); 81 } 82 }, 83 Models.MULTISELECTION_TYPE_ALL 84 ); 85 86 87 private Map fixedWatches = new HashMap (); 88 private HashSet listeners; 89 private ContextProvider contextProvider; 90 91 92 public FixedWatchesManager (ContextProvider contextProvider) { 93 this.contextProvider = contextProvider; 94 } 95 96 97 99 public Object getRoot (TreeModel original) { 100 return original.getRoot (); 101 } 102 103 public Object [] getChildren ( 104 TreeModel original, 105 Object parent, 106 int from, 107 int to 108 ) throws UnknownTypeException { 109 if (parent == TreeModel.ROOT) { 110 if (fixedWatches.size () == 0) 111 return original.getChildren (parent, from, to); 112 113 int fixedSize = fixedWatches.size (); 114 int originalFrom = from - fixedSize; 115 int originalTo = to - fixedSize; 116 if (originalFrom < 0) originalFrom = 0; 117 118 Object [] children; 119 if (originalTo > originalFrom) { 120 children = original.getChildren 121 (parent, originalFrom, originalTo); 122 } else { 123 children = new Object [0]; 124 } 125 Object [] allChildren = new Object [children.length + fixedSize]; 126 127 fixedWatches.keySet ().toArray (allChildren); 128 System.arraycopy ( 129 children, 130 0, 131 allChildren, 132 fixedSize, 133 children.length 134 ); 135 Object [] fallChildren = new Object [to - from]; 136 System.arraycopy (allChildren, from, fallChildren, 0, to - from); 137 return fallChildren; 138 } 139 return original.getChildren (parent, from, to); 140 } 141 142 public int getChildrenCount ( 143 TreeModel original, 144 Object parent 145 ) throws UnknownTypeException { 146 if (parent == TreeModel.ROOT) { 147 int chc = original.getChildrenCount (parent); 148 if (chc < Integer.MAX_VALUE) { 149 chc += fixedWatches.size (); 150 } 151 return chc; 152 } 153 return original.getChildrenCount (parent); 154 } 155 156 public boolean isLeaf (TreeModel original, Object node) 157 throws UnknownTypeException { 158 return original.isLeaf (node); 159 } 160 161 public synchronized void addModelListener (ModelListener l) { 162 if (listeners == null) { 163 listeners = new HashSet(); 164 } 165 listeners.add(l); 166 } 167 168 public synchronized void removeModelListener (ModelListener l) { 169 if (listeners == null) return; 170 listeners.remove (l); 171 } 172 173 174 176 public void performDefaultAction ( 177 NodeActionsProvider original, 178 Object node 179 ) throws UnknownTypeException { 180 original.performDefaultAction (node); 181 } 182 183 public Action[] getActions (NodeActionsProvider original, Object node) 184 throws UnknownTypeException { 185 Action [] actions = original.getActions (node); 186 List myActions = new ArrayList(); 187 if (fixedWatches.containsKey (node)) { 188 return new Action[] { 189 DELETE_ACTION 190 }; 191 } 192 if (node instanceof Variable) { 193 myActions.add (CREATE_FIXED_WATCH_ACTION); 194 } else 195 if (node instanceof JPDAWatch) { 196 myActions.add (CREATE_FIXED_WATCH_ACTION); 197 } else 198 return actions; 199 myActions.addAll (Arrays.asList (actions)); 200 return (Action[]) myActions.toArray (new Action [myActions.size ()]); 201 } 202 203 204 206 public String getDisplayName (NodeModel original, Object node) 207 throws UnknownTypeException { 208 if (fixedWatches.containsKey (node)) 209 return (String ) fixedWatches.get (node); 210 return original.getDisplayName (node); 211 } 212 213 public String getShortDescription (NodeModel original, Object node) 214 throws UnknownTypeException { 215 if (fixedWatches.containsKey (node)) { 216 Variable v = (Variable) node; 217 return ((String ) fixedWatches.get (node)) + 218 " = (" + v.getType () + ") " + 219 v.getValue (); 220 } 221 return original.getShortDescription (node); 222 } 223 224 public String getIconBase (NodeModel original, Object node) 225 throws UnknownTypeException { 226 if (fixedWatches.containsKey (node)) 227 return FIXED_WATCH; 228 return original.getIconBase (node); 229 } 230 231 232 234 private void createFixedWatch (Object node) { 235 if (node instanceof JPDAWatch) { 236 JPDAWatch jw = (JPDAWatch) node; 237 addFixedWatch (jw.getExpression (), jw); 238 } else { 239 Variable variable = (Variable) node; 240 String name = null; 241 if (variable instanceof LocalVariable) { 242 name = ((LocalVariable) variable).getName (); 243 } else if (variable instanceof Field) { 244 name = ((Field) variable).getName(); 245 } else if (variable instanceof This) { 246 name = "this"; 247 } else if (variable instanceof ObjectVariable) { 248 name = "object"; 249 } else { 250 name = "unnamed"; 251 } 252 addFixedWatch (name, variable); 253 } 254 } 255 256 private void addFixedWatch (String name, Variable variable) { 257 if (variable instanceof Cloneable ) { 259 try { java.lang.reflect.Method cloneMethod = variable.getClass().getMethod("clone", new Class [] {}); 261 cloneMethod.setAccessible(true); 262 Object newVar = cloneMethod.invoke(variable, new Object [] {}); 263 if (newVar instanceof Variable) { 264 variable = (Variable) newVar; 265 } 266 } catch (Exception ex) {} } 268 fixedWatches.put (variable, name); 269 fireModelChanged (new ModelEvent.NodeChanged( 270 this, 271 TreeModel.ROOT, 272 ModelEvent.NodeChanged.CHILDREN_MASK)); 273 } 274 275 private void fireModelChanged (ModelEvent event) { 276 HashSet listenersCopy; 277 synchronized (this) { 278 if (listeners == null) return; 279 listenersCopy = new HashSet(listeners); 280 } 281 for (Iterator i = listenersCopy.iterator (); i.hasNext ();) { 282 ModelListener listener = (ModelListener) i.next(); 283 listener.modelChanged(event); 284 } 285 } 286 287 } 288 | Popular Tags |