1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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/>.
17 *******************************************************************************/
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()) : val.getSelectedItem()!=initVal;
174 poparent.argSetModified(this, !notmod);
177 public OptionI getOptionIfEnabled()
179 if (!enabled.isSelected())
183 OptionI opt = option.copy();
184 if (opt.getPossibleValues()!=null && opt.getPossibleValues().size()==1)
186 // Hack to make sure the default value for an enabled option with only one value is actually returned
187 opt.setValue(opt.getPossibleValues().get(0));
189 if (val.getSelectedItem() != null)
191 opt.setValue((String) val.getSelectedItem());
193 if (option.getValue()!=null)
195 opt.setValue(option.getValue());
201 public void mouseClicked(MouseEvent e)
203 if (javax.swing.SwingUtilities.isRightMouseButton(e))
205 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
209 public void mouseEntered(MouseEvent e)
211 // TODO Auto-generated method stub
215 public void mouseExited(MouseEvent e)
217 // TODO Auto-generated method stub
221 public void mousePressed(MouseEvent e)
223 // TODO Auto-generated method stub
227 public void mouseReleased(MouseEvent e)
229 // TODO Auto-generated method stub
233 public void resetToDefault()
235 enabled.setSelected(false);
236 if (option.isRequired())
238 // Apply default value
239 selectOption(option, option.getValue());
243 public void setInitialValue()
245 initEnabled = enabled.isSelected();
246 if (option.getPossibleValues() != null
247 && option.getPossibleValues().size() > 1)
249 initVal = (String) val.getSelectedItem();
253 initVal = (initEnabled) ? (String)val.getSelectedItem() : null;
259 public class ParamBox extends JPanel implements ChangeListener,
260 ActionListener, MouseListener
262 boolean adjusting = false;
264 boolean choice = false;
268 JPanel controlPanel = new JPanel();
270 boolean descisvisible = false;
272 JScrollPane descPanel = new JScrollPane();
276 boolean integ = false;
280 ParameterI parameter;
282 final OptsParametersContainerI pmdialogbox;
284 JPanel settingPanel = new JPanel();
286 JButton showDesc = new JButton();
288 JSlider slider = null;
290 JTextArea string = new JTextArea();
292 ValueConstrainI validator = null;
294 JTextField valueField = null;
296 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
298 pmdialogbox = pmlayout;
299 finfo = parm.getFurtherDetails();
300 validator = parm.getValidValue();
302 if (validator != null)
304 integ = validator.getType() == ValueType.Integer;
308 if (parameter.getPossibleValues() != null)
316 makeExpanderParam(parm);
320 makeCompactParam(parm);
325 private void makeCompactParam(ParameterI parm)
327 setLayout(new MigLayout("","[][grow]"));
329 String ttipText=null;
331 controlPanel.setLayout(new BorderLayout());
333 if (parm.getDescription() != null
334 && parm.getDescription().trim().length() > 0)
336 // Only create description boxes if there actually is a description.
339 .wrapTooltip(parm.getDescription()+(finfo!=null ?"<br><img src=\""
341 + "\"/> Right click for further information.":""))
345 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),controlPanel, "");
346 updateControls(parm);
350 private void makeExpanderParam(ParameterI parm)
352 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
353 setBorder(new TitledBorder(parm.getName()));
355 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
356 showDesc.setText("+");
357 string.setFont(new Font("Verdana", Font.PLAIN, 11));
358 string.setBackground(getBackground());
360 string.setEditable(false);
361 descPanel.getViewport().setView(string);
363 descPanel.setVisible(false);
365 JPanel firstrow = new JPanel();
366 firstrow.setLayout(null);
367 controlPanel.setLayout(new BorderLayout());
368 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
369 PARAM_CLOSEDHEIGHT - 50));
370 firstrow.add(controlPanel);
371 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
372 PARAM_CLOSEDHEIGHT - 30));
374 final ParamBox me = this;
376 if (parm.getDescription() != null
377 && parm.getDescription().trim().length() > 0)
379 // Only create description boxes if there actually is a description.
382 showDesc.setToolTipText("<html>"
384 .wrapTooltip("Click to show brief description<br><img src=\""
386 + "\"/> Right click for further information.")
388 showDesc.addMouseListener(this);
392 showDesc.setToolTipText("<html>"
394 .wrapTooltip("Click to show brief description.")
397 showDesc.addActionListener(new ActionListener()
400 public void actionPerformed(ActionEvent e)
402 descisvisible = !descisvisible;
403 descPanel.setVisible(descisvisible);
404 descPanel.getVerticalScrollBar().setValue(0);
405 me.setPreferredSize(new Dimension(PARAM_WIDTH,
406 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
408 pmdialogbox.refreshParamLayout();
411 string.setWrapStyleWord(true);
412 string.setLineWrap(true);
413 string.setColumns(32);
414 string.setText(parm.getDescription());
415 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
416 firstrow.add(showDesc);
419 validator = parm.getValidValue();
421 if (validator != null)
423 integ = validator.getType() == ValueType.Integer;
427 if (parameter.getPossibleValues() != null)
432 updateControls(parm);
433 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
434 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
439 public void actionPerformed(ActionEvent e)
447 updateSliderFromValueField();
452 private void checkIfModified()
454 Object cstate = updateSliderFromValueField();
455 boolean notmod = false;
456 if (cstate.getClass() == lastVal.getClass())
458 if (cstate instanceof int[])
460 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
462 else if (cstate instanceof float[])
464 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
466 else if (cstate instanceof String[])
468 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
471 pmdialogbox.argSetModified(this, !notmod);
475 public int getBaseline(int width, int height)
481 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
482 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
484 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
486 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
489 public int getBoxHeight()
491 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
494 public ParameterI getParameter()
496 ParameterI prm = parameter.copy();
499 prm.setValue((String) choicebox.getSelectedItem());
503 prm.setValue(valueField.getText());
510 // reset the widget's initial value.
514 public void mouseClicked(MouseEvent e)
516 if (javax.swing.SwingUtilities.isRightMouseButton(e))
518 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
522 public void mouseEntered(MouseEvent e)
524 // TODO Auto-generated method stub
528 public void mouseExited(MouseEvent e)
530 // TODO Auto-generated method stub
534 public void mousePressed(MouseEvent e)
536 // TODO Auto-generated method stub
540 public void mouseReleased(MouseEvent e)
542 // TODO Auto-generated method stub
546 public void stateChanged(ChangeEvent e)
550 valueField.setText(""
551 + ((integ) ? ("" + (int) slider.getValue())
552 : ("" + (float) (slider.getValue() / 1000f))));
558 public void updateControls(ParameterI parm)
561 boolean init = (choicebox == null && valueField == null);
566 choicebox = new JComboBox();
567 choicebox.addActionListener(this);
568 controlPanel.add(choicebox, BorderLayout.CENTER);
572 slider = new JSlider();
573 slider.addChangeListener(this);
574 valueField = new JTextField();
575 valueField.addActionListener(this);
576 valueField.addKeyListener(new KeyListener()
580 public void keyTyped(KeyEvent e)
585 public void keyReleased(KeyEvent e)
587 if (valueField.getText().trim().length()>0)
589 actionPerformed(null);
594 public void keyPressed(KeyEvent e)
598 valueField.setPreferredSize(new Dimension(60, 25));
599 controlPanel.add(slider, BorderLayout.WEST);
600 controlPanel.add(valueField, BorderLayout.EAST);
611 List vals = parm.getPossibleValues();
612 for (Object val : vals)
614 choicebox.addItem(val);
618 if (parm.getValue() != null)
620 choicebox.setSelectedItem(parm.getValue());
625 valueField.setText(parm.getValue());
628 lastVal = updateSliderFromValueField();
632 public Object updateSliderFromValueField()
636 if (validator != null)
643 valueField.setText(valueField.getText().trim());
644 iVal = Integer.valueOf(valueField.getText());
645 if (validator.getMin() != null
646 && validator.getMin().intValue() > iVal)
648 iVal = validator.getMin().intValue();
649 // TODO: provide visual indication that hard limit was reached for
652 if (validator.getMax() != null
653 && validator.getMax().intValue() < iVal)
655 iVal = validator.getMax().intValue();
656 // TODO: provide visual indication that hard limit was reached for
659 } catch (Exception e)
663 if (validator.getMin() != null && validator.getMax() != null)
665 slider.getModel().setRangeProperties(iVal, 1,
666 validator.getMin().intValue(),
667 validator.getMax().intValue(), true);
671 slider.setVisible(false);
681 fVal = Float.valueOf(valueField.getText());
682 if (validator.getMin() != null
683 && validator.getMin().floatValue() > fVal)
685 fVal = validator.getMin().floatValue();
686 // TODO: provide visual indication that hard limit was reached for
689 if (validator.getMax() != null
690 && validator.getMax().floatValue() < fVal)
692 fVal = validator.getMax().floatValue();
693 // TODO: provide visual indication that hard limit was reached for
696 } catch (Exception e)
700 if (validator.getMin() != null && validator.getMax() != null)
702 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
703 (int) validator.getMin().floatValue() * 1000,
704 (int) validator.getMax().floatValue() * 1000, true);
708 slider.setVisible(false);
718 slider.setVisible(false);
720 { valueField.getText().trim() };
725 { (String) choicebox.getSelectedItem() };
732 public static final int PARAM_WIDTH = 340;
734 public static final int PARAM_HEIGHT = 150;
736 public static final int PARAM_CLOSEDHEIGHT = 80;
738 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
740 this(paramContainer,false);
742 public OptsAndParamsPage(OptsParametersContainerI paramContainer, boolean compact)
744 poparent = paramContainer;
745 this.compact=compact;
748 public static void showUrlPopUp(JComponent invoker, final String finfo,
752 JPopupMenu mnu = new JPopupMenu();
753 JMenuItem mitem = new JMenuItem("View " + finfo);
754 mitem.addActionListener(new ActionListener()
758 public void actionPerformed(ActionEvent e)
760 Desktop.showUrl(finfo);
765 mnu.show(invoker, x, y);
768 URL linkImageURL = getClass().getResource("/images/link.gif");
770 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
772 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
774 public Map<String, OptionBox> getOptSet()
779 public void setOptSet(Map<String, OptionBox> optSet)
781 this.optSet = optSet;
784 public Map<String, ParamBox> getParamSet()
789 public void setParamSet(Map<String, ParamBox> paramSet)
791 this.paramSet = paramSet;
794 OptsParametersContainerI poparent;
796 OptionBox addOption(OptionI opt)
798 OptionBox cb = optSet.get(opt.getName());
801 cb = new OptionBox(opt);
802 optSet.put(opt.getName(), cb);
803 // jobOptions.add(cb, FlowLayout.LEFT);
808 ParamBox addParameter(ParameterI arg)
810 ParamBox pb = paramSet.get(arg.getName());
813 pb = new ParamBox(poparent, arg);
814 paramSet.put(arg.getName(), pb);
815 // paramList.add(pb);
818 // take the defaults from the parameter
819 pb.updateControls(arg);
823 void selectOption(OptionI option, String string)
825 OptionBox cb = optSet.get(option.getName());
828 cb = addOption(option);
830 cb.enabled.setSelected(string != null); // initial state for an option.
833 if (option.getPossibleValues().contains(string))
835 cb.val.setSelectedItem(string);
839 throw new Error("Invalid value " + string + " for option " + option);
843 if (option.isRequired() && !cb.enabled.isSelected())
845 // TODO: indicate paramset is not valid.. option needs to be selected!
847 cb.setInitialValue();
850 void setParameter(ParameterI arg)
852 ParamBox pb = paramSet.get(arg.getName());
859 pb.updateControls(arg);
865 * recover options and parameters from GUI
869 public List<ArgumentI> getCurrentSettings()
871 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
872 for (OptionBox opts : getOptSet().values())
874 OptionI opt = opts.getOptionIfEnabled();
880 for (ParamBox parambox : getParamSet().values())
882 ParameterI parm = parambox.getParameter();