1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/make/tools/anttasks/SelectToolTask.java Wed Mar 07 13:11:27 2012 +0100 1.3 @@ -0,0 +1,286 @@ 1.4 +/* 1.5 + * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package anttasks; 1.30 + 1.31 +import java.awt.GridBagConstraints; 1.32 +import java.awt.GridBagLayout; 1.33 +import java.awt.event.ActionEvent; 1.34 +import java.awt.event.ActionListener; 1.35 +import java.awt.event.FocusEvent; 1.36 +import java.awt.event.FocusListener; 1.37 +import java.awt.event.ItemEvent; 1.38 +import java.awt.event.ItemListener; 1.39 +import java.io.BufferedReader; 1.40 +import java.io.BufferedWriter; 1.41 +import java.io.File; 1.42 +import java.io.FileReader; 1.43 +import java.io.FileWriter; 1.44 +import java.io.IOException; 1.45 +import java.io.Reader; 1.46 +import java.io.Writer; 1.47 +import java.util.ArrayList; 1.48 +import java.util.Arrays; 1.49 +import java.util.List; 1.50 +import java.util.Properties; 1.51 +import javax.swing.JButton; 1.52 +import javax.swing.JCheckBox; 1.53 +import javax.swing.JComboBox; 1.54 +import javax.swing.JDialog; 1.55 +import javax.swing.JLabel; 1.56 +import javax.swing.JOptionPane; 1.57 +import javax.swing.JPanel; 1.58 +import javax.swing.JTextField; 1.59 + 1.60 +import javax.swing.SwingUtilities; 1.61 +import org.apache.tools.ant.BuildException; 1.62 +import org.apache.tools.ant.Project; 1.63 +import org.apache.tools.ant.Task; 1.64 + 1.65 +/** 1.66 + * Task to allow the user to control langtools tools built when using NetBeans. 1.67 + * 1.68 + * There are two primary modes. 1.69 + * 1) Property mode. In this mode, property names are provided to get values 1.70 + * that may be specified by the user, either directly in a GUI dialog, or 1.71 + * read from a properties file. If the GUI dialog is invoked, values may 1.72 + * optionally be set for future use. 1.73 + * 2) Setup mode. In this mode, no property names are provided, and the GUI 1.74 + * is invoked to allow the user to set or reset values for use in property mode. 1.75 + */ 1.76 +public class SelectToolTask extends Task { 1.77 + /** 1.78 + * Set the location of the private properties file used to keep the retain 1.79 + * user preferences for this repository. 1.80 + */ 1.81 + public void setPropertyFile(File propertyFile) { 1.82 + this.propertyFile = propertyFile; 1.83 + } 1.84 + 1.85 + /** 1.86 + * Set the name of the property which will be set to the name of the 1.87 + * selected tool, if any. If no tool is selected, the property will 1.88 + * remain unset. 1.89 + */ 1.90 + public void setToolProperty(String toolProperty) { 1.91 + this.toolProperty = toolProperty; 1.92 + } 1.93 + 1.94 + /** 1.95 + * Set the name of the property which will be set to the execution args of the 1.96 + * selected tool, if any. The args default to an empty string. 1.97 + */ 1.98 + public void setArgsProperty(String argsProperty) { 1.99 + this.argsProperty = argsProperty; 1.100 + } 1.101 + 1.102 + /** 1.103 + * Specify whether or not to pop up a dialog if the user has not specified 1.104 + * a default value for a property. 1.105 + */ 1.106 + public void setAskIfUnset(boolean askIfUnset) { 1.107 + this.askIfUnset = askIfUnset; 1.108 + } 1.109 + 1.110 + @Override 1.111 + public void execute() { 1.112 + Project p = getProject(); 1.113 + 1.114 + Properties props = readProperties(propertyFile); 1.115 + toolName = props.getProperty("tool.name"); 1.116 + if (toolName != null) { 1.117 + toolArgs = props.getProperty(toolName + ".args", ""); 1.118 + } 1.119 + 1.120 + if (toolProperty == null || 1.121 + askIfUnset && (toolName == null 1.122 + || (argsProperty != null && toolArgs == null))) { 1.123 + showGUI(props); 1.124 + } 1.125 + 1.126 + // finally, return required values, if any 1.127 + if (toolProperty != null && !(toolName == null || toolName.equals(""))) { 1.128 + p.setProperty(toolProperty, toolName); 1.129 + 1.130 + if (argsProperty != null && toolArgs != null) 1.131 + p.setProperty(argsProperty, toolArgs); 1.132 + } 1.133 + } 1.134 + 1.135 + void showGUI(Properties fileProps) { 1.136 + Properties guiProps = new Properties(fileProps); 1.137 + JOptionPane p = createPane(guiProps); 1.138 + p.createDialog("Select Tool").setVisible(true); 1.139 + 1.140 + toolName = (String) toolChoice.getSelectedItem(); 1.141 + toolArgs = argsField.getText(); 1.142 + 1.143 + if (defaultCheck.isSelected()) { 1.144 + if (toolName.equals("")) { 1.145 + fileProps.remove("tool.name"); 1.146 + } else { 1.147 + fileProps.put("tool.name", toolName); 1.148 + fileProps.put(toolName + ".args", toolArgs); 1.149 + } 1.150 + writeProperties(propertyFile, fileProps); 1.151 + } 1.152 + } 1.153 + 1.154 + JOptionPane createPane(final Properties props) { 1.155 + JPanel body = new JPanel(new GridBagLayout()); 1.156 + GridBagConstraints lc = new GridBagConstraints(); 1.157 + lc.insets.right = 10; 1.158 + lc.insets.bottom = 3; 1.159 + GridBagConstraints fc = new GridBagConstraints(); 1.160 + fc.anchor = GridBagConstraints.WEST; 1.161 + fc.gridx = 1; 1.162 + fc.gridwidth = GridBagConstraints.REMAINDER; 1.163 + fc.insets.bottom = 3; 1.164 + 1.165 + JLabel toolLabel = new JLabel("Tool:"); 1.166 + body.add(toolLabel, lc); 1.167 + String[] toolChoices = { "apt", "javac", "javadoc", "javah", "javap" }; 1.168 + if (true || toolProperty == null) { 1.169 + // include empty value in setup mode 1.170 + List<String> l = new ArrayList<String>(Arrays.asList(toolChoices)); 1.171 + l.add(0, ""); 1.172 + toolChoices = l.toArray(new String[l.size()]); 1.173 + } 1.174 + toolChoice = new JComboBox(toolChoices); 1.175 + if (toolName != null) 1.176 + toolChoice.setSelectedItem(toolName); 1.177 + toolChoice.addItemListener(new ItemListener() { 1.178 + public void itemStateChanged(ItemEvent e) { 1.179 + String tn = (String) e.getItem(); 1.180 + argsField.setText(getDefaultArgsForTool(props, tn)); 1.181 + if (toolProperty != null) 1.182 + okButton.setEnabled(!tn.equals("")); 1.183 + } 1.184 + }); 1.185 + body.add(toolChoice, fc); 1.186 + 1.187 + argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40); 1.188 + if (toolProperty == null || argsProperty != null) { 1.189 + JLabel argsLabel = new JLabel("Args:"); 1.190 + body.add(argsLabel, lc); 1.191 + body.add(argsField, fc); 1.192 + argsField.addFocusListener(new FocusListener() { 1.193 + public void focusGained(FocusEvent e) { 1.194 + } 1.195 + public void focusLost(FocusEvent e) { 1.196 + String toolName = (String) toolChoice.getSelectedItem(); 1.197 + if (toolName.length() > 0) 1.198 + props.put(toolName + ".args", argsField.getText()); 1.199 + } 1.200 + }); 1.201 + } 1.202 + 1.203 + defaultCheck = new JCheckBox("Set as default"); 1.204 + if (toolProperty == null) 1.205 + defaultCheck.setSelected(true); 1.206 + else 1.207 + body.add(defaultCheck, fc); 1.208 + 1.209 + final JOptionPane p = new JOptionPane(body); 1.210 + okButton = new JButton("OK"); 1.211 + okButton.setEnabled(toolProperty == null || (toolName != null && !toolName.equals(""))); 1.212 + okButton.addActionListener(new ActionListener() { 1.213 + public void actionPerformed(ActionEvent e) { 1.214 + JDialog d = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, p); 1.215 + d.setVisible(false); 1.216 + } 1.217 + }); 1.218 + p.setOptions(new Object[] { okButton }); 1.219 + 1.220 + return p; 1.221 + } 1.222 + 1.223 + Properties readProperties(File file) { 1.224 + Properties p = new Properties(); 1.225 + if (file != null && file.exists()) { 1.226 + Reader in = null; 1.227 + try { 1.228 + in = new BufferedReader(new FileReader(file)); 1.229 + p.load(in); 1.230 + in.close(); 1.231 + } catch (IOException e) { 1.232 + throw new BuildException("error reading property file", e); 1.233 + } finally { 1.234 + if (in != null) { 1.235 + try { 1.236 + in.close(); 1.237 + } catch (IOException e) { 1.238 + throw new BuildException("cannot close property file", e); 1.239 + } 1.240 + } 1.241 + } 1.242 + } 1.243 + return p; 1.244 + } 1.245 + 1.246 + void writeProperties(File file, Properties p) { 1.247 + if (file != null) { 1.248 + Writer out = null; 1.249 + try { 1.250 + File dir = file.getParentFile(); 1.251 + if (dir != null && !dir.exists()) 1.252 + dir.mkdirs(); 1.253 + out = new BufferedWriter(new FileWriter(file)); 1.254 + p.store(out, "langtools properties"); 1.255 + out.close(); 1.256 + } catch (IOException e) { 1.257 + throw new BuildException("error writing property file", e); 1.258 + } finally { 1.259 + if (out != null) { 1.260 + try { 1.261 + out.close(); 1.262 + } catch (IOException e) { 1.263 + throw new BuildException("cannot close property file", e); 1.264 + } 1.265 + } 1.266 + } 1.267 + } 1.268 + } 1.269 + 1.270 + String getDefaultArgsForTool(Properties props, String tn) { 1.271 + return (tn == null || tn.equals("")) ? "" : props.getProperty(tn + ".args", ""); 1.272 + } 1.273 + 1.274 + // Ant task parameters 1.275 + private boolean askIfUnset; 1.276 + private String toolProperty; 1.277 + private String argsProperty; 1.278 + private File propertyFile; 1.279 + 1.280 + // GUI components 1.281 + private JComboBox toolChoice; 1.282 + private JTextField argsField; 1.283 + private JCheckBox defaultCheck; 1.284 + private JButton okButton; 1.285 + 1.286 + // Result values for the client 1.287 + private String toolName; 1.288 + private String toolArgs; 1.289 +}