KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > FixedWatchesManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.ui;
21
22 import javax.swing.KeyStroke JavaDoc;
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 /**
34  * Manages lifecycle and presentation of fixed watches. Should be
35  * registered as an action provider in both
36  * locals and watches views and as a tree model filter in the watches view.
37  *
38  * @author Jan Jancura, Maros Sandor
39  */

40 public class FixedWatchesManager implements TreeModelFilter,
41 NodeActionsProviderFilter, NodeModelFilter {
42             
43     public static final String JavaDoc 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 JavaDoc node) {
50                 return true;
51             }
52             public void perform (Object JavaDoc[] 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 JavaDoc node) {
75                 return true;
76             }
77             public void perform (Object JavaDoc[] 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     // TreeModelFilter .........................................................
98

99     public Object JavaDoc getRoot (TreeModel original) {
100         return original.getRoot ();
101     }
102
103     public Object JavaDoc[] getChildren (
104         TreeModel original,
105         Object JavaDoc 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 JavaDoc[] children;
119             if (originalTo > originalFrom) {
120                 children = original.getChildren
121                     (parent, originalFrom, originalTo);
122             } else {
123                 children = new Object JavaDoc [0];
124             }
125             Object JavaDoc [] allChildren = new Object JavaDoc [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 JavaDoc[] fallChildren = new Object JavaDoc [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 JavaDoc 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 JavaDoc 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     // NodeActionsProviderFilter ...............................................
175

176     public void performDefaultAction (
177         NodeActionsProvider original,
178         Object JavaDoc node
179     ) throws UnknownTypeException {
180         original.performDefaultAction (node);
181     }
182
183     public Action[] getActions (NodeActionsProvider original, Object JavaDoc 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     // NodeModel ...............................................................
205

206     public String JavaDoc getDisplayName (NodeModel original, Object JavaDoc node)
207     throws UnknownTypeException {
208         if (fixedWatches.containsKey (node))
209             return (String JavaDoc) fixedWatches.get (node);
210         return original.getDisplayName (node);
211     }
212     
213     public String JavaDoc getShortDescription (NodeModel original, Object JavaDoc node)
214     throws UnknownTypeException {
215         if (fixedWatches.containsKey (node)) {
216             Variable v = (Variable) node;
217             return ((String JavaDoc) fixedWatches.get (node)) +
218                 " = (" + v.getType () + ") " +
219                 v.getValue ();
220         }
221         return original.getShortDescription (node);
222     }
223     
224     public String JavaDoc getIconBase (NodeModel original, Object JavaDoc node)
225     throws UnknownTypeException {
226         if (fixedWatches.containsKey (node))
227             return FIXED_WATCH;
228         return original.getIconBase (node);
229     }
230     
231     
232     // other methods ...........................................................
233

234     private void createFixedWatch (Object JavaDoc 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 JavaDoc 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 JavaDoc name, Variable variable) {
257         // Clone the variable to assure that it's unique and sticks to the JDI value.
258
if (variable instanceof Cloneable JavaDoc) {
259             try { // terrible code to invoke the clone() method
260
java.lang.reflect.Method JavaDoc cloneMethod = variable.getClass().getMethod("clone", new Class JavaDoc[] {});
261                 cloneMethod.setAccessible(true);
262                 Object JavaDoc newVar = cloneMethod.invoke(variable, new Object JavaDoc[] {});
263                 if (newVar instanceof Variable) {
264                     variable = (Variable) newVar;
265                 }
266             } catch (Exception JavaDoc ex) {} // Ignore any exceptions
267
}
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