make/tools/anttasks/SelectToolTask.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package anttasks;
aoqi@0 27
aoqi@0 28 import java.awt.GridBagConstraints;
aoqi@0 29 import java.awt.GridBagLayout;
aoqi@0 30 import java.awt.event.ActionEvent;
aoqi@0 31 import java.awt.event.ActionListener;
aoqi@0 32 import java.awt.event.FocusEvent;
aoqi@0 33 import java.awt.event.FocusListener;
aoqi@0 34 import java.awt.event.ItemEvent;
aoqi@0 35 import java.awt.event.ItemListener;
aoqi@0 36 import java.io.BufferedReader;
aoqi@0 37 import java.io.BufferedWriter;
aoqi@0 38 import java.io.File;
aoqi@0 39 import java.io.FileReader;
aoqi@0 40 import java.io.FileWriter;
aoqi@0 41 import java.io.IOException;
aoqi@0 42 import java.io.Reader;
aoqi@0 43 import java.io.Writer;
aoqi@0 44 import java.util.ArrayList;
aoqi@0 45 import java.util.Arrays;
aoqi@0 46 import java.util.EnumSet;
aoqi@0 47 import java.util.List;
aoqi@0 48 import java.util.Properties;
aoqi@0 49 import javax.swing.JButton;
aoqi@0 50 import javax.swing.JCheckBox;
aoqi@0 51 import javax.swing.JComboBox;
aoqi@0 52 import javax.swing.JDialog;
aoqi@0 53 import javax.swing.JLabel;
aoqi@0 54 import javax.swing.JOptionPane;
aoqi@0 55 import javax.swing.JPanel;
aoqi@0 56 import javax.swing.JTextField;
aoqi@0 57
aoqi@0 58 import javax.swing.SwingUtilities;
aoqi@0 59 import org.apache.tools.ant.BuildException;
aoqi@0 60 import org.apache.tools.ant.Project;
aoqi@0 61 import org.apache.tools.ant.Task;
aoqi@0 62
aoqi@0 63 /**
aoqi@0 64 * Task to allow the user to control langtools tools built when using NetBeans.
aoqi@0 65 *
aoqi@0 66 * There are two primary modes.
aoqi@0 67 * 1) Property mode. In this mode, property names are provided to get values
aoqi@0 68 * that may be specified by the user, either directly in a GUI dialog, or
aoqi@0 69 * read from a properties file. If the GUI dialog is invoked, values may
aoqi@0 70 * optionally be set for future use.
aoqi@0 71 * 2) Setup mode. In this mode, no property names are provided, and the GUI
aoqi@0 72 * is invoked to allow the user to set or reset values for use in property mode.
aoqi@0 73 */
aoqi@0 74 public class SelectToolTask extends Task {
aoqi@0 75
aoqi@0 76 enum ToolChoices {
aoqi@0 77 NONE(""),
aoqi@0 78 JAVAC("javac"),
aoqi@0 79 JAVADOC("javadoc"),
aoqi@0 80 JAVAH("javah"),
aoqi@0 81 JAVAP("javap");
aoqi@0 82
aoqi@0 83 String toolName;
aoqi@0 84 boolean bootstrap;
aoqi@0 85
aoqi@0 86 ToolChoices(String toolName) {
aoqi@0 87 this(toolName, false);
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 ToolChoices(String toolName, boolean bootstrap) {
aoqi@0 91 this.toolName = toolName;
aoqi@0 92 this.bootstrap = bootstrap;
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 @Override
aoqi@0 96 public String toString() {
aoqi@0 97 return toolName;
aoqi@0 98 }
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 /**
aoqi@0 102 * Set the location of the private properties file used to keep the retain
aoqi@0 103 * user preferences for this repository.
aoqi@0 104 */
aoqi@0 105 public void setPropertyFile(File propertyFile) {
aoqi@0 106 this.propertyFile = propertyFile;
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 /**
aoqi@0 110 * Set the name of the property which will be set to the name of the
aoqi@0 111 * selected tool, if any. If no tool is selected, the property will
aoqi@0 112 * remain unset.
aoqi@0 113 */
aoqi@0 114 public void setToolProperty(String toolProperty) {
aoqi@0 115 this.toolProperty = toolProperty;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 /**
aoqi@0 119 * Set the name of the property which will be set to the execution args of the
aoqi@0 120 * selected tool, if any. The args default to an empty string.
aoqi@0 121 */
aoqi@0 122 public void setArgsProperty(String argsProperty) {
aoqi@0 123 this.argsProperty = argsProperty;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 /**
aoqi@0 127 * Set the name of the property which will be set to the execution args of the
aoqi@0 128 * selected tool, if any. The args default to an empty string.
aoqi@0 129 */
aoqi@0 130 public void setBootstrapProperty(String bootstrapProperty) {
aoqi@0 131 this.bootstrapProperty = bootstrapProperty;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 /**
aoqi@0 135 * Specify whether or not to pop up a dialog if the user has not specified
aoqi@0 136 * a default value for a property.
aoqi@0 137 */
aoqi@0 138 public void setAskIfUnset(boolean askIfUnset) {
aoqi@0 139 this.askIfUnset = askIfUnset;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 @Override
aoqi@0 143 public void execute() {
aoqi@0 144 Project p = getProject();
aoqi@0 145
aoqi@0 146 Properties props = readProperties(propertyFile);
aoqi@0 147 toolName = props.getProperty("tool.name");
aoqi@0 148 toolBootstrap = props.getProperty("tool.bootstrap") != null;
aoqi@0 149 if (toolName != null) {
aoqi@0 150 toolArgs = props.getProperty(toolName + ".args", "");
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 if (toolProperty == null ||
aoqi@0 154 askIfUnset && (toolName == null
aoqi@0 155 || (argsProperty != null && toolArgs == null))) {
aoqi@0 156 showGUI(props);
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 // finally, return required values, if any
aoqi@0 160 if (toolProperty != null && !(toolName == null || toolName.equals(""))) {
aoqi@0 161 p.setProperty(toolProperty, toolName);
aoqi@0 162 if (toolBootstrap)
aoqi@0 163 p.setProperty(bootstrapProperty, "true");
aoqi@0 164
aoqi@0 165 if (argsProperty != null && toolArgs != null)
aoqi@0 166 p.setProperty(argsProperty, toolArgs);
aoqi@0 167 }
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 void showGUI(Properties fileProps) {
aoqi@0 171 Properties guiProps = new Properties(fileProps);
aoqi@0 172 JOptionPane p = createPane(guiProps);
aoqi@0 173 p.createDialog("Select Tool").setVisible(true);
aoqi@0 174
aoqi@0 175 toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;
aoqi@0 176 toolArgs = argsField.getText();
aoqi@0 177 toolBootstrap = bootstrapCheckbox.isSelected();
aoqi@0 178 if (defaultCheck.isSelected()) {
aoqi@0 179 if (toolName.equals("")) {
aoqi@0 180 fileProps.remove("tool.name");
aoqi@0 181 fileProps.remove("tool.bootstrap");
aoqi@0 182 } else {
aoqi@0 183 fileProps.put("tool.name", toolName);
aoqi@0 184 if (toolBootstrap) {
aoqi@0 185 fileProps.put("tool.bootstrap", "true");
aoqi@0 186 } else {
aoqi@0 187 fileProps.remove("tool.bootstrap");
aoqi@0 188 }
aoqi@0 189 fileProps.put(toolName + ".args", toolArgs);
aoqi@0 190 }
aoqi@0 191 writeProperties(propertyFile, fileProps);
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 JOptionPane createPane(final Properties props) {
aoqi@0 196 JPanel body = new JPanel(new GridBagLayout());
aoqi@0 197 GridBagConstraints lc = new GridBagConstraints();
aoqi@0 198 lc.insets.right = 10;
aoqi@0 199 lc.insets.bottom = 3;
aoqi@0 200 GridBagConstraints fc = new GridBagConstraints();
aoqi@0 201 fc.gridx = 1;
aoqi@0 202 fc.gridwidth = GridBagConstraints.NONE;
aoqi@0 203 fc.insets.bottom = 3;
aoqi@0 204
aoqi@0 205 JPanel toolPane = new JPanel(new GridBagLayout());
aoqi@0 206
aoqi@0 207 JLabel toolLabel = new JLabel("Tool:");
aoqi@0 208 body.add(toolLabel, lc);
aoqi@0 209 EnumSet<ToolChoices> toolChoices = toolProperty == null ?
aoqi@0 210 EnumSet.allOf(ToolChoices.class) : EnumSet.range(ToolChoices.JAVAC, ToolChoices.JAVAP);
aoqi@0 211 toolChoice = new JComboBox(toolChoices.toArray());
aoqi@0 212 if (toolName != null)
aoqi@0 213 toolChoice.setSelectedItem(ToolChoices.valueOf(toolName.toUpperCase()));
aoqi@0 214 toolChoice.addItemListener(new ItemListener() {
aoqi@0 215 public void itemStateChanged(ItemEvent e) {
aoqi@0 216 String tn = ((ToolChoices)e.getItem()).toolName;
aoqi@0 217 argsField.setText(getDefaultArgsForTool(props, tn));
aoqi@0 218 if (toolProperty != null)
aoqi@0 219 okButton.setEnabled(!tn.equals(""));
aoqi@0 220 }
aoqi@0 221 });
aoqi@0 222 GridBagConstraints checkConstraint = new GridBagConstraints();
aoqi@0 223 fc.anchor = GridBagConstraints.EAST;
aoqi@0 224
aoqi@0 225 GridBagConstraints toolConstraint = new GridBagConstraints();
aoqi@0 226 fc.anchor = GridBagConstraints.WEST;
aoqi@0 227
aoqi@0 228 toolPane.add(toolChoice, toolConstraint);
aoqi@0 229 bootstrapCheckbox = new JCheckBox("bootstrap", toolBootstrap);
aoqi@0 230 toolPane.add(bootstrapCheckbox, checkConstraint);
aoqi@0 231
aoqi@0 232 body.add(toolPane, fc);
aoqi@0 233
aoqi@0 234 argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40);
aoqi@0 235 if (toolProperty == null || argsProperty != null) {
aoqi@0 236 JLabel argsLabel = new JLabel("Args:");
aoqi@0 237 body.add(argsLabel, lc);
aoqi@0 238 body.add(argsField, fc);
aoqi@0 239 argsField.addFocusListener(new FocusListener() {
aoqi@0 240 public void focusGained(FocusEvent e) {
aoqi@0 241 }
aoqi@0 242 public void focusLost(FocusEvent e) {
aoqi@0 243 String toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;
aoqi@0 244 if (toolName.length() > 0)
aoqi@0 245 props.put(toolName + ".args", argsField.getText());
aoqi@0 246 }
aoqi@0 247 });
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 defaultCheck = new JCheckBox("Set as default");
aoqi@0 251 if (toolProperty == null)
aoqi@0 252 defaultCheck.setSelected(true);
aoqi@0 253 else
aoqi@0 254 body.add(defaultCheck, fc);
aoqi@0 255
aoqi@0 256 final JOptionPane p = new JOptionPane(body);
aoqi@0 257 okButton = new JButton("OK");
aoqi@0 258 okButton.setEnabled(toolProperty == null || (toolName != null && !toolName.equals("")));
aoqi@0 259 okButton.addActionListener(new ActionListener() {
aoqi@0 260 public void actionPerformed(ActionEvent e) {
aoqi@0 261 JDialog d = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, p);
aoqi@0 262 d.setVisible(false);
aoqi@0 263 }
aoqi@0 264 });
aoqi@0 265 p.setOptions(new Object[] { okButton });
aoqi@0 266
aoqi@0 267 return p;
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 Properties readProperties(File file) {
aoqi@0 271 Properties p = new Properties();
aoqi@0 272 if (file != null && file.exists()) {
aoqi@0 273 Reader in = null;
aoqi@0 274 try {
aoqi@0 275 in = new BufferedReader(new FileReader(file));
aoqi@0 276 p.load(in);
aoqi@0 277 in.close();
aoqi@0 278 } catch (IOException e) {
aoqi@0 279 throw new BuildException("error reading property file", e);
aoqi@0 280 } finally {
aoqi@0 281 if (in != null) {
aoqi@0 282 try {
aoqi@0 283 in.close();
aoqi@0 284 } catch (IOException e) {
aoqi@0 285 throw new BuildException("cannot close property file", e);
aoqi@0 286 }
aoqi@0 287 }
aoqi@0 288 }
aoqi@0 289 }
aoqi@0 290 return p;
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 void writeProperties(File file, Properties p) {
aoqi@0 294 if (file != null) {
aoqi@0 295 Writer out = null;
aoqi@0 296 try {
aoqi@0 297 File dir = file.getParentFile();
aoqi@0 298 if (dir != null && !dir.exists())
aoqi@0 299 dir.mkdirs();
aoqi@0 300 out = new BufferedWriter(new FileWriter(file));
aoqi@0 301 p.store(out, "langtools properties");
aoqi@0 302 out.close();
aoqi@0 303 } catch (IOException e) {
aoqi@0 304 throw new BuildException("error writing property file", e);
aoqi@0 305 } finally {
aoqi@0 306 if (out != null) {
aoqi@0 307 try {
aoqi@0 308 out.close();
aoqi@0 309 } catch (IOException e) {
aoqi@0 310 throw new BuildException("cannot close property file", e);
aoqi@0 311 }
aoqi@0 312 }
aoqi@0 313 }
aoqi@0 314 }
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 String getDefaultArgsForTool(Properties props, String tn) {
aoqi@0 318 return (tn == null || tn.equals("")) ? "" : props.getProperty(tn + ".args", "");
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 // Ant task parameters
aoqi@0 322 private boolean askIfUnset;
aoqi@0 323 private String toolProperty;
aoqi@0 324 private String bootstrapProperty;
aoqi@0 325 private String argsProperty;
aoqi@0 326 private File propertyFile;
aoqi@0 327
aoqi@0 328 // GUI components
aoqi@0 329 private JComboBox toolChoice;
aoqi@0 330 private JCheckBox bootstrapCheckbox;
aoqi@0 331 private JTextField argsField;
aoqi@0 332 private JCheckBox defaultCheck;
aoqi@0 333 private JButton okButton;
aoqi@0 334
aoqi@0 335 // Result values for the client
aoqi@0 336 private String toolName;
aoqi@0 337 private boolean toolBootstrap;
aoqi@0 338 private String toolArgs;
aoqi@0 339 }

mercurial