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(
116 .wrapTooltip(true, ((desc == null || desc.trim().length() == 0) ? MessageManager.getString("label.opt_and_params_further_details ")
118 + "<br><img src=\"" + linkImageURL + "\"/>")
120 enabled.addMouseListener(this);
124 if (desc != null && desc.trim().length() > 0)
126 enabled.setToolTipText(
127 JvSwingUtils.wrapTooltip(true, 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.
347 ttipText = (JvSwingUtils
348 .wrapTooltip(true, parm.getDescription()
349 + (finfo != null ? "<br><img src=\""
351 + "\"/>"+MessageManager.getString("label.opt_and_params_further_detail")
355 JvSwingUtils.mgAddtoLayout(this, ttipText,
356 new JLabel(parm.getName()), controlPanel, "");
357 updateControls(parm);
361 private void makeExpanderParam(ParameterI parm)
363 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
364 setBorder(new TitledBorder(parm.getName()));
366 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
367 showDesc.setText("+");
368 string.setFont(new Font("Verdana", Font.PLAIN, 11));
369 string.setBackground(getBackground());
371 string.setEditable(false);
372 descPanel.getViewport().setView(string);
374 descPanel.setVisible(false);
376 JPanel firstrow = new JPanel();
377 firstrow.setLayout(null);
378 controlPanel.setLayout(new BorderLayout());
379 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
380 PARAM_CLOSEDHEIGHT - 50));
381 firstrow.add(controlPanel);
382 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
383 PARAM_CLOSEDHEIGHT - 30));
385 final ParamBox me = this;
387 if (parm.getDescription() != null
388 && parm.getDescription().trim().length() > 0)
390 // Only create description boxes if there actually is a description.
393 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true, MessageManager.formatMessage("label.opt_and_params_show_brief_desc_image_link", new String[]{linkImageURL.toExternalForm()})));
394 showDesc.addMouseListener(this);
398 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true, MessageManager.getString("label.opt_and_params_show_brief_desc")));
400 showDesc.addActionListener(new ActionListener()
403 public void actionPerformed(ActionEvent e)
405 descisvisible = !descisvisible;
406 descPanel.setVisible(descisvisible);
407 descPanel.getVerticalScrollBar().setValue(0);
408 me.setPreferredSize(new Dimension(PARAM_WIDTH,
409 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
411 pmdialogbox.refreshParamLayout();
414 string.setWrapStyleWord(true);
415 string.setLineWrap(true);
416 string.setColumns(32);
417 string.setText(parm.getDescription());
418 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
419 firstrow.add(showDesc);
422 validator = parm.getValidValue();
424 if (validator != null)
426 integ = validator.getType() == ValueType.Integer;
430 if (parameter.getPossibleValues() != null)
435 updateControls(parm);
436 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
437 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
442 public void actionPerformed(ActionEvent e)
450 updateSliderFromValueField();
455 private void checkIfModified()
457 Object cstate = updateSliderFromValueField();
458 boolean notmod = false;
459 if (cstate.getClass() == lastVal.getClass())
461 if (cstate instanceof int[])
463 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
465 else if (cstate instanceof float[])
467 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
469 else if (cstate instanceof String[])
471 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
474 pmdialogbox.argSetModified(this, !notmod);
478 public int getBaseline(int width, int height)
484 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
485 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
487 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
489 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
492 public int getBoxHeight()
494 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
497 public ParameterI getParameter()
499 ParameterI prm = parameter.copy();
502 prm.setValue((String) choicebox.getSelectedItem());
506 prm.setValue(valueField.getText());
513 // reset the widget's initial value.
517 public void mouseClicked(MouseEvent e)
519 if (javax.swing.SwingUtilities.isRightMouseButton(e))
521 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
525 public void mouseEntered(MouseEvent e)
527 // TODO Auto-generated method stub
531 public void mouseExited(MouseEvent e)
533 // TODO Auto-generated method stub
537 public void mousePressed(MouseEvent e)
539 // TODO Auto-generated method stub
543 public void mouseReleased(MouseEvent e)
545 // TODO Auto-generated method stub
549 public void stateChanged(ChangeEvent e)
553 valueField.setText(""
554 + ((integ) ? ("" + (int) slider.getValue())
555 : ("" + (float) (slider.getValue() / 1000f))));
561 public void updateControls(ParameterI parm)
564 boolean init = (choicebox == null && valueField == null);
569 choicebox = new JComboBox();
570 choicebox.addActionListener(this);
571 controlPanel.add(choicebox, BorderLayout.CENTER);
575 slider = new JSlider();
576 slider.addChangeListener(this);
577 valueField = new JTextField();
578 valueField.addActionListener(this);
579 valueField.addKeyListener(new KeyListener()
583 public void keyTyped(KeyEvent e)
588 public void keyReleased(KeyEvent e)
592 if (valueField.getText().trim().length() > 0)
594 actionPerformed(null);
600 public void keyPressed(KeyEvent e)
604 valueField.setPreferredSize(new Dimension(60, 25));
605 controlPanel.add(slider, BorderLayout.WEST);
606 controlPanel.add(valueField, BorderLayout.EAST);
617 List vals = parm.getPossibleValues();
618 for (Object val : vals)
620 choicebox.addItem(val);
624 if (parm.getValue() != null)
626 choicebox.setSelectedItem(parm.getValue());
631 valueField.setText(parm.getValue());
634 lastVal = updateSliderFromValueField();
638 public Object updateSliderFromValueField()
642 if (validator != null)
649 valueField.setText(valueField.getText().trim());
650 iVal = Integer.valueOf(valueField.getText());
651 if (validator.getMin() != null
652 && validator.getMin().intValue() > iVal)
654 iVal = validator.getMin().intValue();
655 // TODO: provide visual indication that hard limit was reached for
658 if (validator.getMax() != null
659 && validator.getMax().intValue() < iVal)
661 iVal = validator.getMax().intValue();
662 // TODO: provide visual indication that hard limit was reached for
665 } catch (Exception e)
669 // update value field to reflect any bound checking we performed.
670 valueField.setText("" + iVal);
671 if (validator.getMin() != null && validator.getMax() != null)
673 slider.getModel().setRangeProperties(iVal, 1,
674 validator.getMin().intValue(),
675 validator.getMax().intValue() + 1, true);
679 slider.setVisible(false);
689 valueField.setText(valueField.getText().trim());
690 fVal = Float.valueOf(valueField.getText());
691 if (validator.getMin() != null
692 && validator.getMin().floatValue() > fVal)
694 fVal = validator.getMin().floatValue();
695 // TODO: provide visual indication that hard limit was reached for
697 // update value field to reflect any bound checking we performed.
698 valueField.setText("" + fVal);
700 if (validator.getMax() != null
701 && validator.getMax().floatValue() < fVal)
703 fVal = validator.getMax().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 } catch (Exception e)
713 if (validator.getMin() != null && validator.getMax() != null)
715 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
716 (int) (validator.getMin().floatValue() * 1000f),
717 1 + (int) (validator.getMax().floatValue() * 1000f),
722 slider.setVisible(false);
732 slider.setVisible(false);
734 { valueField.getText().trim() };
739 { (String) choicebox.getSelectedItem() };
746 public static final int PARAM_WIDTH = 340;
748 public static final int PARAM_HEIGHT = 150;
750 public static final int PARAM_CLOSEDHEIGHT = 80;
752 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
754 this(paramContainer, false);
757 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
760 poparent = paramContainer;
761 this.compact = compact;
764 public static void showUrlPopUp(JComponent invoker, final String finfo,
768 JPopupMenu mnu = new JPopupMenu();
769 JMenuItem mitem = new JMenuItem(MessageManager.formatMessage(
770 "label.view_params", new String[]
772 mitem.addActionListener(new ActionListener()
776 public void actionPerformed(ActionEvent e)
778 Desktop.showUrl(finfo);
783 mnu.show(invoker, x, y);
786 URL linkImageURL = getClass().getResource("/images/link.gif");
788 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
790 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
792 public Map<String, OptionBox> getOptSet()
797 public void setOptSet(Map<String, OptionBox> optSet)
799 this.optSet = optSet;
802 public Map<String, ParamBox> getParamSet()
807 public void setParamSet(Map<String, ParamBox> paramSet)
809 this.paramSet = paramSet;
812 OptsParametersContainerI poparent;
814 OptionBox addOption(OptionI opt)
816 OptionBox cb = optSet.get(opt.getName());
819 cb = new OptionBox(opt);
820 optSet.put(opt.getName(), cb);
821 // jobOptions.add(cb, FlowLayout.LEFT);
826 ParamBox addParameter(ParameterI arg)
828 ParamBox pb = paramSet.get(arg.getName());
831 pb = new ParamBox(poparent, arg);
832 paramSet.put(arg.getName(), pb);
833 // paramList.add(pb);
836 // take the defaults from the parameter
837 pb.updateControls(arg);
841 void selectOption(OptionI option, String string)
843 OptionBox cb = optSet.get(option.getName());
846 cb = addOption(option);
848 cb.enabled.setSelected(string != null); // initial state for an option.
851 if (option.getPossibleValues().contains(string))
853 cb.val.setSelectedItem(string);
857 throw new Error(MessageManager.formatMessage("error.invalid_value_for_option", new String[]{string,option.getName()}));
861 if (option.isRequired() && !cb.enabled.isSelected())
863 // TODO: indicate paramset is not valid.. option needs to be selected!
865 cb.setInitialValue();
868 void setParameter(ParameterI arg)
870 ParamBox pb = paramSet.get(arg.getName());
877 pb.updateControls(arg);
883 * recover options and parameters from GUI
887 public List<ArgumentI> getCurrentSettings()
889 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
890 for (OptionBox opts : getOptSet().values())
892 OptionI opt = opts.getOptionIfEnabled();
898 for (ParamBox parambox : getParamSet().values())
900 ParameterI parm = parambox.getParameter();