2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.util.MessageManager;
24 import jalview.ws.params.ArgumentI;
25 import jalview.ws.params.OptionI;
26 import jalview.ws.params.ParameterI;
27 import jalview.ws.params.ValueConstrainI;
28 import jalview.ws.params.ValueConstrainI.ValueType;
30 import java.awt.BorderLayout;
31 import java.awt.Component;
32 import java.awt.Dimension;
34 import java.awt.GridLayout;
35 import java.awt.Rectangle;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.KeyEvent;
39 import java.awt.event.KeyListener;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
43 import java.util.ArrayList;
44 import java.util.Hashtable;
45 import java.util.List;
48 import javax.swing.JButton;
49 import javax.swing.JCheckBox;
50 import javax.swing.JComboBox;
51 import javax.swing.JComponent;
52 import javax.swing.JLabel;
53 import javax.swing.JMenuItem;
54 import javax.swing.JPanel;
55 import javax.swing.JPopupMenu;
56 import javax.swing.JScrollPane;
57 import javax.swing.JSlider;
58 import javax.swing.JTextArea;
59 import javax.swing.JTextField;
60 import javax.swing.border.TitledBorder;
61 import javax.swing.event.ChangeEvent;
62 import javax.swing.event.ChangeListener;
64 import net.miginfocom.swing.MigLayout;
67 * GUI generator/manager for options and parameters. Originally abstracted from
68 * the WsJobParameters dialog box.
73 public class OptsAndParamsPage
76 * compact or verbose style parameters
78 boolean compact = false;
80 public class OptionBox extends JPanel implements MouseListener,
83 JCheckBox enabled = new JCheckBox();
87 boolean hasLink = false;
89 boolean initEnabled = false;
91 String initVal = null;
95 JLabel optlabel = new JLabel();
97 JComboBox val = new JComboBox();
99 public OptionBox(OptionI opt)
102 setLayout(new BorderLayout());
103 enabled.setSelected(opt.isRequired()); // TODO: lock required options
104 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
106 enabled.setText(opt.getName());
107 enabled.addActionListener(this);
108 finfo = option.getFurtherDetails();
109 String desc = opt.getDescription();
114 enabled.setToolTipText("<html>"
116 .wrapTooltip(((desc == null || desc.trim().length() == 0) ? "see further details by right-clicking"
118 + "<br><img src=\"" + linkImageURL + "\"/>")
120 enabled.addMouseListener(this);
124 if (desc != null && desc.trim().length() > 0)
126 enabled.setToolTipText("<html>"
127 + JvSwingUtils.wrapTooltip(opt.getDescription())
131 add(enabled, BorderLayout.NORTH);
132 for (Object str : opt.getPossibleValues())
134 val.addItem((String) str);
136 val.setSelectedItem((String) opt.getValue());
137 if (opt.getPossibleValues().size() > 1)
139 setLayout(new GridLayout(1, 2));
140 val.addActionListener(this);
141 add(val, BorderLayout.SOUTH);
143 // TODO: add actionListeners for popup (to open further info),
144 // and to update list of parameters if an option is enabled
145 // that takes a value. JBPNote: is this TODO still valid ?
149 public void actionPerformed(ActionEvent e)
151 if (e.getSource() != enabled)
153 enabled.setSelected(true);
158 private void checkIfModified()
160 boolean notmod = (initEnabled == enabled.isSelected());
161 if (enabled.isSelected())
165 notmod &= initVal.equals(val.getSelectedItem());
169 // compare against default service setting
170 notmod &= option.getValue() == null
171 || option.getValue().equals(val.getSelectedItem());
176 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
177 : val.getSelectedItem() != initVal;
179 poparent.argSetModified(this, !notmod);
182 public OptionI getOptionIfEnabled()
184 if (!enabled.isSelected())
188 OptionI opt = option.copy();
189 if (opt.getPossibleValues() != null
190 && opt.getPossibleValues().size() == 1)
192 // Hack to make sure the default value for an enabled option with only
193 // one value is actually returned
194 opt.setValue(opt.getPossibleValues().get(0));
196 if (val.getSelectedItem() != null)
198 opt.setValue((String) val.getSelectedItem());
202 if (option.getValue() != null)
204 opt.setValue(option.getValue());
210 public void mouseClicked(MouseEvent e)
212 if (javax.swing.SwingUtilities.isRightMouseButton(e))
214 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
218 public void mouseEntered(MouseEvent e)
220 // TODO Auto-generated method stub
224 public void mouseExited(MouseEvent e)
226 // TODO Auto-generated method stub
230 public void mousePressed(MouseEvent e)
232 // TODO Auto-generated method stub
236 public void mouseReleased(MouseEvent e)
238 // TODO Auto-generated method stub
242 public void resetToDefault(boolean setDefaultParams)
244 enabled.setSelected(false);
245 if (option.isRequired()
246 || (setDefaultParams && option.getValue() != null))
248 // Apply default value
249 selectOption(option, option.getValue());
253 public void setInitialValue()
255 initEnabled = enabled.isSelected();
256 if (option.getPossibleValues() != null
257 && option.getPossibleValues().size() > 1)
259 initVal = (String) val.getSelectedItem();
263 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
269 public class ParamBox extends JPanel implements ChangeListener,
270 ActionListener, MouseListener
272 boolean adjusting = false;
274 boolean choice = false;
278 JPanel controlPanel = new JPanel();
280 boolean descisvisible = false;
282 JScrollPane descPanel = new JScrollPane();
286 boolean integ = false;
290 ParameterI parameter;
292 final OptsParametersContainerI pmdialogbox;
294 JPanel settingPanel = new JPanel();
296 JButton showDesc = new JButton();
298 JSlider slider = null;
300 JTextArea string = new JTextArea();
302 ValueConstrainI validator = null;
304 JTextField valueField = null;
306 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
308 pmdialogbox = pmlayout;
309 finfo = parm.getFurtherDetails();
310 validator = parm.getValidValue();
312 if (validator != null)
314 integ = validator.getType() == ValueType.Integer;
318 if (parameter.getPossibleValues() != null)
326 makeExpanderParam(parm);
330 makeCompactParam(parm);
335 private void makeCompactParam(ParameterI parm)
337 setLayout(new MigLayout("", "[][grow]"));
339 String ttipText = null;
341 controlPanel.setLayout(new BorderLayout());
343 if (parm.getDescription() != null
344 && parm.getDescription().trim().length() > 0)
346 // Only create description boxes if there actually is a description.
349 .wrapTooltip(parm.getDescription()
350 + (finfo != null ? "<br><img src=\""
352 + "\"/> Right click for further information."
356 JvSwingUtils.mgAddtoLayout(this, ttipText,
357 new JLabel(parm.getName()), controlPanel, "");
358 updateControls(parm);
362 private void makeExpanderParam(ParameterI parm)
364 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
365 setBorder(new TitledBorder(parm.getName()));
367 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
368 showDesc.setText("+");
369 string.setFont(new Font("Verdana", Font.PLAIN, 11));
370 string.setBackground(getBackground());
372 string.setEditable(false);
373 descPanel.getViewport().setView(string);
375 descPanel.setVisible(false);
377 JPanel firstrow = new JPanel();
378 firstrow.setLayout(null);
379 controlPanel.setLayout(new BorderLayout());
380 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
381 PARAM_CLOSEDHEIGHT - 50));
382 firstrow.add(controlPanel);
383 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
384 PARAM_CLOSEDHEIGHT - 30));
386 final ParamBox me = this;
388 if (parm.getDescription() != null
389 && parm.getDescription().trim().length() > 0)
391 // Only create description boxes if there actually is a description.
394 showDesc.setToolTipText("<html>"
396 .wrapTooltip("Click to show brief description<br><img src=\""
398 + "\"/> Right click for further information.")
400 showDesc.addMouseListener(this);
404 showDesc.setToolTipText("<html>"
406 .wrapTooltip("Click to show brief description.")
409 showDesc.addActionListener(new ActionListener()
412 public void actionPerformed(ActionEvent e)
414 descisvisible = !descisvisible;
415 descPanel.setVisible(descisvisible);
416 descPanel.getVerticalScrollBar().setValue(0);
417 me.setPreferredSize(new Dimension(PARAM_WIDTH,
418 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
420 pmdialogbox.refreshParamLayout();
423 string.setWrapStyleWord(true);
424 string.setLineWrap(true);
425 string.setColumns(32);
426 string.setText(parm.getDescription());
427 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
428 firstrow.add(showDesc);
431 validator = parm.getValidValue();
433 if (validator != null)
435 integ = validator.getType() == ValueType.Integer;
439 if (parameter.getPossibleValues() != null)
444 updateControls(parm);
445 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
446 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
451 public void actionPerformed(ActionEvent e)
459 updateSliderFromValueField();
464 private void checkIfModified()
466 Object cstate = updateSliderFromValueField();
467 boolean notmod = false;
468 if (cstate.getClass() == lastVal.getClass())
470 if (cstate instanceof int[])
472 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
474 else if (cstate instanceof float[])
476 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
478 else if (cstate instanceof String[])
480 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
483 pmdialogbox.argSetModified(this, !notmod);
487 public int getBaseline(int width, int height)
493 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
494 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
496 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
498 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
501 public int getBoxHeight()
503 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
506 public ParameterI getParameter()
508 ParameterI prm = parameter.copy();
511 prm.setValue((String) choicebox.getSelectedItem());
515 prm.setValue(valueField.getText());
522 // reset the widget's initial value.
526 public void mouseClicked(MouseEvent e)
528 if (javax.swing.SwingUtilities.isRightMouseButton(e))
530 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
534 public void mouseEntered(MouseEvent e)
536 // TODO Auto-generated method stub
540 public void mouseExited(MouseEvent e)
542 // TODO Auto-generated method stub
546 public void mousePressed(MouseEvent e)
548 // TODO Auto-generated method stub
552 public void mouseReleased(MouseEvent e)
554 // TODO Auto-generated method stub
558 public void stateChanged(ChangeEvent e)
562 valueField.setText(""
563 + ((integ) ? ("" + (int) slider.getValue())
564 : ("" + (float) (slider.getValue() / 1000f))));
570 public void updateControls(ParameterI parm)
573 boolean init = (choicebox == null && valueField == null);
578 choicebox = new JComboBox();
579 choicebox.addActionListener(this);
580 controlPanel.add(choicebox, BorderLayout.CENTER);
584 slider = new JSlider();
585 slider.addChangeListener(this);
586 valueField = new JTextField();
587 valueField.addActionListener(this);
588 valueField.addKeyListener(new KeyListener()
592 public void keyTyped(KeyEvent e)
597 public void keyReleased(KeyEvent e)
601 if (valueField.getText().trim().length() > 0)
603 actionPerformed(null);
609 public void keyPressed(KeyEvent e)
613 valueField.setPreferredSize(new Dimension(60, 25));
614 controlPanel.add(slider, BorderLayout.WEST);
615 controlPanel.add(valueField, BorderLayout.EAST);
626 List vals = parm.getPossibleValues();
627 for (Object val : vals)
629 choicebox.addItem(val);
633 if (parm.getValue() != null)
635 choicebox.setSelectedItem(parm.getValue());
640 valueField.setText(parm.getValue());
643 lastVal = updateSliderFromValueField();
647 public Object updateSliderFromValueField()
651 if (validator != null)
658 valueField.setText(valueField.getText().trim());
659 iVal = Integer.valueOf(valueField.getText());
660 if (validator.getMin() != null
661 && validator.getMin().intValue() > iVal)
663 iVal = validator.getMin().intValue();
664 // TODO: provide visual indication that hard limit was reached for
667 if (validator.getMax() != null
668 && validator.getMax().intValue() < iVal)
670 iVal = validator.getMax().intValue();
671 // TODO: provide visual indication that hard limit was reached for
674 } catch (Exception e)
678 // update value field to reflect any bound checking we performed.
679 valueField.setText("" + iVal);
680 if (validator.getMin() != null && validator.getMax() != null)
682 slider.getModel().setRangeProperties(iVal, 1,
683 validator.getMin().intValue(),
684 validator.getMax().intValue() + 1, true);
688 slider.setVisible(false);
698 valueField.setText(valueField.getText().trim());
699 fVal = Float.valueOf(valueField.getText());
700 if (validator.getMin() != null
701 && validator.getMin().floatValue() > fVal)
703 fVal = validator.getMin().floatValue();
704 // TODO: provide visual indication that hard limit was reached for
706 // update value field to reflect any bound checking we performed.
707 valueField.setText("" + fVal);
709 if (validator.getMax() != null
710 && validator.getMax().floatValue() < fVal)
712 fVal = validator.getMax().floatValue();
713 // TODO: provide visual indication that hard limit was reached for
715 // update value field to reflect any bound checking we performed.
716 valueField.setText("" + fVal);
718 } catch (Exception e)
722 if (validator.getMin() != null && validator.getMax() != null)
724 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
725 (int) (validator.getMin().floatValue() * 1000f),
726 1 + (int) (validator.getMax().floatValue() * 1000f),
731 slider.setVisible(false);
741 slider.setVisible(false);
743 { valueField.getText().trim() };
748 { (String) choicebox.getSelectedItem() };
755 public static final int PARAM_WIDTH = 340;
757 public static final int PARAM_HEIGHT = 150;
759 public static final int PARAM_CLOSEDHEIGHT = 80;
761 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
763 this(paramContainer, false);
766 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
769 poparent = paramContainer;
770 this.compact = compact;
773 public static void showUrlPopUp(JComponent invoker, final String finfo,
777 JPopupMenu mnu = new JPopupMenu();
778 JMenuItem mitem = new JMenuItem(MessageManager.formatMessage(
779 "label.view_params", new String[]
781 mitem.addActionListener(new ActionListener()
785 public void actionPerformed(ActionEvent e)
787 Desktop.showUrl(finfo);
792 mnu.show(invoker, x, y);
795 URL linkImageURL = getClass().getResource("/images/link.gif");
797 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
799 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
801 public Map<String, OptionBox> getOptSet()
806 public void setOptSet(Map<String, OptionBox> optSet)
808 this.optSet = optSet;
811 public Map<String, ParamBox> getParamSet()
816 public void setParamSet(Map<String, ParamBox> paramSet)
818 this.paramSet = paramSet;
821 OptsParametersContainerI poparent;
823 OptionBox addOption(OptionI opt)
825 OptionBox cb = optSet.get(opt.getName());
828 cb = new OptionBox(opt);
829 optSet.put(opt.getName(), cb);
830 // jobOptions.add(cb, FlowLayout.LEFT);
835 ParamBox addParameter(ParameterI arg)
837 ParamBox pb = paramSet.get(arg.getName());
840 pb = new ParamBox(poparent, arg);
841 paramSet.put(arg.getName(), pb);
842 // paramList.add(pb);
845 // take the defaults from the parameter
846 pb.updateControls(arg);
850 void selectOption(OptionI option, String string)
852 OptionBox cb = optSet.get(option.getName());
855 cb = addOption(option);
857 cb.enabled.setSelected(string != null); // initial state for an option.
860 if (option.getPossibleValues().contains(string))
862 cb.val.setSelectedItem(string);
866 throw new Error("Invalid value " + string + " for option " + option);
870 if (option.isRequired() && !cb.enabled.isSelected())
872 // TODO: indicate paramset is not valid.. option needs to be selected!
874 cb.setInitialValue();
877 void setParameter(ParameterI arg)
879 ParamBox pb = paramSet.get(arg.getName());
886 pb.updateControls(arg);
892 * recover options and parameters from GUI
896 public List<ArgumentI> getCurrentSettings()
898 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
899 for (OptionBox opts : getOptSet().values())
901 OptionI opt = opts.getOptionIfEnabled();
907 for (ParamBox parambox : getParamSet().values())
909 ParameterI parm = parambox.getParameter();