KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > models > VariablesModel


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.dbginterface.models;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import org.netbeans.modules.scripting.php.dbginterface.ModelSupport;
25 import org.netbeans.modules.scripting.php.dbginterface.api.VariableNode;
26 import org.netbeans.spi.debugger.ContextProvider;
27 import org.netbeans.spi.debugger.ui.Constants;
28 import org.netbeans.spi.viewmodel.ModelEvent;
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.UnknownTypeException;
33
34 public class VariablesModel extends ModelSupport
35         implements TreeModel, TableModel, NodeModel {
36
37     private final ContextProvider contextProvider;
38
39     private DebugFrame currentFrame; // Stack frame used to generate current model
40
private VariableNode theModel;
41
42     public VariablesModel(final ContextProvider contextProvider) {
43         this.contextProvider = contextProvider;
44         this.theModel = null;
45     }
46
47     public DebugFrame getCurrentFrame() {
48         return currentFrame;
49     }
50
51     // ------------------------------------------------------------------------
52
// Model management
53
// ------------------------------------------------------------------------
54
public void setStackFrame(DebugFrame newFrame) {
55         if (currentFrame != newFrame) {
56             currentFrame = newFrame;
57         }
58
59         // if model is undefined
60
if (theModel == null) {
61             theModel = currentFrame.getScopeVariables();
62             fireTreeChanged();
63         }
64         else {
65             Collection JavaDoc<ModelEvent> events = new ArrayList JavaDoc<ModelEvent>();
66             
67             theModel.collectUpdates(this, events, currentFrame.getScopeVariables());
68             fireTableUpdate(events);
69         }
70     }
71
72     public void clearModel() {
73         theModel = null;
74         fireTreeChanged();
75     }
76
77
78     // ------------------------------------------------------------------------
79
// TreeModel implementation
80
// ------------------------------------------------------------------------
81
public Object JavaDoc getRoot() {
82         return ROOT; // ROOT is defined by TreeModel
83
}
84
85     public Object JavaDoc[] getChildren(Object JavaDoc parent, int from, int to) throws UnknownTypeException {
86         // Should be only two cases -- ROOT or a node from our tree
87
if (parent == ROOT) {
88             if (theModel == null) {
89                 return new Object JavaDoc[0];
90             }
91             
92             return theModel.getChildren(0, theModel.getChildrenCount());
93         }
94         else if (parent instanceof VariableNode) {
95             return ((VariableNode) parent).getChildren(from, to);
96         }
97
98         throw new UnknownTypeException(parent + " " + parent.getClass().getName());
99     }
100
101     public boolean isLeaf(Object JavaDoc node) throws UnknownTypeException {
102         if (node == null) {
103             return true;
104         }
105         else if (node == ROOT) {
106             return theModel == null;
107         }
108         else if (node instanceof VariableNode) {
109             return ((VariableNode)node).isLeaf();
110         }
111
112         throw new UnknownTypeException(node);
113     }
114
115     public int getChildrenCount(Object JavaDoc node) throws UnknownTypeException {
116         if (node == ROOT) {
117             if (theModel == null) {
118                 return 0;
119             }
120             
121             return theModel.getChildrenCount();
122         }
123         else if (node instanceof VariableNode) {
124             return ((VariableNode)node).getChildrenCount();
125         }
126         
127         throw new UnknownTypeException(node);
128     }
129
130     // ------------------------------------------------------------------------
131
// TableModel implementation
132
// ------------------------------------------------------------------------
133
public Object JavaDoc getValueAt(Object JavaDoc node, String JavaDoc columnID) throws UnknownTypeException {
134         String JavaDoc result = ""; // default is blank
135

136         if (Constants.LOCALS_TYPE_COLUMN_ID.equals(columnID)) {
137             if (node instanceof VariableNode) {
138                 String JavaDoc className = ((VariableNode) node).getTypeName();
139                 result = (className == null) ? "" : className;
140             }
141             else {
142                 result = (node != null) ? node.getClass().getName() : "";
143             }
144         }
145         else if (Constants.LOCALS_VALUE_COLUMN_ID.equals(columnID)) {
146             if (node instanceof VariableNode) {
147                 VariableNode vn = (VariableNode) node;
148                 
149                 if (vn.isLeaf()) {
150                     return vn.getTooltipValue();
151                 }
152                 else {
153                     return "";
154                 }
155             }
156             else if (node == null) {
157                 result = "";
158             }
159         }
160
161         return result;
162     }
163
164     public boolean isReadOnly(Object JavaDoc node, String JavaDoc string) throws UnknownTypeException {
165         if (node instanceof VariableNode && Constants.LOCALS_VALUE_COLUMN_ID.equals(string)) {
166             return ((VariableNode)node).isReadOnly();
167         }
168         
169         return true;
170     }
171
172     public void setValueAt(Object JavaDoc node, String JavaDoc string, Object JavaDoc value) throws UnknownTypeException {
173         if (!Constants.LOCALS_VALUE_COLUMN_ID.equals(string)) {
174             throw new UnknownTypeException(node);
175         }
176         
177         if (!(node instanceof VariableNode)) {
178             throw new UnknownTypeException(node);
179         }
180         
181         VariableNode v = (VariableNode)node;
182         
183         if (v.isReadOnly()) {
184             throw new UnknownTypeException(node);
185         }
186         
187         currentFrame.setVariableValue(v, value);
188     }
189
190     // ------------------------------------------------------------------------
191
// NodeModel implementation
192
// ------------------------------------------------------------------------
193
public String JavaDoc getDisplayName(Object JavaDoc node) throws UnknownTypeException {
194         if(node == null) {
195             return "null";
196         } else if(node == ROOT) {
197             return ROOT;
198         } else if(node instanceof VariableNode) {
199             return ((VariableNode) node).getDisplayName();
200         }
201         throw new UnknownTypeException(node);
202     }
203
204     public String JavaDoc getIconBase(Object JavaDoc node) throws UnknownTypeException {
205         if(node == null || node == ROOT) {
206             return VariableNode.LOCAL_VARIABLE_ICON;
207         } else if(node instanceof VariableNode) {
208             return ((VariableNode) node).getIconBase();
209         }
210         throw new UnknownTypeException(node);
211     }
212
213     public String JavaDoc getShortDescription(Object JavaDoc node) throws UnknownTypeException {
214         if(node == null || node == ROOT) {
215             return null;
216         } else if(node instanceof VariableNode) {
217             return ((VariableNode) node).getShortDescription();
218         }
219         throw new UnknownTypeException(node);
220     }
221
222
223     // ------------------------------------------------------------------------
224
// Event support
225
// ------------------------------------------------------------------------
226

227     public void fireTreeChanged() {
228         refresh(true);
229     }
230
231     private void fireTableUpdate(Collection JavaDoc<ModelEvent> events) {
232         fireChangeEvents(events);
233     }
234 }
235
Popular Tags