Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / src / jalview / gui / OptsAndParamsPage.java
index b13d77d..577a1ef 100644 (file)
  */
 package jalview.gui;
 
+import jalview.bin.Cache;
+import jalview.io.DataSourceType;
+import jalview.io.FileLoader;
+import jalview.io.JalviewFileChooser;
+import jalview.io.JalviewFileView;
 import jalview.util.MessageManager;
 import jalview.ws.jws2.dm.JabaOption;
 import jalview.ws.params.ArgumentI;
@@ -27,28 +32,35 @@ import jalview.ws.params.OptionI;
 import jalview.ws.params.ParameterI;
 import jalview.ws.params.ValueConstrainI;
 import jalview.ws.params.ValueConstrainI.ValueType;
+import jalview.ws.params.simple.FileParameter;
 import jalview.ws.params.simple.LogarithmicParameter;
+import jalview.ws.params.simple.RadioChoiceParameter;
 import jalview.ws.params.simple.StringParameter;
-
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Component;
+import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Font;
 import java.awt.Rectangle;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
+import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
+import java.io.File;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.swing.ButtonGroup;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
@@ -57,6 +69,7 @@ import javax.swing.JLabel;
 import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.swing.JPopupMenu;
+import javax.swing.JRadioButton;
 import javax.swing.JScrollPane;
 import javax.swing.JSlider;
 import javax.swing.JTextArea;
@@ -114,18 +127,19 @@ public class OptsAndParamsPage
 
     OptionI option;
 
-    JComboBox<String> val;
+    JComboBox<Object> val;
 
     /**
      * Constructs and adds labels and controls to the panel for one Option
      * 
      * @param opt
      */
+
     public OptionBox(OptionI opt)
     {
       option = opt;
       setLayout(new FlowLayout(FlowLayout.LEFT));
-      enabled = new JCheckBox(opt.getName());
+      enabled = new JCheckBox(opt.getLabel());
       enabled.setSelected(opt.isRequired());
 
       /*
@@ -135,7 +149,7 @@ public class OptsAndParamsPage
       if (opt.isRequired() && !(opt instanceof JabaOption))
       {
         finfo = null;
-        add(new JLabel(opt.getName()));
+        add(new JLabel(opt.getLabel()));
       }
       else
       {
@@ -148,20 +162,7 @@ public class OptsAndParamsPage
        * construct the choice box with possible values, 
        * or their display names if provided
        */
-      List<String> displayNames = opt.getDisplayNames();
-      if (displayNames != null)
-      {
-        val = JvSwingUtils.buildComboWithTooltips(displayNames,
-                opt.getPossibleValues());
-      }
-      else
-      {
-        val = new JComboBox<>();
-        for (String v : opt.getPossibleValues())
-        {
-          val.addItem(v);
-        }
-      }
+      val = buildComboBox(opt);
       val.setSelectedItem(opt.getValue());
 
       /*
@@ -191,6 +192,7 @@ public class OptsAndParamsPage
       if (finfo != null)
       {
         hasLink = true;
+
         String description = desc;
         if (desc == null || desc.trim().isEmpty())
         {
@@ -252,51 +254,18 @@ public class OptsAndParamsPage
      * 
      * @return
      */
-    public OptionI getSelectedOption()
+    public ArgumentI getSelectedOption()
     {
       if (!enabled.isSelected())
       {
         return null;
       }
-      String value = getSelectedValue();
+      String value = getSelectedValue(option, val.getSelectedIndex());
       OptionI opt = option.copy();
       opt.setValue(value);
       return opt;
     }
 
-    /*
-     * Answers the value corresponding to the selected item in the choice combo
-     * box. If display names were not provided, this is simply the selected
-     * value. If display names were provided, it is the value corresponding to
-     * the selected item index.
-     * 
-     * @return
-     */
-    protected String getSelectedValue()
-    {
-      List<String> possibleValues = option.getPossibleValues();
-      String value = null;
-      if (possibleValues != null && possibleValues.size() == 1)
-      {
-        // Hack to make sure the default value for an enabled option with only
-        // one value is actually returned even if this.val is not displayed
-        value = possibleValues.get(0);
-      }
-      else
-      {
-        int sel = val.getSelectedIndex();
-        if (sel >= 0 && sel < possibleValues.size())
-        {
-          value = possibleValues.get(sel);
-        }
-        else
-        {
-          value = option.getValue();
-        }
-      }
-      return value;
-    }
-
     @Override
     public void mouseClicked(MouseEvent e)
     {
@@ -309,11 +278,13 @@ public class OptsAndParamsPage
     @Override
     public void mouseEntered(MouseEvent e)
     {
+
     }
 
     @Override
     public void mouseExited(MouseEvent e)
     {
+
     }
 
     @Override
@@ -363,7 +334,6 @@ public class OptsAndParamsPage
     {
       return option == null ? super.toString() : option.toString();
     }
-
   }
 
   /**
@@ -388,7 +358,15 @@ public class OptsAndParamsPage
 
     boolean adjusting;
 
-    JComboBox<String> choicebox;
+    /*
+     * drop-down list of choice options (if applicable)
+     */
+    JComboBox<Object> choicebox;
+
+    /*
+     * radio buttons as an alternative to combo box
+     */
+    ButtonGroup buttonGroup;
 
     JPanel controlsPanel = new JPanel();
 
@@ -423,14 +401,13 @@ public class OptsAndParamsPage
       finfo = parm.getFurtherDetails();
       validator = parm.getValidValue();
       parameter = parm;
-
       isLogarithmicParameter = parm instanceof LogarithmicParameter;
-
       if (validator != null)
       {
         ValueType type = validator.getType();
         isIntegerParameter = type == ValueType.Integer;
-        isStringParameter = type == ValueType.String;
+        isStringParameter = type == ValueType.String
+                || type == ValueType.File;
 
         /*
          * ensure slider has an integer range corresponding to
@@ -463,6 +440,7 @@ public class OptsAndParamsPage
       else
       {
         addExpandableParam(parm);
+
       }
     }
 
@@ -474,6 +452,7 @@ public class OptsAndParamsPage
     private void addCompactParameter(ParameterI parm)
     {
       setLayout(new MigLayout("", "[][grow]"));
+
       String ttipText = null;
 
       controlsPanel.setLayout(new BorderLayout());
@@ -523,6 +502,7 @@ public class OptsAndParamsPage
       firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
               PARAM_CLOSEDHEIGHT - 30));
 
+
       if (parm.getDescription() != null
               && parm.getDescription().trim().length() > 0)
       {
@@ -599,7 +579,6 @@ public class OptsAndParamsPage
       showDescBtn.setBounds(new Rectangle(10, 10, 16, 16));
       container.add(showDescBtn);
     }
-
     @Override
     public void actionPerformed(ActionEvent e)
     {
@@ -607,10 +586,6 @@ public class OptsAndParamsPage
       {
         return;
       }
-      if (!isChoiceParameter)
-      {
-        updateSliderFromValueField();
-      }
       checkIfModified();
     }
 
@@ -620,25 +595,16 @@ public class OptsAndParamsPage
      */
     private void checkIfModified()
     {
-      if (!adjusting)
+      Object newValue = updateSliderFromValueField();
+      boolean modified = true;
+      if (newValue.getClass() == lastVal.getClass())
       {
-        try
-        {
-          adjusting = true;
-          Object newValue = updateSliderFromValueField();
-          boolean modified = true;
-          if (newValue.getClass() == lastVal.getClass())
-          {
-            modified = !newValue.equals(lastVal);
-          }
-          pmdialogbox.argSetModified(this, modified);
-        } finally
-        {
-          adjusting = false;
-        }
+        modified = !newValue.equals(lastVal);
       }
+      pmdialogbox.argSetModified(this, modified);
     }
 
+
     @Override
     public int getBaseline(int width, int height)
     {
@@ -654,37 +620,30 @@ public class OptsAndParamsPage
       return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
     }
 
-    public ParameterI getParameter()
+    /**
+     * Answers an argument holding the value entered or selected in the dialog
+     * 
+     * @return
+     */
+    public ArgumentI getParameter()
     {
       ParameterI prm = parameter.copy();
-      if (isChoiceParameter)
+      String value = null;
+      if (parameter instanceof RadioChoiceParameter)
       {
-        String value = getChoice();
-        prm.setValue(value);
+        value = buttonGroup.getSelection().getActionCommand();
       }
-      else
+      else if (isChoiceParameter)
       {
-        prm.setValue(valueField.getText());
+        value = getSelectedValue(this.parameter,
+                choicebox.getSelectedIndex());
       }
-      return prm;
-    }
-
-    /**
-     * Answers the value corresponding to the selected item in the choice combo
-     * box. If display names were not provided, this is simply the selected
-     * value. If display names were provided, it is the value corresponding to
-     * the selected item index.
-     * 
-     * @return
-     */
-    protected String getChoice()
-    {
-      int sel = choicebox.getSelectedIndex();
-      if (sel < 0 || sel >= parameter.getPossibleValues().size())
+      else
       {
-        return null;
+        value = valueField.getText();
       }
-      return parameter.getPossibleValues().get(sel);
+      prm.setValue(value);
+      return prm;
     }
 
     public void init()
@@ -705,11 +664,13 @@ public class OptsAndParamsPage
     @Override
     public void mouseEntered(MouseEvent e)
     {
+
     }
 
     @Override
     public void mouseExited(MouseEvent e)
     {
+
     }
 
     @Override
@@ -724,6 +685,7 @@ public class OptsAndParamsPage
     @Override
     public void mouseReleased(MouseEvent e)
     {
+
     }
 
     @Override
@@ -787,25 +749,17 @@ public class OptsAndParamsPage
     void updateControls(ParameterI parm)
     {
       adjusting = true;
-      boolean init = (choicebox == null && valueField == null);
-      List<String> displayNames = parm.getDisplayNames();
+      boolean init = (choicebox == null && valueField == null
+              && buttonGroup == null);
       if (init)
       {
-        if (isChoiceParameter)
+        if (parm instanceof RadioChoiceParameter)
         {
-          if (displayNames != null)
-          {
-            choicebox = JvSwingUtils.buildComboWithTooltips(displayNames,
-                    parm.getPossibleValues());
-          }
-          else
-          {
-            choicebox = new JComboBox<>();
-            for (String val : parm.getPossibleValues())
-            {
-              choicebox.addItem(val);
-            }
-          }
+          buttonGroup = addRadioButtons(parameter, controlsPanel);
+        }
+        else if (isChoiceParameter)
+        {
+          choicebox = buildComboBox(parm);
           choicebox.addActionListener(this);
           controlsPanel.add(choicebox, BorderLayout.CENTER);
         }
@@ -818,6 +772,8 @@ public class OptsAndParamsPage
           valueField.addActionListener(this);
           valueField.addKeyListener(new KeyAdapter()
           {
+
+
             @Override
             public void keyReleased(KeyEvent e)
             {
@@ -831,26 +787,112 @@ public class OptsAndParamsPage
                 }
               }
             }
+
+          });
+          valueField.addFocusListener(new FocusAdapter() {
+
+            @Override
+            public void focusLost(FocusEvent e)
+            {
+              actionPerformed(null);
+            }
+            
           });
           valueField.setPreferredSize(new Dimension(65, 25));
+          if (parm instanceof FileParameter)
+          {
+            valueField.setToolTipText(MessageManager
+                    .getString("label.double_click_to_browse"));
+            valueField.addMouseListener(new MouseAdapter()
+            {
+              @Override
+              public void mouseClicked(MouseEvent e)
+              {
+                if (e.getClickCount() == 2)
+                {
+                  String dir = Cache.getProperty("LAST_DIRECTORY");
+                  JalviewFileChooser chooser = new JalviewFileChooser(dir);
+                  chooser.setFileView(new JalviewFileView());
+                  chooser.setDialogTitle(
+                          MessageManager.getString("action.select_ddbb"));
+
+                  int val = chooser.showOpenDialog(ParamBox.this);
+                  if (val == JalviewFileChooser.APPROVE_OPTION)
+                  {
+                    File choice = chooser.getSelectedFile();
+                    String path = choice.getPath();
+                    valueField.setText(path);
+                    Cache.setProperty("LAST_DIRECTORY", choice.getParent());
+                    FileLoader.updateRecentlyOpened(path,
+                            DataSourceType.FILE);
+                  }
+                }
+              }
+            });
+          }
+          
           controlsPanel.add(slider, BorderLayout.WEST);
           controlsPanel.add(valueField, BorderLayout.EAST);
         }
       }
 
-      if (!isChoiceParameter && parm != null)
+      String value = parm.getValue();
+      if (value != null)
       {
-        valueField.setText(parm.getValue());
+        if (isChoiceParameter)
+        {
+          if (!(parm instanceof RadioChoiceParameter))
+          {
+            choicebox.setSelectedItem(value);
+          }
+        }
+        else
+        {
+          valueField.setText(value);
+        }
       }
       lastVal = updateSliderFromValueField();
       adjusting = false;
     }
 
     /**
+     * Adds a panel to comp, containing a label and radio buttons for the choice
+     * of values of the given option. Returns a ButtonGroup whose members are
+     * the added radio buttons.
+     * 
+     * @param option
+     * @param comp
+     * 
+     * @return
+     */
+    protected ButtonGroup addRadioButtons(OptionI option, Container comp)
+    {
+      ButtonGroup bg = new ButtonGroup();
+      JPanel radioPanel = new JPanel();
+      radioPanel.add(new JLabel(option.getDescription()));
+
+      String value = option.getValue();
+
+      for (String opt : option.getPossibleValues())
+      {
+        JRadioButton btn = new JRadioButton(opt);
+        btn.setActionCommand(opt);
+        boolean selected = opt.equals(value);
+        btn.setSelected(selected);
+        btn.addActionListener(this);
+        bg.add(btn);
+        radioPanel.add(btn);
+      }
+      comp.add(radioPanel);
+
+      return bg;
+    }
+
+    /**
      * Action depends on the type of the input parameter:
      * <ul>
      * <li>if a text input, returns the trimmed value</li>
-     * <li>if a choice list, returns the selected value</li>
+     * <li>if a choice list or radio button, returns the selected value</li>
      * <li>if a value slider and input field, sets the value of the slider from
      * the value in the text field, limiting it to any defined min-max
      * range.</li>
@@ -866,12 +908,25 @@ public class OptsAndParamsPage
       {
         if (isChoiceParameter)
         {
-          return getChoice();
+          if (parameter instanceof RadioChoiceParameter)
+          {
+            return buttonGroup.getSelection().getActionCommand();
+          }
+          else
+          {
+            return getSelectedValue(this.parameter,
+                    choicebox.getSelectedIndex());
+          }
         }
         slider.setVisible(false);
         return valueField.getText().trim();
       }
 
+      if (validator.getMin() == null || validator.getMax() == null)
+      {
+        slider.setVisible(false);
+      }
+
       valueField.setText(valueField.getText().trim());
 
       /*
@@ -897,7 +952,6 @@ public class OptsAndParamsPage
         valueField.setBackground(Color.yellow);
         return Float.NaN;
       }
-
       if (isIntegerParameter)
       {
         int iVal = 0;
@@ -920,16 +974,15 @@ public class OptsAndParamsPage
         {
           slider.setVisible(false);
         }
-        return new Integer(iVal);
+        return Integer.valueOf(iVal);
       }
-
       if (isLogarithmicParameter)
       {
         double dVal = 0d;
         try
         {
           double eValue = Double.valueOf(valueField.getText());
-          dVal = Math.log(eValue) * sliderScaleFactor;
+          dVal = Math.log(eValue);
         } catch (Exception e)
         {
           // shouldn't be possible here
@@ -942,16 +995,16 @@ public class OptsAndParamsPage
                   * sliderScaleFactor;
           double scaleMax = Math.log(validator.getMax().doubleValue())
                   * sliderScaleFactor;
-          slider.getModel().setRangeProperties((int) (dVal), 1,
+          slider.getModel().setRangeProperties(
+                  (int) (sliderScaleFactor * dVal), 1,
                   (int) scaleMin, 1 + (int) scaleMax, true);
         }
         else
         {
           slider.setVisible(false);
         }
-        return new Double(dVal);
+        return Double.valueOf(dVal);
       }
-
       float fVal = 0f;
       try
       {
@@ -974,7 +1027,7 @@ public class OptsAndParamsPage
       {
         slider.setVisible(false);
       }
-      return new Float(fVal);
+      return Float.valueOf(fVal);
     }
   }
 
@@ -986,6 +1039,7 @@ public class OptsAndParamsPage
    * 
    * @param paramContainer
    */
+
   public OptsAndParamsPage(OptsParametersContainerI paramContainer,
           boolean compact)
   {
@@ -1015,6 +1069,7 @@ public class OptsAndParamsPage
     mnu.show(invoker, x, y);
   }
 
+
   public Map<String, OptionBox> getOptSet()
   {
     return optSet;
@@ -1035,6 +1090,7 @@ public class OptsAndParamsPage
     this.paramSet = paramSet;
   }
 
+
   OptionBox addOption(OptionI opt)
   {
     OptionBox cb = optSet.get(opt.getName());
@@ -1078,9 +1134,11 @@ public class OptsAndParamsPage
       }
       else
       {
-        throw new Error(String.format("Invalid value '%s' for option '%s'",
-                string, option.getName()));
+        throw new Error(MessageManager.formatMessage(
+                "error.invalid_value_for_option", new String[]
+                { string, option.getName() }));
       }
+
     }
     if (option.isRequired() && !cb.enabled.isSelected())
     {
@@ -1104,7 +1162,9 @@ public class OptsAndParamsPage
   }
 
   /**
-   * recover options and parameters from GUI
+   * Answers a list of arguments representing all the options and arguments
+   * selected on the dialog, holding their chosen or input values. Optional
+   * parameters which were not selected are not included.
    * 
    * @return
    */
@@ -1113,7 +1173,7 @@ public class OptsAndParamsPage
     List<ArgumentI> argSet = new ArrayList<>();
     for (OptionBox opts : getOptSet().values())
     {
-      OptionI opt = opts.getSelectedOption();
+      ArgumentI opt = opts.getSelectedOption();
       if (opt != null)
       {
         argSet.add(opt);
@@ -1121,7 +1181,7 @@ public class OptsAndParamsPage
     }
     for (ParamBox parambox : getParamSet().values())
     {
-      ParameterI parm = parambox.getParameter();
+      ArgumentI parm = parambox.getParameter();
       if (parm != null)
       {
         argSet.add(parm);
@@ -1130,4 +1190,58 @@ public class OptsAndParamsPage
 
     return argSet;
   }
+
+  /**
+   * A helper method that constructs and returns a CombBox for choice of the
+   * possible option values. If display names are provided, then these are added
+   * as options, otherwise the actual values are added.
+   * 
+   * @param opt
+   * @return
+   */
+  protected static JComboBox<Object> buildComboBox(OptionI opt)
+  {
+    JComboBox<Object> cb = null;
+    List<String> displayNames = opt.getDisplayNames();
+    if (displayNames != null)
+    {
+      List<Object> displayNamesObjects = new ArrayList<>();
+      displayNamesObjects.addAll(displayNames);
+      cb = JvSwingUtils.buildComboWithTooltips(displayNamesObjects,
+              opt.getPossibleValues());
+    }
+    else
+    {
+      cb = new JComboBox<>();
+      for (String v : opt.getPossibleValues())
+      {
+        cb.addItem(v);
+      }
+    }
+    return cb;
+  }
+
+  /**
+   * Answers the value corresponding to the selected item in the choice combo
+   * box. Note that this returns the underlying value even if a different
+   * display name is used in the combo box.
+   * 
+   * @return
+   */
+  protected static String getSelectedValue(OptionI opt, int sel)
+  {
+    List<String> possibleValues = opt.getPossibleValues();
+    String value = null;
+    if (possibleValues != null && possibleValues.size() == 1)
+    {
+      // Hack to make sure the default value for an enabled option with only
+      // one value is actually returned even if this.val is not displayed
+      value = possibleValues.get(0);
+    }
+    else if (sel >= 0 && sel < possibleValues.size())
+    {
+      value = possibleValues.get(sel);
+    }
+    return value;
+  }
 }