KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > jpda > FieldBreakpoint


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.api.debugger.jpda;
21
22 import javax.swing.event.ChangeEvent JavaDoc;
23 import javax.swing.event.ChangeListener JavaDoc;
24 import org.netbeans.api.debugger.Breakpoint;
25
26 /**
27  * Notifies about variable change or access events.
28  *
29  * <br><br>
30  * <b>How to use it:</b>
31  * <pre style="background-color: rgb(255, 255, 153);">
32  * DebuggerManager.addBreakpoint (FieldBreakpoint.create (
33  * "org.netbeans.modules.editor.EditorPanel",
34  * "state",
35  * FieldBreakpoint.TYPE_MODIFICATION
36  * ));</pre>
37  * This breakpoint stops when state field of EditorPanel class is modified.
38  *
39  * @author Jan Jancura
40  */

41 public class FieldBreakpoint extends JPDABreakpoint {
42
43     /** Property name constant. */
44     public static final String JavaDoc PROP_FIELD_NAME = "fieldName"; // NOI18N
45
/** Property name constant. */
46     public static final String JavaDoc PROP_CLASS_NAME = "className"; // NOI18N
47
/** Property name constant. */
48     public static final String JavaDoc PROP_CONDITION = "condition"; // NOI18N
49
/** Property name constant. */
50     public static final String JavaDoc PROP_BREAKPOINT_TYPE = "breakpointType"; // NOI18N
51

52     /** Property type value constant. */
53     public static final int TYPE_ACCESS = 1;
54     /** Property type value constant. */
55     public static final int TYPE_MODIFICATION = 2;
56
57     private String JavaDoc className = "";
58     private String JavaDoc fieldName = "";
59     private int type = TYPE_MODIFICATION;
60     private String JavaDoc condition = ""; // NOI18N
61

62     
63     private FieldBreakpoint () {
64     }
65     
66     /**
67      * Creates a new breakpoint for given parameters.
68      *
69      * @param className class name
70      * @param fieldName name of field
71      * @param breakpointType one of constants: TYPE_ACCESS,
72      * TYPE_MODIFICATION
73      * @return a new breakpoint for given parameters
74      */

75     public static FieldBreakpoint create (
76         String JavaDoc className,
77         String JavaDoc fieldName,
78         int breakpointType
79     ) {
80         FieldBreakpoint b = new FieldBreakpointImpl ();
81         b.setClassName (className);
82         b.setFieldName (fieldName);
83         b.setBreakpointType (breakpointType);
84         return b;
85     }
86
87     /**
88      * Get name of class the field is defined in.
89      *
90      * @return the name of class the field is defined in
91      */

92     public String JavaDoc getClassName () {
93         return className;
94     }
95
96     /**
97      * Set name of class the field is defined in.
98      *
99      * @param className a new name of class the field is defined in
100      */

101     public void setClassName (String JavaDoc className) {
102         if ( (className == this.className) ||
103              ( (className != null) &&
104                (this.className != null) &&
105                this.className.equals (className)
106              )
107         ) return;
108         Object JavaDoc old = this.className;
109         this.className = className;
110         firePropertyChange (PROP_CLASS_NAME, old, className);
111     }
112
113     /**
114      * Returns name of field.
115      *
116      * @return a name of field
117      */

118     public String JavaDoc getFieldName () {
119         return fieldName;
120     }
121
122     /**
123      * Sets name of field.
124      *
125      * @param name a name of field
126      */

127     public void setFieldName (String JavaDoc name) {
128         if (name != null) {
129             name = name.trim();
130         }
131         if ( (name == fieldName) ||
132              ((name != null) && (fieldName != null) && fieldName.equals (name))
133         ) return;
134         String JavaDoc old = fieldName;
135         fieldName = name;
136         firePropertyChange (PROP_FIELD_NAME, old, fieldName);
137     }
138
139     /**
140      * Returns type of breakpoint (one of TYPE_ACCESS and TYPE_MODIFICATION).
141      *
142      * @return type of breakpoint
143      */

144     public int getBreakpointType () {
145         return type;
146     }
147
148     /**
149      * Sets type of breakpoint.
150      *
151      * @param type a new type of breakpoint
152      */

153     public void setBreakpointType (int type) {
154         if (this.type == type) return;
155         if ( (type != TYPE_MODIFICATION) &&
156                 (type != TYPE_ACCESS)
157            ) throw new IllegalArgumentException JavaDoc ();
158         int old = this.type;
159         this.type = type;
160         firePropertyChange (PROP_BREAKPOINT_TYPE, new Integer JavaDoc (old), new Integer JavaDoc (type));
161     }
162     
163     /**
164      * Returns condition.
165      *
166      * @return cond a condition
167      */

168     public String JavaDoc getCondition () {
169         return condition;
170     }
171
172     /**
173      * Sets condition.
174      *
175      * @param cond a c new condition
176      */

177     public void setCondition (String JavaDoc cond) {
178         if (cond != null) {
179             cond = cond.trim();
180         }
181         String JavaDoc old = condition;
182         condition = cond;
183         firePropertyChange (PROP_CONDITION, old, cond);
184     }
185
186     /**
187      * Returns a string representation of this object.
188      *
189      * @return a string representation of the object
190      */

191     public String JavaDoc toString () {
192         return "FieldBreakpoint " + className + "." + fieldName;
193     }
194     
195     private static final class FieldBreakpointImpl extends FieldBreakpoint implements ChangeListener JavaDoc {
196         
197         public void stateChanged(ChangeEvent JavaDoc chev) {
198             Object JavaDoc source = chev.getSource();
199             if (source instanceof Breakpoint.VALIDITY) {
200                 setValidity((Breakpoint.VALIDITY) source, chev.toString());
201             } else {
202                 throw new UnsupportedOperationException JavaDoc(chev.toString());
203             }
204         }
205     }
206 }
207
Popular Tags