2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.ws.params.ArgumentI;
22 import jalview.ws.params.OptionI;
23 import jalview.ws.params.ParameterI;
24 import jalview.ws.params.ValueConstrainI;
25 import jalview.ws.params.ValueConstrainI.ValueType;
27 import java.awt.BorderLayout;
28 import java.awt.Component;
29 import java.awt.Dimension;
31 import java.awt.GridLayout;
32 import java.awt.Rectangle;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.KeyListener;
37 import java.awt.event.MouseEvent;
38 import java.awt.event.MouseListener;
40 import java.util.ArrayList;
41 import java.util.Hashtable;
42 import java.util.List;
45 import javax.swing.JButton;
46 import javax.swing.JCheckBox;
47 import javax.swing.JComboBox;
48 import javax.swing.JComponent;
49 import javax.swing.JLabel;
50 import javax.swing.JMenuItem;
51 import javax.swing.JPanel;
52 import javax.swing.JPopupMenu;
53 import javax.swing.JScrollPane;
54 import javax.swing.JSlider;
55 import javax.swing.JTextArea;
56 import javax.swing.JTextField;
57 import javax.swing.border.TitledBorder;
58 import javax.swing.event.ChangeEvent;
59 import javax.swing.event.ChangeListener;
61 import net.miginfocom.swing.MigLayout;
64 * GUI generator/manager for options and parameters. Originally abstracted from
65 * the WsJobParameters dialog box.
70 public class OptsAndParamsPage
73 * compact or verbose style parameters
75 boolean compact = false;
77 public class OptionBox extends JPanel implements MouseListener,
80 JCheckBox enabled = new JCheckBox();
84 boolean hasLink = false;
86 boolean initEnabled = false;
88 String initVal = null;
92 JLabel optlabel = new JLabel();
94 JComboBox val = new JComboBox();
96 public OptionBox(OptionI opt)
99 setLayout(new BorderLayout());
100 enabled.setSelected(opt.isRequired()); // TODO: lock required options
101 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
103 enabled.setText(opt.getName());
104 enabled.addActionListener(this);
105 finfo = option.getFurtherDetails();
106 String desc = opt.getDescription();
111 enabled.setToolTipText("<html>"
113 .wrapTooltip(((desc == null || desc.trim().length()==0) ? "see further details by right-clicking"
115 + "<br><img src=\"" + linkImageURL + "\"/>")
117 enabled.addMouseListener(this);
121 if (desc != null && desc.trim().length()>0)
123 enabled.setToolTipText("<html>"
124 + JvSwingUtils.wrapTooltip(opt.getDescription())
128 add(enabled, BorderLayout.NORTH);
129 for (Object str : opt.getPossibleValues())
131 val.addItem((String) str);
133 val.setSelectedItem((String) 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 ?
146 public void actionPerformed(ActionEvent e)
148 if (e.getSource() != enabled)
150 enabled.setSelected(true);
155 private void checkIfModified()
157 boolean notmod = (initEnabled == enabled.isSelected());
158 if (enabled.isSelected())
162 notmod &= initVal.equals(val.getSelectedItem());
166 // compare against default service setting
167 notmod &= option.getValue() == null
168 || option.getValue().equals(val.getSelectedItem());
173 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
174 : val.getSelectedItem() != initVal;
176 poparent.argSetModified(this, !notmod);
179 public OptionI getOptionIfEnabled()
181 if (!enabled.isSelected())
185 OptionI opt = option.copy();
186 if (opt.getPossibleValues() != null
187 && opt.getPossibleValues().size() == 1)
189 // Hack to make sure the default value for an enabled option with only
190 // one value is actually returned
191 opt.setValue(opt.getPossibleValues().get(0));
193 if (val.getSelectedItem() != null)
195 opt.setValue((String) val.getSelectedItem());
199 if (option.getValue() != null)
201 opt.setValue(option.getValue());
207 public void mouseClicked(MouseEvent e)
209 if (javax.swing.SwingUtilities.isRightMouseButton(e))
211 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
215 public void mouseEntered(MouseEvent e)
217 // TODO Auto-generated method stub
221 public void mouseExited(MouseEvent e)
223 // TODO Auto-generated method stub
227 public void mousePressed(MouseEvent e)
229 // TODO Auto-generated method stub
233 public void mouseReleased(MouseEvent e)
235 // TODO Auto-generated method stub
240 public void resetToDefault(boolean setDefaultParams)
242 enabled.setSelected(false);
243 if (option.isRequired() || (setDefaultParams && option.getValue()!=null))
245 // Apply default value
246 selectOption(option, option.getValue());
250 public void setInitialValue()
252 initEnabled = enabled.isSelected();
253 if (option.getPossibleValues() != null
254 && option.getPossibleValues().size() > 1)
256 initVal = (String) val.getSelectedItem();
260 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
266 public class ParamBox extends JPanel implements ChangeListener,
267 ActionListener, MouseListener
269 boolean adjusting = false;
271 boolean choice = false;
275 JPanel controlPanel = new JPanel();
277 boolean descisvisible = false;
279 JScrollPane descPanel = new JScrollPane();
283 boolean integ = false;
287 ParameterI parameter;
289 final OptsParametersContainerI pmdialogbox;
291 JPanel settingPanel = new JPanel();
293 JButton showDesc = new JButton();
295 JSlider slider = null;
297 JTextArea string = new JTextArea();
299 ValueConstrainI validator = null;
301 JTextField valueField = null;
303 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
305 pmdialogbox = pmlayout;
306 finfo = parm.getFurtherDetails();
307 validator = parm.getValidValue();
309 if (validator != null)
311 integ = validator.getType() == ValueType.Integer;
315 if (parameter.getPossibleValues() != null)
323 makeExpanderParam(parm);
327 makeCompactParam(parm);
332 private void makeCompactParam(ParameterI parm)
334 setLayout(new MigLayout("", "[][grow]"));
336 String ttipText = null;
338 controlPanel.setLayout(new BorderLayout());
340 if (parm.getDescription() != null
341 && parm.getDescription().trim().length() > 0)
343 // Only create description boxes if there actually is a description.
346 .wrapTooltip(parm.getDescription()
347 + (finfo != null ? "<br><img src=\""
349 + "\"/> Right click for further information."
353 JvSwingUtils.mgAddtoLayout(this, ttipText,
354 new JLabel(parm.getName()), controlPanel, "");
355 updateControls(parm);
359 private void makeExpanderParam(ParameterI parm)
361 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
362 setBorder(new TitledBorder(parm.getName()));
364 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
365 showDesc.setText("+");
366 string.setFont(new Font("Verdana", Font.PLAIN, 11));
367 string.setBackground(getBackground());
369 string.setEditable(false);
370 descPanel.getViewport().setView(string);
372 descPanel.setVisible(false);
374 JPanel firstrow = new JPanel();
375 firstrow.setLayout(null);
376 controlPanel.setLayout(new BorderLayout());
377 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
378 PARAM_CLOSEDHEIGHT - 50));
379 firstrow.add(controlPanel);
380 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
381 PARAM_CLOSEDHEIGHT - 30));
383 final ParamBox me = this;
385 if (parm.getDescription() != null
386 && parm.getDescription().trim().length() > 0)
388 // Only create description boxes if there actually is a description.
391 showDesc.setToolTipText("<html>"
393 .wrapTooltip("Click to show brief description<br><img src=\""
395 + "\"/> Right click for further information.")
397 showDesc.addMouseListener(this);
401 showDesc.setToolTipText("<html>"
403 .wrapTooltip("Click to show brief description.")
406 showDesc.addActionListener(new ActionListener()
409 public void actionPerformed(ActionEvent e)
411 descisvisible = !descisvisible;
412 descPanel.setVisible(descisvisible);
413 descPanel.getVerticalScrollBar().setValue(0);
414 me.setPreferredSize(new Dimension(PARAM_WIDTH,
415 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
417 pmdialogbox.refreshParamLayout();
420 string.setWrapStyleWord(true);
421 string.setLineWrap(true);
422 string.setColumns(32);
423 string.setText(parm.getDescription());
424 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
425 firstrow.add(showDesc);
428 validator = parm.getValidValue();
430 if (validator != null)
432 integ = validator.getType() == ValueType.Integer;
436 if (parameter.getPossibleValues() != null)
441 updateControls(parm);
442 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
443 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
448 public void actionPerformed(ActionEvent e)
456 updateSliderFromValueField();
461 private void checkIfModified()
463 Object cstate = updateSliderFromValueField();
464 boolean notmod = false;
465 if (cstate.getClass() == lastVal.getClass())
467 if (cstate instanceof int[])
469 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
471 else if (cstate instanceof float[])
473 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
475 else if (cstate instanceof String[])
477 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
480 pmdialogbox.argSetModified(this, !notmod);
484 public int getBaseline(int width, int height)
490 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
491 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
493 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
495 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
498 public int getBoxHeight()
500 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
503 public ParameterI getParameter()
505 ParameterI prm = parameter.copy();
508 prm.setValue((String) choicebox.getSelectedItem());
512 prm.setValue(valueField.getText());
519 // reset the widget's initial value.
523 public void mouseClicked(MouseEvent e)
525 if (javax.swing.SwingUtilities.isRightMouseButton(e))
527 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
531 public void mouseEntered(MouseEvent e)
533 // TODO Auto-generated method stub
537 public void mouseExited(MouseEvent e)
539 // TODO Auto-generated method stub
543 public void mousePressed(MouseEvent e)
545 // TODO Auto-generated method stub
549 public void mouseReleased(MouseEvent e)
551 // TODO Auto-generated method stub
555 public void stateChanged(ChangeEvent e)
559 valueField.setText(""
560 + ((integ) ? ("" + (int) slider.getValue())
561 : ("" + (float) (slider.getValue() / 1000f))));
567 public void updateControls(ParameterI parm)
570 boolean init = (choicebox == null && valueField == null);
575 choicebox = new JComboBox();
576 choicebox.addActionListener(this);
577 controlPanel.add(choicebox, BorderLayout.CENTER);
581 slider = new JSlider();
582 slider.addChangeListener(this);
583 valueField = new JTextField();
584 valueField.addActionListener(this);
585 valueField.addKeyListener(new KeyListener()
589 public void keyTyped(KeyEvent e)
594 public void keyReleased(KeyEvent e)
596 if (valueField.getText().trim().length() > 0)
598 actionPerformed(null);
603 public void keyPressed(KeyEvent e)
607 valueField.setPreferredSize(new Dimension(60, 25));
608 controlPanel.add(slider, BorderLayout.WEST);
609 controlPanel.add(valueField, BorderLayout.EAST);
620 List vals = parm.getPossibleValues();
621 for (Object val : vals)
623 choicebox.addItem(val);
627 if (parm.getValue() != null)
629 choicebox.setSelectedItem(parm.getValue());
634 valueField.setText(parm.getValue());
637 lastVal = updateSliderFromValueField();
641 public Object updateSliderFromValueField()
645 if (validator != null)
652 valueField.setText(valueField.getText().trim());
653 iVal = Integer.valueOf(valueField.getText());
654 if (validator.getMin() != null
655 && validator.getMin().intValue() > iVal)
657 iVal = validator.getMin().intValue();
658 // TODO: provide visual indication that hard limit was reached for
661 if (validator.getMax() != null
662 && validator.getMax().intValue() < iVal)
664 iVal = validator.getMax().intValue();
665 // TODO: provide visual indication that hard limit was reached for
668 } catch (Exception e)
672 // update value field to reflect any bound checking we performed.
673 valueField.setText("" + iVal);
674 if (validator.getMin() != null && validator.getMax() != null)
676 slider.getModel().setRangeProperties(iVal, 1,
677 validator.getMin().intValue(),
678 validator.getMax().intValue(), true);
682 slider.setVisible(false);
692 valueField.setText(valueField.getText().trim());
693 fVal = Float.valueOf(valueField.getText());
694 if (validator.getMin() != null
695 && validator.getMin().floatValue() > fVal)
697 fVal = validator.getMin().floatValue();
698 // TODO: provide visual indication that hard limit was reached for
700 // update value field to reflect any bound checking we performed.
701 valueField.setText("" + fVal);
703 if (validator.getMax() != null
704 && validator.getMax().floatValue() < fVal)
706 fVal = validator.getMax().floatValue();
707 // TODO: provide visual indication that hard limit was reached for
709 // update value field to reflect any bound checking we performed.
710 valueField.setText("" + fVal);
712 } catch (Exception e)
716 if (validator.getMin() != null && validator.getMax() != null)
718 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
719 (int) validator.getMin().floatValue() * 1000,
720 (int) validator.getMax().floatValue() * 1000, true);
724 slider.setVisible(false);
734 slider.setVisible(false);
736 { valueField.getText().trim() };
741 { (String) choicebox.getSelectedItem() };
748 public static final int PARAM_WIDTH = 340;
750 public static final int PARAM_HEIGHT = 150;
752 public static final int PARAM_CLOSEDHEIGHT = 80;
754 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
756 this(paramContainer, false);
759 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
762 poparent = paramContainer;
763 this.compact = compact;
766 public static void showUrlPopUp(JComponent invoker, final String finfo,
770 JPopupMenu mnu = new JPopupMenu();
771 JMenuItem mitem = new JMenuItem("View " + finfo);
772 mitem.addActionListener(new ActionListener()
776 public void actionPerformed(ActionEvent e)
778 Desktop.showUrl(finfo);
783 mnu.show(invoker, x, y);
786 URL linkImageURL = getClass().getResource("/images/link.gif");
788 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
790 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
792 public Map<String, OptionBox> getOptSet()
797 public void setOptSet(Map<String, OptionBox> optSet)
799 this.optSet = optSet;
802 public Map<String, ParamBox> getParamSet()
807 public void setParamSet(Map<String, ParamBox> paramSet)
809 this.paramSet = paramSet;
812 OptsParametersContainerI poparent;
814 OptionBox addOption(OptionI opt)
816 OptionBox cb = optSet.get(opt.getName());
819 cb = new OptionBox(opt);
820 optSet.put(opt.getName(), cb);
821 // jobOptions.add(cb, FlowLayout.LEFT);
826 ParamBox addParameter(ParameterI arg)
828 ParamBox pb = paramSet.get(arg.getName());
831 pb = new ParamBox(poparent, arg);
832 paramSet.put(arg.getName(), pb);
833 // paramList.add(pb);
836 // take the defaults from the parameter
837 pb.updateControls(arg);
841 void selectOption(OptionI option, String string)
843 OptionBox cb = optSet.get(option.getName());
846 cb = addOption(option);
848 cb.enabled.setSelected(string != null); // initial state for an option.
851 if (option.getPossibleValues().contains(string))
853 cb.val.setSelectedItem(string);
857 throw new Error("Invalid value " + string + " for option " + option);
861 if (option.isRequired() && !cb.enabled.isSelected())
863 // TODO: indicate paramset is not valid.. option needs to be selected!
865 cb.setInitialValue();
868 void setParameter(ParameterI arg)
870 ParamBox pb = paramSet.get(arg.getName());
877 pb.updateControls(arg);
883 * recover options and parameters from GUI
887 public List<ArgumentI> getCurrentSettings()
889 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
890 for (OptionBox opts : getOptSet().values())
892 OptionI opt = opts.getOptionIfEnabled();
898 for (ParamBox parambox : getParamSet().values())
900 ParameterI parm = parambox.getParameter();