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 Micro//S ystems, Inc. Portions Copyright 1997-2006 Sun
028: * Micro//S ystems, 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: package org.netbeans.modules.web.debug;
042:
043: import java.beans.*;
044: import java.util.HashMap;
045: import java.util.Iterator;
046:
047: import org.netbeans.api.debugger.*;
048: import org.netbeans.api.debugger.jpda.*;
049:
050: import org.netbeans.modules.web.debug.breakpoints.*;
051:
052: /**
053: * Listens on {@org.netbeans.api.debugger.DebuggerManager} on
054: * {@link org.netbeans.api.debugger.DebuggerManager#PROP_BREAKPOINTS}
055: * property and annotates
056: * JSP breakpoints in NetBeans editor.
057: *
058: * @author Martin Grebac
059: */
060: public class JspBreakpointAnnotationListener extends
061: DebuggerManagerAdapter {
062:
063: private HashMap breakpointToAnnotation = new HashMap();
064: private boolean listen = true;
065:
066: public String[] getProperties() {
067: return new String[] { DebuggerManager.PROP_BREAKPOINTS };
068: }
069:
070: /**
071: * Listens on breakpoint.
072: */
073: public void propertyChange(PropertyChangeEvent e) {
074: String propertyName = e.getPropertyName();
075: if (propertyName == null)
076: return;
077: if (!listen)
078: return;
079: if ((!propertyName.equals(JspLineBreakpoint.PROP_CONDITION))
080: && (!propertyName.equals(JspLineBreakpoint.PROP_URL))
081: && (!propertyName
082: .equals(JspLineBreakpoint.PROP_LINE_NUMBER))
083: && (!propertyName
084: .equals(JspLineBreakpoint.PROP_ENABLED)))
085: return;
086: JspLineBreakpoint b = (JspLineBreakpoint) e.getSource();
087: annotate(b);
088: }
089:
090: /**
091: * Called when some breakpoint is added.
092: *
093: * @param b breakpoint
094: */
095: public void breakpointAdded(Breakpoint b) {
096: if (b instanceof JspLineBreakpoint) {
097: ((JspLineBreakpoint) b).addPropertyChangeListener(this );
098: annotate((JspLineBreakpoint) b);
099: }
100: }
101:
102: /**
103: * Called when some breakpoint is removed.
104: *
105: * @param breakpoint
106: */
107: public void breakpointRemoved(Breakpoint b) {
108: if (b instanceof JspLineBreakpoint) {
109: ((JspLineBreakpoint) b).removePropertyChangeListener(this );
110: removeAnnotation((JspLineBreakpoint) b);
111: }
112: }
113:
114: public JspLineBreakpoint findBreakpoint(String url, int lineNumber) {
115: Iterator i = breakpointToAnnotation.keySet().iterator();
116: while (i.hasNext()) {
117: JspLineBreakpoint lb = (JspLineBreakpoint) i.next();
118: if (!lb.getURL().equals(url))
119: continue;
120: Object annotation = breakpointToAnnotation.get(lb);
121: int ln = Context.getLineNumber(annotation, null);
122: if (ln == lineNumber)
123: return lb;
124: }
125: return null;
126: }
127:
128: // helper methods ..........................................................
129:
130: private void annotate(JspLineBreakpoint b) {
131: // remove old annotation
132: Object annotation = breakpointToAnnotation.get(b);
133: if (annotation != null)
134: Context.removeAnnotation(annotation);
135: if (b.isHidden())
136: return;
137:
138: // add new one
139: annotation = Context.annotate(b);
140: if (annotation == null)
141: return;
142:
143: breakpointToAnnotation.put(b, annotation);
144:
145: DebuggerEngine de = DebuggerManager.getDebuggerManager()
146: .getCurrentEngine();
147: Object timeStamp = null;
148: if (de != null)
149: timeStamp = de.lookupFirst(null, JPDADebugger.class);
150: update(b, timeStamp);
151: }
152:
153: public void updateJspLineBreakpoints() {
154: Iterator it = breakpointToAnnotation.keySet().iterator();
155: while (it.hasNext()) {
156: JspLineBreakpoint lb = (JspLineBreakpoint) it.next();
157: update(lb, null);
158: }
159: }
160:
161: private void update(JspLineBreakpoint b, Object timeStamp) {
162: Object annotation = breakpointToAnnotation.get(b);
163: if (annotation == null)
164: return;
165: int ln = Context.getLineNumber(annotation, timeStamp);
166: listen = false;
167: b.setLineNumber(ln);
168: listen = true;
169: }
170:
171: private void removeAnnotation(JspLineBreakpoint b) {
172: Object annotation = breakpointToAnnotation.remove(b);
173: if (annotation != null)
174: Context.removeAnnotation(annotation);
175: }
176: }
|