make/tools/anttasks/SelectToolTask.java

Mon, 16 Sep 2013 14:13:44 +0200

author
jlahoda
date
Mon, 16 Sep 2013 14:13:44 +0200
changeset 2028
4ce8148ffc4f
parent 2021
d87f017ec217
child 2064
13eba2e322e6
permissions
-rw-r--r--

8021112: Spurious unchecked warning reported by javac
6480588: No way to suppress deprecation warnings when implementing deprecated interface
Summary: Fixing DeferredLintHandler configuration, so lint warnings are reported with correct @SuppressWarnings settings
Reviewed-by: jjg, vromero

jjg@201 1 /*
mcimadamore@2021 2 * Copyright (c) 2008, 2013, 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;
mcimadamore@2021 46 import java.util.EnumSet;
jjg@201 47 import java.util.List;
jjg@201 48 import java.util.Properties;
jjg@201 49 import javax.swing.JButton;
jjg@201 50 import javax.swing.JCheckBox;
jjg@201 51 import javax.swing.JComboBox;
jjg@201 52 import javax.swing.JDialog;
jjg@201 53 import javax.swing.JLabel;
jjg@201 54 import javax.swing.JOptionPane;
jjg@201 55 import javax.swing.JPanel;
jjg@201 56 import javax.swing.JTextField;
jjg@201 57
jjg@201 58 import javax.swing.SwingUtilities;
jjg@201 59 import org.apache.tools.ant.BuildException;
jjg@201 60 import org.apache.tools.ant.Project;
jjg@201 61 import org.apache.tools.ant.Task;
jjg@201 62
jjg@201 63 /**
jjg@201 64 * Task to allow the user to control langtools tools built when using NetBeans.
jjg@201 65 *
jjg@201 66 * There are two primary modes.
jjg@201 67 * 1) Property mode. In this mode, property names are provided to get values
jjg@201 68 * that may be specified by the user, either directly in a GUI dialog, or
jjg@201 69 * read from a properties file. If the GUI dialog is invoked, values may
jjg@201 70 * optionally be set for future use.
jjg@201 71 * 2) Setup mode. In this mode, no property names are provided, and the GUI
jjg@201 72 * is invoked to allow the user to set or reset values for use in property mode.
jjg@201 73 */
jjg@201 74 public class SelectToolTask extends Task {
mcimadamore@2021 75
mcimadamore@2021 76 enum ToolChoices {
mcimadamore@2021 77 NONE(""),
mcimadamore@2021 78 JAVAC("javac"),
mcimadamore@2021 79 JAVADOC("javadoc"),
mcimadamore@2021 80 JAVAH("javah"),
mcimadamore@2021 81 JAVAP("javap");
mcimadamore@2021 82
mcimadamore@2021 83 String toolName;
mcimadamore@2021 84 boolean bootstrap;
mcimadamore@2021 85
mcimadamore@2021 86 ToolChoices(String toolName) {
mcimadamore@2021 87 this(toolName, false);
mcimadamore@2021 88 }
mcimadamore@2021 89
mcimadamore@2021 90 ToolChoices(String toolName, boolean boostrap) {
mcimadamore@2021 91 this.toolName = toolName;
mcimadamore@2021 92 }
mcimadamore@2021 93
mcimadamore@2021 94 @Override
mcimadamore@2021 95 public String toString() {
mcimadamore@2021 96 return toolName;
mcimadamore@2021 97 }
mcimadamore@2021 98 }
mcimadamore@2021 99
jjg@201 100 /**
jjg@201 101 * Set the location of the private properties file used to keep the retain
jjg@201 102 * user preferences for this repository.
jjg@201 103 */
jjg@201 104 public void setPropertyFile(File propertyFile) {
jjg@201 105 this.propertyFile = propertyFile;
jjg@201 106 }
jjg@201 107
jjg@201 108 /**
jjg@201 109 * Set the name of the property which will be set to the name of the
jjg@201 110 * selected tool, if any. If no tool is selected, the property will
jjg@201 111 * remain unset.
jjg@201 112 */
jjg@201 113 public void setToolProperty(String toolProperty) {
jjg@201 114 this.toolProperty = toolProperty;
jjg@201 115 }
jjg@201 116
jjg@201 117 /**
jjg@201 118 * Set the name of the property which will be set to the execution args of the
jjg@201 119 * selected tool, if any. The args default to an empty string.
jjg@201 120 */
jjg@201 121 public void setArgsProperty(String argsProperty) {
jjg@201 122 this.argsProperty = argsProperty;
jjg@201 123 }
jjg@201 124
jjg@201 125 /**
mcimadamore@2021 126 * Set the name of the property which will be set to the execution args of the
mcimadamore@2021 127 * selected tool, if any. The args default to an empty string.
mcimadamore@2021 128 */
mcimadamore@2021 129 public void setBootstrapProperty(String bootstrapProperty) {
mcimadamore@2021 130 this.bootstrapProperty = bootstrapProperty;
mcimadamore@2021 131 }
mcimadamore@2021 132
mcimadamore@2021 133 /**
jjg@201 134 * Specify whether or not to pop up a dialog if the user has not specified
jjg@201 135 * a default value for a property.
jjg@201 136 */
jjg@201 137 public void setAskIfUnset(boolean askIfUnset) {
jjg@201 138 this.askIfUnset = askIfUnset;
jjg@201 139 }
jjg@201 140
jjg@201 141 @Override
jjg@201 142 public void execute() {
jjg@201 143 Project p = getProject();
jjg@201 144
jjg@201 145 Properties props = readProperties(propertyFile);
jjg@201 146 toolName = props.getProperty("tool.name");
mcimadamore@2021 147 toolBootstrap = props.getProperty("tool.bootstrap") != null;
jjg@201 148 if (toolName != null) {
jjg@201 149 toolArgs = props.getProperty(toolName + ".args", "");
jjg@201 150 }
jjg@201 151
jjg@201 152 if (toolProperty == null ||
jjg@201 153 askIfUnset && (toolName == null
jjg@201 154 || (argsProperty != null && toolArgs == null))) {
jjg@201 155 showGUI(props);
jjg@201 156 }
jjg@201 157
jjg@201 158 // finally, return required values, if any
jjg@201 159 if (toolProperty != null && !(toolName == null || toolName.equals(""))) {
jjg@201 160 p.setProperty(toolProperty, toolName);
mcimadamore@2021 161 if (toolBootstrap)
mcimadamore@2021 162 p.setProperty(bootstrapProperty, "true");
jjg@201 163
jjg@201 164 if (argsProperty != null && toolArgs != null)
jjg@201 165 p.setProperty(argsProperty, toolArgs);
jjg@201 166 }
jjg@201 167 }
jjg@201 168
jjg@201 169 void showGUI(Properties fileProps) {
jjg@201 170 Properties guiProps = new Properties(fileProps);
jjg@201 171 JOptionPane p = createPane(guiProps);
jjg@201 172 p.createDialog("Select Tool").setVisible(true);
jjg@201 173
mcimadamore@2021 174 toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;
jjg@201 175 toolArgs = argsField.getText();
mcimadamore@2021 176 toolBootstrap = bootstrapCheckbox.isSelected();
jjg@201 177 if (defaultCheck.isSelected()) {
jjg@201 178 if (toolName.equals("")) {
jjg@201 179 fileProps.remove("tool.name");
mcimadamore@2021 180 fileProps.remove("tool.bootstrap");
jjg@201 181 } else {
jjg@201 182 fileProps.put("tool.name", toolName);
mcimadamore@2021 183 if (toolBootstrap) {
mcimadamore@2021 184 fileProps.put("tool.bootstrap", "true");
mcimadamore@2021 185 } else {
mcimadamore@2021 186 fileProps.remove("tool.bootstrap");
mcimadamore@2021 187 }
jjg@201 188 fileProps.put(toolName + ".args", toolArgs);
jjg@201 189 }
jjg@201 190 writeProperties(propertyFile, fileProps);
jjg@201 191 }
jjg@201 192 }
jjg@201 193
jjg@201 194 JOptionPane createPane(final Properties props) {
jjg@201 195 JPanel body = new JPanel(new GridBagLayout());
jjg@201 196 GridBagConstraints lc = new GridBagConstraints();
jjg@201 197 lc.insets.right = 10;
jjg@201 198 lc.insets.bottom = 3;
jjg@201 199 GridBagConstraints fc = new GridBagConstraints();
jjg@201 200 fc.gridx = 1;
mcimadamore@2021 201 fc.gridwidth = GridBagConstraints.NONE;
jjg@201 202 fc.insets.bottom = 3;
jjg@201 203
mcimadamore@2021 204 JPanel toolPane = new JPanel(new GridBagLayout());
mcimadamore@2021 205
jjg@201 206 JLabel toolLabel = new JLabel("Tool:");
jjg@201 207 body.add(toolLabel, lc);
mcimadamore@2021 208 EnumSet<ToolChoices> toolChoices = toolProperty == null ?
mcimadamore@2021 209 EnumSet.allOf(ToolChoices.class) : EnumSet.range(ToolChoices.JAVAC, ToolChoices.JAVAP);
mcimadamore@2021 210 toolChoice = new JComboBox(toolChoices.toArray());
jjg@201 211 if (toolName != null)
mcimadamore@2021 212 toolChoice.setSelectedItem(ToolChoices.valueOf(toolName.toUpperCase()));
jjg@201 213 toolChoice.addItemListener(new ItemListener() {
jjg@201 214 public void itemStateChanged(ItemEvent e) {
mcimadamore@2021 215 String tn = ((ToolChoices)e.getItem()).toolName;
jjg@201 216 argsField.setText(getDefaultArgsForTool(props, tn));
jjg@201 217 if (toolProperty != null)
jjg@201 218 okButton.setEnabled(!tn.equals(""));
jjg@201 219 }
jjg@201 220 });
mcimadamore@2021 221 GridBagConstraints checkConstraint = new GridBagConstraints();
mcimadamore@2021 222 fc.anchor = GridBagConstraints.EAST;
mcimadamore@2021 223
mcimadamore@2021 224 GridBagConstraints toolConstraint = new GridBagConstraints();
mcimadamore@2021 225 fc.anchor = GridBagConstraints.WEST;
mcimadamore@2021 226
mcimadamore@2021 227 toolPane.add(toolChoice, toolConstraint);
mcimadamore@2021 228 bootstrapCheckbox = new JCheckBox("bootstrap", toolBootstrap);
mcimadamore@2021 229 toolPane.add(bootstrapCheckbox, checkConstraint);
mcimadamore@2021 230
mcimadamore@2021 231 body.add(toolPane, fc);
jjg@201 232
jjg@201 233 argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40);
jjg@201 234 if (toolProperty == null || argsProperty != null) {
jjg@201 235 JLabel argsLabel = new JLabel("Args:");
jjg@201 236 body.add(argsLabel, lc);
jjg@201 237 body.add(argsField, fc);
jjg@201 238 argsField.addFocusListener(new FocusListener() {
jjg@201 239 public void focusGained(FocusEvent e) {
jjg@201 240 }
jjg@201 241 public void focusLost(FocusEvent e) {
mcimadamore@2021 242 String toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;
jjg@201 243 if (toolName.length() > 0)
jjg@201 244 props.put(toolName + ".args", argsField.getText());
jjg@201 245 }
jjg@201 246 });
jjg@201 247 }
jjg@201 248
jjg@201 249 defaultCheck = new JCheckBox("Set as default");
jjg@201 250 if (toolProperty == null)
jjg@201 251 defaultCheck.setSelected(true);
jjg@201 252 else
jjg@201 253 body.add(defaultCheck, fc);
jjg@201 254
jjg@201 255 final JOptionPane p = new JOptionPane(body);
jjg@201 256 okButton = new JButton("OK");
jjg@201 257 okButton.setEnabled(toolProperty == null || (toolName != null && !toolName.equals("")));
jjg@201 258 okButton.addActionListener(new ActionListener() {
jjg@201 259 public void actionPerformed(ActionEvent e) {
jjg@201 260 JDialog d = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, p);
jjg@201 261 d.setVisible(false);
jjg@201 262 }
jjg@201 263 });
jjg@201 264 p.setOptions(new Object[] { okButton });
jjg@201 265
jjg@201 266 return p;
jjg@201 267 }
jjg@201 268
jjg@201 269 Properties readProperties(File file) {
jjg@201 270 Properties p = new Properties();
jjg@201 271 if (file != null && file.exists()) {
jjg@201 272 Reader in = null;
jjg@201 273 try {
jjg@201 274 in = new BufferedReader(new FileReader(file));
jjg@201 275 p.load(in);
jjg@201 276 in.close();
jjg@201 277 } catch (IOException e) {
jjg@201 278 throw new BuildException("error reading property file", e);
jjg@201 279 } finally {
jjg@201 280 if (in != null) {
jjg@201 281 try {
jjg@201 282 in.close();
jjg@201 283 } catch (IOException e) {
jjg@201 284 throw new BuildException("cannot close property file", e);
jjg@201 285 }
jjg@201 286 }
jjg@201 287 }
jjg@201 288 }
jjg@201 289 return p;
jjg@201 290 }
jjg@201 291
jjg@201 292 void writeProperties(File file, Properties p) {
jjg@201 293 if (file != null) {
jjg@201 294 Writer out = null;
jjg@201 295 try {
jjg@201 296 File dir = file.getParentFile();
jjg@201 297 if (dir != null && !dir.exists())
jjg@201 298 dir.mkdirs();
jjg@201 299 out = new BufferedWriter(new FileWriter(file));
jjg@201 300 p.store(out, "langtools properties");
jjg@201 301 out.close();
jjg@201 302 } catch (IOException e) {
jjg@201 303 throw new BuildException("error writing property file", e);
jjg@201 304 } finally {
jjg@201 305 if (out != null) {
jjg@201 306 try {
jjg@201 307 out.close();
jjg@201 308 } catch (IOException e) {
jjg@201 309 throw new BuildException("cannot close property file", e);
jjg@201 310 }
jjg@201 311 }
jjg@201 312 }
jjg@201 313 }
jjg@201 314 }
jjg@201 315
jjg@201 316 String getDefaultArgsForTool(Properties props, String tn) {
jjg@201 317 return (tn == null || tn.equals("")) ? "" : props.getProperty(tn + ".args", "");
jjg@201 318 }
jjg@201 319
jjg@201 320 // Ant task parameters
jjg@201 321 private boolean askIfUnset;
jjg@201 322 private String toolProperty;
mcimadamore@2021 323 private String bootstrapProperty;
jjg@201 324 private String argsProperty;
jjg@201 325 private File propertyFile;
jjg@201 326
jjg@201 327 // GUI components
jjg@201 328 private JComboBox toolChoice;
mcimadamore@2021 329 private JCheckBox bootstrapCheckbox;
jjg@201 330 private JTextField argsField;
jjg@201 331 private JCheckBox defaultCheck;
jjg@201 332 private JButton okButton;
jjg@201 333
jjg@201 334 // Result values for the client
jjg@201 335 private String toolName;
mcimadamore@2021 336 private boolean toolBootstrap;
jjg@201 337 private String toolArgs;
jjg@201 338 }

mercurial