2
|
1 |
/*
|
715
|
2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.example.debug.gui;
|
|
27 |
|
|
28 |
import java.util.List;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.Map;
|
|
31 |
import java.util.HashMap;
|
|
32 |
import java.util.Iterator;
|
|
33 |
|
|
34 |
import java.io.IOException;
|
|
35 |
|
|
36 |
import java.awt.BorderLayout;
|
|
37 |
import java.awt.Color;
|
|
38 |
import java.awt.Container;
|
|
39 |
import java.awt.event.ActionEvent;
|
|
40 |
import java.awt.event.ActionListener;
|
|
41 |
import java.awt.event.WindowAdapter;
|
|
42 |
import java.awt.event.WindowEvent;
|
|
43 |
|
|
44 |
import javax.swing.*;
|
|
45 |
import javax.swing.border.Border;
|
|
46 |
import javax.swing.border.TitledBorder;
|
|
47 |
|
|
48 |
import com.sun.jdi.*;
|
|
49 |
import com.sun.jdi.connect.*;
|
|
50 |
|
|
51 |
import com.sun.tools.example.debug.bdi.*;
|
|
52 |
|
|
53 |
class LaunchTool {
|
|
54 |
|
|
55 |
private final ExecutionManager runtime;
|
|
56 |
|
|
57 |
private abstract class ArgRep {
|
|
58 |
final Connector.Argument arg;
|
|
59 |
final JPanel panel;
|
|
60 |
|
|
61 |
ArgRep(Connector.Argument arg) {
|
|
62 |
this.arg = arg;
|
|
63 |
panel = new JPanel();
|
|
64 |
Border etched = BorderFactory.createEtchedBorder();
|
|
65 |
Border titled = BorderFactory.createTitledBorder(etched,
|
|
66 |
arg.description(),
|
|
67 |
TitledBorder.LEFT, TitledBorder.TOP);
|
|
68 |
panel.setBorder(titled);
|
|
69 |
}
|
|
70 |
|
|
71 |
abstract String getText();
|
|
72 |
|
|
73 |
boolean isValid() {
|
|
74 |
return arg.isValid(getText());
|
|
75 |
}
|
|
76 |
|
|
77 |
boolean isSpecified() {
|
|
78 |
String value = getText();
|
|
79 |
return (value != null && value.length() > 0) ||
|
|
80 |
!arg.mustSpecify();
|
|
81 |
}
|
|
82 |
|
|
83 |
void install() {
|
|
84 |
arg.setValue(getText());
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
private class StringArgRep extends ArgRep {
|
|
89 |
final JTextField textField;
|
|
90 |
|
|
91 |
StringArgRep(Connector.Argument arg, JPanel comp) {
|
|
92 |
super(arg);
|
|
93 |
textField = new JTextField(arg.value(), 50 );
|
|
94 |
textField.setBorder(BorderFactory.createLoweredBevelBorder());
|
|
95 |
|
|
96 |
panel.add(new JLabel(arg.label(), SwingConstants.RIGHT));
|
|
97 |
panel.add(textField); // , BorderLayout.CENTER);
|
|
98 |
comp.add(panel);
|
|
99 |
}
|
|
100 |
|
|
101 |
String getText() {
|
|
102 |
return textField.getText();
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
private class BooleanArgRep extends ArgRep {
|
|
107 |
final JCheckBox check;
|
|
108 |
|
|
109 |
BooleanArgRep(Connector.BooleanArgument barg, JPanel comp) {
|
|
110 |
super(barg);
|
|
111 |
check = new JCheckBox(barg.label());
|
|
112 |
check.setSelected(barg.booleanValue());
|
|
113 |
panel.add(check);
|
|
114 |
comp.add(panel);
|
|
115 |
}
|
|
116 |
|
|
117 |
String getText() {
|
|
118 |
return ((Connector.BooleanArgument)arg)
|
|
119 |
.stringValueOf(check.getModel().isSelected());
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
|
|
124 |
private LaunchTool(ExecutionManager runtime) {
|
|
125 |
this.runtime = runtime;
|
|
126 |
}
|
|
127 |
|
|
128 |
private Connector selectConnector() {
|
|
129 |
final JDialog dialog = new JDialog();
|
|
130 |
Container content = dialog.getContentPane();
|
|
131 |
final JPanel radioPanel = new JPanel();
|
|
132 |
final ButtonGroup radioGroup = new ButtonGroup();
|
|
133 |
VirtualMachineManager manager = Bootstrap.virtualMachineManager();
|
51
|
134 |
List<Connector> all = manager.allConnectors();
|
2
|
135 |
Map<ButtonModel, Connector> modelToConnector = new HashMap<ButtonModel, Connector>(all.size(), 0.5f);
|
|
136 |
|
|
137 |
dialog.setModal(true);
|
|
138 |
dialog.setTitle("Select Connector Type");
|
|
139 |
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
|
51
|
140 |
for (Connector connector : all) {
|
2
|
141 |
JRadioButton radio = new JRadioButton(connector.description());
|
|
142 |
modelToConnector.put(radio.getModel(), connector);
|
|
143 |
radioPanel.add(radio);
|
|
144 |
radioGroup.add(radio);
|
|
145 |
}
|
|
146 |
content.add(radioPanel);
|
|
147 |
|
|
148 |
final boolean[] oked = {false};
|
|
149 |
JPanel buttonPanel = okCancel( dialog, new ActionListener() {
|
|
150 |
public void actionPerformed(ActionEvent event) {
|
|
151 |
if (radioGroup.getSelection() == null) {
|
|
152 |
JOptionPane.showMessageDialog(dialog,
|
|
153 |
"Please select a connector type",
|
|
154 |
"No Selection",
|
|
155 |
JOptionPane.ERROR_MESSAGE);
|
|
156 |
} else {
|
|
157 |
oked[0] = true;
|
|
158 |
dialog.setVisible(false);
|
|
159 |
dialog.dispose();
|
|
160 |
}
|
|
161 |
}
|
|
162 |
} );
|
|
163 |
content.add(BorderLayout.SOUTH, buttonPanel);
|
|
164 |
dialog.pack();
|
|
165 |
dialog.show();
|
|
166 |
|
|
167 |
return oked[0] ?
|
51
|
168 |
modelToConnector.get(radioGroup.getSelection()) :
|
2
|
169 |
null;
|
|
170 |
}
|
|
171 |
|
|
172 |
private void configureAndConnect(final Connector connector) {
|
|
173 |
final JDialog dialog = new JDialog();
|
|
174 |
final Map<String, Connector.Argument> args = connector.defaultArguments();
|
|
175 |
|
|
176 |
dialog.setModal(true);
|
|
177 |
dialog.setTitle("Connector Arguments");
|
|
178 |
Container content = dialog.getContentPane();
|
|
179 |
JPanel guts = new JPanel();
|
|
180 |
Border etched = BorderFactory.createEtchedBorder();
|
|
181 |
Border titled = BorderFactory.createTitledBorder(etched,
|
|
182 |
connector.description(),
|
|
183 |
TitledBorder.LEFT, TitledBorder.TOP);
|
|
184 |
guts.setBorder(etched);
|
|
185 |
guts.setLayout(new BoxLayout(guts, BoxLayout.Y_AXIS));
|
|
186 |
|
|
187 |
// guts.add(new JLabel(connector.description()));
|
|
188 |
|
|
189 |
final List<ArgRep> argReps = new ArrayList<ArgRep>(args.size());
|
51
|
190 |
for (Connector.Argument arg : args.values()) {
|
2
|
191 |
ArgRep ar;
|
|
192 |
if (arg instanceof Connector.BooleanArgument) {
|
|
193 |
ar = new BooleanArgRep((Connector.BooleanArgument)arg, guts);
|
|
194 |
} else {
|
51
|
195 |
ar = new StringArgRep(arg, guts);
|
2
|
196 |
}
|
|
197 |
argReps.add(ar);
|
|
198 |
}
|
|
199 |
content.add(guts);
|
|
200 |
|
|
201 |
JPanel buttonPanel = okCancel( dialog, new ActionListener() {
|
|
202 |
public void actionPerformed(ActionEvent event) {
|
51
|
203 |
for (ArgRep ar : argReps) {
|
2
|
204 |
if (!ar.isSpecified()) {
|
|
205 |
JOptionPane.showMessageDialog(dialog,
|
|
206 |
ar.arg.label() +
|
|
207 |
": Argument must be specified",
|
|
208 |
"No argument", JOptionPane.ERROR_MESSAGE);
|
|
209 |
return;
|
|
210 |
}
|
|
211 |
if (!ar.isValid()) {
|
|
212 |
JOptionPane.showMessageDialog(dialog,
|
|
213 |
ar.arg.label() +
|
|
214 |
": Bad argument value: " +
|
|
215 |
ar.getText(),
|
|
216 |
"Bad argument", JOptionPane.ERROR_MESSAGE);
|
|
217 |
return;
|
|
218 |
}
|
|
219 |
ar.install();
|
|
220 |
}
|
|
221 |
try {
|
|
222 |
if (runtime.explictStart(connector, args)) {
|
|
223 |
dialog.setVisible(false);
|
|
224 |
dialog.dispose();
|
|
225 |
} else {
|
|
226 |
JOptionPane.showMessageDialog(dialog,
|
|
227 |
"Bad arguments values: See diagnostics window.",
|
|
228 |
"Bad arguments", JOptionPane.ERROR_MESSAGE);
|
|
229 |
}
|
|
230 |
} catch (VMLaunchFailureException exc) {
|
|
231 |
JOptionPane.showMessageDialog(dialog,
|
|
232 |
"Launch Failure: " + exc,
|
|
233 |
"Launch Failed",JOptionPane.ERROR_MESSAGE);
|
|
234 |
}
|
|
235 |
}
|
|
236 |
} );
|
|
237 |
content.add(BorderLayout.SOUTH, buttonPanel);
|
|
238 |
dialog.pack();
|
|
239 |
dialog.show();
|
|
240 |
}
|
|
241 |
|
|
242 |
private JPanel okCancel(final JDialog dialog, ActionListener okListener) {
|
|
243 |
JPanel buttonPanel = new JPanel();
|
|
244 |
JButton ok = new JButton("OK");
|
|
245 |
JButton cancel = new JButton("Cancel");
|
|
246 |
buttonPanel.add(ok);
|
|
247 |
buttonPanel.add(cancel);
|
|
248 |
ok.addActionListener(okListener);
|
|
249 |
cancel.addActionListener( new ActionListener() {
|
|
250 |
public void actionPerformed(ActionEvent event) {
|
|
251 |
dialog.setVisible(false);
|
|
252 |
dialog.dispose();
|
|
253 |
}
|
|
254 |
} );
|
|
255 |
return buttonPanel;
|
|
256 |
}
|
|
257 |
|
|
258 |
static void queryAndLaunchVM(ExecutionManager runtime)
|
|
259 |
throws VMLaunchFailureException {
|
|
260 |
LaunchTool lt = new LaunchTool(runtime);
|
|
261 |
Connector connector = lt.selectConnector();
|
|
262 |
if (connector != null) {
|
|
263 |
lt.configureAndConnect(connector);
|
|
264 |
}
|
|
265 |
}
|
|
266 |
}
|