2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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.util.MessageManager;
22 import jalview.ws.params.ArgumentI;
23 import jalview.ws.params.OptionI;
24 import jalview.ws.params.ParameterI;
25 import jalview.ws.params.ValueConstrainI;
26 import jalview.ws.params.ValueConstrainI.ValueType;
28 import java.awt.BorderLayout;
29 import java.awt.Component;
30 import java.awt.Dimension;
32 import java.awt.GridLayout;
33 import java.awt.Rectangle;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.KeyEvent;
37 import java.awt.event.KeyListener;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
41 import java.util.ArrayList;
42 import java.util.Hashtable;
43 import java.util.List;
46 import javax.swing.JButton;
47 import javax.swing.JCheckBox;
48 import javax.swing.JComboBox;
49 import javax.swing.JComponent;
50 import javax.swing.JLabel;
51 import javax.swing.JMenuItem;
52 import javax.swing.JPanel;
53 import javax.swing.JPopupMenu;
54 import javax.swing.JScrollPane;
55 import javax.swing.JSlider;
56 import javax.swing.JTextArea;
57 import javax.swing.JTextField;
58 import javax.swing.border.TitledBorder;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
62 import net.miginfocom.swing.MigLayout;
65 * GUI generator/manager for options and parameters. Originally abstracted from
66 * the WsJobParameters dialog box.
71 public class OptsAndParamsPage
74 * compact or verbose style parameters
76 boolean compact = false;
78 public class OptionBox extends JPanel implements MouseListener,
81 JCheckBox enabled = new JCheckBox();
85 boolean hasLink = false;
87 boolean initEnabled = false;
89 String initVal = null;
93 JLabel optlabel = new JLabel();
95 JComboBox val = new JComboBox();
97 public OptionBox(OptionI opt)
100 setLayout(new BorderLayout());
101 enabled.setSelected(opt.isRequired()); // TODO: lock required options
102 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
104 enabled.setText(opt.getName());
105 enabled.addActionListener(this);
106 finfo = option.getFurtherDetails();
107 String desc = opt.getDescription();
112 enabled.setToolTipText("<html>"
114 .wrapTooltip(((desc == null || desc.trim().length()==0) ? "see further details by right-clicking"
116 + "<br><img src=\"" + linkImageURL + "\"/>")
118 enabled.addMouseListener(this);
122 if (desc != null && desc.trim().length()>0)
124 enabled.setToolTipText("<html>"
125 + JvSwingUtils.wrapTooltip(opt.getDescription())
129 add(enabled, BorderLayout.NORTH);
130 for (Object str : opt.getPossibleValues())
132 val.addItem((String) str);
134 val.setSelectedItem((String) opt.getValue());
135 if (opt.getPossibleValues().size() > 1)
137 setLayout(new GridLayout(1, 2));
138 val.addActionListener(this);
139 add(val, BorderLayout.SOUTH);
141 // TODO: add actionListeners for popup (to open further info),
142 // and to update list of parameters if an option is enabled
143 // that takes a value. JBPNote: is this TODO still valid ?
147 public void actionPerformed(ActionEvent e)
149 if (e.getSource() != enabled)
151 enabled.setSelected(true);
156 private void checkIfModified()
158 boolean notmod = (initEnabled == enabled.isSelected());
159 if (enabled.isSelected())
163 notmod &= initVal.equals(val.getSelectedItem());
167 // compare against default service setting
168 notmod &= option.getValue() == null
169 || option.getValue().equals(val.getSelectedItem());
174 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
175 : val.getSelectedItem() != initVal;
177 poparent.argSetModified(this, !notmod);
180 public OptionI getOptionIfEnabled()
182 if (!enabled.isSelected())
186 OptionI opt = option.copy();
187 if (opt.getPossibleValues() != null
188 && opt.getPossibleValues().size() == 1)
190 // Hack to make sure the default value for an enabled option with only
191 // one value is actually returned
192 opt.setValue(opt.getPossibleValues().get(0));
194 if (val.getSelectedItem() != null)
196 opt.setValue((String) val.getSelectedItem());
200 if (option.getValue() != null)
202 opt.setValue(option.getValue());
208 public void mouseClicked(MouseEvent e)
210 if (javax.swing.SwingUtilities.isRightMouseButton(e))
212 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
216 public void mouseEntered(MouseEvent e)
218 // TODO Auto-generated method stub
222 public void mouseExited(MouseEvent e)
224 // TODO Auto-generated method stub
228 public void mousePressed(MouseEvent e)
230 // TODO Auto-generated method stub
234 public void mouseReleased(MouseEvent e)
236 // TODO Auto-generated method stub
241 public void resetToDefault(boolean setDefaultParams)
243 enabled.setSelected(false);
244 if (option.isRequired() || (setDefaultParams && option.getValue()!=null))
246 // Apply default value
247 selectOption(option, option.getValue());
251 public void setInitialValue()
253 initEnabled = enabled.isSelected();
254 if (option.getPossibleValues() != null
255 && option.getPossibleValues().size() > 1)
257 initVal = (String) val.getSelectedItem();
261 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
267 public class ParamBox extends JPanel implements ChangeListener,
268 ActionListener, MouseListener
270 boolean adjusting = false;
272 boolean choice = false;
276 JPanel controlPanel = new JPanel();
278 boolean descisvisible = false;
280 JScrollPane descPanel = new JScrollPane();
284 boolean integ = false;
288 ParameterI parameter;
290 final OptsParametersContainerI pmdialogbox;
292 JPanel settingPanel = new JPanel();
294 JButton showDesc = new JButton();
296 JSlider slider = null;
298 JTextArea string = new JTextArea();
300 ValueConstrainI validator = null;
302 JTextField valueField = null;
304 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
306 pmdialogbox = pmlayout;
307 finfo = parm.getFurtherDetails();
308 validator = parm.getValidValue();
310 if (validator != null)
312 integ = validator.getType() == ValueType.Integer;
316 if (parameter.getPossibleValues() != null)
324 makeExpanderParam(parm);
328 makeCompactParam(parm);
333 private void makeCompactParam(ParameterI parm)
335 setLayout(new MigLayout("", "[][grow]"));
337 String ttipText = null;
339 controlPanel.setLayout(new BorderLayout());
341 if (parm.getDescription() != null
342 && parm.getDescription().trim().length() > 0)
344 // Only create description boxes if there actually is a description.
347 .wrapTooltip(parm.getDescription()
348 + (finfo != null ? "<br><img src=\""
350 + "\"/> Right click for further information."
354 JvSwingUtils.mgAddtoLayout(this, ttipText,
355 new JLabel(parm.getName()), controlPanel, "");
356 updateControls(parm);
360 private void makeExpanderParam(ParameterI parm)
362 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
363 setBorder(new TitledBorder(parm.getName()));
365 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
366 showDesc.setText("+");
367 string.setFont(new Font("Verdana", Font.PLAIN, 11));
368 string.setBackground(getBackground());
370 string.setEditable(false);
371 descPanel.getViewport().setView(string);
373 descPanel.setVisible(false);
375 JPanel firstrow = new JPanel();
376 firstrow.setLayout(null);
377 controlPanel.setLayout(new BorderLayout());
378 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
379 PARAM_CLOSEDHEIGHT - 50));
380 firstrow.add(controlPanel);
381 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
382 PARAM_CLOSEDHEIGHT - 30));
384 final ParamBox me = this;
386 if (parm.getDescription() != null
387 && parm.getDescription().trim().length() > 0)
389 // Only create description boxes if there actually is a description.
392 showDesc.setToolTipText("<html>"
394 .wrapTooltip("Click to show brief description<br><img src=\""
396 + "\"/> Right click for further information.")
398 showDesc.addMouseListener(this);
402 showDesc.setToolTipText("<html>"
404 .wrapTooltip("Click to show brief description.")
407 showDesc.addActionListener(new ActionListener()
410 public void actionPerformed(ActionEvent e)
412 descisvisible = !descisvisible;
413 descPanel.setVisible(descisvisible);
414 descPanel.getVerticalScrollBar().setValue(0);
415 me.setPreferredSize(new Dimension(PARAM_WIDTH,
416 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
418 pmdialogbox.refreshParamLayout();
421 string.setWrapStyleWord(true);
422 string.setLineWrap(true);
423 string.setColumns(32);
424 string.setText(parm.getDescription());
425 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
426 firstrow.add(showDesc);
429 validator = parm.getValidValue();
431 if (validator != null)
433 integ = validator.getType() == ValueType.Integer;
437 if (parameter.getPossibleValues() != null)
442 updateControls(parm);
443 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
444 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
449 public void actionPerformed(ActionEvent e)
457 updateSliderFromValueField();
462 private void checkIfModified()
464 Object cstate = updateSliderFromValueField();
465 boolean notmod = false;
466 if (cstate.getClass() == lastVal.getClass())
468 if (cstate instanceof int[])
470 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
472 else if (cstate instanceof float[])
474 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
476 else if (cstate instanceof String[])
478 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
481 pmdialogbox.argSetModified(this, !notmod);
485 public int getBaseline(int width, int height)
491 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
492 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
494 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
496 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
499 public int getBoxHeight()
501 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
504 public ParameterI getParameter()
506 ParameterI prm = parameter.copy();
509 prm.setValue((String) choicebox.getSelectedItem());
513 prm.setValue(valueField.getText());
520 // reset the widget's initial value.
524 public void mouseClicked(MouseEvent e)
526 if (javax.swing.SwingUtilities.isRightMouseButton(e))
528 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
532 public void mouseEntered(MouseEvent e)
534 // TODO Auto-generated method stub
538 public void mouseExited(MouseEvent e)
540 // TODO Auto-generated method stub
544 public void mousePressed(MouseEvent e)
546 // TODO Auto-generated method stub
550 public void mouseReleased(MouseEvent e)
552 // TODO Auto-generated method stub
556 public void stateChanged(ChangeEvent e)
560 valueField.setText(""
561 + ((integ) ? ("" + (int) slider.getValue())
562 : ("" + (float) (slider.getValue() / 1000f))));
568 public void updateControls(ParameterI parm)
571 boolean init = (choicebox == null && valueField == null);
576 choicebox = new JComboBox();
577 choicebox.addActionListener(this);
578 controlPanel.add(choicebox, BorderLayout.CENTER);
582 slider = new JSlider();
583 slider.addChangeListener(this);
584 valueField = new JTextField();
585 valueField.addActionListener(this);
586 valueField.addKeyListener(new KeyListener()
590 public void keyTyped(KeyEvent e)
595 public void keyReleased(KeyEvent e)
599 if (valueField.getText().trim().length() > 0)
601 actionPerformed(null);
607 public void keyPressed(KeyEvent e)
611 valueField.setPreferredSize(new Dimension(60, 25));
612 controlPanel.add(slider, BorderLayout.WEST);
613 controlPanel.add(valueField, BorderLayout.EAST);
624 List vals = parm.getPossibleValues();
625 for (Object val : vals)
627 choicebox.addItem(val);
631 if (parm.getValue() != null)
633 choicebox.setSelectedItem(parm.getValue());
638 valueField.setText(parm.getValue());
641 lastVal = updateSliderFromValueField();
645 public Object updateSliderFromValueField()
649 if (validator != null)
656 valueField.setText(valueField.getText().trim());
657 iVal = Integer.valueOf(valueField.getText());
658 if (validator.getMin() != null
659 && validator.getMin().intValue() > iVal)
661 iVal = validator.getMin().intValue();
662 // TODO: provide visual indication that hard limit was reached for
665 if (validator.getMax() != null
666 && validator.getMax().intValue() < iVal)
668 iVal = validator.getMax().intValue();
669 // TODO: provide visual indication that hard limit was reached for
672 } catch (Exception e)
676 // update value field to reflect any bound checking we performed.
677 valueField.setText("" + iVal);
678 if (validator.getMin() != null && validator.getMax() != null)
680 slider.getModel().setRangeProperties(iVal, 1,
681 validator.getMin().intValue(),
682 validator.getMax().intValue()+1, true);
686 slider.setVisible(false);
696 valueField.setText(valueField.getText().trim());
697 fVal = Float.valueOf(valueField.getText());
698 if (validator.getMin() != null
699 && validator.getMin().floatValue() > fVal)
701 fVal = validator.getMin().floatValue();
702 // TODO: provide visual indication that hard limit was reached for
704 // update value field to reflect any bound checking we performed.
705 valueField.setText("" + fVal);
707 if (validator.getMax() != null
708 && validator.getMax().floatValue() < fVal)
710 fVal = validator.getMax().floatValue();
711 // TODO: provide visual indication that hard limit was reached for
713 // update value field to reflect any bound checking we performed.
714 valueField.setText("" + fVal);
716 } catch (Exception e)
720 if (validator.getMin() != null && validator.getMax() != null)
722 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
723 (int) (validator.getMin().floatValue() * 1000f),
724 1+(int) (validator.getMax().floatValue() * 1000f), true);
728 slider.setVisible(false);
738 slider.setVisible(false);
740 { valueField.getText().trim() };
745 { (String) choicebox.getSelectedItem() };
752 public static final int PARAM_WIDTH = 340;
754 public static final int PARAM_HEIGHT = 150;
756 public static final int PARAM_CLOSEDHEIGHT = 80;
758 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
760 this(paramContainer, false);
763 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
766 poparent = paramContainer;
767 this.compact = compact;
770 public static void showUrlPopUp(JComponent invoker, final String finfo,
774 JPopupMenu mnu = new JPopupMenu();
775 JMenuItem mitem = new JMenuItem(MessageManager.formatMessage("label.view_params", new String[]{finfo}));
776 mitem.addActionListener(new ActionListener()
780 public void actionPerformed(ActionEvent e)
782 Desktop.showUrl(finfo);
787 mnu.show(invoker, x, y);
790 URL linkImageURL = getClass().getResource("/images/link.gif");
792 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
794 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
796 public Map<String, OptionBox> getOptSet()
801 public void setOptSet(Map<String, OptionBox> optSet)
803 this.optSet = optSet;
806 public Map<String, ParamBox> getParamSet()
811 public void setParamSet(Map<String, ParamBox> paramSet)
813 this.paramSet = paramSet;
816 OptsParametersContainerI poparent;
818 OptionBox addOption(OptionI opt)
820 OptionBox cb = optSet.get(opt.getName());
823 cb = new OptionBox(opt);
824 optSet.put(opt.getName(), cb);
825 // jobOptions.add(cb, FlowLayout.LEFT);
830 ParamBox addParameter(ParameterI arg)
832 ParamBox pb = paramSet.get(arg.getName());
835 pb = new ParamBox(poparent, arg);
836 paramSet.put(arg.getName(), pb);
837 // paramList.add(pb);
840 // take the defaults from the parameter
841 pb.updateControls(arg);
845 void selectOption(OptionI option, String string)
847 OptionBox cb = optSet.get(option.getName());
850 cb = addOption(option);
852 cb.enabled.setSelected(string != null); // initial state for an option.
855 if (option.getPossibleValues().contains(string))
857 cb.val.setSelectedItem(string);
861 throw new Error("Invalid value " + string + " for option " + option);
865 if (option.isRequired() && !cb.enabled.isSelected())
867 // TODO: indicate paramset is not valid.. option needs to be selected!
869 cb.setInitialValue();
872 void setParameter(ParameterI arg)
874 ParamBox pb = paramSet.get(arg.getName());
881 pb.updateControls(arg);
887 * recover options and parameters from GUI
891 public List<ArgumentI> getCurrentSettings()
893 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
894 for (OptionBox opts : getOptSet().values())
896 OptionI opt = opts.getOptionIfEnabled();
902 for (ParamBox parambox : getParamSet().values())
904 ParameterI parm = parambox.getParameter();