JAL-2629 add logarithmic parameter for logarithmic slider
[jalview.git] / src / jalview / gui / OptsAndParamsPage.java
index e584eb7..59e7049 100644 (file)
@@ -26,6 +26,7 @@ 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.LogarithmicParameter;
 
 import java.awt.BorderLayout;
 import java.awt.Component;
@@ -76,8 +77,8 @@ public class OptsAndParamsPage
    */
   boolean compact = false;
 
-  public class OptionBox extends JPanel implements MouseListener,
-          ActionListener
+  public class OptionBox extends JPanel
+          implements MouseListener, ActionListener
   {
     JCheckBox enabled = new JCheckBox();
 
@@ -110,21 +111,20 @@ public class OptsAndParamsPage
       {
         hasLink = true;
 
-        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.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);
       }
       else
       {
         if (desc != null && desc.trim().length() > 0)
         {
-          enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
-                  opt.getDescription()));
+          enabled.setToolTipText(
+                  JvSwingUtils.wrapTooltip(true, opt.getDescription()));
         }
       }
       add(enabled, BorderLayout.NORTH);
@@ -271,9 +271,12 @@ public class OptsAndParamsPage
 
   }
 
-  public class ParamBox extends JPanel implements ChangeListener,
-          ActionListener, MouseListener
+  public class ParamBox extends JPanel
+          implements ChangeListener, ActionListener, MouseListener
   {
+
+    boolean isLogarithmic;
+
     boolean adjusting = false;
 
     boolean choice = false;
@@ -308,7 +311,8 @@ public class OptsAndParamsPage
 
     JTextField valueField = null;
 
-    public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
+    public ParamBox(final OptsParametersContainerI pmlayout,
+            ParameterI parm)
     {
       pmdialogbox = pmlayout;
       finfo = parm.getFurtherDetails();
@@ -325,6 +329,10 @@ public class OptsAndParamsPage
           choice = true;
         }
       }
+      if (parm instanceof LogarithmicParameter)
+      {
+        isLogarithmic = true;
+      }
 
       if (!compact)
       {
@@ -349,20 +357,16 @@ public class OptsAndParamsPage
               && parm.getDescription().trim().length() > 0)
       {
         // Only create description boxes if there actually is a description.
-        ttipText = (JvSwingUtils
-                .wrapTooltip(
-                        true,
-                        parm.getDescription()
-                                + (finfo != null ? "<br><img src=\""
-                                        + linkImageURL
-                                        + "\"/>"
-                                        + MessageManager
-                                                .getString("label.opt_and_params_further_details")
-                                        : "")));
-      }
-
-      JvSwingUtils.mgAddtoLayout(this, ttipText,
-              new JLabel(parm.getName()), controlPanel, "");
+        ttipText = (JvSwingUtils.wrapTooltip(true,
+                parm.getDescription() + (finfo != null ? "<br><img src=\""
+                        + linkImageURL + "\"/>"
+                        + MessageManager.getString(
+                                "label.opt_and_params_further_details")
+                        : "")));
+      }
+
+      JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
+              controlPanel, "");
       updateControls(parm);
       validate();
     }
@@ -399,21 +403,18 @@ public class OptsAndParamsPage
         // Only create description boxes if there actually is a description.
         if (finfo != null)
         {
-          showDesc.setToolTipText(JvSwingUtils.wrapTooltip(
-                  true,
-                  MessageManager
-                          .formatMessage(
-                                  "label.opt_and_params_show_brief_desc_image_link",
-                                  new String[] { linkImageURL
-                                          .toExternalForm() })));
+          showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true,
+                  MessageManager.formatMessage(
+                          "label.opt_and_params_show_brief_desc_image_link",
+                          new String[]
+                          { linkImageURL.toExternalForm() })));
           showDesc.addMouseListener(this);
         }
         else
         {
-          showDesc.setToolTipText(JvSwingUtils.wrapTooltip(
-                  true,
-                  MessageManager
-                          .getString("label.opt_and_params_show_brief_desc")));
+          showDesc.setToolTipText(
+                  JvSwingUtils.wrapTooltip(true, MessageManager.getString(
+                          "label.opt_and_params_show_brief_desc")));
         }
         showDesc.addActionListener(new ActionListener()
         {
@@ -578,14 +579,38 @@ public class OptsAndParamsPage
     {
       if (!adjusting)
       {
-        valueField.setText(""
-                + ((integ) ? ("" + slider.getValue()) : ("" + slider
-                        .getValue() / 1000f)));
+        if (!isLogarithmic)
+        {
+          valueField.setText("" + ((integ) ? ("" + slider.getValue())
+                  : ("" + slider.getValue() / 1000f)));
+        }
+        else
+        {
+          Double base = ((LogarithmicParameter) parameter).getBase();
+          Double value = Math.pow(
+                  base,
+                  slider.getValue());
+          valueField.setText(formatDouble(value));
+        }
         checkIfModified();
       }
 
     }
 
+    public String formatDouble(Double value)
+    {
+      String string = "";
+      if (value < 0.0000001)
+      {
+        string = String.format("%3.3e", value);
+      }
+      else
+      {
+        string = value.toString();
+      }
+      return string;
+    }
+
     public void updateControls(ParameterI parm)
     {
       adjusting = true;
@@ -667,6 +692,7 @@ public class OptsAndParamsPage
     {
       int iVal;
       float fVal;
+      double dVal;
       if (validator != null)
       {
         if (integ)
@@ -708,6 +734,53 @@ public class OptsAndParamsPage
           }
           return new int[] { iVal };
         }
+        else if (isLogarithmic)
+        {
+          double eValue;
+          dVal = 0d;
+          try
+          {
+            valueField.setText(valueField.getText().trim());
+            eValue = Double.valueOf(valueField.getText());
+
+            dVal = Math.log(eValue) / Math
+                    .log(((LogarithmicParameter) parameter).getBase());
+
+            if (validator.getMin() != null
+                    && validator.getMin().doubleValue() > dVal)
+            {
+              dVal = validator.getMin().doubleValue();
+              // TODO: provide visual indication that hard limit was reached for
+              // this parameter
+              // update value field to reflect any bound checking we performed.
+              valueField.setText("" + formatDouble(eValue));
+            }
+            if (validator.getMax() != null
+                    && validator.getMax().doubleValue() < dVal)
+            {
+              dVal = validator.getMax().doubleValue();
+              // TODO: provide visual indication that hard limit was reached for
+              // this parameter
+              // update value field to reflect any bound checking we performed.
+              valueField.setText("" + formatDouble(eValue));
+            }
+          } catch (Exception e)
+          {
+          }
+          ;
+          if (validator.getMin() != null && validator.getMax() != null)
+          {
+            slider.getModel().setRangeProperties((int) (dVal), 1,
+                    (int) (validator.getMin().doubleValue()),
+                    1 + (int) (validator.getMax().doubleValue()),
+                    true);
+          }
+          else
+          {
+            slider.setVisible(false);
+          }
+          return new double[] { dVal };
+        }
         else
         {
           fVal = 0f;
@@ -790,8 +863,9 @@ public class OptsAndParamsPage
   {
 
     JPopupMenu mnu = new JPopupMenu();
-    JMenuItem mitem = new JMenuItem(MessageManager.formatMessage(
-            "label.view_params", new String[] { finfo }));
+    JMenuItem mitem = new JMenuItem(
+            MessageManager.formatMessage("label.view_params", new String[]
+            { finfo }));
     mitem.addActionListener(new ActionListener()
     {
 
@@ -808,9 +882,9 @@ public class OptsAndParamsPage
 
   URL linkImageURL = getClass().getResource("/images/link.gif");
 
-  Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
+  Map<String, OptionBox> optSet = new java.util.LinkedHashMap<>();
 
-  Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
+  Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<>();
 
   public Map<String, OptionBox> getOptSet()
   {
@@ -878,8 +952,8 @@ public class OptsAndParamsPage
       else
       {
         throw new Error(MessageManager.formatMessage(
-                "error.invalid_value_for_option", new String[] { string,
-                    option.getName() }));
+                "error.invalid_value_for_option", new String[]
+                { string, option.getName() }));
       }
 
     }
@@ -911,7 +985,7 @@ public class OptsAndParamsPage
    */
   public List<ArgumentI> getCurrentSettings()
   {
-    List<ArgumentI> argSet = new ArrayList<ArgumentI>();
+    List<ArgumentI> argSet = new ArrayList<>();
     for (OptionBox opts : getOptSet().values())
     {
       OptionI opt = opts.getOptionIfEnabled();