KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > debug > JspBreakpointAnnotationListener


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 Micro//S ystems, Inc. Portions Copyright 1997-2006 Sun
17  * Micro//S ystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.debug;
20
21 import java.beans.*;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.netbeans.api.debugger.*;
26 import org.netbeans.api.debugger.jpda.*;
27
28 import org.netbeans.modules.web.debug.breakpoints.*;
29
30 /**
31  * Listens on {@org.netbeans.api.debugger.DebuggerManager} on
32  * {@link org.netbeans.api.debugger.DebuggerManager#PROP_BREAKPOINTS}
33  * property and annotates
34  * JSP breakpoints in NetBeans editor.
35  *
36  * @author Martin Grebac
37  */

38 public class JspBreakpointAnnotationListener extends DebuggerManagerAdapter {
39     
40     private HashMap JavaDoc breakpointToAnnotation = new HashMap JavaDoc ();
41     private boolean listen = true;
42     
43     public String JavaDoc[] getProperties () {
44         return new String JavaDoc[] {DebuggerManager.PROP_BREAKPOINTS};
45     }
46     
47     /**
48      * Listens on breakpoint.
49      */

50     public void propertyChange (PropertyChangeEvent e) {
51         String JavaDoc propertyName = e.getPropertyName ();
52         if (propertyName == null) return;
53         if (!listen) return;
54         if ( (!propertyName.equals (JspLineBreakpoint.PROP_CONDITION)) &&
55              (!propertyName.equals (JspLineBreakpoint.PROP_URL)) &&
56              (!propertyName.equals (JspLineBreakpoint.PROP_LINE_NUMBER)) &&
57              (!propertyName.equals (JspLineBreakpoint.PROP_ENABLED))
58         ) return;
59         JspLineBreakpoint b = (JspLineBreakpoint) e.getSource ();
60         annotate (b);
61     }
62
63     /**
64     * Called when some breakpoint is added.
65     *
66     * @param b breakpoint
67     */

68     public void breakpointAdded (Breakpoint b) {
69         if (b instanceof JspLineBreakpoint) {
70             ((JspLineBreakpoint) b).addPropertyChangeListener (this);
71             annotate ((JspLineBreakpoint) b);
72         }
73     }
74
75     /**
76     * Called when some breakpoint is removed.
77     *
78     * @param breakpoint
79     */

80     public void breakpointRemoved (Breakpoint b) {
81         if (b instanceof JspLineBreakpoint) {
82             ((JspLineBreakpoint) b).removePropertyChangeListener (this);
83             removeAnnotation ((JspLineBreakpoint) b);
84         }
85     }
86
87     public JspLineBreakpoint findBreakpoint (String JavaDoc url, int lineNumber) {
88         Iterator JavaDoc i = breakpointToAnnotation.keySet ().iterator ();
89         while (i.hasNext ()) {
90             JspLineBreakpoint lb = (JspLineBreakpoint) i.next ();
91             if (!lb.getURL ().equals (url)) continue;
92             Object JavaDoc annotation = breakpointToAnnotation.get (lb);
93             int ln = Context.getLineNumber (annotation, null);
94             if (ln == lineNumber) return lb;
95         }
96         return null;
97     }
98     
99     // helper methods ..........................................................
100

101     private void annotate (JspLineBreakpoint b) {
102         // remove old annotation
103
Object JavaDoc annotation = breakpointToAnnotation.get (b);
104         if (annotation != null)
105             Context.removeAnnotation (annotation);
106         if (b.isHidden ()) return;
107         
108         // add new one
109
annotation = Context.annotate (b);
110         if (annotation == null)
111             return;
112         
113         breakpointToAnnotation.put (b, annotation);
114         
115         DebuggerEngine de = DebuggerManager.getDebuggerManager ().
116             getCurrentEngine ();
117         Object JavaDoc timeStamp = null;
118         if (de != null)
119             timeStamp = de.lookupFirst (null, JPDADebugger.class);
120         update (b, timeStamp);
121     }
122
123     public void updateJspLineBreakpoints () {
124         Iterator JavaDoc it = breakpointToAnnotation.keySet ().iterator ();
125         while (it.hasNext ()) {
126             JspLineBreakpoint lb = (JspLineBreakpoint) it.next ();
127             update (lb, null);
128         }
129     }
130     
131     private void update (JspLineBreakpoint b, Object JavaDoc timeStamp) {
132         Object JavaDoc annotation = breakpointToAnnotation.get (b);
133         if (annotation == null)
134             return;
135         int ln = Context.getLineNumber (annotation, timeStamp);
136         listen = false;
137         b.setLineNumber (ln);
138         listen = true;
139     }
140     
141     private void removeAnnotation(JspLineBreakpoint b) {
142         Object JavaDoc annotation = breakpointToAnnotation.remove (b);
143         if (annotation != null)
144             Context.removeAnnotation (annotation);
145     }
146 }
147
Popular Tags