KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > EditorContextBridge


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;
20
21 import com.sun.jdi.AbsentInformationException;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.File JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.netbeans.api.debugger.DebuggerManager;
29 import org.netbeans.api.debugger.jpda.LineBreakpoint;
30 import org.netbeans.api.debugger.jpda.CallStackFrame;
31 import org.netbeans.api.debugger.jpda.JPDAThread;
32 import org.netbeans.spi.debugger.jpda.EditorContext;
33
34
35 /**
36  *
37  * @author Jan Jancura
38  */

39 public class EditorContextBridge {
40
41     public static final String JavaDoc FIELD = "field";
42     public static final String JavaDoc METHOD = "method";
43     public static final String JavaDoc CLASS = "class";
44     public static final String JavaDoc LINE = "line";
45
46     private static EditorContext context;
47     
48     private static EditorContext getContext () {
49         if (context == null) {
50             List JavaDoc l = DebuggerManager.getDebuggerManager ().lookup
51                 (null, EditorContext.class);
52             context = (EditorContext) l.get (0);
53             int i, k = l.size ();
54             for (i = 1; i < k; i++)
55                 context = new CompoundContextProvider (
56                     (EditorContext) l.get (i),
57                     context
58                 );
59         }
60         return context;
61     }
62
63     
64     // ContextProvider methods .................................................
65

66     /**
67      * Shows source with given url on given line number.
68      *
69      * @param url a url of source to be shown
70      * @param lineNumber a number of line to be shown
71      */

72     public static boolean showSource (
73         String JavaDoc url,
74         int lineNumber,
75         Object JavaDoc timeStamp
76     ) {
77         return getContext ().showSource (url, lineNumber, timeStamp);
78     }
79
80     /**
81      * Creates a new time stamp.
82      *
83      * @param timeStamp a new time stamp
84      */

85     public static void createTimeStamp (Object JavaDoc timeStamp) {
86         getContext ().createTimeStamp (timeStamp);
87     }
88
89     /**
90      * Disposes given time stamp.
91      *
92      * @param timeStamp a time stamp to be disposed
93      */

94     public static void disposeTimeStamp (Object JavaDoc timeStamp) {
95         getContext ().disposeTimeStamp (timeStamp);
96     }
97
98     /**
99      * Adds annotation to given url on given line.
100      *
101      * @param url a url of source annotation should be set into
102      * @param lineNumber a number of line annotation should be set into
103      * @param annotationType a type of annotation to be set
104      *
105      * @return annotation
106      */

107     public static Object JavaDoc annotate (
108         String JavaDoc url,
109         int lineNumber,
110         String JavaDoc annotationType,
111         Object JavaDoc timeStamp
112     ) {
113         return getContext ().annotate (url, lineNumber, annotationType, timeStamp);
114     }
115
116     /**
117      * Adds annotation to given url on given character range.
118      *
119      * @param url a url of source annotation should be set into
120      * @param startPosition the starting position of the annotation
121      * @param endPosition the ending position of the annotation
122      * @param annotationType a type of annotation to be set
123      *
124      * @return annotation
125      */

126     public static Object JavaDoc annotate (
127         String JavaDoc url,
128         int startPosition,
129         int endPosition,
130         String JavaDoc annotationType,
131         Object JavaDoc timeStamp
132     ) {
133         return getContext ().annotate (url, startPosition, endPosition, annotationType, timeStamp);
134     }
135
136     /**
137      * Removes given annotation.
138      */

139     public static void removeAnnotation (
140         Object JavaDoc annotation
141     ) {
142         getContext ().removeAnnotation (annotation);
143     }
144
145     /**
146      * Returns line number given annotation is associated with.
147      *
148      * @return line number given annotation is associated with
149      */

150     public static int getLineNumber (
151         Object JavaDoc annotation,
152         Object JavaDoc timeStamp
153     ) {
154         return getContext ().getLineNumber (annotation, timeStamp);
155     }
156     
157     /**
158      * Returns number of line currently selected in editor or <code>-1</code>.
159      *
160      * @return number of line currently selected in editor or <code>-1</code>
161      */

162     public static int getCurrentLineNumber () {
163         return getContext ().getCurrentLineNumber ();
164     }
165
166     /**
167      * Returns name of class currently selected in editor or <code>null</code>.
168      *
169      * @return name of class currently selected in editor or <code>null</code>
170      */

171     public static String JavaDoc getCurrentClassName () {
172         return getContext ().getCurrentClassName ();
173     }
174
175     /**
176      * Returns URL of source currently selected in editor or <code>null</code>.
177      *
178      * @return URL of source currently selected in editor or <code>null</code>
179      */

180     public static String JavaDoc getCurrentURL () {
181         return getContext ().getCurrentURL ();
182     }
183
184     /**
185      * Returns name of method currently selected in editor or <code>null</code>.
186      *
187      * @return name of method currently selected in editor or <code>null</code>
188      */

189     public static String JavaDoc getCurrentMethodName () {
190         return getContext ().getCurrentMethodName ();
191     }
192
193     /**
194      * Returns name of field currently selected in editor or <code>null</code>.
195      *
196      * @return name of field currently selected in editor or <code>null</code>
197      */

198     public static String JavaDoc getCurrentFieldName () {
199         return getContext ().getCurrentFieldName ();
200     }
201
202     /**
203      * Returns identifier currently selected in editor or <code>null</code>.
204      *
205      * @return identifier currently selected in editor or <code>null</code>
206      */

207     public static String JavaDoc getSelectedIdentifier () {
208         return getContext ().getSelectedIdentifier ();
209     }
210
211     /**
212      * Returns method name currently selected in editor or <code>null</code>.
213      *
214      * @return method name currently selected in editor or <code>null</code>
215      */

216     public static String JavaDoc getSelectedMethodName () {
217         return getContext ().getSelectedMethodName ();
218     }
219     
220     /**
221      * Returns line number of given field in given class.
222      *
223      * @param url the url of file the class is deined in
224      * @param className the name of class (or innerclass) the field is
225      * defined in
226      * @param fieldName the name of field
227      *
228      * @return line number or -1
229      */

230     public static int getFieldLineNumber (
231         String JavaDoc url,
232         String JavaDoc className,
233         String JavaDoc fieldName
234     ) {
235         return getContext ().getFieldLineNumber (url, className, fieldName);
236     }
237     
238     /**
239      * Returns class name for given url and line number or null.
240      *
241      * @param url a url
242      * @param lineNumber a line number
243      *
244      * @return class name for given url and line number or null
245      */

246     public static String JavaDoc getClassName (
247         String JavaDoc url,
248         int lineNumber
249     ) {
250         return getContext ().getClassName (url, lineNumber);
251     }
252     
253     /**
254      * Returns list of imports for given source url.
255      *
256      * @param url the url of source file
257      *
258      * @return list of imports for given source url
259      */

260     public static String JavaDoc[] getImports (String JavaDoc url) {
261         return getContext ().getImports (url);
262     }
263
264     public static void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
265         getContext ().addPropertyChangeListener (l);
266     }
267
268     public static void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
269         getContext ().removePropertyChangeListener (l);
270     }
271
272     /*
273     public static void addPropertyChangeListener (
274         String propertyName,
275         PropertyChangeListener l
276     ) {
277         getContext ().addPropertyChangeListener (propertyName, l);
278     }
279
280     public static void removePropertyChangeListener (
281         String propertyName,
282         PropertyChangeListener l
283     ) {
284         getContext ().removePropertyChangeListener (propertyName, l);
285     }
286      */

287     
288     
289     // utility methods .........................................................
290

291     public static String JavaDoc getFileName (LineBreakpoint b) {
292         try {
293             return new File JavaDoc (new URL JavaDoc (b.getURL ()).getFile ()).getName ();
294         } catch (MalformedURLException JavaDoc e) {
295             return null;
296         }
297     }
298
299     public static boolean showSource (LineBreakpoint b, Object JavaDoc timeStamp) {
300         if (b.getLineNumber () < 1)
301             return EditorContextBridge.showSource (
302                 b.getURL (),
303                 1,
304                 timeStamp
305             );
306         return EditorContextBridge.showSource (
307             b.getURL (),
308             b.getLineNumber (),
309             timeStamp
310         );
311     }
312
313     public static String JavaDoc getDefaultType () {
314         String JavaDoc id = getSelectedIdentifier ();
315         if (id != null) {
316             if (id.equals(getCurrentMethodName())) return METHOD;
317             String JavaDoc s = getCurrentClassName();
318             int i = s.lastIndexOf ('.');
319             if (i >= 0)
320                 s = s.substring (i + 1);
321             if (id.equals (s))
322                 return CLASS;
323             return FIELD;
324         } else {
325             String JavaDoc s = getCurrentFieldName ();
326             if (s != null && s.length () > 0)
327                 return FIELD;
328             s = getCurrentMethodName();
329             if (s != null && s.length () > 0)
330                 return METHOD;
331             if (s != null && s.length () < 1) {
332                 s = getCurrentClassName ();
333                 if (s.length () > 0)
334                     return CLASS;
335             }
336         }
337         return CLASS;
338     }
339
340     public static Object JavaDoc annotate (
341         LineBreakpoint b
342     ) {
343         String JavaDoc url = b.getURL ();
344         int lineNumber = b.getLineNumber ();
345         if (lineNumber < 1) return null;
346         String JavaDoc condition = b.getCondition ();
347         boolean isConditional = (condition != null) &&
348             !"".equals (condition.trim ()); // NOI18N
349
String JavaDoc annotationType = b.isEnabled () ?
350             (isConditional ? EditorContext.CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE :
351                              EditorContext.BREAKPOINT_ANNOTATION_TYPE) :
352             (isConditional ? EditorContext.DISABLED_CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE :
353                              EditorContext.DISABLED_BREAKPOINT_ANNOTATION_TYPE);
354
355         return annotate (
356             url,
357             lineNumber,
358             annotationType,
359             null
360         );
361     }
362
363     public static String JavaDoc getRelativePath (
364         JPDAThread thread,
365         String JavaDoc stratumn
366     ) {
367         try {
368             return convertSlash (thread.getSourcePath (stratumn));
369         } catch (AbsentInformationException e) {
370             return getRelativePath (thread.getClassName ());
371         }
372     }
373
374     public static String JavaDoc getRelativePath (
375         CallStackFrame csf,
376         String JavaDoc stratumn
377     ) {
378         try {
379             return convertSlash (csf.getSourcePath (stratumn));
380         } catch (AbsentInformationException e) {
381             return getRelativePath (csf.getClassName ());
382         }
383     }
384
385     public static String JavaDoc getRelativePath (
386         String JavaDoc className
387     ) {
388         int i = className.indexOf ('$');
389         if (i > 0) className = className.substring (0, i);
390         String JavaDoc sourceName = className.replace
391             ('.', '/') + ".java";
392         return sourceName;
393     }
394     
395     private static String JavaDoc convertSlash (String JavaDoc original) {
396         return original.replace (File.separatorChar, '/');
397     }
398
399     
400     // innerclasses ............................................................
401

402     private static class CompoundContextProvider extends EditorContext {
403
404         private EditorContext cp1, cp2;
405         
406         CompoundContextProvider (
407             EditorContext cp1,
408             EditorContext cp2
409         ) {
410             this.cp1 = cp1;
411             this.cp2 = cp2;
412         }
413
414         public void createTimeStamp (Object JavaDoc timeStamp) {
415             cp1.createTimeStamp (timeStamp);
416             cp2.createTimeStamp (timeStamp);
417         }
418
419         public void disposeTimeStamp (Object JavaDoc timeStamp) {
420             cp1.disposeTimeStamp (timeStamp);
421             cp2.disposeTimeStamp (timeStamp);
422         }
423         
424         public void updateTimeStamp (Object JavaDoc timeStamp, String JavaDoc url) {
425             cp1.updateTimeStamp (timeStamp, url);
426             cp2.updateTimeStamp (timeStamp, url);
427         }
428
429         public String JavaDoc getCurrentClassName () {
430             String JavaDoc s = cp1.getCurrentClassName ();
431             if (s.trim ().length () < 1)
432                 return cp2.getCurrentClassName ();
433             return s;
434         }
435
436         public String JavaDoc getCurrentURL () {
437             String JavaDoc s = cp1.getCurrentURL ();
438             if (s.trim ().length () < 1)
439                 return cp2.getCurrentURL ();
440             return s;
441         }
442         
443         public String JavaDoc getCurrentFieldName () {
444             String JavaDoc s = cp1.getCurrentFieldName ();
445             if ( (s == null) || (s.trim ().length () < 1))
446                 return cp2.getCurrentFieldName ();
447             return s;
448         }
449         
450         public int getCurrentLineNumber () {
451             int i = cp1.getCurrentLineNumber ();
452             if (i < 1)
453                 return cp2.getCurrentLineNumber ();
454             return i;
455         }
456         
457         public String JavaDoc getCurrentMethodName () {
458             String JavaDoc s = cp1.getCurrentMethodName ();
459             if ( (s == null) || (s.trim ().length () < 1))
460                 return cp2.getCurrentMethodName ();
461             return s;
462         }
463         
464         public String JavaDoc getSelectedIdentifier () {
465             String JavaDoc s = cp1.getSelectedIdentifier ();
466             if ( (s == null) || (s.trim ().length () < 1))
467                 return cp2.getSelectedIdentifier ();
468             return s;
469         }
470         
471         public String JavaDoc getSelectedMethodName () {
472             String JavaDoc s = cp1.getSelectedMethodName ();
473             if ( (s == null) || (s.trim ().length () < 1))
474                 return cp2.getSelectedMethodName ();
475             return s;
476         }
477         
478         public void removeAnnotation (Object JavaDoc annotation) {
479             CompoundAnnotation ca = (CompoundAnnotation) annotation;
480             cp1.removeAnnotation (ca.annotation1);
481             cp2.removeAnnotation (ca.annotation2);
482         }
483
484         public Object JavaDoc annotate (
485             String JavaDoc sourceName,
486             int lineNumber,
487             String JavaDoc annotationType,
488             Object JavaDoc timeStamp
489         ) {
490             CompoundAnnotation ca = new CompoundAnnotation ();
491             ca.annotation1 = cp1.annotate
492                 (sourceName, lineNumber, annotationType, timeStamp);
493             ca.annotation2 = cp2.annotate
494                 (sourceName, lineNumber, annotationType, timeStamp);
495             return ca;
496         }
497
498         public int getLineNumber (Object JavaDoc annotation, Object JavaDoc timeStamp) {
499             CompoundAnnotation ca = new CompoundAnnotation ();
500             int ln = cp1.getLineNumber (ca.annotation1, timeStamp);
501             if (ln >= 0) return ln;
502             return cp2.getLineNumber (ca.annotation2, timeStamp);
503         }
504
505         public boolean showSource (String JavaDoc sourceName, int lineNumber, Object JavaDoc timeStamp) {
506             return cp1.showSource (sourceName, lineNumber, timeStamp) |
507                    cp2.showSource (sourceName, lineNumber, timeStamp);
508         }
509     
510         public int getFieldLineNumber (
511             String JavaDoc url,
512             String JavaDoc className,
513             String JavaDoc fieldName
514         ) {
515             int ln = cp1.getFieldLineNumber (url, className, fieldName);
516             if (ln != -1) return ln;
517             return cp2.getFieldLineNumber (url, className, fieldName);
518         }
519     
520         public String JavaDoc getClassName (
521             String JavaDoc url,
522             int lineNumber
523         ) {
524             String JavaDoc className = cp1.getClassName (url, lineNumber);
525             if (className != null) return className;
526             return cp2.getClassName (url, lineNumber);
527         }
528     
529         public String JavaDoc[] getImports (String JavaDoc url) {
530             String JavaDoc[] r1 = cp1.getImports (url);
531             String JavaDoc[] r2 = cp2.getImports (url);
532             String JavaDoc[] r = new String JavaDoc [r1.length + r2.length];
533             System.arraycopy (r1, 0, r, 0, r1.length);
534             System.arraycopy (r2, 0, r, r1.length, r2.length);
535             return r;
536         }
537         
538         public void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
539             cp1.addPropertyChangeListener (l);
540             cp2.addPropertyChangeListener (l);
541         }
542         
543         public void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
544             cp1.removePropertyChangeListener (l);
545             cp2.removePropertyChangeListener (l);
546         }
547         
548         public void addPropertyChangeListener (
549             String JavaDoc propertyName,
550             PropertyChangeListener JavaDoc l
551         ) {
552             cp1.addPropertyChangeListener (propertyName, l);
553             cp2.addPropertyChangeListener (propertyName, l);
554         }
555         
556         public void removePropertyChangeListener (
557             String JavaDoc propertyName,
558             PropertyChangeListener JavaDoc l
559         ) {
560             cp1.removePropertyChangeListener (propertyName, l);
561             cp2.removePropertyChangeListener (propertyName, l);
562         }
563     }
564     
565     private static class CompoundAnnotation {
566         Object JavaDoc annotation1;
567         Object JavaDoc annotation2;
568     }
569 }
570
571
Popular Tags