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