*/
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;
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;
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;
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;
public class OptionBox extends JPanel
implements MouseListener, ActionListener
{
- JCheckBox enabled = new JCheckBox();
+ JCheckBox enabled;
final URL finfo;
OptionI option;
- JLabel optlabel = new JLabel();
-
- JComboBox<String> val = new JComboBox<>();
+ JComboBox<Object> 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.getLabel());
+ enabled.setSelected(opt.isRequired());
- enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
- ((desc == null || desc.trim().length() == 0)
- ? MessageManager.getString(
- "label.opt_and_params_further_details")
- : desc) + "<br><img src=\"" + linkImageURL
- + "\"/>"));
- 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.getLabel()));
}
- 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 + "<br><img src=\"" + linkImageURL
+ + "\"/>";
+ 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)
{
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;
}
boolean adjusting;
- JComboBox<String> choicebox;
+ /*
+ * drop-down list of choice options (if applicable)
+ */
+ JComboBox<Object> choicebox;
+
+ /*
+ * radio buttons as an alternative to combo box
+ */
+ ButtonGroup buttonGroup;
JPanel controlsPanel = new JPanel();
{
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
* 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();
// todo scaleMin, scaleMax could also be final fields
}
}
- else
- {
- isChoiceParameter = parameter.getPossibleValues() != null;
- }
+
+ List<String> possibleValues = parameter.getPossibleValues();
+ isChoiceParameter = possibleValues != null
+ && !possibleValues.isEmpty();
if (compact)
{
{
return;
}
- if (!isChoiceParameter)
- {
- updateSliderFromValueField();
- }
checkIfModified();
}
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 = null;
+ if (parameter instanceof RadioChoiceParameter)
+ {
+ value = buttonGroup.getSelection().getActionCommand();
+ }
+ else if (isChoiceParameter)
{
- prm.setValue((String) choicebox.getSelectedItem());
+ value = getSelectedValue(this.parameter,
+ choicebox.getSelectedIndex());
}
else
{
- prm.setValue(valueField.getText());
+ value = valueField.getText();
}
+ prm.setValue(value);
+
return prm;
}
@Override
public void stateChanged(ChangeEvent e)
{
- if (!adjusting)
+ if (adjusting)
+ {
+ return;
+ }
+ try
{
+ adjusting = true;
if (!isLogarithmicParameter)
{
/*
valueField.setText(formatDouble(value));
}
checkIfModified();
+ } finally
+ {
+ adjusting = false;
}
}
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)
{
- choicebox = new JComboBox<>();
+ buttonGroup = addRadioButtons(parameter, controlsPanel);
+ }
+ else if (isChoiceParameter)
+ {
+ choicebox = buildComboBox(parm);
choicebox.addActionListener(this);
controlsPanel.add(choicebox, BorderLayout.CENTER);
}
@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)
{
}
});
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);
}
}
- if (parm != null)
+ String value = parm.getValue();
+ if (value != null)
{
if (isChoiceParameter)
{
- if (init)
+ if (!(parm instanceof RadioChoiceParameter))
{
- for (String val : parm.getPossibleValues())
- {
- choicebox.addItem(val);
- }
- }
- if (parm.getValue() != null)
- {
- choicebox.setSelectedItem(parm.getValue());
+ choicebox.setSelectedItem(value);
}
}
else
{
- valueField.setText(parm.getValue());
+ valueField.setText(value);
}
}
lastVal = updateSliderFromValueField();
}
/**
+ * 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:
* <ul>
* <li>if a text input, returns the trimmed value</li>
- * <li>if a choice list, returns the selected value</li>
+ * <li>if a choice list or radio button, returns the selected value</li>
* <li>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.</li>
{
if (isChoiceParameter)
{
- return choicebox.getSelectedItem();
+ 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());
/*
{
slider.setVisible(false);
}
- return new Integer(iVal);
+ return Integer.valueOf(iVal);
}
if (isLogarithmicParameter)
try
{
double eValue = Double.valueOf(valueField.getText());
- dVal = Math.log(eValue) * sliderScaleFactor;
+ dVal = Math.log(eValue);
} catch (Exception e)
{
// shouldn't be possible here
* 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;
{
slider.setVisible(false);
}
- return new Float(fVal);
+ return Float.valueOf(fVal);
}
}
}
/**
- * 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
*/
List<ArgumentI> argSet = new ArrayList<>();
for (OptionBox opts : getOptSet().values())
{
- OptionI opt = opts.getOptionIfEnabled();
+ ArgumentI opt = opts.getSelectedOption();
if (opt != null)
{
argSet.add(opt);
}
for (ParamBox parambox : getParamSet().values())
{
- ParameterI parm = parambox.getParameter();
+ ArgumentI parm = parambox.getParameter();
if (parm != null)
{
argSet.add(parm);
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 static JComboBox<Object> buildComboBox(OptionI opt)
+ {
+ JComboBox<Object> cb = null;
+ List<String> displayNames = opt.getDisplayNames();
+ if (displayNames != null)
+ {
+ List<Object> displayNamesObjects = new ArrayList<>();
+ displayNamesObjects.addAll(displayNames);
+ cb = JvSwingUtils.buildComboWithTooltips(displayNamesObjects,
+ 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. Note that this returns the underlying value even if a different
+ * display name is used in the combo box.
+ *
+ * @return
+ */
+ protected static String getSelectedValue(OptionI opt, int sel)
+ {
+ List<String> 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);
+ }
+ return value;
+ }
}