2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3 * Copyright (C) 2015 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.List;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JComboBox;
50 import javax.swing.JComponent;
51 import javax.swing.JLabel;
52 import javax.swing.JMenuItem;
53 import javax.swing.JPanel;
54 import javax.swing.JPopupMenu;
55 import javax.swing.JScrollPane;
56 import javax.swing.JSlider;
57 import javax.swing.JTextArea;
58 import javax.swing.JTextField;
59 import javax.swing.border.TitledBorder;
60 import javax.swing.event.ChangeEvent;
61 import javax.swing.event.ChangeListener;
63 import net.miginfocom.swing.MigLayout;
66 * GUI generator/manager for options and parameters. Originally abstracted from
67 * the WsJobParameters dialog box.
72 public class OptsAndParamsPage
75 * compact or verbose style parameters
77 boolean compact = false;
79 public class OptionBox extends JPanel implements MouseListener,
82 JCheckBox enabled = new JCheckBox();
86 boolean hasLink = false;
88 boolean initEnabled = false;
90 String initVal = null;
94 JLabel optlabel = new JLabel();
96 JComboBox val = new JComboBox();
98 public OptionBox(OptionI opt)
101 setLayout(new BorderLayout());
102 enabled.setSelected(opt.isRequired()); // TODO: lock required options
103 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
105 enabled.setText(opt.getName());
106 enabled.addActionListener(this);
107 finfo = option.getFurtherDetails();
108 String desc = opt.getDescription();
113 enabled.setToolTipText(JvSwingUtils
116 ((desc == null || desc.trim().length() == 0) ? MessageManager
117 .getString("label.opt_and_params_further_details ")
119 + "<br><img src=\"" + linkImageURL + "\"/>"));
120 enabled.addMouseListener(this);
124 if (desc != null && desc.trim().length() > 0)
126 enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
127 opt.getDescription()));
130 add(enabled, BorderLayout.NORTH);
131 for (Object str : opt.getPossibleValues())
133 val.addItem((String) str);
135 val.setSelectedItem((String) opt.getValue());
136 if (opt.getPossibleValues().size() > 1)
138 setLayout(new GridLayout(1, 2));
139 val.addActionListener(this);
140 add(val, BorderLayout.SOUTH);
142 // TODO: add actionListeners for popup (to open further info),
143 // and to update list of parameters if an option is enabled
144 // that takes a value. JBPNote: is this TODO still valid ?
148 public void actionPerformed(ActionEvent e)
150 if (e.getSource() != enabled)
152 enabled.setSelected(true);
157 private void checkIfModified()
159 boolean notmod = (initEnabled == enabled.isSelected());
160 if (enabled.isSelected())
164 notmod &= initVal.equals(val.getSelectedItem());
168 // compare against default service setting
169 notmod &= option.getValue() == null
170 || option.getValue().equals(val.getSelectedItem());
175 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
176 : val.getSelectedItem() != initVal;
178 poparent.argSetModified(this, !notmod);
181 public OptionI getOptionIfEnabled()
183 if (!enabled.isSelected())
187 OptionI opt = option.copy();
188 if (opt.getPossibleValues() != null
189 && opt.getPossibleValues().size() == 1)
191 // Hack to make sure the default value for an enabled option with only
192 // one value is actually returned
193 opt.setValue(opt.getPossibleValues().get(0));
195 if (val.getSelectedItem() != null)
197 opt.setValue((String) val.getSelectedItem());
201 if (option.getValue() != null)
203 opt.setValue(option.getValue());
209 public void mouseClicked(MouseEvent e)
211 if (javax.swing.SwingUtilities.isRightMouseButton(e))
213 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
217 public void mouseEntered(MouseEvent e)
219 // TODO Auto-generated method stub
223 public void mouseExited(MouseEvent e)
225 // TODO Auto-generated method stub
229 public void mousePressed(MouseEvent e)
231 // TODO Auto-generated method stub
235 public void mouseReleased(MouseEvent e)
237 // TODO Auto-generated method stub
241 public void resetToDefault(boolean setDefaultParams)
243 enabled.setSelected(false);
244 if (option.isRequired()
245 || (setDefaultParams && option.getValue() != null))
247 // Apply default value
248 selectOption(option, option.getValue());
252 public void setInitialValue()
254 initEnabled = enabled.isSelected();
255 if (option.getPossibleValues() != null
256 && option.getPossibleValues().size() > 1)
258 initVal = (String) val.getSelectedItem();
262 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
268 public class ParamBox extends JPanel implements ChangeListener,
269 ActionListener, MouseListener
271 boolean adjusting = false;
273 boolean choice = false;
277 JPanel controlPanel = new JPanel();
279 boolean descisvisible = false;
281 JScrollPane descPanel = new JScrollPane();
285 boolean integ = false;
289 ParameterI parameter;
291 final OptsParametersContainerI pmdialogbox;
293 JPanel settingPanel = new JPanel();
295 JButton showDesc = new JButton();
297 JSlider slider = null;
299 JTextArea string = new JTextArea();
301 ValueConstrainI validator = null;
303 JTextField valueField = null;
305 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
307 pmdialogbox = pmlayout;
308 finfo = parm.getFurtherDetails();
309 validator = parm.getValidValue();
311 if (validator != null)
313 integ = validator.getType() == ValueType.Integer;
317 if (parameter.getPossibleValues() != null)
325 makeExpanderParam(parm);
329 makeCompactParam(parm);
334 private void makeCompactParam(ParameterI parm)
336 setLayout(new MigLayout("", "[][grow]"));
338 String ttipText = null;
340 controlPanel.setLayout(new BorderLayout());
342 if (parm.getDescription() != null
343 && parm.getDescription().trim().length() > 0)
345 // Only create description boxes if there actually is a description.
346 ttipText = (JvSwingUtils
349 parm.getDescription()
350 + (finfo != null ? "<br><img src=\""
354 .getString("label.opt_and_params_further_detail")
358 JvSwingUtils.mgAddtoLayout(this, ttipText,
359 new JLabel(parm.getName()), controlPanel, "");
360 updateControls(parm);
364 private void makeExpanderParam(ParameterI parm)
366 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
367 setBorder(new TitledBorder(parm.getName()));
369 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
370 showDesc.setText("+");
371 string.setFont(new Font("Verdana", Font.PLAIN, 11));
372 string.setBackground(getBackground());
374 string.setEditable(false);
375 descPanel.getViewport().setView(string);
377 descPanel.setVisible(false);
379 JPanel firstrow = new JPanel();
380 firstrow.setLayout(null);
381 controlPanel.setLayout(new BorderLayout());
382 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
383 PARAM_CLOSEDHEIGHT - 50));
384 firstrow.add(controlPanel);
385 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
386 PARAM_CLOSEDHEIGHT - 30));
388 final ParamBox me = this;
390 if (parm.getDescription() != null
391 && parm.getDescription().trim().length() > 0)
393 // Only create description boxes if there actually is a description.
396 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(
400 "label.opt_and_params_show_brief_desc_image_link",
401 new String[] { linkImageURL
402 .toExternalForm() })));
403 showDesc.addMouseListener(this);
407 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(
410 .getString("label.opt_and_params_show_brief_desc")));
412 showDesc.addActionListener(new ActionListener()
415 public void actionPerformed(ActionEvent e)
417 descisvisible = !descisvisible;
418 descPanel.setVisible(descisvisible);
419 descPanel.getVerticalScrollBar().setValue(0);
420 me.setPreferredSize(new Dimension(PARAM_WIDTH,
421 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
423 pmdialogbox.refreshParamLayout();
426 string.setWrapStyleWord(true);
427 string.setLineWrap(true);
428 string.setColumns(32);
429 string.setText(parm.getDescription());
430 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
431 firstrow.add(showDesc);
434 validator = parm.getValidValue();
436 if (validator != null)
438 integ = validator.getType() == ValueType.Integer;
442 if (parameter.getPossibleValues() != null)
447 updateControls(parm);
448 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
449 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
454 public void actionPerformed(ActionEvent e)
462 updateSliderFromValueField();
467 private void checkIfModified()
469 Object cstate = updateSliderFromValueField();
470 boolean notmod = false;
471 if (cstate.getClass() == lastVal.getClass())
473 if (cstate instanceof int[])
475 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
477 else if (cstate instanceof float[])
479 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
481 else if (cstate instanceof String[])
483 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
486 pmdialogbox.argSetModified(this, !notmod);
490 public int getBaseline(int width, int height)
496 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
497 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
499 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
501 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
504 public int getBoxHeight()
506 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
509 public ParameterI getParameter()
511 ParameterI prm = parameter.copy();
514 prm.setValue((String) choicebox.getSelectedItem());
518 prm.setValue(valueField.getText());
525 // reset the widget's initial value.
529 public void mouseClicked(MouseEvent e)
531 if (javax.swing.SwingUtilities.isRightMouseButton(e))
533 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
537 public void mouseEntered(MouseEvent e)
539 // TODO Auto-generated method stub
543 public void mouseExited(MouseEvent e)
545 // TODO Auto-generated method stub
549 public void mousePressed(MouseEvent e)
551 // TODO Auto-generated method stub
555 public void mouseReleased(MouseEvent e)
557 // TODO Auto-generated method stub
561 public void stateChanged(ChangeEvent e)
565 valueField.setText(""
566 + ((integ) ? ("" + (int) slider.getValue())
567 : ("" + (float) (slider.getValue() / 1000f))));
573 public void updateControls(ParameterI parm)
576 boolean init = (choicebox == null && valueField == null);
581 choicebox = new JComboBox();
582 choicebox.addActionListener(this);
583 controlPanel.add(choicebox, BorderLayout.CENTER);
587 slider = new JSlider();
588 slider.addChangeListener(this);
589 valueField = new JTextField();
590 valueField.addActionListener(this);
591 valueField.addKeyListener(new KeyListener()
595 public void keyTyped(KeyEvent e)
600 public void keyReleased(KeyEvent e)
604 if (valueField.getText().trim().length() > 0)
606 actionPerformed(null);
612 public void keyPressed(KeyEvent e)
616 valueField.setPreferredSize(new Dimension(60, 25));
617 controlPanel.add(slider, BorderLayout.WEST);
618 controlPanel.add(valueField, BorderLayout.EAST);
629 List vals = parm.getPossibleValues();
630 for (Object val : vals)
632 choicebox.addItem(val);
636 if (parm.getValue() != null)
638 choicebox.setSelectedItem(parm.getValue());
643 valueField.setText(parm.getValue());
646 lastVal = updateSliderFromValueField();
650 public Object updateSliderFromValueField()
654 if (validator != null)
661 valueField.setText(valueField.getText().trim());
662 iVal = Integer.valueOf(valueField.getText());
663 if (validator.getMin() != null
664 && validator.getMin().intValue() > iVal)
666 iVal = validator.getMin().intValue();
667 // TODO: provide visual indication that hard limit was reached for
670 if (validator.getMax() != null
671 && validator.getMax().intValue() < iVal)
673 iVal = validator.getMax().intValue();
674 // TODO: provide visual indication that hard limit was reached for
677 } catch (Exception e)
681 // update value field to reflect any bound checking we performed.
682 valueField.setText("" + iVal);
683 if (validator.getMin() != null && validator.getMax() != null)
685 slider.getModel().setRangeProperties(iVal, 1,
686 validator.getMin().intValue(),
687 validator.getMax().intValue() + 1, true);
691 slider.setVisible(false);
693 return new int[] { iVal };
700 valueField.setText(valueField.getText().trim());
701 fVal = Float.valueOf(valueField.getText());
702 if (validator.getMin() != null
703 && validator.getMin().floatValue() > fVal)
705 fVal = validator.getMin().floatValue();
706 // TODO: provide visual indication that hard limit was reached for
708 // update value field to reflect any bound checking we performed.
709 valueField.setText("" + fVal);
711 if (validator.getMax() != null
712 && validator.getMax().floatValue() < fVal)
714 fVal = validator.getMax().floatValue();
715 // TODO: provide visual indication that hard limit was reached for
717 // update value field to reflect any bound checking we performed.
718 valueField.setText("" + fVal);
720 } catch (Exception e)
724 if (validator.getMin() != null && validator.getMax() != null)
726 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
727 (int) (validator.getMin().floatValue() * 1000f),
728 1 + (int) (validator.getMax().floatValue() * 1000f),
733 slider.setVisible(false);
735 return new float[] { fVal };
742 slider.setVisible(false);
743 return new String[] { valueField.getText().trim() };
747 return new String[] { (String) choicebox.getSelectedItem() };
754 public static final int PARAM_WIDTH = 340;
756 public static final int PARAM_HEIGHT = 150;
758 public static final int PARAM_CLOSEDHEIGHT = 80;
760 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
762 this(paramContainer, false);
765 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
768 poparent = paramContainer;
769 this.compact = compact;
772 public static void showUrlPopUp(JComponent invoker, final String finfo,
776 JPopupMenu mnu = new JPopupMenu();
777 JMenuItem mitem = new JMenuItem(MessageManager.formatMessage(
778 "label.view_params", new String[] { finfo }));
779 mitem.addActionListener(new ActionListener()
783 public void actionPerformed(ActionEvent e)
785 Desktop.showUrl(finfo);
790 mnu.show(invoker, x, y);
793 URL linkImageURL = getClass().getResource("/images/link.gif");
795 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
797 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
799 public Map<String, OptionBox> getOptSet()
804 public void setOptSet(Map<String, OptionBox> optSet)
806 this.optSet = optSet;
809 public Map<String, ParamBox> getParamSet()
814 public void setParamSet(Map<String, ParamBox> paramSet)
816 this.paramSet = paramSet;
819 OptsParametersContainerI poparent;
821 OptionBox addOption(OptionI opt)
823 OptionBox cb = optSet.get(opt.getName());
826 cb = new OptionBox(opt);
827 optSet.put(opt.getName(), cb);
828 // jobOptions.add(cb, FlowLayout.LEFT);
833 ParamBox addParameter(ParameterI arg)
835 ParamBox pb = paramSet.get(arg.getName());
838 pb = new ParamBox(poparent, arg);
839 paramSet.put(arg.getName(), pb);
840 // paramList.add(pb);
843 // take the defaults from the parameter
844 pb.updateControls(arg);
848 void selectOption(OptionI option, String string)
850 OptionBox cb = optSet.get(option.getName());
853 cb = addOption(option);
855 cb.enabled.setSelected(string != null); // initial state for an option.
858 if (option.getPossibleValues().contains(string))
860 cb.val.setSelectedItem(string);
864 throw new Error(MessageManager.formatMessage(
865 "error.invalid_value_for_option", new String[] { string,
866 option.getName() }));
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();