KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.api.debugger.Breakpoint;
27 import org.netbeans.api.debugger.DebuggerManager;
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileAttributeEvent;
30 import org.openide.filesystems.FileChangeListener;
31 import org.openide.filesystems.FileEvent;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileRenameEvent;
34 import org.openide.filesystems.FileStateInvalidException;
35 import org.openide.filesystems.URLMapper;
36 import org.openide.util.WeakListeners;
37
38
39 /**
40  * Notifies about line breakpoint events.
41  *
42  * <br><br>
43  * <b>How to use it:</b>
44  * <pre style="background-color: rgb(255, 255, 153);">
45  * DebuggerManager.addBreakpoint (LineBreakpoint.create (
46  * "examples.texteditor.Ted",
47  * 27
48  * ));</pre>
49  * This breakpoint stops in Ted class on 27 line number.
50  *
51  * @author Jan Jancura
52  */

53 public class LineBreakpoint extends JPDABreakpoint {
54
55     /** Property name constant */
56     public static final String JavaDoc PROP_LINE_NUMBER = "lineNumber"; // NOI18N
57
/** Property name constant */
58     public static final String JavaDoc PROP_URL = "url"; // NOI18N
59
/** Property name constant. */
60     public static final String JavaDoc PROP_CONDITION = "condition"; // NOI18N
61
/** Property name constant. */
62     public static final String JavaDoc PROP_SOURCE_NAME = "sourceName"; // NOI18N
63
/** Property name constant. */
64     public static final String JavaDoc PROP_SOURCE_PATH = "sourcePath"; // NOI18N
65
/** Property name constant. */
66     public static final String JavaDoc PROP_STRATUM = "stratum"; // NOI18N
67
/** Property name constant. */
68     public static final String JavaDoc PROP_PREFERRED_CLASS_NAME = "classNamePreferred"; // NOI18N
69

70     private String JavaDoc url = ""; // NOI18N
71
private int lineNumber;
72     private String JavaDoc condition = ""; // NOI18N
73
private String JavaDoc sourceName = null;
74     private String JavaDoc sourcePath = null;
75     private String JavaDoc stratum = "Java"; // NOI18N
76
private String JavaDoc className = null;
77
78     
79     private LineBreakpoint (String JavaDoc url) {
80         this.url = url;
81     }
82     
83     /**
84      * Creates a new breakpoint for given parameters.
85      *
86      * @param url a string representation of URL of the source file
87      * @param lineNumber a line number
88      * @return a new breakpoint for given parameters
89      */

90     public static LineBreakpoint create (
91         String JavaDoc url,
92         int lineNumber
93     ) {
94         LineBreakpoint b = new LineBreakpointImpl (url);
95         b.setLineNumber (lineNumber);
96         return b;
97     }
98
99     /**
100      * Gets the string representation of URL of the source file,
101      * which contains the class to stop on.
102      *
103      * @return name of class to stop on
104      */

105     public String JavaDoc getURL () {
106         return url;
107     }
108     
109     /**
110      * Sets the string representation of URL of the source file,
111      * which contains the class to stop on.
112      *
113      * @param url the URL of class to stop on
114      */

115     public void setURL (String JavaDoc url) {
116         String JavaDoc old;
117         synchronized (this) {
118             if (url == null) url = "";
119             if ( (url == this.url) ||
120                  ((url != null) && (this.url != null) && url.equals (this.url))
121             ) return;
122             old = this.url;
123             this.url = url;
124         }
125         firePropertyChange (PROP_URL, old, url);
126     }
127     
128     /**
129      * Gets number of line to stop on.
130      *
131      * @return line number to stop on
132      */

133     public int getLineNumber () {
134         return lineNumber;
135     }
136     
137     /**
138      * Sets number of line to stop on.
139      *
140      * @param ln a line number to stop on
141      */

142     public void setLineNumber (int ln) {
143         int old;
144         synchronized (this) {
145             if (ln == lineNumber) return;
146             old = lineNumber;
147             lineNumber = ln;
148         }
149         firePropertyChange (
150             PROP_LINE_NUMBER,
151             new Integer JavaDoc (old),
152             new Integer JavaDoc (ln)
153         );
154     }
155     
156     /**
157      * Returns condition.
158      *
159      * @return cond a condition
160      */

161     public String JavaDoc getCondition () {
162         return condition;
163     }
164     
165     /**
166      * Sets condition.
167      *
168      * @param c a new condition
169      */

170     public void setCondition (String JavaDoc c) {
171         String JavaDoc old;
172         synchronized (this) {
173             if (c == null) c = "";
174             c = c.trim ();
175             if ( (c == condition) ||
176                  ((c != null) && (condition != null) && condition.equals (c))
177             ) return;
178             old = condition;
179             condition = c;
180         }
181         firePropertyChange (PROP_CONDITION, old, c);
182     }
183     
184     /**
185      * Returns stratum.
186      *
187      * @return a stratum
188      */

189     public String JavaDoc getStratum () {
190         return stratum;
191     }
192     
193     /**
194      * Sets stratum.
195      *
196      * @param s a new stratum
197      */

198     public void setStratum (String JavaDoc s) {
199         String JavaDoc old;
200         synchronized (this) {
201             if (s == null) s = "";
202             s = s.trim ();
203             if ( (s == stratum) ||
204                  ((s != null) && (stratum != null) && stratum.equals (s))
205             ) return;
206             old = stratum;
207             stratum = s;
208         }
209         firePropertyChange (PROP_CONDITION, old, s);
210     }
211     
212     /**
213      * Returns the name of the source file.
214      *
215      * @return a source name or <code>null</code> when no source name is defined.
216      */

217     public String JavaDoc getSourceName () {
218         return sourceName;
219     }
220     
221     /**
222      * Sets the name of the source file.
223      *
224      * @param sn a new source name or <code>null</code>.
225      */

226     public void setSourceName (String JavaDoc sn) {
227         String JavaDoc old;
228         synchronized (this) {
229             if (sn != null) sn = sn.trim ();
230             if ( (sn == sourceName) ||
231                  ((sn != null) && (sourceName != null) && sourceName.equals (sn))
232             ) return;
233             old = sourceName;
234             sourceName = sn;
235         }
236         firePropertyChange (PROP_SOURCE_NAME, old, sn);
237     }
238
239     /**
240      * Returns source path, relative to the source root.
241      *
242      * @return a source path or <code>null</code> when no source path is defined.
243      *
244      * @since 1.3
245      */

246     public String JavaDoc getSourcePath() {
247         return sourcePath;
248     }
249
250     /**
251      * Sets source path, relative to the source root.
252      *
253      * @param sp a new source path or <code>null</code>
254      *
255      * @since 1.3
256      */

257     public void setSourcePath (String JavaDoc sp) {
258         String JavaDoc old;
259         synchronized (this) {
260             if (sp != null) sp = sp.trim();
261             if (sp == sourcePath || (sp != null && sp.equals(sourcePath))) {
262                 return ;
263             }
264             old = sourcePath;
265             sourcePath = sp;
266         }
267         firePropertyChange (PROP_SOURCE_PATH, old, sp);
268     }
269     
270     /**
271      * Sets the binary class name that is used to submit the breakpoint.
272      * @param className The binary class name, or <code>null</code> if the class
273      * name should be retrieved automatically from the URL and line number.
274      * @since 2.8
275      */

276     public void setPreferredClassName(String JavaDoc className) {
277         String JavaDoc old;
278         synchronized (this) {
279             if (this.className == className || (className != null && className.equals(this.className))) {
280                 return ;
281             }
282             old = className;
283             this.className = className;
284         }
285         firePropertyChange (PROP_PREFERRED_CLASS_NAME, old, className);
286     }
287     
288     /**
289      * Gets the binary class name that is used to submit the breakpoint.
290      * @return The binary class name, if previously set by {@link setPreferedClassName}
291      * method, or <code>null</code> if the class name should be retrieved
292      * automatically from the URL and line number.
293      * @since 2.8
294      */

295     public String JavaDoc getPreferredClassName() {
296         return className;
297     }
298     
299     /**
300      * Returns a string representation of this object.
301      *
302      * @return a string representation of the object
303      */

304     public String JavaDoc toString () {
305         String JavaDoc fileName = null;
306         try {
307             FileObject fo = URLMapper.findFileObject(new URL JavaDoc(url));
308             if (fo != null) {
309                 fileName = fo.getNameExt();
310             }
311         } catch (MalformedURLException JavaDoc ex) {
312             ErrorManager.getDefault().notify(ex);
313         }
314         if (fileName == null) fileName = url;
315         return "LineBreakpoint " + fileName + " : " + lineNumber;
316     }
317     
318     private static class LineBreakpointImpl extends LineBreakpoint
319                                             implements Comparable JavaDoc, FileChangeListener, ChangeListener JavaDoc {
320         
321        // We need to hold our FileObject so that it's not GC'ed, because we'd loose our listener.
322
private FileObject fo;
323        
324        public LineBreakpointImpl(String JavaDoc url) {
325             super(url);
326             try {
327                 fo = URLMapper.findFileObject(new URL JavaDoc(url));
328                 if (fo != null) {
329                     fo.addFileChangeListener(WeakListeners.create(FileChangeListener.class, this, fo));
330                 }
331             } catch (MalformedURLException JavaDoc ex) {
332                 ErrorManager.getDefault().notify(ex);
333             }
334         }
335         
336         public int compareTo(Object JavaDoc o) {
337             if (o instanceof LineBreakpointImpl) {
338                 LineBreakpoint lbthis = this;
339                 LineBreakpoint lb = (LineBreakpoint) o;
340                 int uc = lbthis.url.compareTo(lb.url);
341                 if (uc != 0) {
342                     return uc;
343                 } else {
344                     return lbthis.lineNumber - lb.lineNumber;
345                 }
346             } else {
347                 return -1;
348             }
349         }
350
351         public void fileFolderCreated(FileEvent fe) {
352         }
353
354         public void fileDataCreated(FileEvent fe) {
355         }
356
357         public void fileChanged(FileEvent fe) {
358         }
359
360         public void fileDeleted(FileEvent fe) {
361             DebuggerManager.getDebuggerManager().removeBreakpoint(this);
362             fo = null;
363         }
364
365         public void fileRenamed(FileRenameEvent fe) {
366             try {
367                 this.setURL(((FileObject) fe.getSource()).getURL().toString());
368             } catch (FileStateInvalidException ex) {
369                 ErrorManager.getDefault().notify(ex);
370             }
371         }
372
373         public void fileAttributeChanged(FileAttributeEvent fe) {
374         }
375     
376         public void stateChanged(ChangeEvent JavaDoc chev) {
377             Object JavaDoc source = chev.getSource();
378             if (source instanceof Breakpoint.VALIDITY) {
379                 setValidity((Breakpoint.VALIDITY) source, chev.toString());
380             } else {
381                 throw new UnsupportedOperationException JavaDoc(chev.toString());
382             }
383         }
384
385     }
386 }
Popular Tags