KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > importd > ImportWatch


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.importd;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24
25 import org.openide.TopManager;
26 import org.openide.debugger.Debugger;
27 import org.openide.debugger.DebuggerException;
28 import org.openide.text.Line;
29
30 import org.netbeans.modules.debugger.*;
31
32
33 /**
34 * Standart implementation of Watch interface.
35 * @see org.openide.debugger.Watch
36 *
37 * @author Jan Jancura
38 * @version 0.18, Feb 23, 1998
39 */

40 public class ImportWatch extends AbstractWatch implements Validator.Object {
41     /** generated Serialized Version UID */
42     static final long serialVersionUID = 3431277044447811206L;
43
44
45     // private variables .....................................................
46

47     private String JavaDoc errorMessage = null;
48     private String JavaDoc name = "";
49     private String JavaDoc type;
50     private String JavaDoc value;
51     private boolean isHidden;
52     private boolean valid = false;
53     private ImportDebugger debugger;
54     private transient PropertyChangeSupport JavaDoc pcs;
55     
56     
57     // init .....................................................................
58

59     /**
60     * Non public constructor called from the JavaDebugger only.
61     * User must create watch from Debugger.getNewWatch () method.
62     */

63     ImportWatch (ImportDebugger debugger, boolean hidden) {
64         this.debugger = debugger;
65         this.isHidden = hidden;
66         init ();
67     }
68
69     protected void init () {
70         pcs = new PropertyChangeSupport JavaDoc (this);
71         debugger.getValidator ().add (this);
72     }
73
74     private void readObject(java.io.ObjectInputStream JavaDoc in)
75     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
76         in.defaultReadObject ();
77         init ();
78     }
79     
80
81     // Watch implementation ....................................................
82

83     
84     /** Remove the watch from the list of all watches in the system.
85     */

86     public void remove () {
87         debugger.removeWatch (this);
88     }
89
90     /** Get the name of the variable to watch.
91     *
92     * @return the variable name
93     */

94     public String JavaDoc getVariableName () {
95         return name;
96     }
97
98     /** Set the variable name to watch.
99     *
100     * @param name string name of the variable to watch
101     */

102     public void setVariableName (String JavaDoc name) {
103         this.name = name;
104         valid = false;
105     }
106
107     /** Get a textual representation of the value.
108     * The watch should convert
109     * the real value to a string representation. So if the watch represents
110     * a <code>null</code> reference, the returned string will be for example <code>"null"</code>.
111     *
112     * @return the value of this watch, or <code>null</code> if the watch is not in scope
113     */

114     public String JavaDoc getAsText () {
115         if (!valid) refresh ();
116         return value;
117     }
118
119     /** Set the value of the watched variable (as text).
120     *
121     * @param value text representation of the new value
122     * @exception DebuggerException if the value cannot be changed, or the
123     * string does not represent valid value, or the value type cannot reasonably be set as text
124     */

125     public void setAsText (String JavaDoc value) throws DebuggerException {
126         valid = false;
127     }
128
129     /** Get the string representation of the type of the variable.
130     *
131     * @return type string (i.e. the class name, or for a primitive e.g. <code>"int"</code>)
132     */

133     public String JavaDoc getType () {
134         if (!valid) refresh ();
135         return type;
136     }
137
138     /** Test whether the watch is hidden.
139     * If so, it
140     * is not presented in the list of all watches. Such a watch can be used
141     * for the IDE's (or some module's) private use, not displaying anything to the user.
142     * @return <code>true</code> if the watch is hidden
143     * @see Debugger#createWatch(String, boolean)
144     */

145     public boolean isHidden () {
146         return isHidden;
147     }
148
149     /**
150     * Add a property change listener.
151     * Change events should be fired for the properties {@link #PROP_VARIABLE_NAME}, {@link #PROP_AS_TEXT}, and {@link #PROP_TYPE}.
152     *
153     * @param l the listener to add
154     */

155     public void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
156         pcs.addPropertyChangeListener (l);
157     }
158
159     /**
160     * Remove a property change listener.
161     *
162     * @param l the listener to remove
163     */

164     public void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
165         pcs.removePropertyChangeListener (l);
166     }
167     
168     
169     // AbstractWatch implementation ............................................
170

171     public String JavaDoc getErrorMessage () {
172         if (!valid) refresh ();
173         return errorMessage;
174     }
175     
176     /**
177     * Checks value of this watch and if is valide, and sets it.
178     */

179     public void validate () {
180         valid = false;
181         firePropertyChange ();
182     }
183     
184     private void refresh () {
185         type = "";
186         value = "";
187         if ((debugger.getState () == debugger.DEBUGGER_NOT_RUNNING) ||
188             (debugger.getState () == debugger.DEBUGGER_STARTING)
189            ) {
190             errorMessage = ImportDebugger.getLocString ("EXC_No_session");
191             firePropertyChange ();
192             return;
193         }
194         if (debugger.getState () != debugger.DEBUGGER_STOPPED) {
195             errorMessage = ImportDebugger.getLocString ("CTL_No_context");
196             firePropertyChange ();
197             return;
198         }
199         try {
200             Line l = debugger.getCurrentLine ();
201             if (l == null) {
202                 errorMessage = ImportDebugger.getLocString ("CTL_No_context");
203                 firePropertyChange ();
204                 return;
205             }
206             errorMessage = null;
207             String JavaDoc s = ImportDebugger.getText (l);
208             int i = s.indexOf (getVariableName ());
209             if (i >= 0) {
210                 value = s.substring (i);
211                 type = s.substring (0, i);
212             } else
213                 errorMessage = ImportDebugger.getLocString ("Unknown_variable");
214         } catch (Exception JavaDoc e) {
215             value = "";
216             errorMessage = e.toString ();
217         }
218     }
219
220     protected void firePropertyChange () {
221         pcs.firePropertyChange (null, null, null);
222     }
223     
224     /**
225      * If return <code>null</code>, this object can be removed from
226      * validator.
227      *
228      * @return <code>null</code>, if this object can be removed from
229      * validator
230      */

231     public boolean canRemove () {
232         return false;
233     }
234     
235 }
236
237
Popular Tags