langtools/make/tools/anttasks/SelectToolTask.java
changeset 19935 8f2c828192c3
parent 13631 dc1212c348f9
child 20266 d5e2fc28504e
equal deleted inserted replaced
19934:1d8232b1aa18 19935:8f2c828192c3
     1 /*
     1 /*
     2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    41 import java.io.IOException;
    41 import java.io.IOException;
    42 import java.io.Reader;
    42 import java.io.Reader;
    43 import java.io.Writer;
    43 import java.io.Writer;
    44 import java.util.ArrayList;
    44 import java.util.ArrayList;
    45 import java.util.Arrays;
    45 import java.util.Arrays;
       
    46 import java.util.EnumSet;
    46 import java.util.List;
    47 import java.util.List;
    47 import java.util.Properties;
    48 import java.util.Properties;
    48 import javax.swing.JButton;
    49 import javax.swing.JButton;
    49 import javax.swing.JCheckBox;
    50 import javax.swing.JCheckBox;
    50 import javax.swing.JComboBox;
    51 import javax.swing.JComboBox;
    69  * optionally be set for future use.
    70  * optionally be set for future use.
    70  * 2) Setup mode. In this mode, no property names are provided, and the GUI
    71  * 2) Setup mode. In this mode, no property names are provided, and the GUI
    71  * is invoked to allow the user to set or reset values for use in property mode.
    72  * is invoked to allow the user to set or reset values for use in property mode.
    72  */
    73  */
    73 public class SelectToolTask extends Task {
    74 public class SelectToolTask extends Task {
       
    75 
       
    76     enum ToolChoices {
       
    77         NONE(""),
       
    78         JAVAC("javac"),
       
    79         JAVADOC("javadoc"),
       
    80         JAVAH("javah"),
       
    81         JAVAP("javap");
       
    82 
       
    83         String toolName;
       
    84         boolean bootstrap;
       
    85 
       
    86         ToolChoices(String toolName) {
       
    87             this(toolName, false);
       
    88         }
       
    89 
       
    90         ToolChoices(String toolName, boolean boostrap) {
       
    91             this.toolName = toolName;
       
    92         }
       
    93 
       
    94         @Override
       
    95         public String toString() {
       
    96             return toolName;
       
    97         }
       
    98     }
       
    99 
    74     /**
   100     /**
    75      * Set the location of the private properties file used to keep the retain
   101      * Set the location of the private properties file used to keep the retain
    76      * user preferences for this repository.
   102      * user preferences for this repository.
    77      */
   103      */
    78     public void setPropertyFile(File propertyFile) {
   104     public void setPropertyFile(File propertyFile) {
    95     public void setArgsProperty(String argsProperty) {
   121     public void setArgsProperty(String argsProperty) {
    96         this.argsProperty = argsProperty;
   122         this.argsProperty = argsProperty;
    97     }
   123     }
    98 
   124 
    99     /**
   125     /**
       
   126      * Set the name of the property which will be set to the execution args of the
       
   127      * selected tool, if any. The args default to an empty string.
       
   128      */
       
   129     public void setBootstrapProperty(String bootstrapProperty) {
       
   130         this.bootstrapProperty = bootstrapProperty;
       
   131     }
       
   132 
       
   133     /**
   100      * Specify whether or not to pop up a dialog if the user has not specified
   134      * Specify whether or not to pop up a dialog if the user has not specified
   101      * a default value for a property.
   135      * a default value for a property.
   102      */
   136      */
   103     public void setAskIfUnset(boolean askIfUnset) {
   137     public void setAskIfUnset(boolean askIfUnset) {
   104         this.askIfUnset = askIfUnset;
   138         this.askIfUnset = askIfUnset;
   108     public void execute() {
   142     public void execute() {
   109         Project p = getProject();
   143         Project p = getProject();
   110 
   144 
   111         Properties props = readProperties(propertyFile);
   145         Properties props = readProperties(propertyFile);
   112         toolName = props.getProperty("tool.name");
   146         toolName = props.getProperty("tool.name");
       
   147         toolBootstrap = props.getProperty("tool.bootstrap") != null;
   113         if (toolName != null) {
   148         if (toolName != null) {
   114             toolArgs = props.getProperty(toolName + ".args", "");
   149             toolArgs = props.getProperty(toolName + ".args", "");
   115         }
   150         }
   116 
   151 
   117         if (toolProperty == null ||
   152         if (toolProperty == null ||
   121         }
   156         }
   122 
   157 
   123         // finally, return required values, if any
   158         // finally, return required values, if any
   124         if (toolProperty != null && !(toolName == null || toolName.equals(""))) {
   159         if (toolProperty != null && !(toolName == null || toolName.equals(""))) {
   125             p.setProperty(toolProperty, toolName);
   160             p.setProperty(toolProperty, toolName);
       
   161             if (toolBootstrap)
       
   162                 p.setProperty(bootstrapProperty, "true");
   126 
   163 
   127             if (argsProperty != null && toolArgs != null)
   164             if (argsProperty != null && toolArgs != null)
   128                 p.setProperty(argsProperty, toolArgs);
   165                 p.setProperty(argsProperty, toolArgs);
   129         }
   166         }
   130     }
   167     }
   132     void showGUI(Properties fileProps) {
   169     void showGUI(Properties fileProps) {
   133         Properties guiProps = new Properties(fileProps);
   170         Properties guiProps = new Properties(fileProps);
   134         JOptionPane p = createPane(guiProps);
   171         JOptionPane p = createPane(guiProps);
   135         p.createDialog("Select Tool").setVisible(true);
   172         p.createDialog("Select Tool").setVisible(true);
   136 
   173 
   137         toolName = (String) toolChoice.getSelectedItem();
   174         toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;
   138         toolArgs = argsField.getText();
   175         toolArgs = argsField.getText();
   139 
   176         toolBootstrap = bootstrapCheckbox.isSelected();
   140         if (defaultCheck.isSelected()) {
   177         if (defaultCheck.isSelected()) {
   141             if (toolName.equals("")) {
   178             if (toolName.equals("")) {
   142                 fileProps.remove("tool.name");
   179                 fileProps.remove("tool.name");
       
   180                 fileProps.remove("tool.bootstrap");
   143             } else {
   181             } else {
   144                 fileProps.put("tool.name", toolName);
   182                 fileProps.put("tool.name", toolName);
       
   183                 if (toolBootstrap) {
       
   184                     fileProps.put("tool.bootstrap", "true");
       
   185                 } else {
       
   186                     fileProps.remove("tool.bootstrap");
       
   187                 }
   145                 fileProps.put(toolName + ".args", toolArgs);
   188                 fileProps.put(toolName + ".args", toolArgs);
   146             }
   189             }
   147             writeProperties(propertyFile, fileProps);
   190             writeProperties(propertyFile, fileProps);
   148         }
   191         }
   149     }
   192     }
   152         JPanel body = new JPanel(new GridBagLayout());
   195         JPanel body = new JPanel(new GridBagLayout());
   153         GridBagConstraints lc = new GridBagConstraints();
   196         GridBagConstraints lc = new GridBagConstraints();
   154         lc.insets.right = 10;
   197         lc.insets.right = 10;
   155         lc.insets.bottom = 3;
   198         lc.insets.bottom = 3;
   156         GridBagConstraints fc = new GridBagConstraints();
   199         GridBagConstraints fc = new GridBagConstraints();
   157         fc.anchor = GridBagConstraints.WEST;
       
   158         fc.gridx = 1;
   200         fc.gridx = 1;
   159         fc.gridwidth = GridBagConstraints.REMAINDER;
   201         fc.gridwidth = GridBagConstraints.NONE;
   160         fc.insets.bottom = 3;
   202         fc.insets.bottom = 3;
       
   203 
       
   204         JPanel toolPane = new JPanel(new GridBagLayout());
   161 
   205 
   162         JLabel toolLabel = new JLabel("Tool:");
   206         JLabel toolLabel = new JLabel("Tool:");
   163         body.add(toolLabel, lc);
   207         body.add(toolLabel, lc);
   164         String[] toolChoices = { "apt", "javac", "javadoc", "javah", "javap" };
   208         EnumSet<ToolChoices> toolChoices = toolProperty == null ?
   165         if (true || toolProperty == null) {
   209                 EnumSet.allOf(ToolChoices.class) : EnumSet.range(ToolChoices.JAVAC, ToolChoices.JAVAP);
   166             // include empty value in setup mode
   210         toolChoice = new JComboBox(toolChoices.toArray());
   167             List<String> l = new ArrayList<String>(Arrays.asList(toolChoices));
       
   168             l.add(0, "");
       
   169             toolChoices = l.toArray(new String[l.size()]);
       
   170         }
       
   171         toolChoice = new JComboBox(toolChoices);
       
   172         if (toolName != null)
   211         if (toolName != null)
   173             toolChoice.setSelectedItem(toolName);
   212             toolChoice.setSelectedItem(ToolChoices.valueOf(toolName.toUpperCase()));
   174         toolChoice.addItemListener(new ItemListener() {
   213         toolChoice.addItemListener(new ItemListener() {
   175             public void itemStateChanged(ItemEvent e) {
   214             public void itemStateChanged(ItemEvent e) {
   176                 String tn = (String) e.getItem();
   215                 String tn = ((ToolChoices)e.getItem()).toolName;
   177                 argsField.setText(getDefaultArgsForTool(props, tn));
   216                 argsField.setText(getDefaultArgsForTool(props, tn));
   178                 if (toolProperty != null)
   217                 if (toolProperty != null)
   179                     okButton.setEnabled(!tn.equals(""));
   218                     okButton.setEnabled(!tn.equals(""));
   180             }
   219             }
   181         });
   220         });
   182         body.add(toolChoice, fc);
   221         GridBagConstraints checkConstraint = new GridBagConstraints();
       
   222         fc.anchor = GridBagConstraints.EAST;
       
   223 
       
   224         GridBagConstraints toolConstraint = new GridBagConstraints();
       
   225         fc.anchor = GridBagConstraints.WEST;
       
   226 
       
   227         toolPane.add(toolChoice, toolConstraint);
       
   228         bootstrapCheckbox = new JCheckBox("bootstrap", toolBootstrap);
       
   229         toolPane.add(bootstrapCheckbox, checkConstraint);
       
   230 
       
   231         body.add(toolPane, fc);
   183 
   232 
   184         argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40);
   233         argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40);
   185         if (toolProperty == null || argsProperty != null) {
   234         if (toolProperty == null || argsProperty != null) {
   186             JLabel argsLabel = new JLabel("Args:");
   235             JLabel argsLabel = new JLabel("Args:");
   187             body.add(argsLabel, lc);
   236             body.add(argsLabel, lc);
   188             body.add(argsField, fc);
   237             body.add(argsField, fc);
   189             argsField.addFocusListener(new FocusListener() {
   238             argsField.addFocusListener(new FocusListener() {
   190                 public void focusGained(FocusEvent e) {
   239                 public void focusGained(FocusEvent e) {
   191                 }
   240                 }
   192                 public void focusLost(FocusEvent e) {
   241                 public void focusLost(FocusEvent e) {
   193                     String toolName = (String) toolChoice.getSelectedItem();
   242                     String toolName = ((ToolChoices)toolChoice.getSelectedItem()).toolName;
   194                     if (toolName.length() > 0)
   243                     if (toolName.length() > 0)
   195                         props.put(toolName + ".args", argsField.getText());
   244                         props.put(toolName + ".args", argsField.getText());
   196                 }
   245                 }
   197             });
   246             });
   198         }
   247         }
   269     }
   318     }
   270 
   319 
   271     // Ant task parameters
   320     // Ant task parameters
   272     private boolean askIfUnset;
   321     private boolean askIfUnset;
   273     private String toolProperty;
   322     private String toolProperty;
       
   323     private String bootstrapProperty;
   274     private String argsProperty;
   324     private String argsProperty;
   275     private File propertyFile;
   325     private File propertyFile;
   276 
   326 
   277     // GUI components
   327     // GUI components
   278     private JComboBox toolChoice;
   328     private JComboBox toolChoice;
       
   329     private JCheckBox bootstrapCheckbox;
   279     private JTextField argsField;
   330     private JTextField argsField;
   280     private JCheckBox defaultCheck;
   331     private JCheckBox defaultCheck;
   281     private JButton okButton;
   332     private JButton okButton;
   282 
   333 
   283     // Result values for the client
   334     // Result values for the client
   284     private String toolName;
   335     private String toolName;
       
   336     private boolean toolBootstrap;
   285     private String toolArgs;
   337     private String toolArgs;
   286 }
   338 }