KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > CallStackModel


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.ant.debugger;
21
22 import java.util.Vector JavaDoc;
23 import javax.swing.Action JavaDoc;
24
25 import org.apache.tools.ant.module.api.support.TargetLister;
26 import org.netbeans.spi.debugger.ContextProvider;
27 import org.netbeans.spi.viewmodel.ModelEvent;
28 import org.netbeans.spi.viewmodel.NodeActionsProvider;
29 import org.netbeans.spi.viewmodel.NodeModel;
30 import org.netbeans.spi.viewmodel.TableModel;
31 import org.netbeans.spi.viewmodel.TreeModel;
32 import org.netbeans.spi.viewmodel.ModelListener;
33 import org.netbeans.spi.viewmodel.UnknownTypeException;
34 import org.netbeans.spi.debugger.ui.Constants;
35 import org.openide.text.Annotatable;
36
37 import org.openide.text.Line;
38
39 /**
40  *
41  * @author Jan Jancura
42  */

43 public class CallStackModel implements TreeModel, NodeModel,
44 NodeActionsProvider, TableModel {
45     
46     public static final String JavaDoc CALL_STACK =
47         "org/netbeans/modules/debugger/resources/callStackView/NonCurrentFrame";
48     public static final String JavaDoc CURRENT_CALL_STACK =
49         "org/netbeans/modules/debugger/resources/callStackView/CurrentFrame";
50     
51     private AntDebugger debugger;
52     private Vector JavaDoc listeners = new Vector JavaDoc ();
53     
54     
55     public CallStackModel (ContextProvider contextProvider) {
56         debugger = (AntDebugger) contextProvider.lookupFirst
57             (null, AntDebugger.class);
58     }
59     
60     
61     // TreeModel implementation ................................................
62

63     /**
64      * Returns the root node of the tree or null, if the tree is empty.
65      *
66      * @return the root node of the tree or null
67      */

68     public Object JavaDoc getRoot () {
69         return ROOT;
70     }
71     
72     /**
73      * Returns children for given parent on given indexes.
74      *
75      * @param parent a parent of returned nodes
76      * @param from a start index
77      * @param to a end index
78      *
79      * @throws NoInformationException if the set of children can not be
80      * resolved
81      * @throws ComputingException if the children resolving process
82      * is time consuming, and will be performed off-line
83      * @throws UnknownTypeException if this TreeModel implementation is not
84      * able to resolve children for given node type
85      *
86      * @return children for given parent on given indexes
87      */

88     public Object JavaDoc[] getChildren (Object JavaDoc parent, int from, int to)
89         throws UnknownTypeException {
90         if (parent == ROOT)
91             return debugger.getCallStack ();
92         throw new UnknownTypeException (parent);
93     }
94     
95     /**
96      * Returns true if node is leaf.
97      *
98      * @throws UnknownTypeException if this TreeModel implementation is not
99      * able to resolve dchildren for given node type
100      * @return true if node is leaf
101      */

102     public boolean isLeaf (Object JavaDoc node) throws UnknownTypeException {
103         if (node == ROOT)
104             return false;
105         if (node instanceof TargetLister.Target)
106             return true;
107         if (node instanceof Task)
108             return true;
109         throw new UnknownTypeException (node);
110     }
111     
112     /**
113      * Returns number of children for given node.
114      *
115      * @param node the parent node
116      * @throws NoInformationException if the set of children can not be
117      * resolved
118      * @throws ComputingException if the children resolving process
119      * is time consuming, and will be performed off-line
120      * @throws UnknownTypeException if this TreeModel implementation is not
121      * able to resolve children for given node type
122      *
123      * @return true if node is leaf
124      * @since 1.1
125      */

126     public int getChildrenCount (Object JavaDoc node) throws UnknownTypeException {
127         if (node == ROOT)
128             return debugger.getCallStack ().length;
129         throw new UnknownTypeException (node);
130     }
131
132     /**
133      * Registers given listener.
134      *
135      * @param l the listener to add
136      */

137     public void addModelListener (ModelListener l) {
138         listeners.add (l);
139     }
140
141     /**
142      * Unregisters given listener.
143      *
144      * @param l the listener to remove
145      */

146     public void removeModelListener (ModelListener l) {
147         listeners.remove (l);
148     }
149     
150     
151     // NodeModel implementation ................................................
152

153     /**
154      * Returns display name for given node.
155      *
156      * @throws ComputingException if the display name resolving process
157      * is time consuming, and the value will be updated later
158      * @throws UnknownTypeException if this NodeModel implementation is not
159      * able to resolve display name for given node type
160      * @return display name for given node
161      */

162     public String JavaDoc getDisplayName (Object JavaDoc node) throws UnknownTypeException {
163         if (node instanceof TargetLister.Target)
164             return ((TargetLister.Target) node).getName ();
165         if (node instanceof Task)
166             return ((Task) node).getTaskStructure ().getName ();
167         if (node == ROOT) {
168             return ROOT;
169         }
170         throw new UnknownTypeException (node);
171     }
172     
173     /**
174      * Returns icon for given node.
175      *
176      * @throws ComputingException if the icon resolving process
177      * is time consuming, and the value will be updated later
178      * @throws UnknownTypeException if this NodeModel implementation is not
179      * able to resolve icon for given node type
180      * @return icon for given node
181      */

182     public String JavaDoc getIconBase (Object JavaDoc node) throws UnknownTypeException {
183         if (node instanceof TargetLister.Target)
184             return CALL_STACK;
185         if (node instanceof Task)
186             return CURRENT_CALL_STACK;
187         if (node == ROOT) {
188             return null;
189         }
190         throw new UnknownTypeException (node);
191     }
192     
193     /**
194      * Returns tooltip for given node.
195      *
196      * @throws ComputingException if the tooltip resolving process
197      * is time consuming, and the value will be updated later
198      * @throws UnknownTypeException if this NodeModel implementation is not
199      * able to resolve tooltip for given node type
200      * @return tooltip for given node
201      */

202     public String JavaDoc getShortDescription (Object JavaDoc node)
203     throws UnknownTypeException {
204         if (node instanceof TargetLister.Target)
205             return null;
206         if (node instanceof Task)
207             return null;
208         throw new UnknownTypeException (node);
209     }
210         
211      
212     // NodeActionsProvider implementation ......................................
213

214     /**
215      * Performs default action for given node.
216      *
217      * @throws UnknownTypeException if this NodeActionsProvider implementation
218      * is not able to resolve actions for given node type
219      * @return display name for given node
220      */

221     public void performDefaultAction (Object JavaDoc node)
222     throws UnknownTypeException {
223         if (node instanceof TargetLister.Target) {
224             Utils.showLine (Utils.getLine ((TargetLister.Target) node, null));
225             return;
226         }
227         if (node instanceof Task) {
228             Utils.showLine (((Task) node).getLine ());
229             return;
230         }
231         throw new UnknownTypeException (node);
232     }
233     
234     /**
235      * Returns set of actions for given node.
236      *
237      * @throws UnknownTypeException if this NodeActionsProvider implementation
238      * is not able to resolve actions for given node type
239      * @return display name for given node
240      */

241     public Action JavaDoc[] getActions (Object JavaDoc node)
242     throws UnknownTypeException {
243         return new Action JavaDoc [] {};
244     }
245         
246      
247     // TableModel implementation ...............................................
248

249     /**
250      * Returns value to be displayed in column <code>columnID</code>
251      * and row identified by <code>node</code>. Column ID is defined in by
252      * {@link ColumnModel#getID}, and rows are defined by values returned from
253      * {@link org.netbeans.spi.viewmodel.TreeModel#getChildren}.
254      *
255      * @param node a object returned from
256      * {@link org.netbeans.spi.viewmodel.TreeModel#getChildren} for this row
257      * @param columnID a id of column defined by {@link ColumnModel#getID}
258      * @throws ComputingException if the value is not known yet and will
259      * be computed later
260      * @throws UnknownTypeException if there is no TableModel defined for given
261      * parameter type
262      *
263      * @return value of variable representing given position in tree table.
264      */

265     public Object JavaDoc getValueAt (Object JavaDoc node, String JavaDoc columnID) throws
266     UnknownTypeException {
267         if (columnID == Constants.CALL_STACK_FRAME_LOCATION_COLUMN_ID) {
268             if (node instanceof TargetLister.Target) {
269                 TargetLister.Target target = (TargetLister.Target) node;
270                 return target.getScript ().getFile ().getName () + ":" +
271                     Utils.getLineNumber (
272                         Utils.getLine ((TargetLister.Target) node, null)
273                     );
274             }
275             if (node instanceof Task) {
276                 Task task = (Task) node;
277                 return task.getFile ().getName () + ":" +
278                     Utils.getLineNumber (task.getLine ()) + 1;
279             }
280         }
281         throw new UnknownTypeException (node);
282     }
283     
284     /**
285      * Returns true if value displayed in column <code>columnID</code>
286      * and row <code>node</code> is read only. Column ID is defined in by
287      * {@link ColumnModel#getID}, and rows are defined by values returned from
288      * {@link TreeModel#getChildren}.
289      *
290      * @param node a object returned from {@link TreeModel#getChildren} for this row
291      * @param columnID a id of column defined by {@link ColumnModel#getID}
292      * @throws UnknownTypeException if there is no TableModel defined for given
293      * parameter type
294      *
295      * @return true if variable on given position is read only
296      */

297     public boolean isReadOnly (Object JavaDoc node, String JavaDoc columnID) throws
298     UnknownTypeException {
299         if (columnID == Constants.CALL_STACK_FRAME_LOCATION_COLUMN_ID) {
300             if (node instanceof TargetLister.Target) {
301                 return true;
302             }
303             if (node instanceof Task) {
304                 return true;
305             }
306         }
307         throw new UnknownTypeException (node);
308     }
309     
310     /**
311      * Changes a value displayed in column <code>columnID</code>
312      * and row <code>node</code>. Column ID is defined in by
313      * {@link ColumnModel#getID}, and rows are defined by values returned from
314      * {@link TreeModel#getChildren}.
315      *
316      * @param node a object returned from {@link TreeModel#getChildren} for this row
317      * @param columnID a id of column defined by {@link ColumnModel#getID}
318      * @param value a new value of variable on given position
319      * @throws UnknownTypeException if there is no TableModel defined for given
320      * parameter type
321      */

322     public void setValueAt (Object JavaDoc node, String JavaDoc columnID, Object JavaDoc value)
323     throws UnknownTypeException {
324         throw new UnknownTypeException (node);
325     }
326
327     
328     // other mothods ...........................................................
329

330     void fireChanges () {
331         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
332         int i, k = v.size ();
333         for (i = 0; i < k; i++)
334             ((ModelListener) v.get (i)).modelChanged (
335                 new ModelEvent.TreeChanged (this)
336             );
337     }
338 }
339
Popular Tags