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