JAL-2629 push latest to spike branch
[jalview.git] / src / jalview / gui / OptsAndParamsPage.java
index 483ba3d..1843f49 100644 (file)
@@ -21,6 +21,7 @@
 package jalview.gui;
 
 import jalview.util.MessageManager;
+import jalview.ws.jws2.dm.JabaOption;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.OptionI;
 import jalview.ws.params.ParameterI;
@@ -101,7 +102,7 @@ public class OptsAndParamsPage
   public class OptionBox extends JPanel
           implements MouseListener, ActionListener
   {
-    JCheckBox enabled = new JCheckBox();
+    JCheckBox enabled;
 
     final URL finfo;
 
@@ -113,42 +114,37 @@ public class OptsAndParamsPage
 
     OptionI option;
 
-    JLabel optlabel = new JLabel();
-
-    JComboBox<String> val = new JComboBox<>();
+    JComboBox<String> 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.setSelected(opt.isRequired()); // TODO: lock required options
-      enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
-      enabled.setText("");
-      enabled.setText(opt.getName());
-      enabled.addActionListener(this);
-      finfo = option.getFurtherDetails();
-      String desc = opt.getDescription();
-      if (finfo != null)
-      {
-        hasLink = true;
+      enabled = new JCheckBox(opt.getName());
+      enabled.setSelected(opt.isRequired());
 
-        enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
-                ((desc == null || desc.trim().length() == 0)
-                        ? MessageManager.getString(
-                                "label.opt_and_params_further_details")
-                        : desc) + "<br><img src=\"" + linkImageURL
-                        + "\"/>"));
-        enabled.addMouseListener(this);
+      /*
+       * If option is required, show a label, if optional a checkbox
+       * (but not for Jabaws pending JWS-126 resolution)
+       */
+      if (opt.isRequired() && !(opt instanceof JabaOption))
+      {
+        finfo = null;
+        add(new JLabel(opt.getName()));
       }
       else
       {
-        if (desc != null && desc.trim().length() > 0)
-        {
-          enabled.setToolTipText(
-                  JvSwingUtils.wrapTooltip(true, opt.getDescription()));
-        }
+        finfo = option.getFurtherDetails();
+        configureCheckbox(opt);
+        add(enabled);
       }
-      add(enabled);
+
+      val = new JComboBox<>();
       for (String str : opt.getPossibleValues())
       {
         val.addItem(str);
@@ -159,12 +155,45 @@ public class OptsAndParamsPage
         val.addActionListener(this);
         add(val);
       }
-      // TODO: add actionListeners for popup (to open further info),
-      // and to update list of parameters if an option is enabled
-      // that takes a value. JBPNote: is this TODO still valid ?
+
       setInitialValue();
     }
 
+    /**
+     * Configures the checkbox that controls whether or not the option is
+     * selected
+     * 
+     * @param opt
+     */
+    protected void configureCheckbox(OptionI opt)
+    {
+      enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
+      enabled.addActionListener(this);
+      final String desc = opt.getDescription();
+      if (finfo != null)
+      {
+        hasLink = true;
+        String description = desc;
+        if (desc == null || desc.trim().isEmpty())
+        {
+          description = MessageManager
+                  .getString("label.opt_and_params_further_details");
+        }
+        description = description + "<br><img src=\"" + linkImageURL
+                + "\"/>";
+        String text = JvSwingUtils.wrapTooltip(true, description);
+        enabled.setToolTipText(text);
+        enabled.addMouseListener(this); // for popup menu to show link
+      }
+      else
+      {
+        if (desc != null && desc.trim().length() > 0)
+        {
+          enabled.setToolTipText(JvSwingUtils.wrapTooltip(true, desc));
+        }
+      }
+    }
+
     @Override
     public void actionPerformed(ActionEvent e)
     {
@@ -366,7 +395,8 @@ public class OptsAndParamsPage
          * ensure slider has an integer range corresponding to
          * the min-max range of the parameter
          */
-        if (validator.getMin() != null && !isIntegerParameter
+        if (validator.getMin() != null && validator.getMax() != null
+        // && !isIntegerParameter
                 && !isStringParameter)
         {
           double min = validator.getMin().doubleValue();
@@ -380,10 +410,10 @@ public class OptsAndParamsPage
           // todo scaleMin, scaleMax could also be final fields
         }
       }
-      else
-      {
-        isChoiceParameter = parameter.getPossibleValues() != null;
-      }
+
+      List<String> possibleValues = parameter.getPossibleValues();
+      isChoiceParameter = possibleValues != null
+              && !possibleValues.isEmpty();
 
       if (compact)
       {
@@ -549,13 +579,23 @@ public class OptsAndParamsPage
      */
     private void checkIfModified()
     {
-      Object newValue = updateSliderFromValueField();
-      boolean modified = true;
-      if (newValue.getClass() == lastVal.getClass())
+      if (!adjusting)
       {
-        modified = !newValue.equals(lastVal);
+        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;
+        }
       }
-      pmdialogbox.argSetModified(this, modified);
     }
 
     @Override
@@ -629,8 +669,13 @@ public class OptsAndParamsPage
     @Override
     public void stateChanged(ChangeEvent e)
     {
-      if (!adjusting)
+      if (adjusting)
       {
+        return;
+      }
+      try
+      {
+        adjusting = true;
         if (!isLogarithmicParameter)
         {
           /*
@@ -648,6 +693,9 @@ public class OptsAndParamsPage
           valueField.setText(formatDouble(value));
         }
         checkIfModified();
+      } finally
+      {
+        adjusting = false;
       }
     }
 
@@ -700,7 +748,9 @@ public class OptsAndParamsPage
             @Override
             public void keyReleased(KeyEvent e)
             {
-              if (e.isActionKey())
+              int keyCode = e.getKeyCode();
+              if (e.isActionKey() && keyCode != KeyEvent.VK_LEFT
+                      && keyCode != KeyEvent.VK_RIGHT)
               {
                 if (valueField.getText().trim().length() > 0)
                 {