KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > breakpoints > BreakpointAnnotationListener


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.debugger.jpda.ui.breakpoints;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.netbeans.api.debugger.Breakpoint;
27 import org.netbeans.api.debugger.DebuggerEngine;
28 import org.netbeans.api.debugger.DebuggerManager;
29 import org.netbeans.api.debugger.DebuggerManagerAdapter;
30 import org.netbeans.api.debugger.LazyDebuggerManagerListener;
31 import org.netbeans.api.debugger.jpda.JPDABreakpoint;
32 import org.netbeans.api.debugger.jpda.JPDADebugger;
33 import org.netbeans.api.debugger.jpda.LineBreakpoint;
34 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
35
36
37 /**
38  * Listens on {@org.netbeans.api.debugger.DebuggerManager} on
39  * {@link org.netbeans.api.debugger.DebuggerManager#PROP_BREAKPOINTS}
40  * property and annotates JPDA Debugger line breakpoints in NetBeans editor.
41  * It manages list of line breakpoint annotations for ToggleBreakpointPerformer
42  * and BreakpointsUpdated too.
43  *
44  * @author Jan Jancura
45  */

46 public class BreakpointAnnotationListener extends DebuggerManagerAdapter {
47     
48     private HashMap JavaDoc breakpointToAnnotation = new HashMap JavaDoc ();
49     private boolean listen = true;
50     
51  
52     public String JavaDoc[] getProperties () {
53         return new String JavaDoc[] {DebuggerManager.PROP_BREAKPOINTS};
54     }
55     
56     /**
57      * Listens on breakpoint.
58      */

59     public void propertyChange (PropertyChangeEvent JavaDoc e) {
60         String JavaDoc propertyName = e.getPropertyName ();
61         if (propertyName == null) return;
62         if (!listen) return;
63         if ( (!LineBreakpoint.PROP_CONDITION.equals (propertyName)) &&
64              (!LineBreakpoint.PROP_URL.equals (propertyName)) &&
65              (!LineBreakpoint.PROP_LINE_NUMBER.equals (propertyName)) &&
66              (!JPDABreakpoint.PROP_ENABLED.equals (propertyName))
67         ) return;
68         LineBreakpoint b = (LineBreakpoint) e.getSource ();
69         annotate (b);
70     }
71
72     /**
73     * Called when some breakpoint is added.
74     *
75     * @param b breakpoint
76     */

77     public void breakpointAdded (Breakpoint b) {
78         if (b instanceof LineBreakpoint) {
79             ((LineBreakpoint) b).addPropertyChangeListener (this);
80             annotate ((LineBreakpoint) b);
81         }
82     }
83
84     /**
85     * Called when some breakpoint is removed.
86     *
87     * @param breakpoint
88     */

89     public void breakpointRemoved (Breakpoint b) {
90         if (b instanceof LineBreakpoint) {
91             ((LineBreakpoint) b).removePropertyChangeListener (this);
92             removeAnnotation ((LineBreakpoint) b);
93         }
94     }
95     
96     public LineBreakpoint findBreakpoint (String JavaDoc url, int lineNumber) {
97         Iterator JavaDoc i = breakpointToAnnotation.keySet ().iterator ();
98         while (i.hasNext ()) {
99             LineBreakpoint lb = (LineBreakpoint) i.next ();
100             if (!lb.getURL ().equals (url)) continue;
101             Object JavaDoc annotation = breakpointToAnnotation.get (lb);
102             int ln = EditorContextBridge.getLineNumber (annotation, null);
103             if (ln == lineNumber) return lb;
104         }
105         return null;
106     }
107
108     
109     // helper methods ..........................................................
110

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