001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.importd;
043:
044: import java.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeSupport;
046:
047: import org.openide.TopManager;
048: import org.openide.debugger.Debugger;
049: import org.openide.debugger.DebuggerException;
050: import org.openide.text.Line;
051:
052: import org.netbeans.modules.debugger.*;
053:
054: /**
055: * Standart implementation of Watch interface.
056: * @see org.openide.debugger.Watch
057: *
058: * @author Jan Jancura
059: * @version 0.18, Feb 23, 1998
060: */
061: public class ImportWatch extends AbstractWatch implements
062: Validator.Object {
063: /** generated Serialized Version UID */
064: static final long serialVersionUID = 3431277044447811206L;
065:
066: // private variables .....................................................
067:
068: private String errorMessage = null;
069: private String name = "";
070: private String type;
071: private String value;
072: private boolean isHidden;
073: private boolean valid = false;
074: private ImportDebugger debugger;
075: private transient PropertyChangeSupport pcs;
076:
077: // init .....................................................................
078:
079: /**
080: * Non public constructor called from the JavaDebugger only.
081: * User must create watch from Debugger.getNewWatch () method.
082: */
083: ImportWatch(ImportDebugger debugger, boolean hidden) {
084: this .debugger = debugger;
085: this .isHidden = hidden;
086: init();
087: }
088:
089: protected void init() {
090: pcs = new PropertyChangeSupport(this );
091: debugger.getValidator().add(this );
092: }
093:
094: private void readObject(java.io.ObjectInputStream in)
095: throws java.io.IOException, ClassNotFoundException {
096: in.defaultReadObject();
097: init();
098: }
099:
100: // Watch implementation ....................................................
101:
102: /** Remove the watch from the list of all watches in the system.
103: */
104: public void remove() {
105: debugger.removeWatch(this );
106: }
107:
108: /** Get the name of the variable to watch.
109: *
110: * @return the variable name
111: */
112: public String getVariableName() {
113: return name;
114: }
115:
116: /** Set the variable name to watch.
117: *
118: * @param name string name of the variable to watch
119: */
120: public void setVariableName(String name) {
121: this .name = name;
122: valid = false;
123: }
124:
125: /** Get a textual representation of the value.
126: * The watch should convert
127: * the real value to a string representation. So if the watch represents
128: * a <code>null</code> reference, the returned string will be for example <code>"null"</code>.
129: *
130: * @return the value of this watch, or <code>null</code> if the watch is not in scope
131: */
132: public String getAsText() {
133: if (!valid)
134: refresh();
135: return value;
136: }
137:
138: /** Set the value of the watched variable (as text).
139: *
140: * @param value text representation of the new value
141: * @exception DebuggerException if the value cannot be changed, or the
142: * string does not represent valid value, or the value type cannot reasonably be set as text
143: */
144: public void setAsText(String value) throws DebuggerException {
145: valid = false;
146: }
147:
148: /** Get the string representation of the type of the variable.
149: *
150: * @return type string (i.e. the class name, or for a primitive e.g. <code>"int"</code>)
151: */
152: public String getType() {
153: if (!valid)
154: refresh();
155: return type;
156: }
157:
158: /** Test whether the watch is hidden.
159: * If so, it
160: * is not presented in the list of all watches. Such a watch can be used
161: * for the IDE's (or some module's) private use, not displaying anything to the user.
162: * @return <code>true</code> if the watch is hidden
163: * @see Debugger#createWatch(String, boolean)
164: */
165: public boolean isHidden() {
166: return isHidden;
167: }
168:
169: /**
170: * Add a property change listener.
171: * Change events should be fired for the properties {@link #PROP_VARIABLE_NAME}, {@link #PROP_AS_TEXT}, and {@link #PROP_TYPE}.
172: *
173: * @param l the listener to add
174: */
175: public void addPropertyChangeListener(PropertyChangeListener l) {
176: pcs.addPropertyChangeListener(l);
177: }
178:
179: /**
180: * Remove a property change listener.
181: *
182: * @param l the listener to remove
183: */
184: public void removePropertyChangeListener(PropertyChangeListener l) {
185: pcs.removePropertyChangeListener(l);
186: }
187:
188: // AbstractWatch implementation ............................................
189:
190: public String getErrorMessage() {
191: if (!valid)
192: refresh();
193: return errorMessage;
194: }
195:
196: /**
197: * Checks value of this watch and if is valide, and sets it.
198: */
199: public void validate() {
200: valid = false;
201: firePropertyChange();
202: }
203:
204: private void refresh() {
205: type = "";
206: value = "";
207: if ((debugger.getState() == debugger.DEBUGGER_NOT_RUNNING)
208: || (debugger.getState() == debugger.DEBUGGER_STARTING)) {
209: errorMessage = ImportDebugger
210: .getLocString("EXC_No_session");
211: firePropertyChange();
212: return;
213: }
214: if (debugger.getState() != debugger.DEBUGGER_STOPPED) {
215: errorMessage = ImportDebugger
216: .getLocString("CTL_No_context");
217: firePropertyChange();
218: return;
219: }
220: try {
221: Line l = debugger.getCurrentLine();
222: if (l == null) {
223: errorMessage = ImportDebugger
224: .getLocString("CTL_No_context");
225: firePropertyChange();
226: return;
227: }
228: errorMessage = null;
229: String s = ImportDebugger.getText(l);
230: int i = s.indexOf(getVariableName());
231: if (i >= 0) {
232: value = s.substring(i);
233: type = s.substring(0, i);
234: } else
235: errorMessage = ImportDebugger
236: .getLocString("Unknown_variable");
237: } catch (Exception e) {
238: value = "";
239: errorMessage = e.toString();
240: }
241: }
242:
243: protected void firePropertyChange() {
244: pcs.firePropertyChange(null, null, null);
245: }
246:
247: /**
248: * If return <code>null</code>, this object can be removed from
249: * validator.
250: *
251: * @return <code>null</code>, if this object can be removed from
252: * validator
253: */
254: public boolean canRemove() {
255: return false;
256: }
257:
258: }
|