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.io.File;
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import java.util.List;
047: import java.beans.PropertyChangeListener;
048: import org.netbeans.api.debugger.DebuggerManager;
049:
050: import org.netbeans.api.debugger.jpda.*;
051: import org.netbeans.spi.debugger.jpda.*;
052:
053: import org.netbeans.modules.web.debug.breakpoints.JspLineBreakpoint;
054:
055: /**
056: *
057: * @author Martin Grebac
058: */
059: public class Context {
060:
061: private static EditorContext editorContext;
062:
063: private static EditorContext getContext() {
064: if (editorContext == null) {
065: List l = DebuggerManager.getDebuggerManager().lookup(null,
066: EditorContext.class);
067: if (!l.isEmpty()) {
068: editorContext = (EditorContext) l.get(0);
069: }
070: }
071: return editorContext;
072: }
073:
074: // EditorContext methods .................................................
075:
076: /**
077: * Shows source with given url on given line number.
078: *
079: * @param url a url of source to be shown
080: * @param lineNumber a number of line to be shown
081: */
082: public static boolean showSource(String url, int lineNumber,
083: Object timeStamp) {
084: return getContext().showSource(url, lineNumber, timeStamp);
085: }
086:
087: /**
088: * Adds annotation to given url on given line.
089: *
090: * @param url a url of source annotation should be set into
091: * @param lineNumber a number of line annotation should be set into
092: * @param annotationType a type of annotation to be set
093: *
094: * @return annotation or <code>null</code>, when the annotation can not be
095: * created at the given URL or line number.
096: */
097: public static Object annotate(String url, int lineNumber,
098: String annotationType, Object timeStamp) {
099: return getContext().annotate(url, lineNumber, annotationType,
100: timeStamp);
101: }
102:
103: /**
104: * Removes given annotation.
105: *
106: * @return true if annotation has been successfully removed
107: */
108: public static void removeAnnotation(Object annotation) {
109: getContext().removeAnnotation(annotation);
110: }
111:
112: public static int getLineNumber(Object annotation, Object timeStamp) {
113: return getContext().getLineNumber(annotation, timeStamp);
114: }
115:
116: /**
117: * Returns number of line currently selected in editor or <code>null</code>.
118: *
119: * @return number of line currently selected in editor or <code>0</code>
120: */
121: public static int getCurrentLineNumber() {
122: return getContext().getCurrentLineNumber();
123: }
124:
125: /**
126: * Returns URL of source currently selected in editor or <code>null</code>.
127: *
128: * @return URL of source currently selected in editor or <code>null</code>
129: */
130: public static String getCurrentURL() {
131: return getContext().getCurrentURL();
132: }
133:
134: public static void addPropertyChangeListener(
135: PropertyChangeListener l) {
136: getContext().addPropertyChangeListener(l);
137: }
138:
139: public static void removePropertyChangeListener(
140: PropertyChangeListener l) {
141: getContext().removePropertyChangeListener(l);
142: }
143:
144: /**
145: * Creates a new time stamp.
146: *
147: * @param timeStamp a new time stamp
148: */
149: public static void createTimeStamp(Object timeStamp) {
150: getContext().createTimeStamp(timeStamp);
151: }
152:
153: /**
154: * Disposes given time stamp.
155: *
156: * @param timeStamp a time stamp to be disposed
157: */
158: public static void disposeTimeStamp(Object timeStamp) {
159: getContext().disposeTimeStamp(timeStamp);
160: }
161:
162: // utility methods .........................................................
163:
164: public static String getFileName(JspLineBreakpoint b) {
165: try {
166: return new File(new URL(b.getURL()).getFile()).getName();
167: } catch (MalformedURLException e) {
168: return null;
169: }
170: }
171:
172: public static boolean showSource(JspLineBreakpoint b) {
173: if (b.getLineNumber() < 1)
174: return Context.showSource(b.getURL(), 1, null);
175: return Context.showSource(b.getURL(), b.getLineNumber(), null);
176: }
177:
178: /**
179: * Adds annotation to url:line where the given breakpoint is set.
180: *
181: * @param b breakpoint to annotate
182: *
183: * @return annotation or <code>null</code>, when the annotation can not be
184: * created at the url:line where the given breakpoint is set.
185: */
186: public static Object annotate(JspLineBreakpoint b) {
187: String url = b.getURL();
188: int lineNumber = b.getLineNumber();
189: if (lineNumber < 1)
190: return null;
191: String condition = b.getCondition();
192: boolean isConditional = (condition != null)
193: && !condition.trim().equals(""); // NOI18N
194: String annotationType = b.isEnabled() ? (isConditional ? EditorContext.CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE
195: : EditorContext.BREAKPOINT_ANNOTATION_TYPE)
196: : (isConditional ? EditorContext.DISABLED_CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE
197: : EditorContext.DISABLED_BREAKPOINT_ANNOTATION_TYPE);
198:
199: return annotate(url, lineNumber, annotationType, null);
200: }
201:
202: }
|