Updated with latest from mchmmer branch
[jalview.git] / src / jalview / gui / OptsAndParamsPage.java
index 5342c90..1505df8 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;
@@ -273,6 +274,9 @@ public class OptsAndParamsPage
   public class ParamBox extends JPanel
           implements ChangeListener, ActionListener, MouseListener
   {
+
+    boolean isLogarithmic;
+
     boolean adjusting = false;
 
     boolean choice = false;
@@ -325,6 +329,10 @@ public class OptsAndParamsPage
           choice = true;
         }
       }
+      if (parm instanceof LogarithmicParameter)
+      {
+        isLogarithmic = true;
+      }
 
       if (!compact)
       {
@@ -571,13 +579,34 @@ 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() / 1000000f);
+          valueField.setText(formatDouble(value));
+        }
         checkIfModified();
       }
 
     }
 
+    public String formatDouble(Double value)
+    {
+      String string = String.format("%3.3f", value);
+      if (value < 0.001)
+      {
+        string = String.format("%3.3e", value);
+      }
+      return string;
+    }
+
     public void updateControls(ParameterI parm)
     {
       adjusting = true;
@@ -648,7 +677,17 @@ public class OptsAndParamsPage
         }
         else
         {
-          valueField.setText(parm.getValue());
+          if (parm instanceof LogarithmicParameter)
+          {
+            Double base = ((LogarithmicParameter) parm).getBase();
+            Double value = Math.pow(base,
+                    Double.parseDouble(parm.getValue()) / 1000000);
+            valueField.setText(formatDouble(value));
+          }
+          else
+          {
+            valueField.setText(parm.getValue());
+          }
         }
       }
       lastVal = updateSliderFromValueField();
@@ -659,6 +698,7 @@ public class OptsAndParamsPage
     {
       int iVal;
       float fVal;
+      double dVal;
       if (validator != null)
       {
         if (integ)
@@ -700,6 +740,54 @@ 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())
+                    * 1000000;
+
+            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;
@@ -801,9 +889,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()
   {
@@ -904,7 +992,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();