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