Showing the changes to java/pkg/com/sun/tools/example/debug/gui/LaunchTool.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,263 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * 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.awt.BorderLayout; | |
| 33 | +import java.awt.Container; | |
| 34 | +import java.awt.event.ActionEvent; | |
| 35 | +import java.awt.event.ActionListener; | |
| 36 | +import javax.swing.*; | |
| 37 | +import javax.swing.border.Border; | |
| 38 | +import javax.swing.border.TitledBorder; | |
| 39 | + | |
| 40 | +import com.sun.jdi.*; | |
| 41 | +import com.sun.jdi.connect.*; | |
| 42 | + | |
| 43 | +import com.sun.tools.example.debug.bdi.*; | |
| 44 | + | |
| 45 | +class LaunchTool { | |
| 46 | + | |
| 47 | + private final ExecutionManager runtime; | |
| 48 | + | |
| 49 | + private abstract class ArgRep { | |
| 50 | + final Connector.Argument arg; | |
| 51 | + final JPanel panel; | |
| 52 | + | |
| 53 | + ArgRep(Connector.Argument arg) { | |
| 54 | + this.arg = arg; | |
| 55 | + panel = new JPanel(); | |
| 56 | + Border etched = BorderFactory.createEtchedBorder(); | |
| 57 | + Border titled = BorderFactory.createTitledBorder(etched, | |
| 58 | + arg.description(), | |
| 59 | + TitledBorder.LEFT, TitledBorder.TOP); | |
| 60 | + panel.setBorder(titled); | |
| 61 | + } | |
| 62 | + | |
| 63 | + abstract String getText(); | |
| 64 | + | |
| 65 | + boolean isValid() { | |
| 66 | + return arg.isValid(getText()); | |
| 67 | + } | |
| 68 | + | |
| 69 | + boolean isSpecified() { | |
| 70 | + String value = getText(); | |
| 71 | + return (value != null && value.length() > 0) || | |
| 72 | + !arg.mustSpecify(); | |
| 73 | + } | |
| 74 | + | |
| 75 | + void install() { | |
| 76 | + arg.setValue(getText()); | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + private class StringArgRep extends ArgRep { | |
| 81 | + final JTextField textField; | |
| 82 | + | |
| 83 | + StringArgRep(Connector.Argument arg, JPanel comp) { | |
| 84 | + super(arg); | |
| 85 | + textField = new JTextField(arg.value(), 50 ); | |
| 86 | + textField.setBorder(BorderFactory.createLoweredBevelBorder()); | |
| 87 | + | |
| 88 | + panel.add(new JLabel(arg.label(), SwingConstants.RIGHT)); | |
| 89 | + panel.add(textField); // , BorderLayout.CENTER); | |
| 90 | + comp.add(panel); | |
| 91 | + } | |
| 92 | + | |
| 93 | + @Override | |
| 94 | + String getText() { | |
| 95 | + return textField.getText(); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + private class BooleanArgRep extends ArgRep { | |
| 100 | + final JCheckBox check; | |
| 101 | + | |
| 102 | + BooleanArgRep(Connector.BooleanArgument barg, JPanel comp) { | |
| 103 | + super(barg); | |
| 104 | + check = new JCheckBox(barg.label()); | |
| 105 | + check.setSelected(barg.booleanValue()); | |
| 106 | + panel.add(check); | |
| 107 | + comp.add(panel); | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Override | |
| 111 | + String getText() { | |
| 112 | + return ((Connector.BooleanArgument)arg) | |
| 113 | + .stringValueOf(check.getModel().isSelected()); | |
| 114 | + } | |
| 115 | + } | |
| 116 | + | |
| 117 | + | |
| 118 | + private LaunchTool(ExecutionManager runtime) { | |
| 119 | + this.runtime = runtime; | |
| 120 | + } | |
| 121 | + | |
| 122 | + private Connector selectConnector() { | |
| 123 | + final JDialog dialog = new JDialog(); | |
| 124 | + Container content = dialog.getContentPane(); | |
| 125 | + final JPanel radioPanel = new JPanel(); | |
| 126 | + final ButtonGroup radioGroup = new ButtonGroup(); | |
| 127 | + VirtualMachineManager manager = Bootstrap.virtualMachineManager(); | |
| 128 | + List<Connector> all = manager.allConnectors(); | |
| 129 | + Map<ButtonModel, Connector> modelToConnector = new HashMap<ButtonModel, Connector>(all.size(), 0.5f); | |
| 130 | + | |
| 131 | + dialog.setModal(true); | |
| 132 | + dialog.setTitle("Select Connector Type"); | |
| 133 | + radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); | |
| 134 | + for (Connector connector : all) { | |
| 135 | + JRadioButton radio = new JRadioButton(connector.description()); | |
| 136 | + modelToConnector.put(radio.getModel(), connector); | |
| 137 | + radioPanel.add(radio); | |
| 138 | + radioGroup.add(radio); | |
| 139 | + } | |
| 140 | + content.add(radioPanel); | |
| 141 | + | |
| 142 | + final boolean[] oked = {false}; | |
| 143 | + JPanel buttonPanel = okCancel( dialog, new ActionListener() { | |
| 144 | + @Override | |
| 145 | + public void actionPerformed(ActionEvent event) { | |
| 146 | + if (radioGroup.getSelection() == null) { | |
| 147 | + JOptionPane.showMessageDialog(dialog, | |
| 148 | + "Please select a connector type", | |
| 149 | + "No Selection", | |
| 150 | + JOptionPane.ERROR_MESSAGE); | |
| 151 | + } else { | |
| 152 | + oked[0] = true; | |
| 153 | + dialog.setVisible(false); | |
| 154 | + dialog.dispose(); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + } ); | |
| 158 | + content.add(BorderLayout.SOUTH, buttonPanel); | |
| 159 | + dialog.pack(); | |
| 160 | + dialog.setVisible(true); | |
| 161 | + | |
| 162 | + return oked[0] ? | |
| 163 | + modelToConnector.get(radioGroup.getSelection()) : | |
| 164 | + null; | |
| 165 | + } | |
| 166 | + | |
| 167 | + private void configureAndConnect(final Connector connector) { | |
| 168 | + final JDialog dialog = new JDialog(); | |
| 169 | + final Map<String, Connector.Argument> args = connector.defaultArguments(); | |
| 170 | + | |
| 171 | + dialog.setModal(true); | |
| 172 | + dialog.setTitle("Connector Arguments"); | |
| 173 | + Container content = dialog.getContentPane(); | |
| 174 | + JPanel guts = new JPanel(); | |
| 175 | + Border etched = BorderFactory.createEtchedBorder(); | |
| 176 | + BorderFactory.createTitledBorder(etched, | |
| 177 | + connector.description(), | |
| 178 | + TitledBorder.LEFT, TitledBorder.TOP); | |
| 179 | + guts.setBorder(etched); | |
| 180 | + guts.setLayout(new BoxLayout(guts, BoxLayout.Y_AXIS)); | |
| 181 | + | |
| 182 | + // guts.add(new JLabel(connector.description())); | |
| 183 | + | |
| 184 | + final List<ArgRep> argReps = new ArrayList<ArgRep>(args.size()); | |
| 185 | + for (Connector.Argument arg : args.values()) { | |
| 186 | + ArgRep ar; | |
| 187 | + if (arg instanceof Connector.BooleanArgument) { | |
| 188 | + ar = new BooleanArgRep((Connector.BooleanArgument)arg, guts); | |
| 189 | + } else { | |
| 190 | + ar = new StringArgRep(arg, guts); | |
| 191 | + } | |
| 192 | + argReps.add(ar); | |
| 193 | + } | |
| 194 | + content.add(guts); | |
| 195 | + | |
| 196 | + JPanel buttonPanel = okCancel( dialog, new ActionListener() { | |
| 197 | + @Override | |
| 198 | + public void actionPerformed(ActionEvent event) { | |
| 199 | + for (ArgRep ar : argReps) { | |
| 200 | + if (!ar.isSpecified()) { | |
| 201 | + JOptionPane.showMessageDialog(dialog, | |
| 202 | + ar.arg.label() + | |
| 203 | + ": Argument must be specified", | |
| 204 | + "No argument", JOptionPane.ERROR_MESSAGE); | |
| 205 | + return; | |
| 206 | + } | |
| 207 | + if (!ar.isValid()) { | |
| 208 | + JOptionPane.showMessageDialog(dialog, | |
| 209 | + ar.arg.label() + | |
| 210 | + ": Bad argument value: " + | |
| 211 | + ar.getText(), | |
| 212 | + "Bad argument", JOptionPane.ERROR_MESSAGE); | |
| 213 | + return; | |
| 214 | + } | |
| 215 | + ar.install(); | |
| 216 | + } | |
| 217 | + try { | |
| 218 | + if (runtime.explictStart(connector, args)) { | |
| 219 | + dialog.setVisible(false); | |
| 220 | + dialog.dispose(); | |
| 221 | + } else { | |
| 222 | + JOptionPane.showMessageDialog(dialog, | |
| 223 | + "Bad arguments values: See diagnostics window.", | |
| 224 | + "Bad arguments", JOptionPane.ERROR_MESSAGE); | |
| 225 | + } | |
| 226 | + } catch (VMLaunchFailureException exc) { | |
| 227 | + JOptionPane.showMessageDialog(dialog, | |
| 228 | + "Launch Failure: " + exc, | |
| 229 | + "Launch Failed",JOptionPane.ERROR_MESSAGE); | |
| 230 | + } | |
| 231 | + } | |
| 232 | + } ); | |
| 233 | + content.add(BorderLayout.SOUTH, buttonPanel); | |
| 234 | + dialog.pack(); | |
| 235 | + dialog.setVisible(true); | |
| 236 | + } | |
| 237 | + | |
| 238 | + private JPanel okCancel(final JDialog dialog, ActionListener okListener) { | |
| 239 | + JPanel buttonPanel = new JPanel(); | |
| 240 | + JButton ok = new JButton("OK"); | |
| 241 | + JButton cancel = new JButton("Cancel"); | |
| 242 | + buttonPanel.add(ok); | |
| 243 | + buttonPanel.add(cancel); | |
| 244 | + ok.addActionListener(okListener); | |
| 245 | + cancel.addActionListener( new ActionListener() { | |
| 246 | + @Override | |
| 247 | + public void actionPerformed(ActionEvent event) { | |
| 248 | + dialog.setVisible(false); | |
| 249 | + dialog.dispose(); | |
| 250 | + } | |
| 251 | + } ); | |
| 252 | + return buttonPanel; | |
| 253 | + } | |
| 254 | + | |
| 255 | + static void queryAndLaunchVM(ExecutionManager runtime) | |
| 256 | + throws VMLaunchFailureException { | |
| 257 | + LaunchTool lt = new LaunchTool(runtime); | |
| 258 | + Connector connector = lt.selectConnector(); | |
| 259 | + if (connector != null) { | |
| 260 | + lt.configureAndConnect(connector); | |
| 261 | + } | |
| 262 | + } | |
| 263 | +} | |