KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > ConnectFrame


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.gui;
25
26 import org.aspectj.util.gui.*;
27 import org.aspectj.debugger.base.Debug;
28 import org.aspectj.debugger.base.Util;
29 import org.aspectj.debugger.base.Modes;
30
31 import java.awt.*;
32 import java.awt.event.*;
33 import java.io.*;
34 import java.lang.reflect.*;
35 import java.util.*;
36 import java.util.List JavaDoc;
37 import javax.swing.*;
38
39 import com.sun.jdi.*;
40 import com.sun.jdi.connect.*;
41
42 public class ConnectFrame extends CenteredJDialog {
43
44     final boolean ALLOW_CONNECT_OPTIONS;
45
46     private GUIDebugger guid;
47
48     //XXX Changed true to false so we can see the progress in
49
//XXX the Forte main frame.
50
// --> and this screwed me over!!!!!
51
public ConnectFrame(GUIDebugger guid) {
52         super(null, "Please select a connector", true);//Modes.isJBuilder() ? false : true);
53
this.guid = guid;
54         ALLOW_CONNECT_OPTIONS =
55             guid.getDebugger().getOptions().isSet("allowconnections");
56     }
57
58     //XXX made static for JBuilder compiler
59
private static Connector current = null;
60     private VirtualMachine vm;
61     public void setVm(VirtualMachine vm) {
62         this.vm = vm;
63     }
64     public VirtualMachine getVm() {
65         return vm;
66     }
67     public VirtualMachine select(String JavaDoc vmArgs,
68                                  String JavaDoc className,
69                                  String JavaDoc commandLine) {
70         return ALLOW_CONNECT_OPTIONS ?
71             connectorOptions(vmArgs, className, commandLine) :
72             launchingConnector(vmArgs, className, commandLine);
73     }
74     private VirtualMachine launchingConnector(String JavaDoc vmArgs,
75                                               String JavaDoc className,
76                                               String JavaDoc commandLine) {
77         Iterator iter = Bootstrap.virtualMachineManager().
78             allConnectors().iterator();
79         while (iter.hasNext()) {
80             Connector con = (Connector)iter.next();
81             if (con instanceof LaunchingConnector) {
82                 setVm(new LaunchFrame(current = con).
83                     select(vmArgs, className, commandLine));
84                 return getVm();
85             }
86         }
87         return connectorOptions(vmArgs, className, commandLine);
88     }
89     private VirtualMachine connectorOptions(final String JavaDoc vmArgs,
90                                             final String JavaDoc className,
91                                             final String JavaDoc commandLine) {
92         Container c = getContentPane();
93         JPanel buttons = new JPanel();
94         buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
95         Iterator iter = Bootstrap.virtualMachineManager().
96             allConnectors().iterator();
97         final ButtonGroup group = new ButtonGroup();
98         boolean first = true;
99         final OKCancel okCancel = new OKCancel(this) {
100                 public void ok() {
101                     setVm(new LaunchFrame(current).
102                         select(vmArgs,
103                                className,
104                                commandLine));
105                 }
106             };
107         while (iter.hasNext()) {
108             final Connector con = (Connector)iter.next();
109             JRadioButton rb =
110                 new JRadioButton(new AbstractAction(con.description()) {
111                         public void actionPerformed(ActionEvent e) {
112                             current = con;
113                         }
114                     });
115             if (first) {
116                 rb.setSelected(true);
117                 current = con;
118             }
119             first = false;
120             group.add(rb);
121             buttons.add(rb);
122         }
123         c.add(buttons, BorderLayout.CENTER);
124         c.add(okCancel, BorderLayout.SOUTH);
125         show();
126         Debug.debug("vm="+vm);
127         return vm;
128     }
129
130     private class OKCancel extends JPanel implements ActionListener {
131         JDialog frame;
132         OKCancel(final JDialog frame) {
133             super();
134             this.frame = frame;
135             this.setLayout(new FlowLayout(FlowLayout.CENTER));
136             JButton ok = new JButton(new AbstractAction("OK") {
137                     public void actionPerformed(ActionEvent e) {
138                         realOk();
139                     }
140                 });
141             JButton cancel = new JButton(new AbstractAction("Cancel") {
142                     public void actionPerformed(ActionEvent e) {
143                         realCancel();
144                     }
145                 });
146             this.add(ok);
147             this.add(cancel);
148         }
149         public void ok() {}
150         public void cancel() {}
151         private void realOk() {
152             ok();
153             frame.dispose();
154             frame.setVisible(false);
155         }
156         private void realCancel() {
157             cancel();
158             frame.dispose();
159             frame.setVisible(false);
160         }
161         public void actionPerformed(ActionEvent e) {
162             realOk();
163         }
164     }
165
166     private class LaunchFrame extends CenteredJDialog {
167         Connector con;
168         VirtualMachine vm = null;
169         LaunchFrame(final Connector con) {
170             super(null,
171                   "Please select launch parameters for "
172                   + con.name(),
173                   true);//Modes.isJBuilder() ? false : true);
174
this.con = con;
175         }
176
177         VirtualMachine select(String JavaDoc vmArgs,
178                               String JavaDoc className,
179                               String JavaDoc commandLine) {
180             Debug.debug("vmArgs:" + vmArgs +
181                         " className:" + className +
182                         " commandLine:" + commandLine);
183             final Map args = con.defaultArguments();
184             final List JavaDoc options = new Vector();
185             JPanel p = new JPanel();
186             p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
187             okCancel = new OKCancel(this) {
188                     public void ok() {
189                         Iterator opts = options.iterator();
190                         while (opts.hasNext()) {
191                             ArgPanel ap = (ArgPanel)opts.next();
192                             Connector.Argument arg = ap.arg;
193                             arg.setValue(ap.text.getText().trim());
194                             Debug.debug(arg.name() + " --> " + arg.value());
195                         }
196                         if (con instanceof AttachingConnector) {
197                             try {
198                                 vm = ((AttachingConnector)con).attach(args);
199                             } catch (IOException ioe) {
200                             } catch (IllegalConnectorArgumentsException iae) {
201                             }
202                         } else if (con instanceof LaunchingConnector) {
203                             try {
204                                 vm = ((LaunchingConnector)con).launch(args);
205                             } catch (IOException ioe) {
206                             } catch (IllegalConnectorArgumentsException iae) {
207                             } catch (VMStartException vmse) {
208                             }
209                         } else if (con instanceof ListeningConnector) {
210                             try {
211                                 vm = ((ListeningConnector)con).accept(args);
212                             } catch (IOException ioe) {
213                             } catch (IllegalConnectorArgumentsException iae) {
214                             }
215                         }
216                     }
217                 };
218             Iterator iter = args.keySet().iterator();
219             while (iter.hasNext()) {
220                 final String JavaDoc name = iter.next() + "";
221                 final Connector.Argument arg = (Connector.Argument)args.get(name);
222                 if ("main".equalsIgnoreCase(name)) {
223                     String JavaDoc newMain
224                         = !Util.empty(className)
225                         ? className
226                         : guid.getClassNameToRun();
227                     if (newMain != null) {
228                         arg.setValue(newMain);
229                     }
230                 }
231                 if ("options".equalsIgnoreCase(name)) {
232                     String JavaDoc optionsStr = "";
233                     if (!Util.empty(commandLine)) {
234                         optionsStr += " " + commandLine;
235                     }
236                     String JavaDoc vmParameters =
237                         !Util.empty(vmArgs)
238                         ? vmArgs
239                         : guid.getVMParameters();
240                     boolean alreadyHasClasspath = false;
241                     if (!Util.empty(vmParameters)) {
242                         alreadyHasClasspath =
243                             vmParameters.indexOf("-classpath") != -1;
244                         optionsStr += " " + vmParameters;
245                     }
246                     String JavaDoc classpath = guid.getClasspath();
247                     if (!alreadyHasClasspath && !Util.empty(classpath)) {
248                         optionsStr += " -classpath \"" + classpath + "\"";
249                     }
250
251                     optionsStr = optionsStr.trim();
252                     arg.setValue(optionsStr);
253                 }
254                 ArgPanel argp = new ArgPanel(arg);
255                 options.add(argp);
256                 p.add(argp);
257             }
258             this.getContentPane().add(p, BorderLayout.CENTER);
259             this.getContentPane().add(okCancel, BorderLayout.SOUTH);
260             this.show();
261             return vm;
262         }
263
264         private OKCancel okCancel = null;
265         private class ArgPanel extends JPanel {
266             Connector.Argument arg;
267             JTextField text = new JTextField(ARG_TEXT_WIDTH);
268             ArgPanel(Connector.Argument arg) {
269                 super();
270                 this.arg = arg;
271                 this.setBorder(BorderFactory.
272                                createTitledBorder(arg.description()));
273                 this.add(new JLabel(arg.name() + ":"));
274                 String JavaDoc value = arg.value();
275                 text.setText(value);
276                 text.addActionListener(okCancel);
277                 this.add(text);
278
279             }
280         }
281     }
282
283     private static int ARG_TEXT_WIDTH = 40;
284     public static VirtualMachine vm(GUIDebugger guid,
285                                     String JavaDoc vmArgs,
286                                     String JavaDoc className,
287                                     String JavaDoc commandLine) {
288         return new ConnectFrame(guid).
289             select(vmArgs, className, commandLine);
290     }
291
292     public static void main(String JavaDoc[] args) {
293         System.out.println("my vm=" +
294                            new ConnectFrame(null).
295                                select(null,null,null));
296     }
297 }
298
299
Popular Tags