2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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())
135 val.setSelectedItem(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 ?
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());
211 public void mouseClicked(MouseEvent e)
213 if (e.isPopupTrigger()) // for Windows
215 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
220 public void mouseEntered(MouseEvent e)
222 // TODO Auto-generated method stub
227 public void mouseExited(MouseEvent e)
229 // TODO Auto-generated method stub
234 public void mousePressed(MouseEvent e)
236 if (e.isPopupTrigger()) // Mac
238 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
243 public void mouseReleased(MouseEvent e)
247 public void resetToDefault(boolean setDefaultParams)
249 enabled.setSelected(false);
250 if (option.isRequired()
251 || (setDefaultParams && option.getValue() != null))
253 // Apply default value
254 selectOption(option, option.getValue());
258 public void setInitialValue()
260 initEnabled = enabled.isSelected();
261 if (option.getPossibleValues() != null
262 && option.getPossibleValues().size() > 1)
264 initVal = (String) val.getSelectedItem();
268 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
274 public class ParamBox extends JPanel implements ChangeListener,
275 ActionListener, MouseListener
277 boolean adjusting = false;
279 boolean choice = false;
283 JPanel controlPanel = new JPanel();
285 boolean descisvisible = false;
287 JScrollPane descPanel = new JScrollPane();
291 boolean integ = false;
295 ParameterI parameter;
297 final OptsParametersContainerI pmdialogbox;
299 JPanel settingPanel = new JPanel();
301 JButton showDesc = new JButton();
303 JSlider slider = null;
305 JTextArea string = new JTextArea();
307 ValueConstrainI validator = null;
309 JTextField valueField = null;
311 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
313 pmdialogbox = pmlayout;
314 finfo = parm.getFurtherDetails();
315 validator = parm.getValidValue();
317 if (validator != null)
319 integ = validator.getType() == ValueType.Integer;
323 if (parameter.getPossibleValues() != null)
331 makeExpanderParam(parm);
335 makeCompactParam(parm);
340 private void makeCompactParam(ParameterI parm)
342 setLayout(new MigLayout("", "[][grow]"));
344 String ttipText = null;
346 controlPanel.setLayout(new BorderLayout());
348 if (parm.getDescription() != null
349 && parm.getDescription().trim().length() > 0)
351 // Only create description boxes if there actually is a description.
352 ttipText = (JvSwingUtils
355 parm.getDescription()
356 + (finfo != null ? "<br><img src=\""
360 .getString("label.opt_and_params_further_details")
364 JvSwingUtils.mgAddtoLayout(this, ttipText,
365 new JLabel(parm.getName()), controlPanel, "");
366 updateControls(parm);
370 private void makeExpanderParam(ParameterI parm)
372 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
373 setBorder(new TitledBorder(parm.getName()));
375 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
376 showDesc.setText("+");
377 string.setFont(new Font("Verdana", Font.PLAIN, 11));
378 string.setBackground(getBackground());
380 string.setEditable(false);
381 descPanel.getViewport().setView(string);
383 descPanel.setVisible(false);
385 JPanel firstrow = new JPanel();
386 firstrow.setLayout(null);
387 controlPanel.setLayout(new BorderLayout());
388 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
389 PARAM_CLOSEDHEIGHT - 50));
390 firstrow.add(controlPanel);
391 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
392 PARAM_CLOSEDHEIGHT - 30));
394 final ParamBox me = this;
396 if (parm.getDescription() != null
397 && parm.getDescription().trim().length() > 0)
399 // Only create description boxes if there actually is a description.
402 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(
406 "label.opt_and_params_show_brief_desc_image_link",
407 new String[] { linkImageURL
408 .toExternalForm() })));
409 showDesc.addMouseListener(this);
413 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(
416 .getString("label.opt_and_params_show_brief_desc")));
418 showDesc.addActionListener(new ActionListener()
422 public void actionPerformed(ActionEvent e)
424 descisvisible = !descisvisible;
425 descPanel.setVisible(descisvisible);
426 descPanel.getVerticalScrollBar().setValue(0);
427 me.setPreferredSize(new Dimension(PARAM_WIDTH,
428 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
430 pmdialogbox.refreshParamLayout();
433 string.setWrapStyleWord(true);
434 string.setLineWrap(true);
435 string.setColumns(32);
436 string.setText(parm.getDescription());
437 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
438 firstrow.add(showDesc);
441 validator = parm.getValidValue();
443 if (validator != null)
445 integ = validator.getType() == ValueType.Integer;
449 if (parameter.getPossibleValues() != null)
454 updateControls(parm);
455 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
456 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
462 public void actionPerformed(ActionEvent e)
470 updateSliderFromValueField();
475 private void checkIfModified()
477 Object cstate = updateSliderFromValueField();
478 boolean notmod = false;
479 if (cstate.getClass() == lastVal.getClass())
481 if (cstate instanceof int[])
483 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
485 else if (cstate instanceof float[])
487 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
489 else if (cstate instanceof String[])
491 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
494 pmdialogbox.argSetModified(this, !notmod);
498 public int getBaseline(int width, int height)
504 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
505 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
507 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
509 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
512 public int getBoxHeight()
514 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
517 public ParameterI getParameter()
519 ParameterI prm = parameter.copy();
522 prm.setValue((String) choicebox.getSelectedItem());
526 prm.setValue(valueField.getText());
533 // reset the widget's initial value.
538 public void mouseClicked(MouseEvent e)
540 if (e.isPopupTrigger()) // for Windows
542 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
547 public void mouseEntered(MouseEvent e)
549 // TODO Auto-generated method stub
554 public void mouseExited(MouseEvent e)
556 // TODO Auto-generated method stub
561 public void mousePressed(MouseEvent e)
563 if (e.isPopupTrigger()) // for Mac
565 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
570 public void mouseReleased(MouseEvent e)
572 // TODO Auto-generated method stub
577 public void stateChanged(ChangeEvent e)
581 valueField.setText(""
582 + ((integ) ? ("" + slider.getValue())
583 : ("" + slider.getValue() / 1000f)));
589 public void updateControls(ParameterI parm)
592 boolean init = (choicebox == null && valueField == null);
597 choicebox = new JComboBox();
598 choicebox.addActionListener(this);
599 controlPanel.add(choicebox, BorderLayout.CENTER);
603 slider = new JSlider();
604 slider.addChangeListener(this);
605 valueField = new JTextField();
606 valueField.addActionListener(this);
607 valueField.addKeyListener(new KeyListener()
611 public void keyTyped(KeyEvent e)
616 public void keyReleased(KeyEvent e)
620 if (valueField.getText().trim().length() > 0)
622 actionPerformed(null);
628 public void keyPressed(KeyEvent e)
632 valueField.setPreferredSize(new Dimension(60, 25));
633 controlPanel.add(slider, BorderLayout.WEST);
634 controlPanel.add(valueField, BorderLayout.EAST);
645 List vals = parm.getPossibleValues();
646 for (Object val : vals)
648 choicebox.addItem(val);
652 if (parm.getValue() != null)
654 choicebox.setSelectedItem(parm.getValue());
659 valueField.setText(parm.getValue());
662 lastVal = updateSliderFromValueField();
666 public Object updateSliderFromValueField()
670 if (validator != null)
677 valueField.setText(valueField.getText().trim());
678 iVal = Integer.valueOf(valueField.getText());
679 if (validator.getMin() != null
680 && validator.getMin().intValue() > iVal)
682 iVal = validator.getMin().intValue();
683 // TODO: provide visual indication that hard limit was reached for
686 if (validator.getMax() != null
687 && validator.getMax().intValue() < iVal)
689 iVal = validator.getMax().intValue();
690 // TODO: provide visual indication that hard limit was reached for
693 } catch (Exception e)
697 // update value field to reflect any bound checking we performed.
698 valueField.setText("" + iVal);
699 if (validator.getMin() != null && validator.getMax() != null)
701 slider.getModel().setRangeProperties(iVal, 1,
702 validator.getMin().intValue(),
703 validator.getMax().intValue() + 1, true);
707 slider.setVisible(false);
709 return new int[] { iVal };
716 valueField.setText(valueField.getText().trim());
717 fVal = Float.valueOf(valueField.getText());
718 if (validator.getMin() != null
719 && validator.getMin().floatValue() > fVal)
721 fVal = validator.getMin().floatValue();
722 // TODO: provide visual indication that hard limit was reached for
724 // update value field to reflect any bound checking we performed.
725 valueField.setText("" + fVal);
727 if (validator.getMax() != null
728 && validator.getMax().floatValue() < fVal)
730 fVal = validator.getMax().floatValue();
731 // TODO: provide visual indication that hard limit was reached for
733 // update value field to reflect any bound checking we performed.
734 valueField.setText("" + fVal);
736 } catch (Exception e)
740 if (validator.getMin() != null && validator.getMax() != null)
742 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
743 (int) (validator.getMin().floatValue() * 1000f),
744 1 + (int) (validator.getMax().floatValue() * 1000f),
749 slider.setVisible(false);
751 return new float[] { fVal };
758 slider.setVisible(false);
759 return new String[] { valueField.getText().trim() };
763 return new String[] { (String) choicebox.getSelectedItem() };
770 public static final int PARAM_WIDTH = 340;
772 public static final int PARAM_HEIGHT = 150;
774 public static final int PARAM_CLOSEDHEIGHT = 80;
776 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
778 this(paramContainer, false);
781 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
784 poparent = paramContainer;
785 this.compact = compact;
788 public static void showUrlPopUp(JComponent invoker, final String finfo,
792 JPopupMenu mnu = new JPopupMenu();
793 JMenuItem mitem = new JMenuItem(MessageManager.formatMessage(
794 "label.view_params", new String[] { finfo }));
795 mitem.addActionListener(new ActionListener()
799 public void actionPerformed(ActionEvent e)
801 Desktop.showUrl(finfo);
806 mnu.show(invoker, x, y);
809 URL linkImageURL = getClass().getResource("/images/link.gif");
811 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
813 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
815 public Map<String, OptionBox> getOptSet()
820 public void setOptSet(Map<String, OptionBox> optSet)
822 this.optSet = optSet;
825 public Map<String, ParamBox> getParamSet()
830 public void setParamSet(Map<String, ParamBox> paramSet)
832 this.paramSet = paramSet;
835 OptsParametersContainerI poparent;
837 OptionBox addOption(OptionI opt)
839 OptionBox cb = optSet.get(opt.getName());
842 cb = new OptionBox(opt);
843 optSet.put(opt.getName(), cb);
844 // jobOptions.add(cb, FlowLayout.LEFT);
849 ParamBox addParameter(ParameterI arg)
851 ParamBox pb = paramSet.get(arg.getName());
854 pb = new ParamBox(poparent, arg);
855 paramSet.put(arg.getName(), pb);
856 // paramList.add(pb);
859 // take the defaults from the parameter
860 pb.updateControls(arg);
864 void selectOption(OptionI option, String string)
866 OptionBox cb = optSet.get(option.getName());
869 cb = addOption(option);
871 cb.enabled.setSelected(string != null); // initial state for an option.
874 if (option.getPossibleValues().contains(string))
876 cb.val.setSelectedItem(string);
880 throw new Error(MessageManager.formatMessage(
881 "error.invalid_value_for_option", new String[] { string,
882 option.getName() }));
886 if (option.isRequired() && !cb.enabled.isSelected())
888 // TODO: indicate paramset is not valid.. option needs to be selected!
890 cb.setInitialValue();
893 void setParameter(ParameterI arg)
895 ParamBox pb = paramSet.get(arg.getName());
902 pb.updateControls(arg);
908 * recover options and parameters from GUI
912 public List<ArgumentI> getCurrentSettings()
914 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
915 for (OptionBox opts : getOptSet().values())
917 OptionI opt = opts.getOptionIfEnabled();
923 for (ParamBox parambox : getParamSet().values())
925 ParameterI parm = parambox.getParameter();