X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FOptsAndParamsPage.java;h=046eb819f35281049b82c7b7597e9e7347c60a44;hb=71c5a29a58e5505aaf7d63a8c9f1bea3ae93d629;hp=4cac990e2ba91321d7f5dd8c369caefdbeebe12d;hpb=ccebd462f3616f4c08cda443d246c0c7c0d54e00;p=jalview.git diff --git a/src/jalview/gui/OptsAndParamsPage.java b/src/jalview/gui/OptsAndParamsPage.java index 4cac990..046eb81 100644 --- a/src/jalview/gui/OptsAndParamsPage.java +++ b/src/jalview/gui/OptsAndParamsPage.java @@ -20,6 +20,11 @@ */ package jalview.gui; +import jalview.bin.Cache; +import jalview.io.DataSourceType; +import jalview.io.FileLoader; +import jalview.io.JalviewFileChooser; +import jalview.io.JalviewFileView; import jalview.util.MessageManager; import jalview.ws.jws2.dm.JabaOption; import jalview.ws.params.ArgumentI; @@ -27,12 +32,15 @@ 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.FileParameter; import jalview.ws.params.simple.LogarithmicParameter; +import jalview.ws.params.simple.RadioChoiceParameter; import jalview.ws.params.simple.StringParameter; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; +import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; @@ -41,14 +49,17 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; @@ -57,6 +68,7 @@ import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; +import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSlider; import javax.swing.JTextArea; @@ -114,7 +126,7 @@ public class OptsAndParamsPage OptionI option; - JComboBox val; + JComboBox val; /** * Constructs and adds labels and controls to the panel for one Option @@ -125,7 +137,7 @@ public class OptsAndParamsPage { option = opt; setLayout(new FlowLayout(FlowLayout.LEFT)); - enabled = new JCheckBox(opt.getName()); + enabled = new JCheckBox(opt.getLabel()); enabled.setSelected(opt.isRequired()); /* @@ -135,7 +147,7 @@ public class OptsAndParamsPage if (opt.isRequired() && !(opt instanceof JabaOption)) { finfo = null; - add(new JLabel(opt.getName())); + add(new JLabel(opt.getLabel())); } else { @@ -342,7 +354,15 @@ public class OptsAndParamsPage boolean adjusting; - JComboBox choicebox; + /* + * drop-down list of choice options (if applicable) + */ + JComboBox choicebox; + + /* + * radio buttons as an alternative to combo box + */ + ButtonGroup buttonGroup; JPanel controlsPanel = new JPanel(); @@ -384,7 +404,8 @@ public class OptsAndParamsPage { ValueType type = validator.getType(); isIntegerParameter = type == ValueType.Integer; - isStringParameter = type == ValueType.String; + isStringParameter = type == ValueType.String + || type == ValueType.File; /* * ensure slider has an integer range corresponding to @@ -561,10 +582,6 @@ public class OptsAndParamsPage { return; } - if (!isChoiceParameter) - { - updateSliderFromValueField(); - } checkIfModified(); } @@ -606,15 +623,22 @@ public class OptsAndParamsPage public ArgumentI getParameter() { ParameterI prm = parameter.copy(); - if (isChoiceParameter) + String value = null; + if (parameter instanceof RadioChoiceParameter) { - String value = getSelectedValue(this.parameter, choicebox.getSelectedIndex()); - prm.setValue(value); + value = buttonGroup.getSelection().getActionCommand(); + } + else if (isChoiceParameter) + { + value = getSelectedValue(this.parameter, + choicebox.getSelectedIndex()); } else { - prm.setValue(valueField.getText()); + value = valueField.getText(); } + prm.setValue(value); + return prm; } @@ -718,10 +742,15 @@ public class OptsAndParamsPage void updateControls(ParameterI parm) { adjusting = true; - boolean init = (choicebox == null && valueField == null); + boolean init = (choicebox == null && valueField == null + && buttonGroup == null); if (init) { - if (isChoiceParameter) + if (parm instanceof RadioChoiceParameter) + { + buttonGroup = addRadioButtons(parameter, controlsPanel); + } + else if (isChoiceParameter) { choicebox = buildComboBox(parm); choicebox.addActionListener(this); @@ -751,6 +780,38 @@ public class OptsAndParamsPage } }); valueField.setPreferredSize(new Dimension(65, 25)); + if (parm instanceof FileParameter) + { + valueField.setToolTipText(MessageManager + .getString("label.double_click_to_browse")); + valueField.addMouseListener(new MouseAdapter() + { + @Override + public void mouseClicked(MouseEvent e) + { + if (e.getClickCount() == 2) + { + String dir = Cache.getProperty("LAST_DIRECTORY"); + JalviewFileChooser chooser = new JalviewFileChooser(dir); + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle( + MessageManager.getString("action.select_ddbb")); + + int val = chooser.showOpenDialog(ParamBox.this); + if (val == JalviewFileChooser.APPROVE_OPTION) + { + File choice = chooser.getSelectedFile(); + String path = choice.getPath(); + valueField.setText(path); + Cache.setProperty("LAST_DIRECTORY", choice.getParent()); + FileLoader.updateRecentlyOpened(path, + DataSourceType.FILE); + } + } + } + }); + } + controlsPanel.add(slider, BorderLayout.WEST); controlsPanel.add(valueField, BorderLayout.EAST); } @@ -761,7 +822,10 @@ public class OptsAndParamsPage { if (isChoiceParameter) { - choicebox.setSelectedItem(value); + if (!(parm instanceof RadioChoiceParameter)) + { + choicebox.setSelectedItem(value); + } } else { @@ -773,10 +837,43 @@ public class OptsAndParamsPage } /** + * Adds a panel to comp, containing a label and radio buttons for the choice + * of values of the given option. Returns a ButtonGroup whose members are + * the added radio buttons. + * + * @param option + * @param comp + * + * @return + */ + protected ButtonGroup addRadioButtons(OptionI option, Container comp) + { + ButtonGroup bg = new ButtonGroup(); + JPanel radioPanel = new JPanel(); + radioPanel.add(new JLabel(option.getDescription())); + + String value = option.getValue(); + + for (String opt : option.getPossibleValues()) + { + JRadioButton btn = new JRadioButton(opt); + btn.setActionCommand(opt); + boolean selected = opt.equals(value); + btn.setSelected(selected); + btn.addActionListener(this); + bg.add(btn); + radioPanel.add(btn); + } + comp.add(radioPanel); + + return bg; + } + + /** * Action depends on the type of the input parameter: *
    *
  • if a text input, returns the trimmed value
  • - *
  • if a choice list, returns the selected value
  • + *
  • if a choice list or radio button, returns the selected value
  • *
  • if a value slider and input field, sets the value of the slider from * the value in the text field, limiting it to any defined min-max * range.
  • @@ -792,12 +889,25 @@ public class OptsAndParamsPage { if (isChoiceParameter) { - return getSelectedValue(this.parameter, choicebox.getSelectedIndex()); + if (parameter instanceof RadioChoiceParameter) + { + return buttonGroup.getSelection().getActionCommand(); + } + else + { + return getSelectedValue(this.parameter, + choicebox.getSelectedIndex()); + } } slider.setVisible(false); return valueField.getText().trim(); } + if (validator.getMin() == null || validator.getMax() == null) + { + slider.setVisible(false); + } + valueField.setText(valueField.getText().trim()); /* @@ -846,7 +956,7 @@ public class OptsAndParamsPage { slider.setVisible(false); } - return new Integer(iVal); + return Integer.valueOf(iVal); } if (isLogarithmicParameter) @@ -855,7 +965,7 @@ public class OptsAndParamsPage try { double eValue = Double.valueOf(valueField.getText()); - dVal = Math.log(eValue) * sliderScaleFactor; + dVal = Math.log(eValue); } catch (Exception e) { // shouldn't be possible here @@ -868,14 +978,15 @@ public class OptsAndParamsPage * sliderScaleFactor; double scaleMax = Math.log(validator.getMax().doubleValue()) * sliderScaleFactor; - slider.getModel().setRangeProperties((int) (dVal), 1, + slider.getModel().setRangeProperties( + (int) (sliderScaleFactor * dVal), 1, (int) scaleMin, 1 + (int) scaleMax, true); } else { slider.setVisible(false); } - return new Double(dVal); + return Double.valueOf(dVal); } float fVal = 0f; @@ -900,7 +1011,7 @@ public class OptsAndParamsPage { slider.setVisible(false); } - return new Float(fVal); + return Float.valueOf(fVal); } } @@ -1067,13 +1178,15 @@ public class OptsAndParamsPage * @param opt * @return */ - protected JComboBox buildComboBox(OptionI opt) + protected static JComboBox buildComboBox(OptionI opt) { - JComboBox cb = null; + JComboBox cb = null; List displayNames = opt.getDisplayNames(); if (displayNames != null) { - cb = JvSwingUtils.buildComboWithTooltips(displayNames, + List displayNamesObjects = new ArrayList<>(); + displayNamesObjects.addAll(displayNames); + cb = JvSwingUtils.buildComboWithTooltips(displayNamesObjects, opt.getPossibleValues()); } else @@ -1087,11 +1200,10 @@ public class OptsAndParamsPage 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. + * box. Note that this returns the underlying value even if a different + * display name is used in the combo box. * * @return */ @@ -1105,16 +1217,9 @@ public class OptsAndParamsPage // one value is actually returned even if this.val is not displayed value = possibleValues.get(0); } - else + else if (sel >= 0 && sel < possibleValues.size()) { - if (sel >= 0 && sel < possibleValues.size()) - { - value = possibleValues.get(sel); - } - else - { - value = opt.getValue(); - } + value = possibleValues.get(sel); } return value; }