From fa18a41c0cd9438a527615cce1d49d49a426e096 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 14 May 2018 14:19:31 +0100 Subject: [PATCH] JAL-2989 refactoring to reduce code duplication --- src/jalview/gui/OptsAndParamsPage.java | 165 +++++++++++++++----------------- 1 file changed, 77 insertions(+), 88 deletions(-) diff --git a/src/jalview/gui/OptsAndParamsPage.java b/src/jalview/gui/OptsAndParamsPage.java index b13d77d..298057f 100644 --- a/src/jalview/gui/OptsAndParamsPage.java +++ b/src/jalview/gui/OptsAndParamsPage.java @@ -148,20 +148,7 @@ public class OptsAndParamsPage * construct the choice box with possible values, * or their display names if provided */ - List displayNames = opt.getDisplayNames(); - if (displayNames != null) - { - val = JvSwingUtils.buildComboWithTooltips(displayNames, - opt.getPossibleValues()); - } - else - { - val = new JComboBox<>(); - for (String v : opt.getPossibleValues()) - { - val.addItem(v); - } - } + val = buildComboBox(opt); val.setSelectedItem(opt.getValue()); /* @@ -252,51 +239,18 @@ public class OptsAndParamsPage * * @return */ - public OptionI getSelectedOption() + public ArgumentI getSelectedOption() { if (!enabled.isSelected()) { return null; } - String value = getSelectedValue(); + String value = getSelectedValue(option, val.getSelectedIndex()); OptionI opt = option.copy(); opt.setValue(value); return opt; } - /* - * Answers the value corresponding to the selected item in the choice combo - * box. If display names were not provided, this is simply the selected - * value. If display names were provided, it is the value corresponding to - * the selected item index. - * - * @return - */ - protected String getSelectedValue() - { - List possibleValues = option.getPossibleValues(); - String value = null; - if (possibleValues != null && possibleValues.size() == 1) - { - // Hack to make sure the default value for an enabled option with only - // one value is actually returned even if this.val is not displayed - value = possibleValues.get(0); - } - else - { - int sel = val.getSelectedIndex(); - if (sel >= 0 && sel < possibleValues.size()) - { - value = possibleValues.get(sel); - } - else - { - value = option.getValue(); - } - } - return value; - } - @Override public void mouseClicked(MouseEvent e) { @@ -654,12 +608,17 @@ public class OptsAndParamsPage return Component.BaselineResizeBehavior.CONSTANT_ASCENT; } - public ParameterI getParameter() + /** + * Answers an argument holding the value entered or selected in the dialog + * + * @return + */ + public ArgumentI getParameter() { ParameterI prm = parameter.copy(); if (isChoiceParameter) { - String value = getChoice(); + String value = getSelectedValue(this.parameter, choicebox.getSelectedIndex()); prm.setValue(value); } else @@ -669,24 +628,6 @@ public class OptsAndParamsPage return prm; } - /** - * Answers the value corresponding to the selected item in the choice combo - * box. If display names were not provided, this is simply the selected - * value. If display names were provided, it is the value corresponding to - * the selected item index. - * - * @return - */ - protected String getChoice() - { - int sel = choicebox.getSelectedIndex(); - if (sel < 0 || sel >= parameter.getPossibleValues().size()) - { - return null; - } - return parameter.getPossibleValues().get(sel); - } - public void init() { // reset the widget's initial value. @@ -788,25 +729,11 @@ public class OptsAndParamsPage { adjusting = true; boolean init = (choicebox == null && valueField == null); - List displayNames = parm.getDisplayNames(); if (init) { if (isChoiceParameter) { - if (displayNames != null) - { - choicebox = JvSwingUtils.buildComboWithTooltips(displayNames, - parm.getPossibleValues()); - } - else - { - choicebox = new JComboBox<>(); - for (String val : parm.getPossibleValues()) - { - choicebox.addItem(val); - } - } - choicebox.addActionListener(this); + choicebox = buildComboBox(parm); controlsPanel.add(choicebox, BorderLayout.CENTER); } else @@ -866,7 +793,7 @@ public class OptsAndParamsPage { if (isChoiceParameter) { - return getChoice(); + return getSelectedValue(this.parameter, choicebox.getSelectedIndex()); } slider.setVisible(false); return valueField.getText().trim(); @@ -1104,7 +1031,9 @@ public class OptsAndParamsPage } /** - * recover options and parameters from GUI + * Answers a list of arguments representing all the options and arguments + * selected on the dialog, holding their chosen or input values. Optional + * parameters which were not selected are not included. * * @return */ @@ -1113,7 +1042,7 @@ public class OptsAndParamsPage List argSet = new ArrayList<>(); for (OptionBox opts : getOptSet().values()) { - OptionI opt = opts.getSelectedOption(); + ArgumentI opt = opts.getSelectedOption(); if (opt != null) { argSet.add(opt); @@ -1121,7 +1050,7 @@ public class OptsAndParamsPage } for (ParamBox parambox : getParamSet().values()) { - ParameterI parm = parambox.getParameter(); + ArgumentI parm = parambox.getParameter(); if (parm != null) { argSet.add(parm); @@ -1130,4 +1059,64 @@ public class OptsAndParamsPage return argSet; } + + /** + * A helper method that constructs and returns a CombBox for choice of the + * possible option values. If display names are provided, then these are added + * as options, otherwise the actual values are added. + * + * @param opt + * @return + */ + protected JComboBox buildComboBox(OptionI opt) + { + JComboBox cb = null; + List displayNames = opt.getDisplayNames(); + if (displayNames != null) + { + cb = JvSwingUtils.buildComboWithTooltips(displayNames, + opt.getPossibleValues()); + } + else + { + cb = new JComboBox<>(); + for (String v : opt.getPossibleValues()) + { + cb.addItem(v); + } + } + return cb; + } + + /* + * Answers the value corresponding to the selected item in the choice combo + * box. If display names were not provided, this is simply the selected + * value. If display names were provided, it is the value corresponding to + * the selected item index. + * + * @return + */ + protected static String getSelectedValue(OptionI opt, int sel) + { + List possibleValues = opt.getPossibleValues(); + String value = null; + if (possibleValues != null && possibleValues.size() == 1) + { + // Hack to make sure the default value for an enabled option with only + // one value is actually returned even if this.val is not displayed + value = possibleValues.get(0); + } + else + { + if (sel >= 0 && sel < possibleValues.size()) + { + value = possibleValues.get(sel); + } + else + { + value = opt.getValue(); + } + } + return value; + } } -- 1.7.10.2