J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. |
| 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(); |
| 134 | List all = manager.allConnectors(); |
| 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)); |
| 140 | for (Iterator it = all.iterator(); it.hasNext(); ) { |
| 141 | Connector connector = (Connector)it.next(); |
| 142 | JRadioButton radio = new JRadioButton(connector.description()); |
| 143 | modelToConnector.put(radio.getModel(), connector); |
| 144 | radioPanel.add(radio); |
| 145 | radioGroup.add(radio); |
| 146 | } |
| 147 | content.add(radioPanel); |
| 148 | |
| 149 | final boolean[] oked = {false}; |
| 150 | JPanel buttonPanel = okCancel( dialog, new ActionListener() { |
| 151 | public void actionPerformed(ActionEvent event) { |
| 152 | if (radioGroup.getSelection() == null) { |
| 153 | JOptionPane.showMessageDialog(dialog, |
| 154 | "Please select a connector type", |
| 155 | "No Selection", |
| 156 | JOptionPane.ERROR_MESSAGE); |
| 157 | } else { |
| 158 | oked[0] = true; |
| 159 | dialog.setVisible(false); |
| 160 | dialog.dispose(); |
| 161 | } |
| 162 | } |
| 163 | } ); |
| 164 | content.add(BorderLayout.SOUTH, buttonPanel); |
| 165 | dialog.pack(); |
| 166 | dialog.show(); |
| 167 | |
| 168 | return oked[0] ? |
| 169 | (Connector)(modelToConnector.get(radioGroup.getSelection())) : |
| 170 | null; |
| 171 | } |
| 172 | |
| 173 | private void configureAndConnect(final Connector connector) { |
| 174 | final JDialog dialog = new JDialog(); |
| 175 | final Map<String, Connector.Argument> args = connector.defaultArguments(); |
| 176 | |
| 177 | dialog.setModal(true); |
| 178 | dialog.setTitle("Connector Arguments"); |
| 179 | Container content = dialog.getContentPane(); |
| 180 | JPanel guts = new JPanel(); |
| 181 | Border etched = BorderFactory.createEtchedBorder(); |
| 182 | Border titled = BorderFactory.createTitledBorder(etched, |
| 183 | connector.description(), |
| 184 | TitledBorder.LEFT, TitledBorder.TOP); |
| 185 | guts.setBorder(etched); |
| 186 | guts.setLayout(new BoxLayout(guts, BoxLayout.Y_AXIS)); |
| 187 | |
| 188 | // guts.add(new JLabel(connector.description())); |
| 189 | |
| 190 | final List<ArgRep> argReps = new ArrayList<ArgRep>(args.size()); |
| 191 | for (Iterator it = args.values().iterator(); it.hasNext(); ) { |
| 192 | Object arg = it.next(); |
| 193 | ArgRep ar; |
| 194 | if (arg instanceof Connector.BooleanArgument) { |
| 195 | ar = new BooleanArgRep((Connector.BooleanArgument)arg, guts); |
| 196 | } else { |
| 197 | ar = new StringArgRep((Connector.Argument)arg, guts); |
| 198 | } |
| 199 | argReps.add(ar); |
| 200 | } |
| 201 | content.add(guts); |
| 202 | |
| 203 | JPanel buttonPanel = okCancel( dialog, new ActionListener() { |
| 204 | public void actionPerformed(ActionEvent event) { |
| 205 | for (Iterator it = argReps.iterator(); it.hasNext(); ) { |
| 206 | ArgRep ar = (ArgRep)it.next(); |
| 207 | if (!ar.isSpecified()) { |
| 208 | JOptionPane.showMessageDialog(dialog, |
| 209 | ar.arg.label() + |
| 210 | ": Argument must be specified", |
| 211 | "No argument", JOptionPane.ERROR_MESSAGE); |
| 212 | return; |
| 213 | } |
| 214 | if (!ar.isValid()) { |
| 215 | JOptionPane.showMessageDialog(dialog, |
| 216 | ar.arg.label() + |
| 217 | ": Bad argument value: " + |
| 218 | ar.getText(), |
| 219 | "Bad argument", JOptionPane.ERROR_MESSAGE); |
| 220 | return; |
| 221 | } |
| 222 | ar.install(); |
| 223 | } |
| 224 | try { |
| 225 | if (runtime.explictStart(connector, args)) { |
| 226 | dialog.setVisible(false); |
| 227 | dialog.dispose(); |
| 228 | } else { |
| 229 | JOptionPane.showMessageDialog(dialog, |
| 230 | "Bad arguments values: See diagnostics window.", |
| 231 | "Bad arguments", JOptionPane.ERROR_MESSAGE); |
| 232 | } |
| 233 | } catch (VMLaunchFailureException exc) { |
| 234 | JOptionPane.showMessageDialog(dialog, |
| 235 | "Launch Failure: " + exc, |
| 236 | "Launch Failed",JOptionPane.ERROR_MESSAGE); |
| 237 | } |
| 238 | } |
| 239 | } ); |
| 240 | content.add(BorderLayout.SOUTH, buttonPanel); |
| 241 | dialog.pack(); |
| 242 | dialog.show(); |
| 243 | } |
| 244 | |
| 245 | private JPanel okCancel(final JDialog dialog, ActionListener okListener) { |
| 246 | JPanel buttonPanel = new JPanel(); |
| 247 | JButton ok = new JButton("OK"); |
| 248 | JButton cancel = new JButton("Cancel"); |
| 249 | buttonPanel.add(ok); |
| 250 | buttonPanel.add(cancel); |
| 251 | ok.addActionListener(okListener); |
| 252 | cancel.addActionListener( new ActionListener() { |
| 253 | public void actionPerformed(ActionEvent event) { |
| 254 | dialog.setVisible(false); |
| 255 | dialog.dispose(); |
| 256 | } |
| 257 | } ); |
| 258 | return buttonPanel; |
| 259 | } |
| 260 | |
| 261 | static void queryAndLaunchVM(ExecutionManager runtime) |
| 262 | throws VMLaunchFailureException { |
| 263 | LaunchTool lt = new LaunchTool(runtime); |
| 264 | Connector connector = lt.selectConnector(); |
| 265 | if (connector != null) { |
| 266 | lt.configureAndConnect(connector); |
| 267 | } |
| 268 | } |
| 269 | } |