KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdb > Main


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger 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
23 package org.aspectj.tools.ajdb;
24
25 import org.aspectj.debugger.gui.ComponentDirector;
26 import org.aspectj.debugger.tty.CommandLineDebugger;
27 import org.aspectj.debugger.ide.FullPathSourceShower;
28 // import org.aspectj.debugger.ide.ForteDebuggerFrame;
29
// import org.aspectj.debugger.ide.JBuilder4DebuggerFrame;
30
import org.aspectj.debugger.ide.SourceShower;
31 import org.aspectj.debugger.ide.IDEInterface;
32 import org.aspectj.debugger.ide.IDEInterfaceImpl;
33
34 import org.aspectj.util.gui.CenteredJFrame;
35
36 import java.awt.Container JavaDoc;
37 import java.awt.Component JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import javax.swing.*;
40
41 import java.util.Collection JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Vector JavaDoc;
44
45 public class Main {
46     public static void main(String JavaDoc[] args) {
47         new Main().debug(args);
48     }
49
50     public Main() {}
51
52     /**
53      * All we're doing is launching a debugger, if the debugger is
54      * a command line version, the return value will be non-null,
55      * otherwise, it'll be null.
56      *
57      * This is basically a hack. We started with a command line
58      * debugger, then a GUI one, then IDE debuggers, so that
59      * is from where this comes.
60      *
61      */

62     public CommandLineDebugger debug(String JavaDoc[] args) {
63
64         // The return value
65
CommandLineDebugger debugger = null;
66
67         // These are set to denote the kind
68
// of debugger we're using
69
boolean launch = false;
70         boolean gui = false;
71         boolean forte = false;
72         boolean jbuilder = false;
73
74         // In the commandline version and the GUI version
75
// we want to exit with the window closing, etc...
76
// we don't want this in the IDE versions
77
boolean wantsToExit = true;
78
79         // The link between the debugger and a source pane
80
SourceShower shower = FullPathSourceShower.proto;
81
82         // The link between the debugger and an IDE
83
IDEInterface ide = IDEInterfaceImpl.proto;
84
85         // Check out the arguments, pull out any that
86
// are not valid for the command line debugger
87
for (int i = 0; i < args.length; i++) {
88             if (args[i].equals("-noexit")) {
89                 wantsToExit = false;
90                 System.setProperty("no.logger", "true");
91                 args = shift(args, i--);
92             } else if (args[i].equals("-launch")) {
93                 launch = true;
94                 args = shift(args, i--);
95             } else if (args[i].equals("-gui")) {
96                 gui = true;
97                 args = shift(args, i--);
98             } else if (args[i].equals("-forte")) {
99                 forte = true;
100                 args = shift(args, i--);
101             } else if (args[i].equals("-jbuilder")) {
102                 jbuilder = true;
103                 args = shift(args, i--);
104             } else if (args[i].equals("-ide")) {
105                 args = shift(args, i);
106                 try {
107                     ide = (IDEInterface)Class.forName(args[i]).newInstance();
108                 } catch (Throwable JavaDoc t) { t.printStackTrace(); }
109                 args = shift(args, i);
110             } else if (args[i].equals("-shower")) {
111                 args = shift(args, i);
112                 try {
113                     shower = (SourceShower)Class.forName(args[i]).newInstance();
114                 } catch (Throwable JavaDoc t) { t.printStackTrace(); }
115                 args = shift(args, i);
116             }
117         }
118
119 // // Launch the forte or jbuilder version
120
// if (launch) {
121
// if (forte) new ForteLauncher(args, shower, ide).go();
122
// if (jbuilder) new JBuilderLauncher(args, shower, ide).go();
123
// return null;
124
// }
125

126 // // Launch forte
127
// if (forte) {
128
// ForteDebuggerFrame director =
129
// new ForteDebuggerFrame(args, shower, ide);
130
// if (ide != null) {
131
// ide.setComponentDirector(director);
132
// }
133
// director.go();
134
// return null;
135
// }
136

137 // // Launch jbuilder
138
// if (jbuilder) {
139
// JBuilder4DebuggerFrame director =
140
// new JBuilder4DebuggerFrame(args, shower, ide);
141
// if (ide != null) {
142
// ide.setComponentDirector(director);
143
// }
144
// director.go();
145
// return null;
146
// }
147
// GUI
148
if (gui) {
149             new ComponentDirector(args).go();
150         }
151         debugger = new CommandLineDebugger(args);
152         if (!gui) {
153             debugger.setWantsToExit(wantsToExit);
154         }
155
156         // Start up the debugger...
157
debugger.go();
158         return debugger;
159     }
160
161     /**
162      * Take out unwanted parameters
163      */

164     private String JavaDoc[] shift(String JavaDoc[] args, int i) {
165         String JavaDoc[] newArgs = new String JavaDoc[args.length - 1];
166         System.arraycopy(args, 0, newArgs, 0, i);
167         System.arraycopy(args, i+1, newArgs, i, args.length-i-1);
168         return newArgs;
169     }
170
171 // private class JBuilderLauncher extends AbstractLauncher {
172
// public JBuilderLauncher(String[] args,
173
// SourceShower shower,
174
// IDEInterface ide) {
175
// super(args, shower, ide);
176
// }
177
// protected ComponentDirector debugger(String[] args) {
178
// return new JBuilder4DebuggerFrame(args , shower(), ide);
179
// }
180
// protected String getIDEName() {
181
// return "JBuilder";
182
// }
183
// }
184

185 // private class ForteLauncher extends AbstractLauncher {
186
// public ForteLauncher(String[] args,
187
// SourceShower shower,
188
// IDEInterface ide) {
189
// super(args, shower, ide);
190
// }
191
// protected ComponentDirector debugger(String[] args) {
192
// return new ForteDebuggerFrame(args, shower(), ide);
193
// }
194
// protected String getIDEName() {
195
// return "Forte";
196
// }
197
// }
198

199     private abstract class AbstractLauncher extends CenteredJFrame {
200         private final static int TEXT_WIDTH = 30;
201         private JTextField mainClassField = new JTextField(TEXT_WIDTH);
202         private JTextField classpathField = new JTextField(TEXT_WIDTH);
203         private JTextField sourcepathField = new JTextField(TEXT_WIDTH);
204         protected SourceShower shower;
205         protected IDEInterface ide;
206         protected SourceShower shower() { return shower; }
207         {
208             mainClassField.setText("New");
209             sourcepathField.setText("C:/ajdb-src");
210         }
211         protected String JavaDoc[]args = null;
212         private JButton launchButton =
213             new JButton(new AbstractAction("Launch debugger") {
214                 public void actionPerformed(ActionEvent JavaDoc e) {
215                     launch();
216                     setVisible(false);
217                     dispose();
218                 }
219             });
220         private void launch() {
221             Collection JavaDoc list = new Vector JavaDoc();
222             for (int i = 0; i < args.length; i++) {
223                 list.add(args[i]);
224             }
225             add(list, "-classpath", classpathField);
226             add(list, "-sourcepath", sourcepathField);
227             add(list, "-now", mainClassField);
228             System.out.println("args: " + list);
229             String JavaDoc[] newArgs = new String JavaDoc[list.size()];
230             Iterator JavaDoc iter = list.iterator();
231             int i = 0;
232             while (iter.hasNext()) {
233                 newArgs[i++] = iter.next() + "";
234             }
235             ComponentDirector cd = debugger(newArgs);
236             cd.go();
237         }
238         private void add(Collection JavaDoc list, String JavaDoc arg, JTextField field) {
239             String JavaDoc text = field.getText().trim();
240             if (text == null || text.length() == 0) return;
241             if (arg.length() > 0) {
242                 list.add(arg);
243                 list.add(text);
244             } else {
245                 list.add(text);
246             }
247         }
248         protected abstract ComponentDirector debugger(String JavaDoc[] args);
249         protected abstract String JavaDoc getIDEName();
250         AbstractLauncher(String JavaDoc[] args, SourceShower shower, IDEInterface ide) {
251             super();
252             setTitle(getIDEName() + " Debugger Launcher");
253             this.args = args;
254             this.shower = shower;
255             this.ide = ide;
256             Container JavaDoc c = getContentPane();
257             c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
258             c.add(p("Main class", mainClassField));
259             c.add(p("Class path", classpathField));
260             c.add(p("Source path", sourcepathField));
261             c.add(launchButton);
262             launchButton.requestFocus();
263             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
264         }
265         void go() {
266             pack();
267             setVisible(true);
268         }
269         private Component JavaDoc p(String JavaDoc title, Component JavaDoc c2) {
270             JPanel p = new JPanel();
271             p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
272             p.add(new JLabel(title + ": "));
273             p.add(c2);
274             return p;
275         }
276         
277     }
278 }
279
Popular Tags