3 import jalview.ws.params.ArgumentI;
4 import jalview.ws.params.OptionI;
5 import jalview.ws.params.ParameterI;
6 import jalview.ws.params.ValueConstrainI;
7 import jalview.ws.params.ValueConstrainI.ValueType;
9 import java.awt.BorderLayout;
10 import java.awt.Component;
11 import java.awt.Dimension;
13 import java.awt.GridLayout;
14 import java.awt.Rectangle;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.event.KeyEvent;
18 import java.awt.event.KeyListener;
19 import java.awt.event.MouseEvent;
20 import java.awt.event.MouseListener;
22 import java.util.ArrayList;
23 import java.util.Hashtable;
24 import java.util.List;
27 import javax.swing.JButton;
28 import javax.swing.JCheckBox;
29 import javax.swing.JComboBox;
30 import javax.swing.JComponent;
31 import javax.swing.JLabel;
32 import javax.swing.JMenuItem;
33 import javax.swing.JPanel;
34 import javax.swing.JPopupMenu;
35 import javax.swing.JScrollPane;
36 import javax.swing.JSlider;
37 import javax.swing.JTextArea;
38 import javax.swing.JTextField;
39 import javax.swing.border.TitledBorder;
40 import javax.swing.event.ChangeEvent;
41 import javax.swing.event.ChangeListener;
43 import net.miginfocom.swing.MigLayout;
46 * GUI generator/manager for options and parameters. Originally abstracted from
47 * the WsJobParameters dialog box.
52 public class OptsAndParamsPage
55 * compact or verbose style parameters
57 boolean compact = false;
59 public class OptionBox extends JPanel implements MouseListener,
62 JCheckBox enabled = new JCheckBox();
66 boolean hasLink = false;
68 boolean initEnabled = false;
70 String initVal = null;
74 JLabel optlabel = new JLabel();
76 JComboBox val = new JComboBox();
78 public OptionBox(OptionI opt)
81 setLayout(new BorderLayout());
82 enabled.setSelected(opt.isRequired()); // TODO: lock required options
83 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
85 enabled.setText(opt.getName());
86 enabled.addActionListener(this);
87 finfo = option.getFurtherDetails();
88 String desc = opt.getDescription();
93 enabled.setToolTipText("<html>"
95 .wrapTooltip(((desc == null) ? "see further details by right-clicking"
97 + "<br><img src=\"" + linkImageURL + "\"/>")
99 enabled.addMouseListener(this);
105 enabled.setToolTipText("<html>"
106 + JvSwingUtils.wrapTooltip(opt.getDescription())
110 add(enabled, BorderLayout.NORTH);
111 if (opt.getPossibleValues().size() > 1)
113 setLayout(new GridLayout(1, 2));
114 for (Object str : opt.getPossibleValues())
116 val.addItem((String) str);
118 val.setSelectedItem((String) opt.getValue());
119 val.addActionListener(this);
120 add(val, BorderLayout.SOUTH);
122 // TODO: add actionListeners for popup (to open further info),
123 // and to update list of parameters if an option is enabled
124 // that takes a value. JBPNote: is this TODO still valid ?
128 public void actionPerformed(ActionEvent e)
130 if (e.getSource() != enabled)
132 enabled.setSelected(true);
137 private void checkIfModified()
139 boolean notmod = (initEnabled == enabled.isSelected());
140 if (enabled.isSelected())
144 notmod &= initVal.equals(val.getSelectedItem());
148 // compare against default service setting
149 notmod &= option.getValue() == null
150 || option.getValue().equals(val.getSelectedItem());
155 notmod &= initVal == null;
157 poparent.argSetModified(this, !notmod);
160 public OptionI getOptionIfEnabled()
162 if (!enabled.isSelected())
166 OptionI opt = option.copy();
168 if (val.getSelectedItem() != null)
170 opt.setValue((String) val.getSelectedItem());
175 public void mouseClicked(MouseEvent e)
177 if (javax.swing.SwingUtilities.isRightMouseButton(e))
179 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
183 public void mouseEntered(MouseEvent e)
185 // TODO Auto-generated method stub
189 public void mouseExited(MouseEvent e)
191 // TODO Auto-generated method stub
195 public void mousePressed(MouseEvent e)
197 // TODO Auto-generated method stub
201 public void mouseReleased(MouseEvent e)
203 // TODO Auto-generated method stub
207 public void resetToDefault()
209 enabled.setSelected(false);
210 if (option.isRequired())
212 // Apply default value
213 selectOption(option, option.getValue());
217 public void setInitialValue()
219 initEnabled = enabled.isSelected();
220 if (option.getPossibleValues() != null
221 && option.getPossibleValues().size() > 1)
223 initVal = (String) val.getSelectedItem();
227 initVal = (initEnabled) ? option.getValue() : null;
233 public class ParamBox extends JPanel implements ChangeListener,
234 ActionListener, MouseListener
236 boolean adjusting = false;
238 boolean choice = false;
242 JPanel controlPanel = new JPanel();
244 boolean descisvisible = false;
246 JScrollPane descPanel = new JScrollPane();
250 boolean integ = false;
254 ParameterI parameter;
256 final OptsParametersContainerI pmdialogbox;
258 JPanel settingPanel = new JPanel();
260 JButton showDesc = new JButton();
262 JSlider slider = null;
264 JTextArea string = new JTextArea();
266 ValueConstrainI validator = null;
268 JTextField valueField = null;
270 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
272 pmdialogbox = pmlayout;
273 finfo = parm.getFurtherDetails();
274 validator = parm.getValidValue();
276 if (validator != null)
278 integ = validator.getType() == ValueType.Integer;
282 if (parameter.getPossibleValues() != null)
290 makeExpanderParam(parm);
294 makeCompactParam(parm);
299 private void makeCompactParam(ParameterI parm)
301 setLayout(new MigLayout("","[][grow]"));
303 String ttipText=null;
305 controlPanel.setLayout(new BorderLayout());
307 if (parm.getDescription() != null
308 && parm.getDescription().trim().length() > 0)
310 // Only create description boxes if there actually is a description.
313 .wrapTooltip(parm.getDescription()+(finfo!=null ?"<br><img src=\""
315 + "\"/> Right click for further information.":""))
319 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),controlPanel, "");
320 updateControls(parm);
324 private void makeExpanderParam(ParameterI parm)
326 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
327 setBorder(new TitledBorder(parm.getName()));
329 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
330 showDesc.setText("+");
331 string.setFont(new Font("Verdana", Font.PLAIN, 11));
332 string.setBackground(getBackground());
334 string.setEditable(false);
335 descPanel.getViewport().setView(string);
337 descPanel.setVisible(false);
339 JPanel firstrow = new JPanel();
340 firstrow.setLayout(null);
341 controlPanel.setLayout(new BorderLayout());
342 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
343 PARAM_CLOSEDHEIGHT - 50));
344 firstrow.add(controlPanel);
345 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
346 PARAM_CLOSEDHEIGHT - 30));
348 final ParamBox me = this;
350 if (parm.getDescription() != null
351 && parm.getDescription().trim().length() > 0)
353 // Only create description boxes if there actually is a description.
356 showDesc.setToolTipText("<html>"
358 .wrapTooltip("Click to show brief description<br><img src=\""
360 + "\"/> Right click for further information.")
362 showDesc.addMouseListener(this);
366 showDesc.setToolTipText("<html>"
368 .wrapTooltip("Click to show brief description.")
371 showDesc.addActionListener(new ActionListener()
374 public void actionPerformed(ActionEvent e)
376 descisvisible = !descisvisible;
377 descPanel.setVisible(descisvisible);
378 descPanel.getVerticalScrollBar().setValue(0);
379 me.setPreferredSize(new Dimension(PARAM_WIDTH,
380 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
382 pmdialogbox.refreshParamLayout();
385 string.setWrapStyleWord(true);
386 string.setLineWrap(true);
387 string.setColumns(32);
388 string.setText(parm.getDescription());
389 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
390 firstrow.add(showDesc);
393 validator = parm.getValidValue();
395 if (validator != null)
397 integ = validator.getType() == ValueType.Integer;
401 if (parameter.getPossibleValues() != null)
406 updateControls(parm);
407 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
408 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
413 public void actionPerformed(ActionEvent e)
421 updateSliderFromValueField();
426 private void checkIfModified()
428 Object cstate = updateSliderFromValueField();
429 boolean notmod = false;
430 if (cstate.getClass() == lastVal.getClass())
432 if (cstate instanceof int[])
434 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
436 else if (cstate instanceof float[])
438 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
440 else if (cstate instanceof String[])
442 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
445 pmdialogbox.argSetModified(this, !notmod);
449 public int getBaseline(int width, int height)
455 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
456 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
458 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
460 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
463 public int getBoxHeight()
465 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
468 public ParameterI getParameter()
470 ParameterI prm = parameter.copy();
473 prm.setValue((String) choicebox.getSelectedItem());
477 prm.setValue(valueField.getText());
484 // reset the widget's initial value.
488 public void mouseClicked(MouseEvent e)
490 if (javax.swing.SwingUtilities.isRightMouseButton(e))
492 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
496 public void mouseEntered(MouseEvent e)
498 // TODO Auto-generated method stub
502 public void mouseExited(MouseEvent e)
504 // TODO Auto-generated method stub
508 public void mousePressed(MouseEvent e)
510 // TODO Auto-generated method stub
514 public void mouseReleased(MouseEvent e)
516 // TODO Auto-generated method stub
520 public void stateChanged(ChangeEvent e)
524 valueField.setText(""
525 + ((integ) ? ("" + (int) slider.getValue())
526 : ("" + (float) (slider.getValue() / 1000f))));
532 public void updateControls(ParameterI parm)
535 boolean init = (choicebox == null && valueField == null);
540 choicebox = new JComboBox();
541 choicebox.addActionListener(this);
542 controlPanel.add(choicebox, BorderLayout.CENTER);
546 slider = new JSlider();
547 slider.addChangeListener(this);
548 valueField = new JTextField();
549 valueField.addActionListener(this);
550 valueField.addKeyListener(new KeyListener()
554 public void keyTyped(KeyEvent e)
559 public void keyReleased(KeyEvent e)
561 if (valueField.getText().trim().length()>0)
563 actionPerformed(null);
568 public void keyPressed(KeyEvent e)
572 valueField.setPreferredSize(new Dimension(60, 25));
573 controlPanel.add(slider, BorderLayout.WEST);
574 controlPanel.add(valueField, BorderLayout.EAST);
585 List vals = parm.getPossibleValues();
586 for (Object val : vals)
588 choicebox.addItem(val);
592 if (parm.getValue() != null)
594 choicebox.setSelectedItem(parm.getValue());
599 valueField.setText(parm.getValue());
602 lastVal = updateSliderFromValueField();
606 public Object updateSliderFromValueField()
610 if (validator != null)
617 valueField.setText(valueField.getText().trim());
618 iVal = Integer.valueOf(valueField.getText());
619 if (validator.getMin() != null
620 && validator.getMin().intValue() > iVal)
622 iVal = validator.getMin().intValue();
623 // TODO: provide visual indication that hard limit was reached for
626 if (validator.getMax() != null
627 && validator.getMax().intValue() < iVal)
629 iVal = validator.getMax().intValue();
630 // TODO: provide visual indication that hard limit was reached for
633 } catch (Exception e)
637 if (validator.getMin() != null && validator.getMax() != null)
639 slider.getModel().setRangeProperties(iVal, 1,
640 validator.getMin().intValue(),
641 validator.getMax().intValue(), true);
645 slider.setVisible(false);
655 fVal = Float.valueOf(valueField.getText());
656 if (validator.getMin() != null
657 && validator.getMin().floatValue() > fVal)
659 fVal = validator.getMin().floatValue();
660 // TODO: provide visual indication that hard limit was reached for
663 if (validator.getMax() != null
664 && validator.getMax().floatValue() < fVal)
666 fVal = validator.getMax().floatValue();
667 // TODO: provide visual indication that hard limit was reached for
670 } catch (Exception e)
674 if (validator.getMin() != null && validator.getMax() != null)
676 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
677 (int) validator.getMin().floatValue() * 1000,
678 (int) validator.getMax().floatValue() * 1000, true);
682 slider.setVisible(false);
692 slider.setVisible(false);
694 { valueField.getText().trim() };
699 { (String) choicebox.getSelectedItem() };
706 public static final int PARAM_WIDTH = 340;
708 public static final int PARAM_HEIGHT = 150;
710 public static final int PARAM_CLOSEDHEIGHT = 80;
712 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
714 this(paramContainer,false);
716 public OptsAndParamsPage(OptsParametersContainerI paramContainer, boolean compact)
718 poparent = paramContainer;
719 this.compact=compact;
722 public static void showUrlPopUp(JComponent invoker, final String finfo,
726 JPopupMenu mnu = new JPopupMenu();
727 JMenuItem mitem = new JMenuItem("View " + finfo);
728 mitem.addActionListener(new ActionListener()
732 public void actionPerformed(ActionEvent e)
734 Desktop.showUrl(finfo);
739 mnu.show(invoker, x, y);
742 URL linkImageURL = getClass().getResource("/images/link.gif");
744 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
746 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
748 public Map<String, OptionBox> getOptSet()
753 public void setOptSet(Map<String, OptionBox> optSet)
755 this.optSet = optSet;
758 public Map<String, ParamBox> getParamSet()
763 public void setParamSet(Map<String, ParamBox> paramSet)
765 this.paramSet = paramSet;
768 OptsParametersContainerI poparent;
770 OptionBox addOption(OptionI opt)
772 OptionBox cb = optSet.get(opt.getName());
775 cb = new OptionBox(opt);
776 optSet.put(opt.getName(), cb);
777 // jobOptions.add(cb, FlowLayout.LEFT);
782 ParamBox addParameter(ParameterI arg)
784 ParamBox pb = paramSet.get(arg.getName());
787 pb = new ParamBox(poparent, arg);
788 paramSet.put(arg.getName(), pb);
789 // paramList.add(pb);
792 // take the defaults from the parameter
793 pb.updateControls(arg);
797 void selectOption(OptionI option, String string)
799 OptionBox cb = optSet.get(option.getName());
802 cb = addOption(option);
804 cb.enabled.setSelected(string != null); // initial state for an option.
807 if (option.getPossibleValues().contains(string))
809 cb.val.setSelectedItem(string);
813 throw new Error("Invalid value " + string + " for option " + option);
817 if (option.isRequired() && !cb.enabled.isSelected())
819 // TODO: indicate paramset is not valid.. option needs to be selected!
821 cb.setInitialValue();
824 void setParameter(ParameterI arg)
826 ParamBox pb = paramSet.get(arg.getName());
833 pb.updateControls(arg);
839 * recover options and parameters from GUI
843 public List<ArgumentI> getCurrentSettings()
845 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
846 for (OptionBox opts : getOptSet().values())
848 OptionI opt = opts.getOptionIfEnabled();
854 for (ParamBox parambox : getParamSet().values())
856 ParameterI parm = parambox.getParameter();