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
80 implements MouseListener, ActionListener
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.wrapTooltip(true,
114 ((desc == null || desc.trim().length() == 0)
115 ? MessageManager.getString(
116 "label.opt_and_params_further_details")
117 : desc) + "<br><img src=\"" + linkImageURL
119 enabled.addMouseListener(this);
123 if (desc != null && desc.trim().length() > 0)
125 enabled.setToolTipText(
126 JvSwingUtils.wrapTooltip(true, opt.getDescription()));
129 add(enabled, BorderLayout.NORTH);
130 for (Object str : opt.getPossibleValues())
134 val.setSelectedItem(opt.getValue());
135 if (opt.getPossibleValues().size() > 1)
137 setLayout(new GridLayout(1, 2));
138 val.addActionListener(this);
139 add(val, BorderLayout.SOUTH);
141 // TODO: add actionListeners for popup (to open further info),
142 // and to update list of parameters if an option is enabled
143 // 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());
210 public void mouseClicked(MouseEvent e)
212 if (e.isPopupTrigger()) // for Windows
214 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
219 public void mouseEntered(MouseEvent e)
221 // TODO Auto-generated method stub
226 public void mouseExited(MouseEvent e)
228 // TODO Auto-generated method stub
233 public void mousePressed(MouseEvent e)
235 if (e.isPopupTrigger()) // Mac
237 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
242 public void mouseReleased(MouseEvent e)
246 public void resetToDefault(boolean setDefaultParams)
248 enabled.setSelected(false);
249 if (option.isRequired()
250 || (setDefaultParams && option.getValue() != null))
252 // Apply default value
253 selectOption(option, option.getValue());
257 public void setInitialValue()
259 initEnabled = enabled.isSelected();
260 if (option.getPossibleValues() != null
261 && option.getPossibleValues().size() > 1)
263 initVal = (String) val.getSelectedItem();
267 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
273 public class ParamBox extends JPanel
274 implements ChangeListener, ActionListener, MouseListener
276 boolean adjusting = false;
278 boolean choice = false;
282 JPanel controlPanel = new JPanel();
284 boolean descisvisible = false;
286 JScrollPane descPanel = new JScrollPane();
290 boolean integ = false;
294 ParameterI parameter;
296 final OptsParametersContainerI pmdialogbox;
298 JPanel settingPanel = new JPanel();
300 JButton showDesc = new JButton();
302 JSlider slider = null;
304 JTextArea string = new JTextArea();
306 ValueConstrainI validator = null;
308 JTextField valueField = null;
310 public ParamBox(final OptsParametersContainerI pmlayout,
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.wrapTooltip(true,
353 parm.getDescription() + (finfo != null ? "<br><img src=\""
354 + linkImageURL + "\"/>"
355 + MessageManager.getString(
356 "label.opt_and_params_further_details")
360 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
362 updateControls(parm);
366 private void makeExpanderParam(ParameterI parm)
368 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
369 setBorder(new TitledBorder(parm.getName()));
371 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
372 showDesc.setText("+");
373 string.setFont(new Font("Verdana", Font.PLAIN, 11));
374 string.setBackground(getBackground());
376 string.setEditable(false);
377 descPanel.getViewport().setView(string);
379 descPanel.setVisible(false);
381 JPanel firstrow = new JPanel();
382 firstrow.setLayout(null);
383 controlPanel.setLayout(new BorderLayout());
384 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
385 PARAM_CLOSEDHEIGHT - 50));
386 firstrow.add(controlPanel);
387 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
388 PARAM_CLOSEDHEIGHT - 30));
390 final ParamBox me = this;
392 if (parm.getDescription() != null
393 && parm.getDescription().trim().length() > 0)
395 // Only create description boxes if there actually is a description.
398 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true,
399 MessageManager.formatMessage(
400 "label.opt_and_params_show_brief_desc_image_link",
402 { linkImageURL.toExternalForm() })));
403 showDesc.addMouseListener(this);
407 showDesc.setToolTipText(
408 JvSwingUtils.wrapTooltip(true, MessageManager.getString(
409 "label.opt_and_params_show_brief_desc")));
411 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));
455 public void actionPerformed(ActionEvent e)
463 updateSliderFromValueField();
468 private void checkIfModified()
470 Object cstate = updateSliderFromValueField();
471 boolean notmod = false;
472 if (cstate.getClass() == lastVal.getClass())
474 if (cstate instanceof int[])
476 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
478 else if (cstate instanceof float[])
480 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
482 else if (cstate instanceof String[])
484 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
487 pmdialogbox.argSetModified(this, !notmod);
491 public int getBaseline(int width, int height)
497 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
498 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
500 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
502 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
505 public int getBoxHeight()
507 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
510 public ParameterI getParameter()
512 ParameterI prm = parameter.copy();
515 prm.setValue((String) choicebox.getSelectedItem());
519 prm.setValue(valueField.getText());
526 // reset the widget's initial value.
531 public void mouseClicked(MouseEvent e)
533 if (e.isPopupTrigger()) // for Windows
535 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
540 public void mouseEntered(MouseEvent e)
542 // TODO Auto-generated method stub
547 public void mouseExited(MouseEvent e)
549 // TODO Auto-generated method stub
554 public void mousePressed(MouseEvent e)
556 if (e.isPopupTrigger()) // for Mac
558 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
563 public void mouseReleased(MouseEvent e)
565 // TODO Auto-generated method stub
570 public void stateChanged(ChangeEvent e)
574 valueField.setText("" + ((integ) ? ("" + slider.getValue())
575 : ("" + slider.getValue() / 1000f)));
581 public void updateControls(ParameterI parm)
584 boolean init = (choicebox == null && valueField == null);
589 choicebox = new JComboBox();
590 choicebox.addActionListener(this);
591 controlPanel.add(choicebox, BorderLayout.CENTER);
595 slider = new JSlider();
596 slider.addChangeListener(this);
597 valueField = new JTextField();
598 valueField.addActionListener(this);
599 valueField.addKeyListener(new KeyListener()
603 public void keyTyped(KeyEvent e)
608 public void keyReleased(KeyEvent e)
612 if (valueField.getText().trim().length() > 0)
614 actionPerformed(null);
620 public void keyPressed(KeyEvent e)
624 valueField.setPreferredSize(new Dimension(60, 25));
625 controlPanel.add(slider, BorderLayout.WEST);
626 controlPanel.add(valueField, BorderLayout.EAST);
637 List vals = parm.getPossibleValues();
638 for (Object val : vals)
640 choicebox.addItem(val);
644 if (parm.getValue() != null)
646 choicebox.setSelectedItem(parm.getValue());
651 valueField.setText(parm.getValue());
654 lastVal = updateSliderFromValueField();
658 public Object updateSliderFromValueField()
662 if (validator != null)
669 valueField.setText(valueField.getText().trim());
670 iVal = Integer.valueOf(valueField.getText());
671 if (validator.getMin() != null
672 && validator.getMin().intValue() > iVal)
674 iVal = validator.getMin().intValue();
675 // TODO: provide visual indication that hard limit was reached for
678 if (validator.getMax() != null
679 && validator.getMax().intValue() < iVal)
681 iVal = validator.getMax().intValue();
682 // TODO: provide visual indication that hard limit was reached for
685 } catch (Exception e)
689 // update value field to reflect any bound checking we performed.
690 valueField.setText("" + iVal);
691 if (validator.getMin() != null && validator.getMax() != null)
693 slider.getModel().setRangeProperties(iVal, 1,
694 validator.getMin().intValue(),
695 validator.getMax().intValue() + 1, true);
699 slider.setVisible(false);
701 return new int[] { iVal };
708 valueField.setText(valueField.getText().trim());
709 fVal = Float.valueOf(valueField.getText());
710 if (validator.getMin() != null
711 && validator.getMin().floatValue() > fVal)
713 fVal = validator.getMin().floatValue();
714 // TODO: provide visual indication that hard limit was reached for
716 // update value field to reflect any bound checking we performed.
717 valueField.setText("" + fVal);
719 if (validator.getMax() != null
720 && validator.getMax().floatValue() < fVal)
722 fVal = validator.getMax().floatValue();
723 // TODO: provide visual indication that hard limit was reached for
725 // update value field to reflect any bound checking we performed.
726 valueField.setText("" + fVal);
728 } catch (Exception e)
732 if (validator.getMin() != null && validator.getMax() != null)
734 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
735 (int) (validator.getMin().floatValue() * 1000f),
736 1 + (int) (validator.getMax().floatValue() * 1000f),
741 slider.setVisible(false);
743 return new float[] { fVal };
750 slider.setVisible(false);
751 return new String[] { valueField.getText().trim() };
755 return new String[] { (String) choicebox.getSelectedItem() };
762 public static final int PARAM_WIDTH = 340;
764 public static final int PARAM_HEIGHT = 150;
766 public static final int PARAM_CLOSEDHEIGHT = 80;
768 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
770 this(paramContainer, false);
773 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
776 poparent = paramContainer;
777 this.compact = compact;
780 public static void showUrlPopUp(JComponent invoker, final String finfo,
784 JPopupMenu mnu = new JPopupMenu();
785 JMenuItem mitem = new JMenuItem(
786 MessageManager.formatMessage("label.view_params", new String[]
788 mitem.addActionListener(new ActionListener()
792 public void actionPerformed(ActionEvent e)
794 Desktop.showUrl(finfo);
799 mnu.show(invoker, x, y);
802 URL linkImageURL = getClass().getResource("/images/link.gif");
804 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<String, OptionBox>();
806 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<String, ParamBox>();
808 public Map<String, OptionBox> getOptSet()
813 public void setOptSet(Map<String, OptionBox> optSet)
815 this.optSet = optSet;
818 public Map<String, ParamBox> getParamSet()
823 public void setParamSet(Map<String, ParamBox> paramSet)
825 this.paramSet = paramSet;
828 OptsParametersContainerI poparent;
830 OptionBox addOption(OptionI opt)
832 OptionBox cb = optSet.get(opt.getName());
835 cb = new OptionBox(opt);
836 optSet.put(opt.getName(), cb);
837 // jobOptions.add(cb, FlowLayout.LEFT);
842 ParamBox addParameter(ParameterI arg)
844 ParamBox pb = paramSet.get(arg.getName());
847 pb = new ParamBox(poparent, arg);
848 paramSet.put(arg.getName(), pb);
849 // paramList.add(pb);
852 // take the defaults from the parameter
853 pb.updateControls(arg);
857 void selectOption(OptionI option, String string)
859 OptionBox cb = optSet.get(option.getName());
862 cb = addOption(option);
864 cb.enabled.setSelected(string != null); // initial state for an option.
867 if (option.getPossibleValues().contains(string))
869 cb.val.setSelectedItem(string);
873 throw new Error(MessageManager.formatMessage(
874 "error.invalid_value_for_option", new String[]
875 { string, option.getName() }));
879 if (option.isRequired() && !cb.enabled.isSelected())
881 // TODO: indicate paramset is not valid.. option needs to be selected!
883 cb.setInitialValue();
886 void setParameter(ParameterI arg)
888 ParamBox pb = paramSet.get(arg.getName());
895 pb.updateControls(arg);
901 * recover options and parameters from GUI
905 public List<ArgumentI> getCurrentSettings()
907 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
908 for (OptionBox opts : getOptSet().values())
910 OptionI opt = opts.getOptionIfEnabled();
916 for (ParamBox parambox : getParamSet().values())
918 ParameterI parm = parambox.getParameter();