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 || desc.trim().length()==0) ? "see further details by right-clicking"
114 + "<br><img src=\"" + linkImageURL + "\"/>")
116 enabled.addMouseListener(this);
120 if (desc != null && desc.trim().length()>0)
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
239 public void resetToDefault(boolean setDefaultParams)
241 enabled.setSelected(false);
242 if (option.isRequired() || (setDefaultParams && option.getValue()!=null))
244 // Apply default value
245 selectOption(option, option.getValue());
249 public void setInitialValue()
251 initEnabled = enabled.isSelected();
252 if (option.getPossibleValues() != null
253 && option.getPossibleValues().size() > 1)
255 initVal = (String) val.getSelectedItem();
259 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
265 public class ParamBox extends JPanel implements ChangeListener,
266 ActionListener, MouseListener
268 boolean adjusting = false;
270 boolean choice = false;
274 JPanel controlPanel = new JPanel();
276 boolean descisvisible = false;
278 JScrollPane descPanel = new JScrollPane();
282 boolean integ = false;
286 ParameterI parameter;
288 final OptsParametersContainerI pmdialogbox;
290 JPanel settingPanel = new JPanel();
292 JButton showDesc = new JButton();
294 JSlider slider = null;
296 JTextArea string = new JTextArea();
298 ValueConstrainI validator = null;
300 JTextField valueField = null;
302 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
304 pmdialogbox = pmlayout;
305 finfo = parm.getFurtherDetails();
306 validator = parm.getValidValue();
308 if (validator != null)
310 integ = validator.getType() == ValueType.Integer;
314 if (parameter.getPossibleValues() != null)
322 makeExpanderParam(parm);
326 makeCompactParam(parm);
331 private void makeCompactParam(ParameterI parm)
333 setLayout(new MigLayout("", "[][grow]"));
335 String ttipText = null;
337 controlPanel.setLayout(new BorderLayout());
339 if (parm.getDescription() != null
340 && parm.getDescription().trim().length() > 0)
342 // Only create description boxes if there actually is a description.
345 .wrapTooltip(parm.getDescription()
346 + (finfo != null ? "<br><img src=\""
348 + "\"/> Right click for further information."
352 JvSwingUtils.mgAddtoLayout(this, ttipText,
353 new JLabel(parm.getName()), controlPanel, "");
354 updateControls(parm);
358 private void makeExpanderParam(ParameterI parm)
360 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
361 setBorder(new TitledBorder(parm.getName()));
363 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
364 showDesc.setText("+");
365 string.setFont(new Font("Verdana", Font.PLAIN, 11));
366 string.setBackground(getBackground());
368 string.setEditable(false);
369 descPanel.getViewport().setView(string);
371 descPanel.setVisible(false);
373 JPanel firstrow = new JPanel();
374 firstrow.setLayout(null);
375 controlPanel.setLayout(new BorderLayout());
376 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
377 PARAM_CLOSEDHEIGHT - 50));
378 firstrow.add(controlPanel);
379 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
380 PARAM_CLOSEDHEIGHT - 30));
382 final ParamBox me = this;
384 if (parm.getDescription() != null
385 && parm.getDescription().trim().length() > 0)
387 // Only create description boxes if there actually is a description.
390 showDesc.setToolTipText("<html>"
392 .wrapTooltip("Click to show brief description<br><img src=\""
394 + "\"/> Right click for further information.")
396 showDesc.addMouseListener(this);
400 showDesc.setToolTipText("<html>"
402 .wrapTooltip("Click to show brief description.")
405 showDesc.addActionListener(new ActionListener()
408 public void actionPerformed(ActionEvent e)
410 descisvisible = !descisvisible;
411 descPanel.setVisible(descisvisible);
412 descPanel.getVerticalScrollBar().setValue(0);
413 me.setPreferredSize(new Dimension(PARAM_WIDTH,
414 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
416 pmdialogbox.refreshParamLayout();
419 string.setWrapStyleWord(true);
420 string.setLineWrap(true);
421 string.setColumns(32);
422 string.setText(parm.getDescription());
423 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
424 firstrow.add(showDesc);
427 validator = parm.getValidValue();
429 if (validator != null)
431 integ = validator.getType() == ValueType.Integer;
435 if (parameter.getPossibleValues() != null)
440 updateControls(parm);
441 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
442 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
447 public void actionPerformed(ActionEvent e)
455 updateSliderFromValueField();
460 private void checkIfModified()
462 Object cstate = updateSliderFromValueField();
463 boolean notmod = false;
464 if (cstate.getClass() == lastVal.getClass())
466 if (cstate instanceof int[])
468 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
470 else if (cstate instanceof float[])
472 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
474 else if (cstate instanceof String[])
476 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
479 pmdialogbox.argSetModified(this, !notmod);
483 public int getBaseline(int width, int height)
489 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
490 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
492 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
494 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
497 public int getBoxHeight()
499 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
502 public ParameterI getParameter()
504 ParameterI prm = parameter.copy();
507 prm.setValue((String) choicebox.getSelectedItem());
511 prm.setValue(valueField.getText());
518 // reset the widget's initial value.
522 public void mouseClicked(MouseEvent e)
524 if (javax.swing.SwingUtilities.isRightMouseButton(e))
526 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
530 public void mouseEntered(MouseEvent e)
532 // TODO Auto-generated method stub
536 public void mouseExited(MouseEvent e)
538 // TODO Auto-generated method stub
542 public void mousePressed(MouseEvent e)
544 // TODO Auto-generated method stub
548 public void mouseReleased(MouseEvent e)
550 // TODO Auto-generated method stub
554 public void stateChanged(ChangeEvent e)
558 valueField.setText(""
559 + ((integ) ? ("" + (int) slider.getValue())
560 : ("" + (float) (slider.getValue() / 1000f))));
566 public void updateControls(ParameterI parm)
569 boolean init = (choicebox == null && valueField == null);
574 choicebox = new JComboBox();
575 choicebox.addActionListener(this);
576 controlPanel.add(choicebox, BorderLayout.CENTER);
580 slider = new JSlider();
581 slider.addChangeListener(this);
582 valueField = new JTextField();
583 valueField.addActionListener(this);
584 valueField.addKeyListener(new KeyListener()
588 public void keyTyped(KeyEvent e)
593 public void keyReleased(KeyEvent e)
595 if (valueField.getText().trim().length() > 0)
597 actionPerformed(null);
602 public void keyPressed(KeyEvent e)
606 valueField.setPreferredSize(new Dimension(60, 25));
607 controlPanel.add(slider, BorderLayout.WEST);
608 controlPanel.add(valueField, BorderLayout.EAST);
619 List vals = parm.getPossibleValues();
620 for (Object val : vals)
622 choicebox.addItem(val);
626 if (parm.getValue() != null)
628 choicebox.setSelectedItem(parm.getValue());
633 valueField.setText(parm.getValue());
636 lastVal = updateSliderFromValueField();
640 public Object updateSliderFromValueField()
644 if (validator != null)
651 valueField.setText(valueField.getText().trim());
652 iVal = Integer.valueOf(valueField.getText());
653 if (validator.getMin() != null
654 && validator.getMin().intValue() > iVal)
656 iVal = validator.getMin().intValue();
657 // TODO: provide visual indication that hard limit was reached for
660 if (validator.getMax() != null
661 && validator.getMax().intValue() < iVal)
663 iVal = validator.getMax().intValue();
664 // TODO: provide visual indication that hard limit was reached for
667 } catch (Exception e)
671 // update value field to reflect any bound checking we performed.
672 valueField.setText("" + iVal);
673 if (validator.getMin() != null && validator.getMax() != null)
675 slider.getModel().setRangeProperties(iVal, 1,
676 validator.getMin().intValue(),
677 validator.getMax().intValue(), true);
681 slider.setVisible(false);
691 valueField.setText(valueField.getText().trim());
692 fVal = Float.valueOf(valueField.getText());
693 if (validator.getMin() != null
694 && validator.getMin().floatValue() > fVal)
696 fVal = validator.getMin().floatValue();
697 // TODO: provide visual indication that hard limit was reached for
699 // update value field to reflect any bound checking we performed.
700 valueField.setText("" + fVal);
702 if (validator.getMax() != null
703 && validator.getMax().floatValue() < fVal)
705 fVal = validator.getMax().floatValue();
706 // TODO: provide visual indication that hard limit was reached for
708 // update value field to reflect any bound checking we performed.
709 valueField.setText("" + fVal);
711 } catch (Exception e)
715 if (validator.getMin() != null && validator.getMax() != null)
717 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
718 (int) validator.getMin().floatValue() * 1000,
719 (int) validator.getMax().floatValue() * 1000, true);
723 slider.setVisible(false);
733 slider.setVisible(false);
735 { valueField.getText().trim() };
740 { (String) choicebox.getSelectedItem() };
747 public static final int PARAM_WIDTH = 340;
749 public static final int PARAM_HEIGHT = 150;
751 public static final int PARAM_CLOSEDHEIGHT = 80;
753 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
755 this(paramContainer, false);
758 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
761 poparent = paramContainer;
762 this.compact = compact;
765 public static void showUrlPopUp(JComponent invoker, final String finfo,
769 JPopupMenu mnu = new JPopupMenu();
770 JMenuItem mitem = new JMenuItem("View " + finfo);
771 mitem.addActionListener(new ActionListener()
775 public void actionPerformed(ActionEvent e)
777 Desktop.showUrl(finfo);
782 mnu.show(invoker, x, y);
785 URL linkImageURL = getClass().getResource("/images/link.gif");
787 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
789 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
791 public Map<String, OptionBox> getOptSet()
796 public void setOptSet(Map<String, OptionBox> optSet)
798 this.optSet = optSet;
801 public Map<String, ParamBox> getParamSet()
806 public void setParamSet(Map<String, ParamBox> paramSet)
808 this.paramSet = paramSet;
811 OptsParametersContainerI poparent;
813 OptionBox addOption(OptionI opt)
815 OptionBox cb = optSet.get(opt.getName());
818 cb = new OptionBox(opt);
819 optSet.put(opt.getName(), cb);
820 // jobOptions.add(cb, FlowLayout.LEFT);
825 ParamBox addParameter(ParameterI arg)
827 ParamBox pb = paramSet.get(arg.getName());
830 pb = new ParamBox(poparent, arg);
831 paramSet.put(arg.getName(), pb);
832 // paramList.add(pb);
835 // take the defaults from the parameter
836 pb.updateControls(arg);
840 void selectOption(OptionI option, String string)
842 OptionBox cb = optSet.get(option.getName());
845 cb = addOption(option);
847 cb.enabled.setSelected(string != null); // initial state for an option.
850 if (option.getPossibleValues().contains(string))
852 cb.val.setSelectedItem(string);
856 throw new Error("Invalid value " + string + " for option " + option);
860 if (option.isRequired() && !cb.enabled.isSelected())
862 // TODO: indicate paramset is not valid.. option needs to be selected!
864 cb.setInitialValue();
867 void setParameter(ParameterI arg)
869 ParamBox pb = paramSet.get(arg.getName());
876 pb.updateControls(arg);
882 * recover options and parameters from GUI
886 public List<ArgumentI> getCurrentSettings()
888 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
889 for (OptionBox opts : getOptSet().values())
891 OptionI opt = opts.getOptionIfEnabled();
897 for (ParamBox parambox : getParamSet().values())
899 ParameterI parm = parambox.getParameter();