2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.BorderLayout;
24 import java.awt.Component;
25 import java.awt.Dimension;
27 import java.awt.GridLayout;
28 import java.awt.Rectangle;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.FocusAdapter;
32 import java.awt.event.FocusEvent;
33 import java.awt.event.KeyEvent;
34 import java.awt.event.KeyListener;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
38 import java.util.ArrayList;
39 import java.util.List;
42 import javax.swing.JButton;
43 import javax.swing.JCheckBox;
44 import javax.swing.JComboBox;
45 import javax.swing.JComponent;
46 import javax.swing.JLabel;
47 import javax.swing.JMenuItem;
48 import javax.swing.JPanel;
49 import javax.swing.JPopupMenu;
50 import javax.swing.JScrollPane;
51 import javax.swing.JTextArea;
52 import javax.swing.JTextField;
53 import javax.swing.border.TitledBorder;
54 import javax.swing.event.ChangeEvent;
55 import javax.swing.event.ChangeListener;
57 import jalview.util.MessageManager;
58 import jalview.ws.params.ArgumentI;
59 import jalview.ws.params.OptionI;
60 import jalview.ws.params.ParameterI;
61 import jalview.ws.params.ValueConstrainI;
62 import jalview.ws.params.ValueConstrainI.ValueType;
63 import net.miginfocom.swing.MigLayout;
66 * GUI generator/manager for options and parameters. Originally abstracted from
67 * the WsJobParameters dialog box.
72 public class OptsAndParamsPage
75 * compact or verbose style parameters
77 boolean compact = false;
79 public class OptionBox extends JPanel
80 implements MouseListener, ActionListener
82 JCheckBox enabled = new JCheckBox();
86 boolean hasLink = false;
88 boolean initEnabled = false;
90 String initVal = null;
94 JLabel optlabel = new JLabel();
96 JComboBox<String> val = new JComboBox<>();
98 public OptionBox(OptionI opt)
101 setLayout(new BorderLayout());
102 enabled.setSelected(opt.isRequired()); // TODO: lock required options
103 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
105 enabled.setText(opt.getName());
106 enabled.addActionListener(this);
107 finfo = option.getFurtherDetails();
108 String desc = opt.getDescription();
113 enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
114 ((desc == null || desc.trim().length() == 0)
115 ? MessageManager.getString(
116 "label.opt_and_params_further_details")
117 : desc) + "<br><img src=\"" + linkImageURL
119 enabled.addMouseListener(this);
123 if (desc != null && desc.trim().length() > 0)
125 enabled.setToolTipText(
126 JvSwingUtils.wrapTooltip(true, opt.getDescription()));
129 add(enabled, BorderLayout.NORTH);
130 for (String str : opt.getPossibleValues())
134 val.setSelectedItem(opt.getValue());
135 if (opt.getPossibleValues().size() > 1)
137 setLayout(new GridLayout(1, 2));
138 val.addActionListener(this);
139 add(val, BorderLayout.SOUTH);
141 // TODO: add actionListeners for popup (to open further info),
142 // and to update list of parameters if an option is enabled
143 // that takes a value. JBPNote: is this TODO still valid ?
148 public void actionPerformed(ActionEvent e)
150 if (e.getSource() != enabled)
152 enabled.setSelected(true);
157 private void checkIfModified()
159 boolean notmod = (initEnabled == enabled.isSelected());
160 if (enabled.isSelected())
164 notmod &= initVal.equals(val.getSelectedItem());
168 // compare against default service setting
169 notmod &= option.getValue() == null
170 || option.getValue().equals(val.getSelectedItem());
175 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
176 : val.getSelectedItem() != initVal;
178 poparent.argSetModified(this, !notmod);
181 public OptionI getOptionIfEnabled()
183 if (!enabled.isSelected())
187 OptionI opt = option.copy();
188 if (opt.getPossibleValues() != null
189 && opt.getPossibleValues().size() == 1)
191 // Hack to make sure the default value for an enabled option with only
192 // one value is actually returned
193 opt.setValue(opt.getPossibleValues().get(0));
195 if (val.getSelectedItem() != null)
197 opt.setValue((String) val.getSelectedItem());
201 if (option.getValue() != null)
203 opt.setValue(option.getValue());
210 public void mouseClicked(MouseEvent e)
212 if (e.isPopupTrigger()) // for Windows
214 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
219 public void mouseEntered(MouseEvent e)
221 // TODO Auto-generated method stub
226 public void mouseExited(MouseEvent e)
228 // TODO Auto-generated method stub
233 public void mousePressed(MouseEvent e)
235 if (e.isPopupTrigger()) // Mac
237 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
242 public void mouseReleased(MouseEvent e)
246 public void resetToDefault(boolean setDefaultParams)
248 enabled.setSelected(false);
249 if (option.isRequired()
250 || (setDefaultParams && option.getValue() != null))
252 // Apply default value
253 selectOption(option, option.getValue());
257 public void setInitialValue()
259 initEnabled = enabled.isSelected();
260 if (option.getPossibleValues() != null
261 && option.getPossibleValues().size() > 1)
263 initVal = (String) val.getSelectedItem();
267 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
273 public class ParamBox extends JPanel
274 implements ChangeListener, ActionListener, MouseListener
276 boolean adjusting = false;
278 boolean choice = false;
280 JComboBox<String> choicebox;
282 JPanel controlPanel = new JPanel();
284 boolean descisvisible = false;
286 JScrollPane descPanel = new JScrollPane();
290 boolean integ = false;
294 ParameterI parameter;
296 final OptsParametersContainerI pmdialogbox;
298 JPanel settingPanel = new JPanel();
300 JButton showDesc = new JButton();
302 Slider slider = null;
304 JTextArea string = new JTextArea();
306 ValueConstrainI validator = null;
308 JTextField valueField = null;
310 public ParamBox(final OptsParametersContainerI pmlayout,
313 pmdialogbox = pmlayout;
314 finfo = parm.getFurtherDetails();
315 validator = parm.getValidValue();
317 if (validator != null)
319 integ = validator.getType() == ValueType.Integer;
323 if (parameter.getPossibleValues() != null)
331 makeExpanderParam(parm);
335 makeCompactParam(parm);
340 private void makeCompactParam(ParameterI parm)
342 setLayout(new MigLayout("", "[][grow]"));
344 String ttipText = null;
346 controlPanel.setLayout(new BorderLayout());
348 if (parm.getDescription() != null
349 && parm.getDescription().trim().length() > 0)
351 // Only create description boxes if there actually is a description.
352 ttipText = (JvSwingUtils.wrapTooltip(true,
353 parm.getDescription() + (finfo != null ? "<br><img src=\""
354 + linkImageURL + "\"/>"
355 + MessageManager.getString(
356 "label.opt_and_params_further_details")
360 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
362 updateControls(parm);
366 private void makeExpanderParam(final ParameterI parm)
368 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
369 setBorder(new TitledBorder(parm.getName()));
371 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
372 showDesc.setText("+");
373 string.setFont(new Font("Verdana", Font.PLAIN, 11));
374 string.setBackground(getBackground());
376 string.setEditable(false);
377 descPanel.getViewport().setView(string);
379 descPanel.setVisible(false);
381 JPanel firstrow = new JPanel();
382 firstrow.setLayout(null);
383 controlPanel.setLayout(new BorderLayout());
384 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
385 PARAM_CLOSEDHEIGHT - 50));
386 firstrow.add(controlPanel);
387 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
388 PARAM_CLOSEDHEIGHT - 30));
390 final ParamBox me = this;
392 if (parm.getDescription() != null
393 && parm.getDescription().trim().length() > 0)
395 // Only create description boxes if there actually is a description.
398 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true,
399 MessageManager.formatMessage(
400 "label.opt_and_params_show_brief_desc_image_link",
402 { linkImageURL.toExternalForm() })));
403 showDesc.addMouseListener(this);
407 showDesc.setToolTipText(
408 JvSwingUtils.wrapTooltip(true, MessageManager.getString(
409 "label.opt_and_params_show_brief_desc")));
411 showDesc.addActionListener(new ActionListener()
415 public void actionPerformed(ActionEvent e)
417 descisvisible = !descisvisible;
418 descPanel.setVisible(descisvisible);
419 descPanel.getVerticalScrollBar().setValue(0);
420 me.setPreferredSize(new Dimension(PARAM_WIDTH,
421 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
423 pmdialogbox.refreshParamLayout();
426 string.setWrapStyleWord(true);
427 string.setLineWrap(true);
428 string.setColumns(32);
429 string.setText(parm.getDescription());
430 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
431 firstrow.add(showDesc);
434 validator = parm.getValidValue();
436 if (validator != null)
438 integ = validator.getType() == ValueType.Integer;
442 if (parameter.getPossibleValues() != null)
447 updateControls(parm);
448 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
449 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
455 * Action on input in text field
458 public void actionPerformed(ActionEvent e)
466 updateSliderFromValueField();
471 private void checkIfModified()
473 Object cstate = getCurrentValue();
474 boolean modified = !cstate.equals(lastVal);
475 pmdialogbox.argSetModified(this, modified);
479 * Answers the current value of the parameter, as text
483 private String getCurrentValue()
485 return choice ? (String) choicebox.getSelectedItem()
486 : valueField.getText();
490 public int getBaseline(int width, int height)
496 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
497 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
499 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
501 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
504 public int getBoxHeight()
506 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
509 public ParameterI getParameter()
511 ParameterI prm = parameter.copy();
514 prm.setValue((String) choicebox.getSelectedItem());
518 prm.setValue(valueField.getText());
525 // reset the widget's initial value.
530 public void mouseClicked(MouseEvent e)
532 if (e.isPopupTrigger()) // for Windows
534 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
539 public void mouseEntered(MouseEvent e)
541 // TODO Auto-generated method stub
546 public void mouseExited(MouseEvent e)
548 // TODO Auto-generated method stub
553 public void mousePressed(MouseEvent e)
555 if (e.isPopupTrigger()) // for Mac
557 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
562 public void mouseReleased(MouseEvent e)
564 // TODO Auto-generated method stub
569 * Action on change of slider value
572 public void stateChanged(ChangeEvent e)
576 float value = slider.getSliderValue();
577 valueField.setText(integ ? Integer.toString((int) value)
578 : Float.toString(value));
583 public void updateControls(ParameterI parm)
586 boolean init = (choicebox == null && valueField == null);
591 choicebox = new JComboBox<>();
592 choicebox.addActionListener(this);
593 controlPanel.add(choicebox, BorderLayout.CENTER);
597 valueField = new JTextField();
598 valueField.addActionListener(this);
599 valueField.addKeyListener(new KeyListener()
603 public void keyTyped(KeyEvent e)
608 public void keyReleased(KeyEvent e)
612 if (valueField.getText().trim().length() > 0)
614 actionPerformed(null);
620 public void keyPressed(KeyEvent e)
624 valueField.addFocusListener(new FocusAdapter()
628 public void focusLost(FocusEvent e)
630 actionPerformed(null);
634 valueField.setPreferredSize(new Dimension(60, 25));
635 valueField.setText(parm.getValue());
636 slider = makeSlider(parm.getValidValue());
637 updateSliderFromValueField();
638 slider.addChangeListener(this);
640 controlPanel.add(slider, BorderLayout.WEST);
641 controlPanel.add(valueField, BorderLayout.EAST);
651 List<String> vals = parm.getPossibleValues();
652 for (String val : vals)
654 choicebox.addItem(val);
658 if (parm.getValue() != null)
660 choicebox.setSelectedItem(parm.getValue());
665 valueField.setText(parm.getValue());
668 lastVal = getCurrentValue();
672 private Slider makeSlider(ValueConstrainI validValue)
674 if (validValue != null)
676 final Number minValue = validValue.getMin();
677 final Number maxValue = validValue.getMax();
678 if (minValue != null && maxValue != null)
680 return new Slider(minValue.floatValue(), maxValue.floatValue(),
681 minValue.floatValue());
686 * otherwise, a nominal slider which will not be visible
688 return new Slider(0, 100, 50);
691 public void updateSliderFromValueField()
693 if (validator != null)
695 final Number minValue = validator.getMin();
696 final Number maxValue = validator.getMax();
702 valueField.setText(valueField.getText().trim());
703 iVal = Integer.valueOf(valueField.getText());
704 if (minValue != null && minValue.intValue() > iVal)
706 iVal = minValue.intValue();
707 // TODO: provide visual indication that hard limit was reached for
710 if (maxValue != null && maxValue.intValue() < iVal)
712 iVal = maxValue.intValue();
714 } catch (NumberFormatException e)
716 jalview.bin.Console.errPrintln(e.toString());
718 if (minValue != null || maxValue != null)
720 valueField.setText(String.valueOf(iVal));
721 slider.setSliderValue(iVal);
725 slider.setVisible(false);
733 valueField.setText(valueField.getText().trim());
734 fVal = Float.valueOf(valueField.getText());
735 if (minValue != null && minValue.floatValue() > fVal)
737 fVal = minValue.floatValue();
738 // TODO: provide visual indication that hard limit was reached for
740 // update value field to reflect any bound checking we performed.
741 valueField.setText("" + fVal);
743 if (maxValue != null && maxValue.floatValue() < fVal)
745 fVal = maxValue.floatValue();
746 // TODO: provide visual indication that hard limit was reached for
748 // update value field to reflect any bound checking we performed.
749 valueField.setText("" + fVal);
751 } catch (NumberFormatException e)
753 jalview.bin.Console.errPrintln(e.toString());
755 if (minValue != null && maxValue != null)
757 slider.setSliderModel(minValue.floatValue(),
758 maxValue.floatValue(), fVal);
762 slider.setVisible(false);
770 slider.setVisible(false);
776 public static final int PARAM_WIDTH = 340;
778 public static final int PARAM_HEIGHT = 150;
780 public static final int PARAM_CLOSEDHEIGHT = 80;
782 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
784 this(paramContainer, false);
787 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
790 poparent = paramContainer;
791 this.compact = compact;
794 public static void showUrlPopUp(JComponent invoker, final String finfo,
798 JPopupMenu mnu = new JPopupMenu();
799 JMenuItem mitem = new JMenuItem(
800 MessageManager.formatMessage("label.view_params", new String[]
802 mitem.addActionListener(new ActionListener()
806 public void actionPerformed(ActionEvent e)
808 Desktop.showUrl(finfo);
813 mnu.show(invoker, x, y);
816 URL linkImageURL = getClass().getResource("/images/link.gif");
818 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<>();
820 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<>();
822 public Map<String, OptionBox> getOptSet()
827 public void setOptSet(Map<String, OptionBox> optSet)
829 this.optSet = optSet;
832 public Map<String, ParamBox> getParamSet()
837 public void setParamSet(Map<String, ParamBox> paramSet)
839 this.paramSet = paramSet;
842 OptsParametersContainerI poparent;
844 OptionBox addOption(OptionI opt)
846 OptionBox cb = optSet.get(opt.getName());
849 cb = new OptionBox(opt);
850 optSet.put(opt.getName(), cb);
851 // jobOptions.add(cb, FlowLayout.LEFT);
856 ParamBox addParameter(ParameterI arg)
858 ParamBox pb = paramSet.get(arg.getName());
861 pb = new ParamBox(poparent, arg);
862 paramSet.put(arg.getName(), pb);
863 // paramList.add(pb);
866 // take the defaults from the parameter
867 pb.updateControls(arg);
871 void selectOption(OptionI option, String string)
873 OptionBox cb = optSet.get(option.getName());
876 cb = addOption(option);
878 cb.enabled.setSelected(string != null); // initial state for an option.
881 if (option.getPossibleValues().contains(string))
883 cb.val.setSelectedItem(string);
887 throw new Error(MessageManager.formatMessage(
888 "error.invalid_value_for_option", new String[]
889 { string, option.getName() }));
893 if (option.isRequired() && !cb.enabled.isSelected())
895 // TODO: indicate paramset is not valid.. option needs to be selected!
897 cb.setInitialValue();
900 void setParameter(ParameterI arg)
902 ParamBox pb = paramSet.get(arg.getName());
909 pb.updateControls(arg);
915 * recover options and parameters from GUI
919 public List<ArgumentI> getCurrentSettings()
921 List<ArgumentI> argSet = new ArrayList<>();
922 for (OptionBox opts : getOptSet().values())
924 OptionI opt = opts.getOptionIfEnabled();
930 for (ParamBox parambox : getParamSet().values())
932 ParameterI parm = parambox.getParameter();