KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.List JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import org.netbeans.api.debugger.DebuggerManager;
27
28 import org.netbeans.api.debugger.jpda.*;
29 import org.netbeans.spi.debugger.jpda.*;
30
31 import org.netbeans.modules.web.debug.breakpoints.JspLineBreakpoint;
32
33 /**
34  *
35  * @author Martin Grebac
36  */

37 public class Context {
38
39     private static EditorContext editorContext;
40
41     private static EditorContext getContext () {
42         if (editorContext == null) {
43             List JavaDoc l = DebuggerManager.getDebuggerManager().lookup(null, EditorContext.class);
44             if (!l.isEmpty()) {
45                 editorContext = (EditorContext) l.get (0);
46             }
47         }
48         return editorContext;
49     }
50     
51     // EditorContext methods .................................................
52

53     /**
54      * Shows source with given url on given line number.
55      *
56      * @param url a url of source to be shown
57      * @param lineNumber a number of line to be shown
58      */

59     public static boolean showSource (
60         String JavaDoc url,
61         int lineNumber,
62         Object JavaDoc timeStamp
63     ) {
64         return getContext ().showSource (url, lineNumber, timeStamp);
65     }
66
67     /**
68      * Adds annotation to given url on given line.
69      *
70      * @param url a url of source annotation should be set into
71      * @param lineNumber a number of line annotation should be set into
72      * @param annotationType a type of annotation to be set
73      *
74      * @return annotation or <code>null</code>, when the annotation can not be
75      * created at the given URL or line number.
76      */

77     public static Object JavaDoc annotate (
78         String JavaDoc url,
79         int lineNumber,
80         String JavaDoc annotationType,
81         Object JavaDoc timeStamp
82     ) {
83         return getContext ().annotate (url, lineNumber, annotationType, timeStamp);
84     }
85
86     /**
87      * Removes given annotation.
88      *
89      * @return true if annotation has been successfully removed
90      */

91     public static void removeAnnotation (
92         Object JavaDoc annotation
93     ) {
94         getContext ().removeAnnotation (annotation);
95     }
96     
97     public static int getLineNumber (Object JavaDoc annotation, Object JavaDoc timeStamp) {
98         return getContext ().getLineNumber (annotation, timeStamp);
99     }
100     
101     /**
102      * Returns number of line currently selected in editor or <code>null</code>.
103      *
104      * @return number of line currently selected in editor or <code>0</code>
105      */

106     public static int getCurrentLineNumber () {
107         return getContext ().getCurrentLineNumber ();
108     }
109
110     /**
111      * Returns URL of source currently selected in editor or <code>null</code>.
112      *
113      * @return URL of source currently selected in editor or <code>null</code>
114      */

115     public static String JavaDoc getCurrentURL () {
116         return getContext ().getCurrentURL ();
117     }
118
119     public static void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
120         getContext ().addPropertyChangeListener (l);
121     }
122
123     public static void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
124         getContext ().removePropertyChangeListener (l);
125     }
126     
127     /**
128      * Creates a new time stamp.
129      *
130      * @param timeStamp a new time stamp
131      */

132     public static void createTimeStamp (Object JavaDoc timeStamp) {
133         getContext ().createTimeStamp (timeStamp);
134     }
135
136     /**
137      * Disposes given time stamp.
138      *
139      * @param timeStamp a time stamp to be disposed
140      */

141     public static void disposeTimeStamp (Object JavaDoc timeStamp) {
142         getContext ().disposeTimeStamp (timeStamp);
143     }
144     
145     // utility methods .........................................................
146

147     public static String JavaDoc getFileName (JspLineBreakpoint b) {
148         try {
149             return new File JavaDoc(new URL JavaDoc(b.getURL()).getFile ()).getName ();
150         } catch (MalformedURLException JavaDoc e) {
151             return null;
152         }
153     }
154
155     public static boolean showSource(JspLineBreakpoint b) {
156         if (b.getLineNumber () < 1)
157             return Context.showSource (
158                 b.getURL (),
159                 1,
160                 null
161             );
162         return Context.showSource (
163             b.getURL (),
164             b.getLineNumber (),
165             null
166         );
167     }
168
169     /**
170      * Adds annotation to url:line where the given breakpoint is set.
171      *
172      * @param b breakpoint to annotate
173      *
174      * @return annotation or <code>null</code>, when the annotation can not be
175      * created at the url:line where the given breakpoint is set.
176      */

177     public static Object JavaDoc annotate(JspLineBreakpoint b) {
178         String JavaDoc url = b.getURL ();
179         int lineNumber = b.getLineNumber ();
180         if (lineNumber < 1) return null;
181         String JavaDoc condition = b.getCondition ();
182         boolean isConditional = (condition != null) &&
183             !condition.trim ().equals (""); // NOI18N
184
String JavaDoc annotationType = b.isEnabled () ?
185             (isConditional ? EditorContext.CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE :
186                              EditorContext.BREAKPOINT_ANNOTATION_TYPE) :
187             (isConditional ? EditorContext.DISABLED_CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE :
188                              EditorContext.DISABLED_BREAKPOINT_ANNOTATION_TYPE);
189
190         return annotate (
191             url,
192             lineNumber,
193             annotationType,
194             null
195         );
196     }
197
198 }
199
200
Popular Tags