From d59d262069ac37d288e72f6caba4a45e1a3ebb07 Mon Sep 17 00:00:00 2001 From: tzvanaalten Date: Wed, 23 Aug 2017 15:33:47 +0100 Subject: [PATCH] JAL-2629 add logarithmic parameter for logarithmic slider --- src/jalview/gui/OptsAndParamsPage.java | 91 +++++++++++++++++- src/jalview/ws/params/ValueConstrainI.java | 2 +- .../ws/params/simple/LogarithmicParameter.java | 101 ++++++++++++++++++++ 3 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 src/jalview/ws/params/simple/LogarithmicParameter.java diff --git a/src/jalview/gui/OptsAndParamsPage.java b/src/jalview/gui/OptsAndParamsPage.java index 5342c90..59e7049 100644 --- a/src/jalview/gui/OptsAndParamsPage.java +++ b/src/jalview/gui/OptsAndParamsPage.java @@ -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 optSet = new java.util.LinkedHashMap(); + Map optSet = new java.util.LinkedHashMap<>(); - Map paramSet = new java.util.LinkedHashMap(); + Map paramSet = new java.util.LinkedHashMap<>(); public Map getOptSet() { @@ -904,7 +985,7 @@ public class OptsAndParamsPage */ public List getCurrentSettings() { - List argSet = new ArrayList(); + List argSet = new ArrayList<>(); for (OptionBox opts : getOptSet().values()) { OptionI opt = opts.getOptionIfEnabled(); diff --git a/src/jalview/ws/params/ValueConstrainI.java b/src/jalview/ws/params/ValueConstrainI.java index da46745..47f454a 100644 --- a/src/jalview/ws/params/ValueConstrainI.java +++ b/src/jalview/ws/params/ValueConstrainI.java @@ -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 index 0000000..4944d6b --- /dev/null +++ b/src/jalview/ws/params/simple/LogarithmicParameter.java @@ -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; + } +} -- 1.7.10.2