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())
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 // TODO Auto-generated method stub
241 public void mouseReleased(MouseEvent e)
243 // TODO Auto-generated method stub
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_detail")
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())
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 // TODO Auto-generated method stub
568 public void mouseReleased(MouseEvent e)
570 // TODO Auto-generated method stub
575 public void stateChanged(ChangeEvent e)
579 valueField.setText(""
580 + ((integ) ? ("" + slider.getValue())
581 : ("" + slider.getValue() / 1000f)));
587 public void updateControls(ParameterI parm)
590 boolean init = (choicebox == null && valueField == null);
595 choicebox = new JComboBox();
596 choicebox.addActionListener(this);
597 controlPanel.add(choicebox, BorderLayout.CENTER);
601 slider = new JSlider();
602 slider.addChangeListener(this);
603 valueField = new JTextField();
604 valueField.addActionListener(this);
605 valueField.addKeyListener(new KeyListener()
609 public void keyTyped(KeyEvent e)
614 public void keyReleased(KeyEvent e)
618 if (valueField.getText().trim().length() > 0)
620 actionPerformed(null);
626 public void keyPressed(KeyEvent e)
630 valueField.setPreferredSize(new Dimension(60, 25));
631 controlPanel.add(slider, BorderLayout.WEST);
632 controlPanel.add(valueField, BorderLayout.EAST);
643 List vals = parm.getPossibleValues();
644 for (Object val : vals)
646 choicebox.addItem(val);
650 if (parm.getValue() != null)
652 choicebox.setSelectedItem(parm.getValue());
657 valueField.setText(parm.getValue());
660 lastVal = updateSliderFromValueField();
664 public Object updateSliderFromValueField()
668 if (validator != null)
675 valueField.setText(valueField.getText().trim());
676 iVal = Integer.valueOf(valueField.getText());
677 if (validator.getMin() != null
678 && validator.getMin().intValue() > iVal)
680 iVal = validator.getMin().intValue();
681 // TODO: provide visual indication that hard limit was reached for
684 if (validator.getMax() != null
685 && validator.getMax().intValue() < iVal)
687 iVal = validator.getMax().intValue();
688 // TODO: provide visual indication that hard limit was reached for
691 } catch (Exception e)
695 // update value field to reflect any bound checking we performed.
696 valueField.setText("" + iVal);
697 if (validator.getMin() != null && validator.getMax() != null)
699 slider.getModel().setRangeProperties(iVal, 1,
700 validator.getMin().intValue(),
701 validator.getMax().intValue() + 1, true);
705 slider.setVisible(false);
707 return new int[] { iVal };
714 valueField.setText(valueField.getText().trim());
715 fVal = Float.valueOf(valueField.getText());
716 if (validator.getMin() != null
717 && validator.getMin().floatValue() > fVal)
719 fVal = validator.getMin().floatValue();
720 // TODO: provide visual indication that hard limit was reached for
722 // update value field to reflect any bound checking we performed.
723 valueField.setText("" + fVal);
725 if (validator.getMax() != null
726 && validator.getMax().floatValue() < fVal)
728 fVal = validator.getMax().floatValue();
729 // TODO: provide visual indication that hard limit was reached for
731 // update value field to reflect any bound checking we performed.
732 valueField.setText("" + fVal);
734 } catch (Exception e)
738 if (validator.getMin() != null && validator.getMax() != null)
740 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
741 (int) (validator.getMin().floatValue() * 1000f),
742 1 + (int) (validator.getMax().floatValue() * 1000f),
747 slider.setVisible(false);
749 return new float[] { fVal };
756 slider.setVisible(false);
757 return new String[] { valueField.getText().trim() };
761 return new String[] { (String) choicebox.getSelectedItem() };
768 public static final int PARAM_WIDTH = 340;
770 public static final int PARAM_HEIGHT = 150;
772 public static final int PARAM_CLOSEDHEIGHT = 80;
774 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
776 this(paramContainer, false);
779 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
782 poparent = paramContainer;
783 this.compact = compact;
786 public static void showUrlPopUp(JComponent invoker, final String finfo,
790 JPopupMenu mnu = new JPopupMenu();
791 JMenuItem mitem = new JMenuItem(MessageManager.formatMessage(
792 "label.view_params", new String[] { finfo }));
793 mitem.addActionListener(new ActionListener()
797 public void actionPerformed(ActionEvent e)
799 Desktop.showUrl(finfo);
804 mnu.show(invoker, x, y);
807 URL linkImageURL = getClass().getResource("/images/link.gif");
809 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
811 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
813 public Map<String, OptionBox> getOptSet()
818 public void setOptSet(Map<String, OptionBox> optSet)
820 this.optSet = optSet;
823 public Map<String, ParamBox> getParamSet()
828 public void setParamSet(Map<String, ParamBox> paramSet)
830 this.paramSet = paramSet;
833 OptsParametersContainerI poparent;
835 OptionBox addOption(OptionI opt)
837 OptionBox cb = optSet.get(opt.getName());
840 cb = new OptionBox(opt);
841 optSet.put(opt.getName(), cb);
842 // jobOptions.add(cb, FlowLayout.LEFT);
847 ParamBox addParameter(ParameterI arg)
849 ParamBox pb = paramSet.get(arg.getName());
852 pb = new ParamBox(poparent, arg);
853 paramSet.put(arg.getName(), pb);
854 // paramList.add(pb);
857 // take the defaults from the parameter
858 pb.updateControls(arg);
862 void selectOption(OptionI option, String string)
864 OptionBox cb = optSet.get(option.getName());
867 cb = addOption(option);
869 cb.enabled.setSelected(string != null); // initial state for an option.
872 if (option.getPossibleValues().contains(string))
874 cb.val.setSelectedItem(string);
878 throw new Error(MessageManager.formatMessage(
879 "error.invalid_value_for_option", new String[] { string,
880 option.getName() }));
884 if (option.isRequired() && !cb.enabled.isSelected())
886 // TODO: indicate paramset is not valid.. option needs to be selected!
888 cb.setInitialValue();
891 void setParameter(ParameterI arg)
893 ParamBox pb = paramSet.get(arg.getName());
900 pb.updateControls(arg);
906 * recover options and parameters from GUI
910 public List<ArgumentI> getCurrentSettings()
912 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
913 for (OptionBox opts : getOptSet().values())
915 OptionI opt = opts.getOptionIfEnabled();
921 for (ParamBox parambox : getParamSet().values())
923 ParameterI parm = parambox.getParameter();