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