JAL-2629 add logarithmic parameter for logarithmic slider
authortzvanaalten <tzvanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Wed, 23 Aug 2017 14:33:47 +0000 (15:33 +0100)
committertzvanaalten <tzvanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Wed, 23 Aug 2017 14:33:47 +0000 (15:33 +0100)
src/jalview/gui/OptsAndParamsPage.java
src/jalview/ws/params/ValueConstrainI.java
src/jalview/ws/params/simple/LogarithmicParameter.java [new file with mode: 0644]

index 5342c90..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;
@@ -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,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;
@@ -659,6 +692,7 @@ public class OptsAndParamsPage
     {
       int iVal;
       float fVal;
+      double dVal;
       if (validator != null)
       {
         if (integ)
@@ -700,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;
@@ -801,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()
   {
@@ -904,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();
index da46745..47f454a 100644 (file)
@@ -29,7 +29,7 @@ public interface ValueConstrainI
 
   public enum ValueType
   {
-    Integer, Float, String
+    Integer, Float, String, Double
   };
 
   ValueType getType();
diff --git a/src/jalview/ws/params/simple/LogarithmicParameter.java b/src/jalview/ws/params/simple/LogarithmicParameter.java
new file mode 100644 (file)
index 0000000..4944d6b
--- /dev/null
@@ -0,0 +1,101 @@
+package jalview.ws.params.simple;
+
+import jalview.ws.params.ParameterI;
+import jalview.ws.params.ValueConstrainI;
+
+/**
+ * 
+ * @author TZVanaalten
+ *
+ */
+public class LogarithmicParameter extends Option implements ParameterI
+{
+  Double defval;
+
+  Double min, max;
+
+  Double base;
+
+  @Override
+  public ValueConstrainI getValidValue()
+  {
+    return new ValueConstrainI()
+    {
+
+      @Override
+      public ValueType getType()
+      {
+        return ValueType.Double;
+      }
+
+      @Override
+      public Number getMin()
+      {
+        if (min < max)
+        {
+          return min;
+        }
+        else
+        {
+          return null;
+        }
+      }
+
+      @Override
+      public Number getMax()
+      {
+        if (min < max)
+        {
+          return max;
+        }
+        else
+        {
+          return null;
+        }
+      }
+    };
+  }
+
+  public LogarithmicParameter(LogarithmicParameter parm)
+  {
+    super(parm);
+    max = parm.max;
+    min = parm.min;
+    base = parm.base;
+  }
+
+  public LogarithmicParameter(String name, String description,
+          boolean required, Double defValue, Double min, Double max,
+          Double base)
+  {
+    super(name, description, required, String.valueOf(defValue), null, null,
+            null);
+    defval = defValue;
+    this.min = min;
+    this.max = max;
+    this.base = base;
+  }
+
+  public LogarithmicParameter(String name, String description,
+          boolean required, Double defValue, Double value, Double min,
+          Double max, Double base)
+  {
+    super(name, description, required, String.valueOf(defValue),
+            String.valueOf(value), null, null);
+    defval = defValue;
+    this.min = min;
+    this.max = max;
+    this.base = base;
+  }
+
+  @Override
+  public LogarithmicParameter copy()
+  {
+    return new LogarithmicParameter(this);
+  }
+
+  public Double getBase()
+  {
+    return base;
+  }
+}