2009-01-23 19:23:10 +00:00
|
|
|
/*
|
2014-08-27 13:25:17 +00:00
|
|
|
* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
|
2009-01-23 19:23:10 +00:00
|
|
|
* 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
|
2010-05-25 22:54:51 +00:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2009-01-23 19:23:10 +00:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 22:54:51 +00:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2009-01-23 19:23:10 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 22:54:51 +00:00
|
|
|
* 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.
|
2009-01-23 19:23:10 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-07 12:11:27 +00:00
|
|
|
package anttasks;
|
|
|
|
|
2009-01-23 19:23:10 +00:00
|
|
|
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;
|
2013-09-10 15:47:40 +00:00
|
|
|
import java.util.EnumSet;
|
2009-01-23 19:23:10 +00:00
|
|
|
import java.util.Properties;
|
2014-08-27 13:25:17 +00:00
|
|
|
|
2009-01-23 19:23:10 +00:00
|
|
|
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;
|
2014-08-27 13:25:17 +00:00
|
|
|
|
2009-01-23 19:23:10 +00:00
|
|
|
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 {
|
2013-09-10 15:47:40 +00:00
|
|
|
|
|
|
|
enum ToolChoices {
|
|
|
|
NONE(""),
|
2014-11-07 17:22:36 +00:00
|
|
|
BOOSTRAP_JAVAC("bootstrap-javac", true) {
|
|
|
|
@Override
|
|
|
|
public ToolChoices baseTool() {
|
|
|
|
return JAVAC;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
JAVAC("javac") {
|
|
|
|
@Override
|
|
|
|
public ToolChoices asBootstrap() {
|
|
|
|
return BOOSTRAP_JAVAC;
|
|
|
|
}
|
|
|
|
},
|
2013-09-10 15:47:40 +00:00
|
|
|
JAVADOC("javadoc"),
|
|
|
|
JAVAH("javah"),
|
|
|
|
JAVAP("javap");
|
|
|
|
|
|
|
|
String toolName;
|
|
|
|
boolean bootstrap;
|
|
|
|
|
|
|
|
ToolChoices(String toolName) {
|
|
|
|
this(toolName, false);
|
|
|
|
}
|
|
|
|
|
2013-09-26 18:06:09 +00:00
|
|
|
ToolChoices(String toolName, boolean bootstrap) {
|
2013-09-10 15:47:40 +00:00
|
|
|
this.toolName = toolName;
|
2013-09-26 18:06:09 +00:00
|
|
|
this.bootstrap = bootstrap;
|
2013-09-10 15:47:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 17:22:36 +00:00
|
|
|
public ToolChoices asBootstrap() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ToolChoices baseTool() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-09-10 15:47:40 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return toolName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 19:23:10 +00:00
|
|
|
/**
|
|
|
|
* Set the location of the private properties file used to keep the retain
|
|
|
|
* user preferences for this repository.
|
2014-08-27 13:25:17 +00:00
|
|
|
* @param propertyFile the private properties file
|
2009-01-23 19:23:10 +00:00
|
|
|
*/
|
|
|
|
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.
|
2014-08-27 13:25:17 +00:00
|
|
|
* @param toolProperty the tool name property
|
2009-01-23 19:23:10 +00:00
|
|
|
*/
|
|
|
|
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.
|
2014-08-27 13:25:17 +00:00
|
|
|
* @param argsProperty the execution args property value
|
2009-01-23 19:23:10 +00:00
|
|
|
*/
|
|
|
|
public void setArgsProperty(String argsProperty) {
|
|
|
|
this.argsProperty = argsProperty;
|
|
|
|
}
|
|
|
|
|
2013-09-10 15:47:40 +00:00
|
|
|
/**
|
2014-08-27 13:25:17 +00:00
|
|
|
* Set the name of the property which will be used to bootstrap the
|
|
|
|
* selected tool, if any. The property will remain unset.
|
|
|
|
* @param bootstrapProperty
|
2013-09-10 15:47:40 +00:00
|
|
|
*/
|
|
|
|
public void setBootstrapProperty(String bootstrapProperty) {
|
|
|
|
this.bootstrapProperty = bootstrapProperty;
|
|
|
|
}
|
|
|
|
|
2009-01-23 19:23:10 +00:00
|
|
|
/**
|
|
|
|
* Specify whether or not to pop up a dialog if the user has not specified
|
|
|
|
* a default value for a property.
|
2014-08-27 13:25:17 +00:00
|
|
|
* @param askIfUnset a boolean flag indicating to prompt the user or not
|
2009-01-23 19:23:10 +00:00
|
|
|
*/
|
|
|
|
public void setAskIfUnset(boolean askIfUnset) {
|
|
|
|
this.askIfUnset = askIfUnset;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void execute() {
|
|
|
|
Project p = getProject();
|
|
|
|
|
|
|
|
Properties props = readProperties(propertyFile);
|
|
|
|
toolName = props.getProperty("tool.name");
|
2013-09-10 15:47:40 +00:00
|
|
|
toolBootstrap = props.getProperty("tool.bootstrap") != null;
|
2009-01-23 19:23:10 +00:00
|
|
|
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);
|
2013-09-10 15:47:40 +00:00
|
|
|
if (toolBootstrap)
|
|
|
|
p.setProperty(bootstrapProperty, "true");
|
2009-01-23 19:23:10 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-11-07 17:22:36 +00:00
|
|
|
ToolChoices tool = (ToolChoices)toolChoice.getSelectedItem();
|
|
|
|
|
|
|
|
toolName = tool.baseTool().toolName;
|
|
|
|
toolBootstrap = tool.bootstrap;
|
2009-01-23 19:23:10 +00:00
|
|
|
toolArgs = argsField.getText();
|
|
|
|
if (defaultCheck.isSelected()) {
|
|
|
|
if (toolName.equals("")) {
|
|
|
|
fileProps.remove("tool.name");
|
2013-09-10 15:47:40 +00:00
|
|
|
fileProps.remove("tool.bootstrap");
|
2009-01-23 19:23:10 +00:00
|
|
|
} else {
|
|
|
|
fileProps.put("tool.name", toolName);
|
2013-09-10 15:47:40 +00:00
|
|
|
if (toolBootstrap) {
|
|
|
|
fileProps.put("tool.bootstrap", "true");
|
|
|
|
} else {
|
|
|
|
fileProps.remove("tool.bootstrap");
|
|
|
|
}
|
2009-01-23 19:23:10 +00:00
|
|
|
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;
|
2013-09-10 15:47:40 +00:00
|
|
|
fc.gridwidth = GridBagConstraints.NONE;
|
2009-01-23 19:23:10 +00:00
|
|
|
fc.insets.bottom = 3;
|
|
|
|
|
2013-09-10 15:47:40 +00:00
|
|
|
JPanel toolPane = new JPanel(new GridBagLayout());
|
|
|
|
|
2009-01-23 19:23:10 +00:00
|
|
|
JLabel toolLabel = new JLabel("Tool:");
|
|
|
|
body.add(toolLabel, lc);
|
2013-09-10 15:47:40 +00:00
|
|
|
EnumSet<ToolChoices> toolChoices = toolProperty == null ?
|
|
|
|
EnumSet.allOf(ToolChoices.class) : EnumSet.range(ToolChoices.JAVAC, ToolChoices.JAVAP);
|
2014-08-27 13:25:17 +00:00
|
|
|
toolChoice = new JComboBox<>(toolChoices.toArray());
|
2014-11-07 17:22:36 +00:00
|
|
|
ToolChoices tool = toolName != null ? ToolChoices.valueOf(toolName.toUpperCase()) : null;
|
|
|
|
if (toolName != null) {
|
|
|
|
if (toolBootstrap)
|
|
|
|
tool = tool.asBootstrap();
|
|
|
|
toolChoice.setSelectedItem(tool);
|
|
|
|
}
|
2009-01-23 19:23:10 +00:00
|
|
|
toolChoice.addItemListener(new ItemListener() {
|
2014-08-27 13:25:17 +00:00
|
|
|
@Override
|
2009-01-23 19:23:10 +00:00
|
|
|
public void itemStateChanged(ItemEvent e) {
|
2014-11-07 17:22:36 +00:00
|
|
|
ToolChoices tool = (ToolChoices)e.getItem();
|
|
|
|
argsField.setText(getDefaultArgsForTool(props, tool));
|
2009-01-23 19:23:10 +00:00
|
|
|
if (toolProperty != null)
|
2014-11-07 17:22:36 +00:00
|
|
|
okButton.setEnabled(tool != ToolChoices.NONE);
|
2009-01-23 19:23:10 +00:00
|
|
|
}
|
|
|
|
});
|
2013-09-10 15:47:40 +00:00
|
|
|
fc.anchor = GridBagConstraints.EAST;
|
|
|
|
|
|
|
|
GridBagConstraints toolConstraint = new GridBagConstraints();
|
|
|
|
fc.anchor = GridBagConstraints.WEST;
|
|
|
|
|
|
|
|
toolPane.add(toolChoice, toolConstraint);
|
|
|
|
|
|
|
|
body.add(toolPane, fc);
|
2009-01-23 19:23:10 +00:00
|
|
|
|
2014-11-07 17:22:36 +00:00
|
|
|
argsField = new JTextField(getDefaultArgsForTool(props, tool), 40);
|
2009-01-23 19:23:10 +00:00
|
|
|
if (toolProperty == null || argsProperty != null) {
|
|
|
|
JLabel argsLabel = new JLabel("Args:");
|
|
|
|
body.add(argsLabel, lc);
|
|
|
|
body.add(argsField, fc);
|
|
|
|
argsField.addFocusListener(new FocusListener() {
|
2014-08-27 13:25:17 +00:00
|
|
|
@Override
|
2009-01-23 19:23:10 +00:00
|
|
|
public void focusGained(FocusEvent e) {
|
|
|
|
}
|
2014-08-27 13:25:17 +00:00
|
|
|
@Override
|
2009-01-23 19:23:10 +00:00
|
|
|
public void focusLost(FocusEvent e) {
|
2013-09-10 15:47:40 +00:00
|
|
|
String toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;
|
2009-01-23 19:23:10 +00:00
|
|
|
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(new ActionListener() {
|
2014-08-27 13:25:17 +00:00
|
|
|
@Override
|
2009-01-23 19:23:10 +00:00
|
|
|
public void actionPerformed(ActionEvent 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 17:22:36 +00:00
|
|
|
String getDefaultArgsForTool(Properties props, ToolChoices tool) {
|
|
|
|
if (tool == null)
|
|
|
|
return "";
|
|
|
|
String toolName = tool.baseTool().toolName;
|
|
|
|
return toolName.equals("") ? "" : props.getProperty(toolName + ".args", "");
|
2009-01-23 19:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ant task parameters
|
|
|
|
private boolean askIfUnset;
|
|
|
|
private String toolProperty;
|
2013-09-10 15:47:40 +00:00
|
|
|
private String bootstrapProperty;
|
2009-01-23 19:23:10 +00:00
|
|
|
private String argsProperty;
|
|
|
|
private File propertyFile;
|
|
|
|
|
|
|
|
// GUI components
|
2014-08-27 13:25:17 +00:00
|
|
|
private JComboBox<?> toolChoice;
|
2009-01-23 19:23:10 +00:00
|
|
|
private JTextField argsField;
|
|
|
|
private JCheckBox defaultCheck;
|
|
|
|
private JButton okButton;
|
|
|
|
|
|
|
|
// Result values for the client
|
|
|
|
private String toolName;
|
2013-09-10 15:47:40 +00:00
|
|
|
private boolean toolBootstrap;
|
2009-01-23 19:23:10 +00:00
|
|
|
private String toolArgs;
|
|
|
|
}
|