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