make/tools/anttasks/SelectToolTask.java

Tue, 19 Feb 2013 17:53:16 +0000

author
vromero
date
Tue, 19 Feb 2013 17:53:16 +0000
changeset 1591
dc8b7aa7cef3
parent 1305
9d47f4850714
child 2021
d87f017ec217
permissions
-rw-r--r--

8006212: javac, convert jtreg tests from shell script to java
Reviewed-by: jjg

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

mercurial