KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > ide > IDEInterfaceHelper


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24 package org.aspectj.debugger.ide;
25
26 import org.aspectj.debugger.base.*;
27 import org.aspectj.debugger.gui.*;
28 import org.aspectj.debugger.request.*;
29 import org.aspectj.tools.ide.SourceLine;
30 import java.util.List JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32
33 // For log
34
import javax.swing.*;
35
36 public abstract class IDEInterfaceHelper implements IDEInterface {
37
38     /* ----------------------------------------------------------------------
39      * Abstract methods
40      * ----------------------------------------------------------------------
41      */

42
43     public abstract String JavaDoc getClasspath();
44     public abstract String JavaDoc getVMParameters();
45     public abstract String JavaDoc getClassNameToRun();
46     public abstract String JavaDoc findClassName();
47     public abstract String JavaDoc findFileName();
48     public abstract List JavaDoc getInitialBreakpoints();
49
50     protected abstract void markLine(String JavaDoc className, List JavaDoc argumentTypeNames);
51     protected abstract void markLineWithClass(String JavaDoc className, int line);
52     protected abstract void markLine(String JavaDoc sourceName, int line);
53     protected abstract void unmarkLine(String JavaDoc sourceName, int line);
54
55     public /*static*/ void toggle(String JavaDoc sourceName, int lineNumber) {}
56
57
58     public static IDEInterfaceHelper singletonInstance = null;
59     //public abstract void toggle(String sourceName, int lineNumber);
60

61
62     protected abstract void handle(Throwable JavaDoc t);
63
64     /**
65      * @todo this should return a boolean or throw an exception on failure
66      */

67     public void toggleBreakpoint(String JavaDoc sourcePath, int lineNumber) {
68         this.ajdb().toggleBreakpoint(sourcePath, lineNumber);
69     }
70
71
72     /* ----------------------------------------------------------------------
73      * IDEInterface methods
74      * ----------------------------------------------------------------------
75      */

76
77     /**
78      * Does nothing by default.
79      *
80      * @see org.aspectj.debugger.ide.IDEInterface#exit
81      */

82     public void exit() {}
83
84     /**
85      * Does nothing by default.
86      *
87      * @see org.aspectj.debugger.ide.IDEInterface#init
88      */

89     public void init(IDEComponentDirector director) {}
90
91     /**
92      * @see org.aspectj.debugger.ide.IDEInterface#settingBreakpointsFromIDE
93      */

94     public void settingBreakpointsFromIDE() {
95         downSettingFromDebugger();
96     }
97
98     /**
99      * @see org.aspectj.debugger.ide.IDEInterface#doneSettingBreakpointsFromIDE
100      */

101     public void doneSettingBreakpointsFromIDE() {
102         upSettingFromDebugger();
103     }
104
105
106     /* ----------------------------------------------------------------------
107      * Utility members
108      * ----------------------------------------------------------------------
109      */

110
111     protected ComponentDirector director;
112     public void setComponentDirector(ComponentDirector director) {
113         this.director = director;
114         init(director);
115     }
116
117
118     /* ----------------------------------------------------------------------
119      * Utility methods
120      * ----------------------------------------------------------------------
121      */

122
123     public ComponentDirector getComponentDirector() {
124         return director;
125     }
126
127     public AJDebugger ajdb() {
128         GUIDebugger guid = guid();
129         return guid != null ? (AJDebugger)guid.getDebugger() : null;
130     }
131
132     public GUIDebugger guid() {
133         return director != null ? director.getGUIDebugger() : null;
134     }
135
136     public void init(ComponentDirector _director) {
137         director = _director;
138         director.getDebugger().addDebuggerListener(new RequestListener());
139     }
140
141     final class RequestListener implements DebuggerListener {
142         public void requestSetEvent(RequestEvent re) {
143             debug("requestSetEvent:" + re);
144             set(re);
145         }
146         public void requestClearEvent(RequestEvent re) {
147             debug("requestClearEvent:" + re);
148             clear(re);
149         }
150         public void requestDeferredEvent(RequestEvent re) {
151             debug("requestDeferredEvent:" + re);
152             set(re);
153         }
154         public void requestFailedEvent(RequestEvent re) {
155             debug("requestFailedEvent:" + re);
156             clear(re);
157         }
158         private void set(RequestEvent re) {
159             if (!downSettingFromDebugger()) return;
160             markLine(re);
161             upSettingFromDebugger();
162         }
163         private void clear(RequestEvent re) {
164             if (!downSettingFromDebugger()) return;
165             unmarkLine(re);
166             upSettingFromDebugger();
167         }
168     }
169
170     // Mutex stuff
171
private boolean settingFromIDE = false;
172     protected boolean downSettingFromIDE() {
173         return settingFromIDE ? false : (settingFromIDE = true);
174     }
175     protected void upSettingFromIDE() {
176         settingFromIDE = false;
177     }
178     private boolean settingFromDebugger = false;
179     protected boolean downSettingFromDebugger() {
180         return settingFromDebugger ? false : (settingFromDebugger = true);
181     }
182     protected void upSettingFromDebugger() {
183         settingFromDebugger = false;
184     }
185
186     protected final boolean breakpoint
187         (IDEInterface.AbstractSourceLineBreakpoint breakpoint,
188          boolean stopping) {
189         return breakpoint(breakpoint.getSourceName(),
190                           breakpoint.getLine(),
191                           stopping);
192     }
193
194     protected final boolean breakpoint(String JavaDoc sourceName,
195                                        int line,
196                                        boolean stopping) {
197         try {
198             if (!downSettingFromDebugger()) {
199                 return false;
200             }
201             if (stopping) {
202                 debug("stopping " + sourceName + ":" + line);
203                 director.getDebugger().stopOnCommand(sourceName, line);
204             } else {
205                 debug("clearing " + sourceName + ":" + line);
206                 director.getDebugger().clearOnCommand(sourceName, line);
207             }
208         } catch (Exception JavaDoc t) {
209             handle(t);
210         } finally {
211             upSettingFromDebugger();
212         }
213         return true;
214     }
215
216     private void unmarkLine(RequestEvent re) {
217         Request request = re.getRequest();
218
219         debug("request:" + request);
220
221         if (request == null) {
222             return;
223         }
224
225         String JavaDoc sourceName;
226         int line;
227
228         if (request instanceof BreakpointRequestAction) {
229             BreakpointRequestAction bra =
230                 (BreakpointRequestAction)request;
231             sourceName = bra.getRealSourceName();
232             line = bra.getRealLine();
233             if (ajdb().isAjcFile(sourceName)) {
234                 SourceLine sourceLine = ajdb().getSourceLine(bra);
235                 sourceName = ajdb().getFullSourcePath(bra.getRealSourceName());
236                 debug("*** unmarking.real:" + sourceName + ":" + line);
237                 debug("*** but sourceLine:" + sourceLine);
238                 debug("*** bra:" + bra + ",bra.class:" + bra.getClass());
239                 downSettingFromIDE();
240                 //unmarkLine(sourceName, line);
241
unmarkLine(sourceLine.filename, sourceLine.line);
242                 upSettingFromIDE();
243
244                 return;
245             }
246         }
247     }
248
249     public final void atomicMarkLine(String JavaDoc sourceFilename,
250                                      int lineNumber) {
251         downSettingFromIDE();
252         markLine(sourceFilename, lineNumber);
253         upSettingFromIDE();
254     }
255
256     private final void markLine(RequestEvent re) {
257         Request request = re.getRequest();
258
259         debug("request:" + request);
260
261         if (request == null) {
262             return;
263         }
264
265         String JavaDoc sourceName;
266         int line;
267
268         if (request instanceof BreakpointRequestAction) {
269             BreakpointRequestAction bra = (BreakpointRequestAction)request;
270             sourceName = bra.getRealSourceName();
271             debug("sourceName:" + sourceName);
272             line = bra.getRealLine();
273             if (ajdb().isAjcFile(sourceName)) {
274                 SourceLine sourceLine = ajdb().getSourceLine(bra);
275                 sourceName = ajdb().getFullSourcePath(bra.getRealSourceName());
276                 debug("*** marking sourceLine:" + sourceLine + " " +
277                       sourceName + ":" + line + "," +
278                       bra.getLine() + ",class:" +
279                       bra.getClass().getName());
280                 //markLine(sourceName, line);
281
downSettingFromIDE();
282                 markLine(sourceLine.filename, sourceLine.line);
283                 upSettingFromIDE();
284                 return;
285             }
286         }
287
288         if (request instanceof SourceLineBreakpointRequestAction) {
289             SourceLineBreakpointRequestAction ra =
290                 (SourceLineBreakpointRequestAction)request;
291             SourceLine sl = ajdb().getSourceLine(ra);
292             downSettingFromIDE();
293             markLine(sl.filename, sl.line);
294             upSettingFromIDE();
295         } else if (request instanceof ClassLineBreakpointRequestAction) {
296             ClassLineBreakpointRequestAction ra =
297                 (ClassLineBreakpointRequestAction)request;
298             String JavaDoc className = ra.getClassName();
299             line = ra.getLine();
300             downSettingFromIDE();
301             markLineWithClass(className, line);
302             upSettingFromIDE();
303         } else if (request instanceof ClassMethodBreakpointRequestAction) {
304             ClassMethodBreakpointRequestAction ra =
305                 (ClassMethodBreakpointRequestAction)request;
306             String JavaDoc methodProto = ra.getMethodProto();
307             List JavaDoc argNames = ra.getArgumentTypeNames();
308             String JavaDoc className = ra.getClassName();
309             downSettingFromIDE();
310             markLine(className, argNames);
311             upSettingFromIDE();
312         } else if (request instanceof BreakpointRequestAction) {
313             BreakpointRequestAction ra =
314                 (BreakpointRequestAction)request;
315             SourceLine sl = ajdb().getSourceLine(ra);
316             downSettingFromIDE();
317             markLine(sl.filename, sl.line);
318             upSettingFromIDE();
319         }
320
321         sourceName = "asdf";
322         line = -123;
323         if (request instanceof BreakpointRequestAction) {
324             BreakpointRequestAction bra =
325                 (BreakpointRequestAction)request;
326             sourceName = bra.getSourceName();
327             line = bra.getLine();
328         }
329         debug("sourceName:" + sourceName + " line:" + line);
330     }
331
332     public final static Log log = new Log();
333     public static void debug(Object JavaDoc msg) {
334         log.println(msg);
335     }
336     public static class Log {
337         private JTextArea area = new JTextArea(30,100);
338         private boolean ok = false;
339         public void up() {
340             if (!ok) return;
341             println("up");
342             area.setForeground(java.awt.Color.black);
343         }
344         public void down() {
345             if (!ok) return;
346             println("down");
347             area.setForeground(java.awt.Color.red.darker());
348         }
349         {
350             JFrame frame = new JFrame("IDE Logger");
351             if ((ok =
352                  System.getProperty("user.name").equals("palm") &&
353                  System.getProperty("no.logger") != null)) {
354                 frame.getContentPane().add(new JScrollPane(area));
355                 frame.pack();
356                 frame.setVisible(true);
357             }
358         }
359         public void println() {
360             println("");
361         }
362         public void println(Object JavaDoc msg) {
363             if (ok) area.append(msg + "\n");
364         }
365     }
366 }
367
Popular Tags