2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.ws.params.ArgumentI;
21 import jalview.ws.params.OptionI;
22 import jalview.ws.params.ParameterI;
23 import jalview.ws.params.ValueConstrainI;
24 import jalview.ws.params.ValueConstrainI.ValueType;
26 import java.awt.BorderLayout;
27 import java.awt.Component;
28 import java.awt.Dimension;
30 import java.awt.GridLayout;
31 import java.awt.Rectangle;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyEvent;
35 import java.awt.event.KeyListener;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
39 import java.util.ArrayList;
40 import java.util.Hashtable;
41 import java.util.List;
44 import javax.swing.JButton;
45 import javax.swing.JCheckBox;
46 import javax.swing.JComboBox;
47 import javax.swing.JComponent;
48 import javax.swing.JLabel;
49 import javax.swing.JMenuItem;
50 import javax.swing.JPanel;
51 import javax.swing.JPopupMenu;
52 import javax.swing.JScrollPane;
53 import javax.swing.JSlider;
54 import javax.swing.JTextArea;
55 import javax.swing.JTextField;
56 import javax.swing.border.TitledBorder;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
60 import net.miginfocom.swing.MigLayout;
63 * GUI generator/manager for options and parameters. Originally abstracted from
64 * the WsJobParameters dialog box.
69 public class OptsAndParamsPage
72 * compact or verbose style parameters
74 boolean compact = false;
76 public class OptionBox extends JPanel implements MouseListener,
79 JCheckBox enabled = new JCheckBox();
83 boolean hasLink = false;
85 boolean initEnabled = false;
87 String initVal = null;
91 JLabel optlabel = new JLabel();
93 JComboBox val = new JComboBox();
95 public OptionBox(OptionI opt)
98 setLayout(new BorderLayout());
99 enabled.setSelected(opt.isRequired()); // TODO: lock required options
100 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
102 enabled.setText(opt.getName());
103 enabled.addActionListener(this);
104 finfo = option.getFurtherDetails();
105 String desc = opt.getDescription();
110 enabled.setToolTipText("<html>"
112 .wrapTooltip(((desc == null) ? "see further details by right-clicking"
114 + "<br><img src=\"" + linkImageURL + "\"/>")
116 enabled.addMouseListener(this);
122 enabled.setToolTipText("<html>"
123 + JvSwingUtils.wrapTooltip(opt.getDescription())
127 add(enabled, BorderLayout.NORTH);
128 for (Object str : opt.getPossibleValues())
130 val.addItem((String) str);
132 val.setSelectedItem((String) opt.getValue());
133 if (opt.getPossibleValues().size() > 1)
135 setLayout(new GridLayout(1, 2));
136 val.addActionListener(this);
137 add(val, BorderLayout.SOUTH);
139 // TODO: add actionListeners for popup (to open further info),
140 // and to update list of parameters if an option is enabled
141 // that takes a value. JBPNote: is this TODO still valid ?
145 public void actionPerformed(ActionEvent e)
147 if (e.getSource() != enabled)
149 enabled.setSelected(true);
154 private void checkIfModified()
156 boolean notmod = (initEnabled == enabled.isSelected());
157 if (enabled.isSelected())
161 notmod &= initVal.equals(val.getSelectedItem());
165 // compare against default service setting
166 notmod &= option.getValue() == null
167 || option.getValue().equals(val.getSelectedItem());
172 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
173 : val.getSelectedItem() != initVal;
175 poparent.argSetModified(this, !notmod);
178 public OptionI getOptionIfEnabled()
180 if (!enabled.isSelected())
184 OptionI opt = option.copy();
185 if (opt.getPossibleValues() != null
186 && opt.getPossibleValues().size() == 1)
188 // Hack to make sure the default value for an enabled option with only
189 // one value is actually returned
190 opt.setValue(opt.getPossibleValues().get(0));
192 if (val.getSelectedItem() != null)
194 opt.setValue((String) val.getSelectedItem());
198 if (option.getValue() != null)
200 opt.setValue(option.getValue());
206 public void mouseClicked(MouseEvent e)
208 if (javax.swing.SwingUtilities.isRightMouseButton(e))
210 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
214 public void mouseEntered(MouseEvent e)
216 // TODO Auto-generated method stub
220 public void mouseExited(MouseEvent e)
222 // TODO Auto-generated method stub
226 public void mousePressed(MouseEvent e)
228 // TODO Auto-generated method stub
232 public void mouseReleased(MouseEvent e)
234 // TODO Auto-generated method stub
238 public void resetToDefault()
240 enabled.setSelected(false);
241 if (option.isRequired())
243 // Apply default value
244 selectOption(option, option.getValue());
248 public void setInitialValue()
250 initEnabled = enabled.isSelected();
251 if (option.getPossibleValues() != null
252 && option.getPossibleValues().size() > 1)
254 initVal = (String) val.getSelectedItem();
258 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
264 public class ParamBox extends JPanel implements ChangeListener,
265 ActionListener, MouseListener
267 boolean adjusting = false;
269 boolean choice = false;
273 JPanel controlPanel = new JPanel();
275 boolean descisvisible = false;
277 JScrollPane descPanel = new JScrollPane();
281 boolean integ = false;
285 ParameterI parameter;
287 final OptsParametersContainerI pmdialogbox;
289 JPanel settingPanel = new JPanel();
291 JButton showDesc = new JButton();
293 JSlider slider = null;
295 JTextArea string = new JTextArea();
297 ValueConstrainI validator = null;
299 JTextField valueField = null;
301 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
303 pmdialogbox = pmlayout;
304 finfo = parm.getFurtherDetails();
305 validator = parm.getValidValue();
307 if (validator != null)
309 integ = validator.getType() == ValueType.Integer;
313 if (parameter.getPossibleValues() != null)
321 makeExpanderParam(parm);
325 makeCompactParam(parm);
330 private void makeCompactParam(ParameterI parm)
332 setLayout(new MigLayout("", "[][grow]"));
334 String ttipText = null;
336 controlPanel.setLayout(new BorderLayout());
338 if (parm.getDescription() != null
339 && parm.getDescription().trim().length() > 0)
341 // Only create description boxes if there actually is a description.
344 .wrapTooltip(parm.getDescription()
345 + (finfo != null ? "<br><img src=\""
347 + "\"/> Right click for further information."
351 JvSwingUtils.mgAddtoLayout(this, ttipText,
352 new JLabel(parm.getName()), controlPanel, "");
353 updateControls(parm);
357 private void makeExpanderParam(ParameterI parm)
359 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
360 setBorder(new TitledBorder(parm.getName()));
362 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
363 showDesc.setText("+");
364 string.setFont(new Font("Verdana", Font.PLAIN, 11));
365 string.setBackground(getBackground());
367 string.setEditable(false);
368 descPanel.getViewport().setView(string);
370 descPanel.setVisible(false);
372 JPanel firstrow = new JPanel();
373 firstrow.setLayout(null);
374 controlPanel.setLayout(new BorderLayout());
375 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
376 PARAM_CLOSEDHEIGHT - 50));
377 firstrow.add(controlPanel);
378 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
379 PARAM_CLOSEDHEIGHT - 30));
381 final ParamBox me = this;
383 if (parm.getDescription() != null
384 && parm.getDescription().trim().length() > 0)
386 // Only create description boxes if there actually is a description.
389 showDesc.setToolTipText("<html>"
391 .wrapTooltip("Click to show brief description<br><img src=\""
393 + "\"/> Right click for further information.")
395 showDesc.addMouseListener(this);
399 showDesc.setToolTipText("<html>"
401 .wrapTooltip("Click to show brief description.")
404 showDesc.addActionListener(new ActionListener()
407 public void actionPerformed(ActionEvent e)
409 descisvisible = !descisvisible;
410 descPanel.setVisible(descisvisible);
411 descPanel.getVerticalScrollBar().setValue(0);
412 me.setPreferredSize(new Dimension(PARAM_WIDTH,
413 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
415 pmdialogbox.refreshParamLayout();
418 string.setWrapStyleWord(true);
419 string.setLineWrap(true);
420 string.setColumns(32);
421 string.setText(parm.getDescription());
422 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
423 firstrow.add(showDesc);
426 validator = parm.getValidValue();
428 if (validator != null)
430 integ = validator.getType() == ValueType.Integer;
434 if (parameter.getPossibleValues() != null)
439 updateControls(parm);
440 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
441 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
446 public void actionPerformed(ActionEvent e)
454 updateSliderFromValueField();
459 private void checkIfModified()
461 Object cstate = updateSliderFromValueField();
462 boolean notmod = false;
463 if (cstate.getClass() == lastVal.getClass())
465 if (cstate instanceof int[])
467 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
469 else if (cstate instanceof float[])
471 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
473 else if (cstate instanceof String[])
475 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
478 pmdialogbox.argSetModified(this, !notmod);
482 public int getBaseline(int width, int height)
488 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
489 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
491 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
493 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
496 public int getBoxHeight()
498 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
501 public ParameterI getParameter()
503 ParameterI prm = parameter.copy();
506 prm.setValue((String) choicebox.getSelectedItem());
510 prm.setValue(valueField.getText());
517 // reset the widget's initial value.
521 public void mouseClicked(MouseEvent e)
523 if (javax.swing.SwingUtilities.isRightMouseButton(e))
525 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
529 public void mouseEntered(MouseEvent e)
531 // TODO Auto-generated method stub
535 public void mouseExited(MouseEvent e)
537 // TODO Auto-generated method stub
541 public void mousePressed(MouseEvent e)
543 // TODO Auto-generated method stub
547 public void mouseReleased(MouseEvent e)
549 // TODO Auto-generated method stub
553 public void stateChanged(ChangeEvent e)
557 valueField.setText(""
558 + ((integ) ? ("" + (int) slider.getValue())
559 : ("" + (float) (slider.getValue() / 1000f))));
565 public void updateControls(ParameterI parm)
568 boolean init = (choicebox == null && valueField == null);
573 choicebox = new JComboBox();
574 choicebox.addActionListener(this);
575 controlPanel.add(choicebox, BorderLayout.CENTER);
579 slider = new JSlider();
580 slider.addChangeListener(this);
581 valueField = new JTextField();
582 valueField.addActionListener(this);
583 valueField.addKeyListener(new KeyListener()
587 public void keyTyped(KeyEvent e)
592 public void keyReleased(KeyEvent e)
594 if (valueField.getText().trim().length() > 0)
596 actionPerformed(null);
601 public void keyPressed(KeyEvent e)
605 valueField.setPreferredSize(new Dimension(60, 25));
606 controlPanel.add(slider, BorderLayout.WEST);
607 controlPanel.add(valueField, BorderLayout.EAST);
618 List vals = parm.getPossibleValues();
619 for (Object val : vals)
621 choicebox.addItem(val);
625 if (parm.getValue() != null)
627 choicebox.setSelectedItem(parm.getValue());
632 valueField.setText(parm.getValue());
635 lastVal = updateSliderFromValueField();
639 public Object updateSliderFromValueField()
643 if (validator != null)
650 valueField.setText(valueField.getText().trim());
651 iVal = Integer.valueOf(valueField.getText());
652 if (validator.getMin() != null
653 && validator.getMin().intValue() > iVal)
655 iVal = validator.getMin().intValue();
656 // TODO: provide visual indication that hard limit was reached for
659 if (validator.getMax() != null
660 && validator.getMax().intValue() < iVal)
662 iVal = validator.getMax().intValue();
663 // TODO: provide visual indication that hard limit was reached for
666 } catch (Exception e)
670 // update value field to reflect any bound checking we performed.
671 valueField.setText("" + iVal);
672 if (validator.getMin() != null && validator.getMax() != null)
674 slider.getModel().setRangeProperties(iVal, 1,
675 validator.getMin().intValue(),
676 validator.getMax().intValue(), true);
680 slider.setVisible(false);
690 valueField.setText(valueField.getText().trim());
691 fVal = Float.valueOf(valueField.getText());
692 if (validator.getMin() != null
693 && validator.getMin().floatValue() > fVal)
695 fVal = validator.getMin().floatValue();
696 // TODO: provide visual indication that hard limit was reached for
698 // update value field to reflect any bound checking we performed.
699 valueField.setText("" + fVal);
701 if (validator.getMax() != null
702 && validator.getMax().floatValue() < fVal)
704 fVal = validator.getMax().floatValue();
705 // TODO: provide visual indication that hard limit was reached for
707 // update value field to reflect any bound checking we performed.
708 valueField.setText("" + fVal);
710 } catch (Exception e)
714 if (validator.getMin() != null && validator.getMax() != null)
716 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
717 (int) validator.getMin().floatValue() * 1000,
718 (int) validator.getMax().floatValue() * 1000, true);
722 slider.setVisible(false);
732 slider.setVisible(false);
734 { valueField.getText().trim() };
739 { (String) choicebox.getSelectedItem() };
746 public static final int PARAM_WIDTH = 340;
748 public static final int PARAM_HEIGHT = 150;
750 public static final int PARAM_CLOSEDHEIGHT = 80;
752 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
754 this(paramContainer, false);
757 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
760 poparent = paramContainer;
761 this.compact = compact;
764 public static void showUrlPopUp(JComponent invoker, final String finfo,
768 JPopupMenu mnu = new JPopupMenu();
769 JMenuItem mitem = new JMenuItem("View " + finfo);
770 mitem.addActionListener(new ActionListener()
774 public void actionPerformed(ActionEvent e)
776 Desktop.showUrl(finfo);
781 mnu.show(invoker, x, y);
784 URL linkImageURL = getClass().getResource("/images/link.gif");
786 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
788 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
790 public Map<String, OptionBox> getOptSet()
795 public void setOptSet(Map<String, OptionBox> optSet)
797 this.optSet = optSet;
800 public Map<String, ParamBox> getParamSet()
805 public void setParamSet(Map<String, ParamBox> paramSet)
807 this.paramSet = paramSet;
810 OptsParametersContainerI poparent;
812 OptionBox addOption(OptionI opt)
814 OptionBox cb = optSet.get(opt.getName());
817 cb = new OptionBox(opt);
818 optSet.put(opt.getName(), cb);
819 // jobOptions.add(cb, FlowLayout.LEFT);
824 ParamBox addParameter(ParameterI arg)
826 ParamBox pb = paramSet.get(arg.getName());
829 pb = new ParamBox(poparent, arg);
830 paramSet.put(arg.getName(), pb);
831 // paramList.add(pb);
834 // take the defaults from the parameter
835 pb.updateControls(arg);
839 void selectOption(OptionI option, String string)
841 OptionBox cb = optSet.get(option.getName());
844 cb = addOption(option);
846 cb.enabled.setSelected(string != null); // initial state for an option.
849 if (option.getPossibleValues().contains(string))
851 cb.val.setSelectedItem(string);
855 throw new Error("Invalid value " + string + " for option " + option);
859 if (option.isRequired() && !cb.enabled.isSelected())
861 // TODO: indicate paramset is not valid.. option needs to be selected!
863 cb.setInitialValue();
866 void setParameter(ParameterI arg)
868 ParamBox pb = paramSet.get(arg.getName());
875 pb.updateControls(arg);
881 * recover options and parameters from GUI
885 public List<ArgumentI> getCurrentSettings()
887 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
888 for (OptionBox opts : getOptSet().values())
890 OptionI opt = opts.getOptionIfEnabled();
896 for (ParamBox parambox : getParamSet().values())
898 ParameterI parm = parambox.getParameter();