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 jalview.util.MessageManager;
24 import jalview.ws.params.ArgumentI;
25 import jalview.ws.params.OptionI;
26 import jalview.ws.params.ParameterI;
27 import jalview.ws.params.ValueConstrainI;
28 import jalview.ws.params.ValueConstrainI.ValueType;
30 import java.awt.BorderLayout;
31 import java.awt.Component;
32 import java.awt.Dimension;
34 import java.awt.GridLayout;
35 import java.awt.Rectangle;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.KeyEvent;
39 import java.awt.event.KeyListener;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
43 import java.util.ArrayList;
44 import java.util.List;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JComboBox;
50 import javax.swing.JComponent;
51 import javax.swing.JLabel;
52 import javax.swing.JMenuItem;
53 import javax.swing.JPanel;
54 import javax.swing.JPopupMenu;
55 import javax.swing.JScrollPane;
56 import javax.swing.JTextArea;
57 import javax.swing.JTextField;
58 import javax.swing.border.TitledBorder;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
62 import net.miginfocom.swing.MigLayout;
65 * GUI generator/manager for options and parameters. Originally abstracted from
66 * the WsJobParameters dialog box.
71 public class OptsAndParamsPage
74 * compact or verbose style parameters
76 boolean compact = false;
78 public class OptionBox extends JPanel
79 implements MouseListener, ActionListener
81 JCheckBox enabled = new JCheckBox();
85 boolean hasLink = false;
87 boolean initEnabled = false;
89 String initVal = null;
93 JLabel optlabel = new JLabel();
95 JComboBox val = new JComboBox();
97 public OptionBox(OptionI opt)
100 setLayout(new BorderLayout());
101 enabled.setSelected(opt.isRequired()); // TODO: lock required options
102 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
104 enabled.setText(opt.getName());
105 enabled.addActionListener(this);
106 finfo = option.getFurtherDetails();
107 String desc = opt.getDescription();
112 enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
113 ((desc == null || desc.trim().length() == 0)
114 ? MessageManager.getString(
115 "label.opt_and_params_further_details")
116 : desc) + "<br><img src=\"" + linkImageURL
118 enabled.addMouseListener(this);
122 if (desc != null && desc.trim().length() > 0)
124 enabled.setToolTipText(
125 JvSwingUtils.wrapTooltip(true, opt.getDescription()));
128 add(enabled, BorderLayout.NORTH);
129 for (Object str : opt.getPossibleValues())
133 val.setSelectedItem(opt.getValue());
134 if (opt.getPossibleValues().size() > 1)
136 setLayout(new GridLayout(1, 2));
137 val.addActionListener(this);
138 add(val, BorderLayout.SOUTH);
140 // TODO: add actionListeners for popup (to open further info),
141 // and to update list of parameters if an option is enabled
142 // that takes a value. JBPNote: is this TODO still valid ?
147 public void actionPerformed(ActionEvent e)
149 if (e.getSource() != enabled)
151 enabled.setSelected(true);
156 private void checkIfModified()
158 boolean notmod = (initEnabled == enabled.isSelected());
159 if (enabled.isSelected())
163 notmod &= initVal.equals(val.getSelectedItem());
167 // compare against default service setting
168 notmod &= option.getValue() == null
169 || option.getValue().equals(val.getSelectedItem());
174 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
175 : val.getSelectedItem() != initVal;
177 poparent.argSetModified(this, !notmod);
180 public OptionI getOptionIfEnabled()
182 if (!enabled.isSelected())
186 OptionI opt = option.copy();
187 if (opt.getPossibleValues() != null
188 && opt.getPossibleValues().size() == 1)
190 // Hack to make sure the default value for an enabled option with only
191 // one value is actually returned
192 opt.setValue(opt.getPossibleValues().get(0));
194 if (val.getSelectedItem() != null)
196 opt.setValue((String) val.getSelectedItem());
200 if (option.getValue() != null)
202 opt.setValue(option.getValue());
209 public void mouseClicked(MouseEvent e)
211 if (e.isPopupTrigger()) // for Windows
213 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
218 public void mouseEntered(MouseEvent e)
220 // TODO Auto-generated method stub
225 public void mouseExited(MouseEvent e)
227 // TODO Auto-generated method stub
232 public void mousePressed(MouseEvent e)
234 if (e.isPopupTrigger()) // Mac
236 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
241 public void mouseReleased(MouseEvent e)
245 public void resetToDefault(boolean setDefaultParams)
247 enabled.setSelected(false);
248 if (option.isRequired()
249 || (setDefaultParams && option.getValue() != null))
251 // Apply default value
252 selectOption(option, option.getValue());
256 public void setInitialValue()
258 initEnabled = enabled.isSelected();
259 if (option.getPossibleValues() != null
260 && option.getPossibleValues().size() > 1)
262 initVal = (String) val.getSelectedItem();
266 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
272 public class ParamBox extends JPanel
273 implements ChangeListener, ActionListener, MouseListener
275 boolean adjusting = false;
277 boolean choice = false;
279 JComboBox<String> choicebox;
281 JPanel controlPanel = new JPanel();
283 boolean descisvisible = false;
285 JScrollPane descPanel = new JScrollPane();
289 boolean integ = false;
293 ParameterI parameter;
295 final OptsParametersContainerI pmdialogbox;
297 JPanel settingPanel = new JPanel();
299 JButton showDesc = new JButton();
301 Slider slider = null;
303 JTextArea string = new JTextArea();
305 ValueConstrainI validator = null;
307 JTextField valueField = null;
309 public ParamBox(final OptsParametersContainerI pmlayout,
312 pmdialogbox = pmlayout;
313 finfo = parm.getFurtherDetails();
314 validator = parm.getValidValue();
316 if (validator != null)
318 integ = validator.getType() == ValueType.Integer;
322 if (parameter.getPossibleValues() != null)
330 makeExpanderParam(parm);
334 makeCompactParam(parm);
339 private void makeCompactParam(ParameterI parm)
341 setLayout(new MigLayout("", "[][grow]"));
343 String ttipText = null;
345 controlPanel.setLayout(new BorderLayout());
347 if (parm.getDescription() != null
348 && parm.getDescription().trim().length() > 0)
350 // Only create description boxes if there actually is a description.
351 ttipText = (JvSwingUtils.wrapTooltip(true,
352 parm.getDescription() + (finfo != null ? "<br><img src=\""
353 + linkImageURL + "\"/>"
354 + MessageManager.getString(
355 "label.opt_and_params_further_details")
359 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
361 updateControls(parm);
365 private void makeExpanderParam(final ParameterI parm)
367 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
368 setBorder(new TitledBorder(parm.getName()));
370 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
371 showDesc.setText("+");
372 string.setFont(new Font("Verdana", Font.PLAIN, 11));
373 string.setBackground(getBackground());
375 string.setEditable(false);
376 descPanel.getViewport().setView(string);
378 descPanel.setVisible(false);
380 JPanel firstrow = new JPanel();
381 firstrow.setLayout(null);
382 controlPanel.setLayout(new BorderLayout());
383 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
384 PARAM_CLOSEDHEIGHT - 50));
385 firstrow.add(controlPanel);
386 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
387 PARAM_CLOSEDHEIGHT - 30));
389 final ParamBox me = this;
391 if (parm.getDescription() != null
392 && parm.getDescription().trim().length() > 0)
394 // Only create description boxes if there actually is a description.
397 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true,
398 MessageManager.formatMessage(
399 "label.opt_and_params_show_brief_desc_image_link",
401 { linkImageURL.toExternalForm() })));
402 showDesc.addMouseListener(this);
406 showDesc.setToolTipText(
407 JvSwingUtils.wrapTooltip(true, MessageManager.getString(
408 "label.opt_and_params_show_brief_desc")));
410 showDesc.addActionListener(new ActionListener()
414 public void actionPerformed(ActionEvent e)
416 descisvisible = !descisvisible;
417 descPanel.setVisible(descisvisible);
418 descPanel.getVerticalScrollBar().setValue(0);
419 me.setPreferredSize(new Dimension(PARAM_WIDTH,
420 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
422 pmdialogbox.refreshParamLayout();
425 string.setWrapStyleWord(true);
426 string.setLineWrap(true);
427 string.setColumns(32);
428 string.setText(parm.getDescription());
429 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
430 firstrow.add(showDesc);
433 validator = parm.getValidValue();
435 if (validator != null)
437 integ = validator.getType() == ValueType.Integer;
441 if (parameter.getPossibleValues() != null)
446 updateControls(parm);
447 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
448 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
454 * Action on input in text field
457 public void actionPerformed(ActionEvent e)
465 updateSliderFromValueField();
470 private void checkIfModified()
472 Object cstate = getCurrentValue();
473 boolean modified = !cstate.equals(lastVal);
474 pmdialogbox.argSetModified(this, modified);
478 * Answers the current value of the parameter, as text
482 private String getCurrentValue()
484 return choice ? (String) choicebox.getSelectedItem()
485 : valueField.getText();
489 public int getBaseline(int width, int height)
495 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
496 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
498 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
500 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
503 public int getBoxHeight()
505 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
508 public ParameterI getParameter()
510 ParameterI prm = parameter.copy();
513 prm.setValue((String) choicebox.getSelectedItem());
517 prm.setValue(valueField.getText());
524 // reset the widget's initial value.
529 public void mouseClicked(MouseEvent e)
531 if (e.isPopupTrigger()) // for Windows
533 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
538 public void mouseEntered(MouseEvent e)
540 // TODO Auto-generated method stub
545 public void mouseExited(MouseEvent e)
547 // TODO Auto-generated method stub
552 public void mousePressed(MouseEvent e)
554 if (e.isPopupTrigger()) // for Mac
556 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
561 public void mouseReleased(MouseEvent e)
563 // TODO Auto-generated method stub
568 * Action on change of slider value
571 public void stateChanged(ChangeEvent e)
575 float value = slider.getSliderValue();
577 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.setPreferredSize(new Dimension(60, 25));
625 slider = makeSlider(parm.getValidValue());
626 slider.addChangeListener(this);
628 controlPanel.add(slider, BorderLayout.WEST);
629 controlPanel.add(valueField, BorderLayout.EAST);
639 List<String> vals = parm.getPossibleValues();
640 for (String val : vals)
642 choicebox.addItem(val);
646 if (parm.getValue() != null)
648 choicebox.setSelectedItem(parm.getValue());
653 valueField.setText(parm.getValue());
656 lastVal = getCurrentValue();
660 private Slider makeSlider(ValueConstrainI validValue)
662 if (validValue != null)
664 final Number minValue = validValue.getMin();
665 final Number maxValue = validValue.getMax();
666 if (minValue != null && maxValue != null)
668 return new Slider(minValue.floatValue(), maxValue.floatValue(),
669 minValue.floatValue());
674 * otherwise, a nominal slider which will not be visible
676 return new Slider(0, 100, 50);
679 public void updateSliderFromValueField()
681 if (validator != null)
683 final Number minValue = validator.getMin();
684 final Number maxValue = validator.getMax();
690 valueField.setText(valueField.getText().trim());
691 iVal = Integer.valueOf(valueField.getText());
693 && minValue.intValue() > iVal)
695 iVal = minValue.intValue();
696 // TODO: provide visual indication that hard limit was reached for
699 if (maxValue != null && maxValue.intValue() < iVal)
701 iVal = maxValue.intValue();
703 } catch (NumberFormatException e)
705 System.err.println(e.toString());
707 if (minValue != null || maxValue != null)
709 valueField.setText(String.valueOf(iVal));
710 slider.setSliderValue(iVal);
714 slider.setVisible(false);
722 valueField.setText(valueField.getText().trim());
723 fVal = Float.valueOf(valueField.getText());
725 && minValue.floatValue() > fVal)
727 fVal = minValue.floatValue();
728 // TODO: provide visual indication that hard limit was reached for
730 // update value field to reflect any bound checking we performed.
731 valueField.setText("" + fVal);
734 && maxValue.floatValue() < fVal)
736 fVal = maxValue.floatValue();
737 // TODO: provide visual indication that hard limit was reached for
739 // update value field to reflect any bound checking we performed.
740 valueField.setText("" + fVal);
742 } catch (NumberFormatException e)
744 System.err.println(e.toString());
746 if (minValue != null && maxValue != null)
748 slider.setSliderModel(minValue.floatValue(),
749 maxValue.floatValue(), fVal);
753 slider.setVisible(false);
761 slider.setVisible(false);
767 public static final int PARAM_WIDTH = 340;
769 public static final int PARAM_HEIGHT = 150;
771 public static final int PARAM_CLOSEDHEIGHT = 80;
773 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
775 this(paramContainer, false);
778 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
781 poparent = paramContainer;
782 this.compact = compact;
785 public static void showUrlPopUp(JComponent invoker, final String finfo,
789 JPopupMenu mnu = new JPopupMenu();
790 JMenuItem mitem = new JMenuItem(
791 MessageManager.formatMessage("label.view_params", new String[]
793 mitem.addActionListener(new ActionListener()
797 public void actionPerformed(ActionEvent e)
799 Desktop.showUrl(finfo);
804 mnu.show(invoker, x, y);
807 URL linkImageURL = getClass().getResource("/images/link.gif");
809 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<>();
811 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<>();
813 public Map<String, OptionBox> getOptSet()
818 public void setOptSet(Map<String, OptionBox> optSet)
820 this.optSet = optSet;
823 public Map<String, ParamBox> getParamSet()
828 public void setParamSet(Map<String, ParamBox> paramSet)
830 this.paramSet = paramSet;
833 OptsParametersContainerI poparent;
835 OptionBox addOption(OptionI opt)
837 OptionBox cb = optSet.get(opt.getName());
840 cb = new OptionBox(opt);
841 optSet.put(opt.getName(), cb);
842 // jobOptions.add(cb, FlowLayout.LEFT);
847 ParamBox addParameter(ParameterI arg)
849 ParamBox pb = paramSet.get(arg.getName());
852 pb = new ParamBox(poparent, arg);
853 paramSet.put(arg.getName(), pb);
854 // paramList.add(pb);
857 // take the defaults from the parameter
858 pb.updateControls(arg);
862 void selectOption(OptionI option, String string)
864 OptionBox cb = optSet.get(option.getName());
867 cb = addOption(option);
869 cb.enabled.setSelected(string != null); // initial state for an option.
872 if (option.getPossibleValues().contains(string))
874 cb.val.setSelectedItem(string);
878 throw new Error(MessageManager.formatMessage(
879 "error.invalid_value_for_option", new String[]
880 { string, option.getName() }));
884 if (option.isRequired() && !cb.enabled.isSelected())
886 // TODO: indicate paramset is not valid.. option needs to be selected!
888 cb.setInitialValue();
891 void setParameter(ParameterI arg)
893 ParamBox pb = paramSet.get(arg.getName());
900 pb.updateControls(arg);
906 * recover options and parameters from GUI
910 public List<ArgumentI> getCurrentSettings()
912 List<ArgumentI> argSet = new ArrayList<>();
913 for (OptionBox opts : getOptSet().values())
915 OptionI opt = opts.getOptionIfEnabled();
921 for (ParamBox parambox : getParamSet().values())
923 ParameterI parm = parambox.getParameter();