Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/make/tools/anttasks/SelectToolTask.java
32285 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package anttasks;2627import java.awt.GridBagConstraints;28import java.awt.GridBagLayout;29import java.awt.event.ActionEvent;30import java.awt.event.ActionListener;31import java.awt.event.FocusEvent;32import java.awt.event.FocusListener;33import java.awt.event.ItemEvent;34import java.awt.event.ItemListener;35import java.io.BufferedReader;36import java.io.BufferedWriter;37import java.io.File;38import java.io.FileReader;39import java.io.FileWriter;40import java.io.IOException;41import java.io.Reader;42import java.io.Writer;43import java.util.ArrayList;44import java.util.Arrays;45import java.util.EnumSet;46import java.util.List;47import java.util.Properties;48import javax.swing.JButton;49import javax.swing.JCheckBox;50import javax.swing.JComboBox;51import javax.swing.JDialog;52import javax.swing.JLabel;53import javax.swing.JOptionPane;54import javax.swing.JPanel;55import javax.swing.JTextField;5657import javax.swing.SwingUtilities;58import org.apache.tools.ant.BuildException;59import org.apache.tools.ant.Project;60import org.apache.tools.ant.Task;6162/**63* Task to allow the user to control langtools tools built when using NetBeans.64*65* There are two primary modes.66* 1) Property mode. In this mode, property names are provided to get values67* that may be specified by the user, either directly in a GUI dialog, or68* read from a properties file. If the GUI dialog is invoked, values may69* optionally be set for future use.70* 2) Setup mode. In this mode, no property names are provided, and the GUI71* is invoked to allow the user to set or reset values for use in property mode.72*/73public class SelectToolTask extends Task {7475enum ToolChoices {76NONE(""),77JAVAC("javac"),78JAVADOC("javadoc"),79JAVAH("javah"),80JAVAP("javap");8182String toolName;83boolean bootstrap;8485ToolChoices(String toolName) {86this(toolName, false);87}8889ToolChoices(String toolName, boolean bootstrap) {90this.toolName = toolName;91this.bootstrap = bootstrap;92}9394@Override95public String toString() {96return toolName;97}98}99100/**101* Set the location of the private properties file used to keep the retain102* user preferences for this repository.103*/104public void setPropertyFile(File propertyFile) {105this.propertyFile = propertyFile;106}107108/**109* Set the name of the property which will be set to the name of the110* selected tool, if any. If no tool is selected, the property will111* remain unset.112*/113public void setToolProperty(String toolProperty) {114this.toolProperty = toolProperty;115}116117/**118* Set the name of the property which will be set to the execution args of the119* selected tool, if any. The args default to an empty string.120*/121public void setArgsProperty(String argsProperty) {122this.argsProperty = argsProperty;123}124125/**126* Set the name of the property which will be set to the execution args of the127* selected tool, if any. The args default to an empty string.128*/129public void setBootstrapProperty(String bootstrapProperty) {130this.bootstrapProperty = bootstrapProperty;131}132133/**134* Specify whether or not to pop up a dialog if the user has not specified135* a default value for a property.136*/137public void setAskIfUnset(boolean askIfUnset) {138this.askIfUnset = askIfUnset;139}140141@Override142public void execute() {143Project p = getProject();144145Properties props = readProperties(propertyFile);146toolName = props.getProperty("tool.name");147toolBootstrap = props.getProperty("tool.bootstrap") != null;148if (toolName != null) {149toolArgs = props.getProperty(toolName + ".args", "");150}151152if (toolProperty == null ||153askIfUnset && (toolName == null154|| (argsProperty != null && toolArgs == null))) {155showGUI(props);156}157158// finally, return required values, if any159if (toolProperty != null && !(toolName == null || toolName.equals(""))) {160p.setProperty(toolProperty, toolName);161if (toolBootstrap)162p.setProperty(bootstrapProperty, "true");163164if (argsProperty != null && toolArgs != null)165p.setProperty(argsProperty, toolArgs);166}167}168169void showGUI(Properties fileProps) {170Properties guiProps = new Properties(fileProps);171JOptionPane p = createPane(guiProps);172p.createDialog("Select Tool").setVisible(true);173174toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;175toolArgs = argsField.getText();176toolBootstrap = bootstrapCheckbox.isSelected();177if (defaultCheck.isSelected()) {178if (toolName.equals("")) {179fileProps.remove("tool.name");180fileProps.remove("tool.bootstrap");181} else {182fileProps.put("tool.name", toolName);183if (toolBootstrap) {184fileProps.put("tool.bootstrap", "true");185} else {186fileProps.remove("tool.bootstrap");187}188fileProps.put(toolName + ".args", toolArgs);189}190writeProperties(propertyFile, fileProps);191}192}193194JOptionPane createPane(final Properties props) {195JPanel body = new JPanel(new GridBagLayout());196GridBagConstraints lc = new GridBagConstraints();197lc.insets.right = 10;198lc.insets.bottom = 3;199GridBagConstraints fc = new GridBagConstraints();200fc.gridx = 1;201fc.gridwidth = GridBagConstraints.NONE;202fc.insets.bottom = 3;203204JPanel toolPane = new JPanel(new GridBagLayout());205206JLabel toolLabel = new JLabel("Tool:");207body.add(toolLabel, lc);208EnumSet<ToolChoices> toolChoices = toolProperty == null ?209EnumSet.allOf(ToolChoices.class) : EnumSet.range(ToolChoices.JAVAC, ToolChoices.JAVAP);210toolChoice = new JComboBox(toolChoices.toArray());211if (toolName != null)212toolChoice.setSelectedItem(ToolChoices.valueOf(toolName.toUpperCase()));213toolChoice.addItemListener(new ItemListener() {214public void itemStateChanged(ItemEvent e) {215String tn = ((ToolChoices)e.getItem()).toolName;216argsField.setText(getDefaultArgsForTool(props, tn));217if (toolProperty != null)218okButton.setEnabled(!tn.equals(""));219}220});221GridBagConstraints checkConstraint = new GridBagConstraints();222fc.anchor = GridBagConstraints.EAST;223224GridBagConstraints toolConstraint = new GridBagConstraints();225fc.anchor = GridBagConstraints.WEST;226227toolPane.add(toolChoice, toolConstraint);228bootstrapCheckbox = new JCheckBox("bootstrap", toolBootstrap);229toolPane.add(bootstrapCheckbox, checkConstraint);230231body.add(toolPane, fc);232233argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40);234if (toolProperty == null || argsProperty != null) {235JLabel argsLabel = new JLabel("Args:");236body.add(argsLabel, lc);237body.add(argsField, fc);238argsField.addFocusListener(new FocusListener() {239public void focusGained(FocusEvent e) {240}241public void focusLost(FocusEvent e) {242String toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;243if (toolName.length() > 0)244props.put(toolName + ".args", argsField.getText());245}246});247}248249defaultCheck = new JCheckBox("Set as default");250if (toolProperty == null)251defaultCheck.setSelected(true);252else253body.add(defaultCheck, fc);254255final JOptionPane p = new JOptionPane(body);256okButton = new JButton("OK");257okButton.setEnabled(toolProperty == null || (toolName != null && !toolName.equals("")));258okButton.addActionListener(new ActionListener() {259public void actionPerformed(ActionEvent e) {260JDialog d = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, p);261d.setVisible(false);262}263});264p.setOptions(new Object[] { okButton });265266return p;267}268269Properties readProperties(File file) {270Properties p = new Properties();271if (file != null && file.exists()) {272Reader in = null;273try {274in = new BufferedReader(new FileReader(file));275p.load(in);276in.close();277} catch (IOException e) {278throw new BuildException("error reading property file", e);279} finally {280if (in != null) {281try {282in.close();283} catch (IOException e) {284throw new BuildException("cannot close property file", e);285}286}287}288}289return p;290}291292void writeProperties(File file, Properties p) {293if (file != null) {294Writer out = null;295try {296File dir = file.getParentFile();297if (dir != null && !dir.exists())298dir.mkdirs();299out = new BufferedWriter(new FileWriter(file));300p.store(out, "langtools properties");301out.close();302} catch (IOException e) {303throw new BuildException("error writing property file", e);304} finally {305if (out != null) {306try {307out.close();308} catch (IOException e) {309throw new BuildException("cannot close property file", e);310}311}312}313}314}315316String getDefaultArgsForTool(Properties props, String tn) {317return (tn == null || tn.equals("")) ? "" : props.getProperty(tn + ".args", "");318}319320// Ant task parameters321private boolean askIfUnset;322private String toolProperty;323private String bootstrapProperty;324private String argsProperty;325private File propertyFile;326327// GUI components328private JComboBox toolChoice;329private JCheckBox bootstrapCheckbox;330private JTextField argsField;331private JCheckBox defaultCheck;332private JButton okButton;333334// Result values for the client335private String toolName;336private boolean toolBootstrap;337private String toolArgs;338}339340341