X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FOptsAndParamsPage.java;h=298057fcac0969cf05eeb4ef1a99a7bec2c3e05a;hb=fa18a41c0cd9438a527615cce1d49d49a426e096;hp=fcb293cb0c579bc1f1bfafced2ed7519eb83cdde;hpb=f024e66fd13fc3c4fb251291b69c36ad9ff5c8d8;p=jalview.git diff --git a/src/jalview/gui/OptsAndParamsPage.java b/src/jalview/gui/OptsAndParamsPage.java index fcb293c..298057f 100644 --- a/src/jalview/gui/OptsAndParamsPage.java +++ b/src/jalview/gui/OptsAndParamsPage.java @@ -21,6 +21,7 @@ package jalview.gui; import jalview.util.MessageManager; +import jalview.ws.jws2.dm.JabaOption; import jalview.ws.params.ArgumentI; import jalview.ws.params.OptionI; import jalview.ws.params.ParameterI; @@ -101,7 +102,7 @@ public class OptsAndParamsPage public class OptionBox extends JPanel implements MouseListener, ActionListener { - JCheckBox enabled = new JCheckBox(); + JCheckBox enabled; final URL finfo; @@ -113,58 +114,91 @@ public class OptsAndParamsPage OptionI option; - JLabel optlabel = new JLabel(); - - JComboBox val = new JComboBox<>(); + JComboBox val; + /** + * Constructs and adds labels and controls to the panel for one Option + * + * @param opt + */ public OptionBox(OptionI opt) { option = opt; setLayout(new FlowLayout(FlowLayout.LEFT)); - enabled.setSelected(opt.isRequired()); // TODO: lock required options - enabled.setFont(new Font("Verdana", Font.PLAIN, 11)); - enabled.setText(""); - enabled.setText(opt.getName()); - enabled.addActionListener(this); - finfo = option.getFurtherDetails(); - String desc = opt.getDescription(); - if (finfo != null) - { - hasLink = true; + enabled = new JCheckBox(opt.getName()); + enabled.setSelected(opt.isRequired()); - enabled.setToolTipText(JvSwingUtils.wrapTooltip(true, - ((desc == null || desc.trim().length() == 0) - ? MessageManager.getString( - "label.opt_and_params_further_details") - : desc) + "
")); - enabled.addMouseListener(this); - } - else + /* + * If option is required, show a label, if optional a checkbox + * (but not for Jabaws pending JWS-126 resolution) + */ + if (opt.isRequired() && !(opt instanceof JabaOption)) { - if (desc != null && desc.trim().length() > 0) - { - enabled.setToolTipText( - JvSwingUtils.wrapTooltip(true, opt.getDescription())); - } + finfo = null; + add(new JLabel(opt.getName())); } - add(enabled); - for (String str : opt.getPossibleValues()) + else { - val.addItem(str); + finfo = option.getFurtherDetails(); + configureCheckbox(opt); + add(enabled); } + + /* + * construct the choice box with possible values, + * or their display names if provided + */ + val = buildComboBox(opt); val.setSelectedItem(opt.getValue()); + + /* + * only show the choicebox if there is more than one option, + * or the option is mandatory + */ if (opt.getPossibleValues().size() > 1 || opt.isRequired()) { val.addActionListener(this); add(val); } - // TODO: add actionListeners for popup (to open further info), - // and to update list of parameters if an option is enabled - // that takes a value. JBPNote: is this TODO still valid ? + setInitialValue(); } + /** + * Configures the checkbox that controls whether or not the option is + * selected + * + * @param opt + */ + protected void configureCheckbox(OptionI opt) + { + enabled.setFont(new Font("Verdana", Font.PLAIN, 11)); + enabled.addActionListener(this); + final String desc = opt.getDescription(); + if (finfo != null) + { + hasLink = true; + String description = desc; + if (desc == null || desc.trim().isEmpty()) + { + description = MessageManager + .getString("label.opt_and_params_further_details"); + } + description = description + "
"; + String text = JvSwingUtils.wrapTooltip(true, description); + enabled.setToolTipText(text); + enabled.addMouseListener(this); // for popup menu to show link + } + else + { + if (desc != null && desc.trim().length() > 0) + { + enabled.setToolTipText(JvSwingUtils.wrapTooltip(true, desc)); + } + } + } + @Override public void actionPerformed(ActionEvent e) { @@ -199,31 +233,21 @@ public class OptsAndParamsPage poparent.argSetModified(this, !notmod); } - public OptionI getOptionIfEnabled() + /** + * Answers null if the option is not selected, else a new Option holding the + * selected value + * + * @return + */ + public ArgumentI getSelectedOption() { if (!enabled.isSelected()) { return null; } + String value = getSelectedValue(option, val.getSelectedIndex()); OptionI opt = option.copy(); - if (opt.getPossibleValues() != null - && opt.getPossibleValues().size() == 1) - { - // Hack to make sure the default value for an enabled option with only - // one value is actually returned - opt.setValue(opt.getPossibleValues().get(0)); - } - if (val.getSelectedItem() != null) - { - opt.setValue((String) val.getSelectedItem()); - } - else - { - if (option.getValue() != null) - { - opt.setValue(option.getValue()); - } - } + opt.setValue(value); return opt; } @@ -314,6 +338,8 @@ public class OptsAndParamsPage boolean isIntegerParameter; + boolean isStringParameter; + boolean adjusting; JComboBox choicebox; @@ -358,12 +384,15 @@ public class OptsAndParamsPage { ValueType type = validator.getType(); isIntegerParameter = type == ValueType.Integer; + isStringParameter = type == ValueType.String; /* * ensure slider has an integer range corresponding to * the min-max range of the parameter */ - if (validator.getMin() != null && !isIntegerParameter) + if (validator.getMin() != null && validator.getMax() != null + // && !isIntegerParameter + && !isStringParameter) { double min = validator.getMin().doubleValue(); double max = validator.getMax().doubleValue(); @@ -376,10 +405,10 @@ public class OptsAndParamsPage // todo scaleMin, scaleMax could also be final fields } } - else - { - isChoiceParameter = parameter.getPossibleValues() != null; - } + + List possibleValues = parameter.getPossibleValues(); + isChoiceParameter = possibleValues != null + && !possibleValues.isEmpty(); if (compact) { @@ -545,13 +574,23 @@ public class OptsAndParamsPage */ private void checkIfModified() { - Object newValue = updateSliderFromValueField(); - boolean modified = true; - if (newValue.getClass() == lastVal.getClass()) + if (!adjusting) { - modified = !newValue.equals(lastVal); + try + { + adjusting = true; + Object newValue = updateSliderFromValueField(); + boolean modified = true; + if (newValue.getClass() == lastVal.getClass()) + { + modified = !newValue.equals(lastVal); + } + pmdialogbox.argSetModified(this, modified); + } finally + { + adjusting = false; + } } - pmdialogbox.argSetModified(this, modified); } @Override @@ -569,12 +608,18 @@ 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) { - prm.setValue((String) choicebox.getSelectedItem()); + String value = getSelectedValue(this.parameter, choicebox.getSelectedIndex()); + prm.setValue(value); } else { @@ -625,8 +670,13 @@ public class OptsAndParamsPage @Override public void stateChanged(ChangeEvent e) { - if (!adjusting) + if (adjusting) + { + return; + } + try { + adjusting = true; if (!isLogarithmicParameter) { /* @@ -644,6 +694,9 @@ public class OptsAndParamsPage valueField.setText(formatDouble(value)); } checkIfModified(); + } finally + { + adjusting = false; } } @@ -680,8 +733,7 @@ public class OptsAndParamsPage { if (isChoiceParameter) { - choicebox = new JComboBox<>(); - choicebox.addActionListener(this); + choicebox = buildComboBox(parm); controlsPanel.add(choicebox, BorderLayout.CENTER); } else @@ -696,7 +748,9 @@ public class OptsAndParamsPage @Override public void keyReleased(KeyEvent e) { - if (e.isActionKey()) + int keyCode = e.getKeyCode(); + if (e.isActionKey() && keyCode != KeyEvent.VK_LEFT + && keyCode != KeyEvent.VK_RIGHT) { if (valueField.getText().trim().length() > 0) { @@ -711,26 +765,9 @@ public class OptsAndParamsPage } } - if (parm != null) + if (!isChoiceParameter && parm != null) { - if (isChoiceParameter) - { - if (init) - { - for (String val : parm.getPossibleValues()) - { - choicebox.addItem(val); - } - } - if (parm.getValue() != null) - { - choicebox.setSelectedItem(parm.getValue()); - } - } - else - { - valueField.setText(parm.getValue()); - } + valueField.setText(parm.getValue()); } lastVal = updateSliderFromValueField(); adjusting = false; @@ -752,11 +789,11 @@ public class OptsAndParamsPage */ Object updateSliderFromValueField() { - if (validator == null) + if (validator == null || isStringParameter) { if (isChoiceParameter) { - return choicebox.getSelectedItem(); + return getSelectedValue(this.parameter, choicebox.getSelectedIndex()); } slider.setVisible(false); return valueField.getText().trim(); @@ -994,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 */ @@ -1003,7 +1042,7 @@ public class OptsAndParamsPage List argSet = new ArrayList<>(); for (OptionBox opts : getOptSet().values()) { - OptionI opt = opts.getOptionIfEnabled(); + ArgumentI opt = opts.getSelectedOption(); if (opt != null) { argSet.add(opt); @@ -1011,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); @@ -1020,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; + } }