KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > 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;
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      * Removes given annotation.
118      */

119     public static void removeAnnotation (
120         Object JavaDoc annotation
121     ) {
122         getContext ().removeAnnotation (annotation);
123     }
124
125     /**
126      * Returns line number given annotation is associated with.
127      *
128      * @return line number given annotation is associated with
129      */

130     public static int getLineNumber (
131         Object JavaDoc annotation,
132         Object JavaDoc timeStamp
133     ) {
134         return getContext ().getLineNumber (annotation, timeStamp);
135     }
136     
137     /**
138      * Returns number of line currently selected in editor or <code>null</code>.
139      *
140      * @return number of line currently selected in editor or <code>0</code>
141      */

142     public static int getCurrentLineNumber () {
143         return getContext ().getCurrentLineNumber ();
144     }
145
146     /**
147      * Returns name of class currently selected in editor or <code>null</code>.
148      *
149      * @return name of class currently selected in editor or <code>null</code>
150      */

151     public static String JavaDoc getCurrentClassName () {
152         return getContext ().getCurrentClassName ();
153     }
154
155     /**
156      * Returns URL of source currently selected in editor or <code>null</code>.
157      *
158      * @return URL of source currently selected in editor or <code>null</code>
159      */

160     public static String JavaDoc getCurrentURL () {
161         return getContext ().getCurrentURL ();
162     }
163
164     /**
165      * Returns name of method currently selected in editor or <code>null</code>.
166      *
167      * @return name of method currently selected in editor or <code>null</code>
168      */

169     public static String JavaDoc getCurrentMethodName () {
170         return getContext ().getCurrentMethodName ();
171     }
172
173     /**
174      * Returns name of field currently selected in editor or <code>null</code>.
175      *
176      * @return name of field currently selected in editor or <code>null</code>
177      */

178     public static String JavaDoc getCurrentFieldName () {
179         return getContext ().getCurrentFieldName ();
180     }
181
182     /**
183      * Returns identifier currently selected in editor or <code>null</code>.
184      *
185      * @return identifier currently selected in editor or <code>null</code>
186      */

187     public static String JavaDoc getSelectedIdentifier () {
188         return getContext ().getSelectedIdentifier ();
189     }
190
191     /**
192      * Returns method name currently selected in editor or <code>null</code>.
193      *
194      * @return method name currently selected in editor or <code>null</code>
195      */

196     public static String JavaDoc getSelectedMethodName () {
197         return getContext ().getSelectedMethodName ();
198     }
199     
200     /**
201      * Returns line number of given field in given class.
202      *
203      * @param url the url of file the class is deined in
204      * @param className the name of class (or innerclass) the field is
205      * defined in
206      * @param fieldName the name of field
207      *
208      * @return line number or -1
209      */

210     public static int getFieldLineNumber (
211         String JavaDoc url,
212         String JavaDoc className,
213         String JavaDoc fieldName
214     ) {
215         return getContext ().getFieldLineNumber (url, className, fieldName);
216     }
217     
218     /**
219      * Returns class name for given url and line number or null.
220      *
221      * @param url a url
222      * @param lineNumber a line number
223      *
224      * @return class name for given url and line number or null
225      */

226     public static String JavaDoc getClassName (
227         String JavaDoc url,
228         int lineNumber
229     ) {
230         return getContext ().getClassName (url, lineNumber);
231     }
232     
233     /*
234     public static EditorContext.ExpressionTree[] getExpressionTrees(String url, long lineNumber) {
235         return getContext().getExpressionTrees(url, lineNumber);
236     }
237     
238     public static EditorContext.ExpressionTree getExpressionTree(
239             String url, long lineNumber,
240             EditorContext.BytecodeProvider bytecodeProvider,
241             long locationInExpression) {
242         return getContext().getExpressionTree(url, lineNumber, bytecodeProvider,
243                                               locationInExpression);
244     }
245      */

246     
247     public static EditorContext.Operation[] getOperations(
248             String JavaDoc url, int lineNumber,
249             EditorContext.BytecodeProvider bytecodeProvider) {
250         return getContext().getOperations(url, lineNumber, bytecodeProvider);
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             !condition.trim ().equals (""); // 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         public CompoundAnnotation() {}
567         
568         Object JavaDoc annotation1;
569         Object JavaDoc annotation2;
570     }
571 }
572
573
Popular Tags