*/
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.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;
boolean adjusting;
+ /*
+ * drop-down list of choice options (if applicable)
+ */
JComboBox<String> choicebox;
+ /*
+ * radio buttons as an alternative to combo box
+ */
+ ButtonGroup buttonGroup;
+
JPanel controlsPanel = new JPanel();
boolean descriptionIsVisible = false;
{
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
{
return;
}
- if (!isChoiceParameter)
- {
- updateSliderFromValueField();
- }
checkIfModified();
}
public ArgumentI getParameter()
{
ParameterI prm = parameter.copy();
- if (isChoiceParameter)
+ String value = null;
+ if (parameter instanceof RadioChoiceParameter)
+ {
+ value = buttonGroup.getSelection().getActionCommand();
+ }
+ else if (isChoiceParameter)
{
- String value = getSelectedValue(this.parameter, choicebox.getSelectedIndex());
- prm.setValue(value);
+ value = getSelectedValue(this.parameter,
+ choicebox.getSelectedIndex());
}
else
{
- prm.setValue(valueField.getText());
+ value = valueField.getText();
}
+ prm.setValue(value);
+
return prm;
}
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);
}
});
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 (isChoiceParameter)
{
- choicebox.setSelectedItem(value);
+ if (!(parm instanceof RadioChoiceParameter))
+ {
+ choicebox.setSelectedItem(value);
+ }
}
else
{
}
/**
+ * 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 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();
* @param opt
* @return
*/
- protected JComboBox<String> buildComboBox(OptionI opt)
+ protected static JComboBox<String> buildComboBox(OptionI opt)
{
JComboBox<String> cb = null;
List<String> displayNames = opt.getDisplayNames();
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
*/
// 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;
}