Update spike branch to latest
[jalview.git] / src / jalview / gui / OptsAndParamsPage.java
index 1843f49..298057f 100644 (file)
@@ -144,12 +144,17 @@ public class OptsAndParamsPage
         add(enabled);
       }
 
-      val = new JComboBox<>();
-      for (String str : opt.getPossibleValues())
-      {
-        val.addItem(str);
-      }
+      /*
+       * construct the choice box with possible values, 
+       * or their display names if provided
+       */
+      val = buildComboBox(opt);
       val.setSelectedItem(opt.getValue());
+
+      /*
+       * only show the choicebox if there is more than one option,
+       * or the option is mandatory
+       */
       if (opt.getPossibleValues().size() > 1 || opt.isRequired())
       {
         val.addActionListener(this);
@@ -228,31 +233,21 @@ public class OptsAndParamsPage
       poparent.argSetModified(this, !notmod);
     }
 
-    public OptionI getOptionIfEnabled()
+    /**
+     * Answers null if the option is not selected, else a new Option holding the
+     * selected value
+     * 
+     * @return
+     */
+    public ArgumentI getSelectedOption()
     {
       if (!enabled.isSelected())
       {
         return null;
       }
+      String value = getSelectedValue(option, val.getSelectedIndex());
       OptionI opt = option.copy();
-      if (opt.getPossibleValues() != null
-              && opt.getPossibleValues().size() == 1)
-      {
-        // Hack to make sure the default value for an enabled option with only
-        // one value is actually returned
-        opt.setValue(opt.getPossibleValues().get(0));
-      }
-      if (val.getSelectedItem() != null)
-      {
-        opt.setValue((String) val.getSelectedItem());
-      }
-      else
-      {
-        if (option.getValue() != null)
-        {
-          opt.setValue(option.getValue());
-        }
-      }
+      opt.setValue(value);
       return opt;
     }
 
@@ -613,12 +608,18 @@ 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)
       {
-        prm.setValue((String) choicebox.getSelectedItem());
+        String value = getSelectedValue(this.parameter, choicebox.getSelectedIndex());
+        prm.setValue(value);
       }
       else
       {
@@ -732,8 +733,7 @@ public class OptsAndParamsPage
       {
         if (isChoiceParameter)
         {
-          choicebox = new JComboBox<>();
-          choicebox.addActionListener(this);
+          choicebox = buildComboBox(parm);
           controlsPanel.add(choicebox, BorderLayout.CENTER);
         }
         else
@@ -765,26 +765,9 @@ public class OptsAndParamsPage
         }
       }
 
-      if (parm != null)
+      if (!isChoiceParameter && parm != null)
       {
-        if (isChoiceParameter)
-        {
-          if (init)
-          {
-            for (String val : parm.getPossibleValues())
-            {
-              choicebox.addItem(val);
-            }
-          }
-          if (parm.getValue() != null)
-          {
-            choicebox.setSelectedItem(parm.getValue());
-          }
-        }
-        else
-        {
-          valueField.setText(parm.getValue());
-        }
+        valueField.setText(parm.getValue());
       }
       lastVal = updateSliderFromValueField();
       adjusting = false;
@@ -810,7 +793,7 @@ public class OptsAndParamsPage
       {
         if (isChoiceParameter)
         {
-          return choicebox.getSelectedItem();
+          return getSelectedValue(this.parameter, choicebox.getSelectedIndex());
         }
         slider.setVisible(false);
         return valueField.getText().trim();
@@ -1048,7 +1031,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
    */
@@ -1057,7 +1042,7 @@ public class OptsAndParamsPage
     List<ArgumentI> argSet = new ArrayList<>();
     for (OptionBox opts : getOptSet().values())
     {
-      OptionI opt = opts.getOptionIfEnabled();
+      ArgumentI opt = opts.getSelectedOption();
       if (opt != null)
       {
         argSet.add(opt);
@@ -1065,7 +1050,7 @@ public class OptsAndParamsPage
     }
     for (ParamBox parambox : getParamSet().values())
     {
-      ParameterI parm = parambox.getParameter();
+      ArgumentI parm = parambox.getParameter();
       if (parm != null)
       {
         argSet.add(parm);
@@ -1074,4 +1059,64 @@ 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 JComboBox<String> buildComboBox(OptionI opt)
+  {
+    JComboBox<String> cb = null;
+    List<String> displayNames = opt.getDisplayNames();
+    if (displayNames != null)
+    {
+      cb = JvSwingUtils.buildComboWithTooltips(displayNames,
+              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. 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 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);
+      }
+      else
+      {
+        value = opt.getValue();
+      }
+    }
+    return value;
+  }
 }