KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > importd > ImportDebugger


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 Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.importd;
21
22 import java.util.*;
23 import javax.swing.text.StyledDocument JavaDoc;
24
25 import org.openide.cookies.EditorCookie;
26 import org.openide.debugger.*;
27 import org.openide.text.Line;
28 import org.openide.text.NbDocument;
29 import org.openide.util.MapFormat;
30 import org.openide.util.NbBundle;
31
32 import org.netbeans.modules.debugger.*;
33 import org.netbeans.modules.debugger.support.DebuggerAnnotation;
34 import org.netbeans.modules.debugger.support.SecondaryDebuggerSupport;
35 import org.netbeans.modules.debugger.support.StateSupport;
36 import org.netbeans.modules.debugger.support.util.Utils;
37 import org.netbeans.modules.debugger.support.util.ValidatorImpl;
38
39 /**
40  * Example of Debugger Implementation. It "debugs" import statements
41  * in Java files (so it does not need compiled .class files). Step
42  * Into on "import java.awt.Button" opens .java file for Button and
43  * puts current line to the first line of it.
44  *
45  * @author Jan Jancura
46  */

47 public class ImportDebugger extends SecondaryDebuggerSupport {
48
49     // static ..................................................................
50

51     /** generated Serialized Version UID */
52     static final long serialVersionUID = 84466455228078708L;
53     /** bundle to obtain text information from */
54     private static ResourceBundle bundle;
55
56     static String JavaDoc getLocString (String JavaDoc s) {
57         if (bundle == null)
58             bundle = NbBundle.getBundle (ImportDebugger.class);
59         return bundle.getString (s);
60     }
61
62     /** debugger State constant. */
63     public static final State STATE_STOPPED =
64         new StateSupport (
65             StateSupport.PAUSE |
66             StateSupport.STEP_OVER |
67             StateSupport.STEP_INTO |
68             StateSupport.STEP_OUT
69         );
70     /** debugger State constant. */
71     public static final State STATE_RUNNING =
72         new StateSupport (
73             StateSupport.PAUSE
74         );
75     
76     static DebuggerAnnotation.CurrentPC currentLineAnnotation =
77         new DebuggerAnnotation.CurrentPC ();
78
79     
80     // variables ...............................................................
81

82     transient private ValidatorImpl validator;
83
84
85     // init ....................................................................
86

87     public ImportDebugger () {
88         validator = new ValidatorImpl ();
89     }
90
91
92     // Debugger implementation .................................................
93

94     /** Starts the debugger. The method stops the current debugging (if any)
95     * and takes information from the provided info (containing the class to start and
96     * arguments to pass it and name of class to stop debugging in) and starts
97     * new debugging session.
98     *
99     * @param info debugger info about class to start
100     * @exception DebuggerException if an error occures during the start of the debugger
101     */

102     public void startDebugger (DebuggerInfo info) throws DebuggerException {
103         boolean stopOnMain = info.getStopClassName () != null;
104         
105         super.startDebugger (info);
106         setState (Debugger.DEBUGGER_RUNNING);
107         String JavaDoc stopClassName = info.getClassName ();
108         Line l = Utils.getLine (stopClassName, 1);
109         stack = new Stack ();
110         stack.push (l);
111         isOnStack = new HashSet ();
112         isOnStack.add (l.getDataObject ());
113         
114         // stop on main
115
if (stopOnMain) {
116             setState (DEBUGGER_STOPPED);
117             setCurrentLine (l);
118             refreshWatches ();
119         } else
120             go ();
121     }
122
123     /**
124     * Finishes debugger.
125     */

126     public void finishDebugger () throws DebuggerException {
127         super.finishDebugger ();
128     }
129
130     /**
131     * Trace into.
132     */

133     synchronized public void traceInto () throws DebuggerException {
134         setState (DEBUGGER_RUNNING);
135         Line l = step ();
136         if (l == null) {
137             finishDebugger ();
138             refreshWatches ();
139             return;
140         }
141         setCurrentLine (l);
142         setState (DEBUGGER_STOPPED);
143         setLastAction (ACTION_TRACE_INTO);
144         refreshWatches ();
145     }
146
147     /**
148     * Trace over.
149     */

150     synchronized public void traceOver () throws DebuggerException {
151         setState (DEBUGGER_RUNNING);
152         int d = stack.size ();
153         Line l = null;
154         do {
155             l = step ();
156             if (l == null) {
157                 finishDebugger ();
158                 refreshWatches ();
159                 return;
160             }
161         } while (stack.size () > d);
162         setCurrentLine (l);
163         setState (DEBUGGER_STOPPED);
164         setLastAction (ACTION_TRACE_OVER);
165         refreshWatches ();
166     }
167
168     /**
169     * Go.
170     */

171     synchronized public void go () throws DebuggerException {
172         setLastAction (ACTION_CONTINUE);
173         setState (DEBUGGER_RUNNING);
174         
175         Line l = null;
176         do {
177             l = step ();
178         } while (l != null);
179         finishDebugger ();
180         refreshWatches ();
181     }
182
183     /**
184     * Step out.
185     */

186     synchronized public void stepOut () throws DebuggerException {
187         setLastAction (ACTION_STEP_OUT);
188         setState (DEBUGGER_RUNNING);
189         
190         int d = stack.size () - 1;
191         Line l = null;
192         do {
193             l = step ();
194             if (l == null) {
195                 finishDebugger ();
196                 refreshWatches ();
197                 return;
198             }
199         } while (stack.size () > d);
200         setCurrentLine (l);
201         setState (DEBUGGER_STOPPED);
202         setLastAction (ACTION_TRACE_INTO);
203         refreshWatches ();
204     }
205
206     /**
207      * Jumps to the next call site (towards the top of the call-stack).
208      */

209     public void goToCalledMethod () {
210     }
211     
212     /**
213      * Jumps to the previous call site (towards the bottom of the call-stack).
214      */

215     public void goToCallingMethod () {
216     }
217
218     /**
219      * Jumps to a given line.
220      *
221      * @param l a line jump to
222      */

223     public void runToCursor (Line l) {
224     }
225
226     /**
227      * Pauses debugging.
228      */

229     public void pause () {
230     }
231
232     public void setCurrentLine (Line l) {
233         Line old = getCurrentLine ();
234         if (old != null) {
235            // old.unmarkCurrentLine ();
236
currentLineAnnotation.detachLine ();
237         }
238         if (l != null) {
239             Utils.showInEditor (l);
240             //l.markCurrentLine ();
241
currentLineAnnotation.attachLine (l);
242         }
243         super.setCurrentLine (l);
244     }
245
246     protected void setState (int state) {
247         super.setState (state);
248         switch (state) {
249             case DEBUGGER_NOT_RUNNING:
250                 setDebuggerState (STATE_NOT_RUNNING);
251                 break;
252             case DEBUGGER_RUNNING:
253                 setDebuggerState (STATE_RUNNING);
254                 break;
255             case DEBUGGER_STOPPED:
256                 setDebuggerState (STATE_STOPPED);
257                 break;
258         }
259     }
260     
261     /** Creates new uninitialized watch. The watch is visible (not hidden).
262     *
263     * @return new uninitialized watch
264     */

265     public Watch createWatch () {
266         ImportWatch w = new ImportWatch (this, false);
267         addWatch (w);
268         return w;
269     }
270
271     /** Creates a watch its expression is set to initial value. Also
272     * allows to create a watch not presented to the user, for example
273     * for internal usage in the editor to obtain values of variables
274     * under the mouse pointer.
275     *
276     * @param expr expresion to watch for
277     * @param hidden allows to specify whether the watch should be presented
278     * to the user or not (be only of internal usage of the IDE).
279     * @return new watch
280     */

281     public Watch createWatch (String JavaDoc expr, boolean hidden) {
282         ImportWatch w = new ImportWatch (this, hidden);
283         w.setVariableName (expr);
284         addWatch (w);
285         return w;
286     }
287     
288     
289     // inner debugger implementation ...........................................
290

291     Validator getValidator () {
292         return validator;
293     }
294     
295     void refreshWatches () {
296         validator.validate ();
297     }
298     
299     
300     // inner debugger implementation ...........................................
301

302     private Stack stack = new Stack ();
303     private HashSet isOnStack = new HashSet ();
304     HashMap lineBreakpoints = new HashMap ();
305     
306     Stack getStack () {
307         return stack;
308     }
309     
310     Line step () {
311         Line l = (Line) stack.lastElement ();
312         try {
313             // Trace into
314
String JavaDoc str = getText (l);
315             if (str.startsWith ("import ")) {
316                 str = str.substring (7, str.length () - 1); //.replace ('.', File.separatorChar);
317
Line ll = Utils.getLine (str, 1);
318                 if (ll != null)
319                     if (!isOnStack.contains (ll.getDataObject ())) {
320                         stack.push (ll);
321                         isOnStack.add (ll.getDataObject ());
322                         return ll;
323                     }
324             }
325         } catch (Exception JavaDoc e) {
326         }
327             
328         stack.pop ();
329
330         // trace over
331
try {
332             if (l.getLineNumber () < 50) {
333                 Line ll = Utils.getLine (
334                     l.getDataObject ().getPrimaryFile ().getPackageName ('.'),
335                     l.getLineNumber () + 2
336                 );
337                 if (ll != null) {
338                     stack.push (ll);
339                     return ll;
340                 }
341             }
342         } catch (Exception JavaDoc e) {
343         }
344
345         // Step out
346
if (stack.empty ()) return null;
347         Line ll = (Line) stack.pop ();
348         if (ll.getLineNumber () < 50)
349             try {
350                 ll = Utils.getLine (ll.getDataObject ().getPrimaryFile ().
351                     getPackageName ('.'), ll.getLineNumber () + 2
352                 );
353             } catch (Exception JavaDoc e) {
354             }
355         stack.push (ll);
356         return ll;
357     }
358     
359     static String JavaDoc getText (Line l) throws Exception JavaDoc {
360         EditorCookie ec = (EditorCookie) l.getDataObject ().
361             getCookie (EditorCookie.class);
362         StyledDocument JavaDoc doc = ec.openDocument ();
363         if (doc == null) return "";
364         int off = NbDocument.findLineOffset (doc, l.getLineNumber ());
365         int len = NbDocument.findLineOffset (doc, l.getLineNumber () + 1) -
366             off - 1;
367         return doc.getText (off, len);
368     }
369 }
370
Popular Tags