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.JTextArea;
57 import javax.swing.JTextField;
58 import javax.swing.border.TitledBorder;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
62 import net.miginfocom.swing.MigLayout;
65 * GUI generator/manager for options and parameters. Originally abstracted from
66 * the WsJobParameters dialog box.
71 public class OptsAndParamsPage
74 * compact or verbose style parameters
76 boolean compact = false;
78 public class OptionBox extends JPanel
79 implements MouseListener, ActionListener
81 JCheckBox enabled = new JCheckBox();
85 boolean hasLink = false;
87 boolean initEnabled = false;
89 String initVal = null;
93 JLabel optlabel = new JLabel();
95 JComboBox val = new JComboBox();
97 public OptionBox(OptionI opt)
100 setLayout(new BorderLayout());
101 enabled.setSelected(opt.isRequired()); // TODO: lock required options
102 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
104 enabled.setText(opt.getName());
105 enabled.addActionListener(this);
106 finfo = option.getFurtherDetails();
107 String desc = opt.getDescription();
112 enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
113 ((desc == null || desc.trim().length() == 0)
114 ? MessageManager.getString(
115 "label.opt_and_params_further_details")
116 : desc) + "<br><img src=\"" + linkImageURL
118 enabled.addMouseListener(this);
122 if (desc != null && desc.trim().length() > 0)
124 enabled.setToolTipText(
125 JvSwingUtils.wrapTooltip(true, opt.getDescription()));
128 add(enabled, BorderLayout.NORTH);
129 for (Object str : opt.getPossibleValues())
133 val.setSelectedItem(opt.getValue());
134 if (opt.getPossibleValues().size() > 1)
136 setLayout(new GridLayout(1, 2));
137 val.addActionListener(this);
138 add(val, BorderLayout.SOUTH);
140 // TODO: add actionListeners for popup (to open further info),
141 // and to update list of parameters if an option is enabled
142 // that takes a value. JBPNote: is this TODO still valid ?
147 public void actionPerformed(ActionEvent e)
149 if (e.getSource() != enabled)
151 enabled.setSelected(true);
156 private void checkIfModified()
158 boolean notmod = (initEnabled == enabled.isSelected());
159 if (enabled.isSelected())
163 notmod &= initVal.equals(val.getSelectedItem());
167 // compare against default service setting
168 notmod &= option.getValue() == null
169 || option.getValue().equals(val.getSelectedItem());
174 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
175 : val.getSelectedItem() != initVal;
177 poparent.argSetModified(this, !notmod);
180 public OptionI getOptionIfEnabled()
182 if (!enabled.isSelected())
186 OptionI opt = option.copy();
187 if (opt.getPossibleValues() != null
188 && opt.getPossibleValues().size() == 1)
190 // Hack to make sure the default value for an enabled option with only
191 // one value is actually returned
192 opt.setValue(opt.getPossibleValues().get(0));
194 if (val.getSelectedItem() != null)
196 opt.setValue((String) val.getSelectedItem());
200 if (option.getValue() != null)
202 opt.setValue(option.getValue());
209 public void mouseClicked(MouseEvent e)
211 if (e.isPopupTrigger()) // for Windows
213 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
218 public void mouseEntered(MouseEvent e)
220 // TODO Auto-generated method stub
225 public void mouseExited(MouseEvent e)
227 // TODO Auto-generated method stub
232 public void mousePressed(MouseEvent e)
234 if (e.isPopupTrigger()) // Mac
236 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
241 public void mouseReleased(MouseEvent e)
245 public void resetToDefault(boolean setDefaultParams)
247 enabled.setSelected(false);
248 if (option.isRequired()
249 || (setDefaultParams && option.getValue() != null))
251 // Apply default value
252 selectOption(option, option.getValue());
256 public void setInitialValue()
258 initEnabled = enabled.isSelected();
259 if (option.getPossibleValues() != null
260 && option.getPossibleValues().size() > 1)
262 initVal = (String) val.getSelectedItem();
266 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
272 public class ParamBox extends JPanel
273 implements ChangeListener, ActionListener, MouseListener
275 boolean adjusting = false;
277 boolean choice = false;
279 JComboBox<String> choicebox;
281 JPanel controlPanel = new JPanel();
283 boolean descisvisible = false;
285 JScrollPane descPanel = new JScrollPane();
289 boolean integ = false;
293 ParameterI parameter;
295 final OptsParametersContainerI pmdialogbox;
297 JPanel settingPanel = new JPanel();
299 JButton showDesc = new JButton();
301 Slider slider = null;
304 * true minimum corresponding to 0 (minimum) of the slider
308 JTextArea string = new JTextArea();
310 ValueConstrainI validator = null;
312 JTextField valueField = null;
314 public ParamBox(final OptsParametersContainerI pmlayout,
317 pmdialogbox = pmlayout;
318 finfo = parm.getFurtherDetails();
319 validator = parm.getValidValue();
321 if (validator != null)
323 integ = validator.getType() == ValueType.Integer;
327 if (parameter.getPossibleValues() != null)
335 makeExpanderParam(parm);
339 makeCompactParam(parm);
344 private void makeCompactParam(ParameterI parm)
346 setLayout(new MigLayout("", "[][grow]"));
348 String ttipText = null;
350 controlPanel.setLayout(new BorderLayout());
352 if (parm.getDescription() != null
353 && parm.getDescription().trim().length() > 0)
355 // Only create description boxes if there actually is a description.
356 ttipText = (JvSwingUtils.wrapTooltip(true,
357 parm.getDescription() + (finfo != null ? "<br><img src=\""
358 + linkImageURL + "\"/>"
359 + MessageManager.getString(
360 "label.opt_and_params_further_details")
364 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
366 updateControls(parm);
370 private void makeExpanderParam(final 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(true,
403 MessageManager.formatMessage(
404 "label.opt_and_params_show_brief_desc_image_link",
406 { linkImageURL.toExternalForm() })));
407 showDesc.addMouseListener(this);
411 showDesc.setToolTipText(
412 JvSwingUtils.wrapTooltip(true, MessageManager.getString(
413 "label.opt_and_params_show_brief_desc")));
415 showDesc.addActionListener(new ActionListener()
419 public void actionPerformed(ActionEvent e)
421 descisvisible = !descisvisible;
422 descPanel.setVisible(descisvisible);
423 descPanel.getVerticalScrollBar().setValue(0);
424 me.setPreferredSize(new Dimension(PARAM_WIDTH,
425 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
427 pmdialogbox.refreshParamLayout();
430 string.setWrapStyleWord(true);
431 string.setLineWrap(true);
432 string.setColumns(32);
433 string.setText(parm.getDescription());
434 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
435 firstrow.add(showDesc);
438 validator = parm.getValidValue();
440 if (validator != null)
442 integ = validator.getType() == ValueType.Integer;
446 if (parameter.getPossibleValues() != null)
451 updateControls(parm);
452 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
453 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
459 * Action on input in text field
462 public void actionPerformed(ActionEvent e)
470 updateSliderFromValueField();
475 private void checkIfModified()
477 Object cstate = getCurrentValue();
478 boolean modified = !cstate.equals(lastVal);
479 pmdialogbox.argSetModified(this, modified);
483 * Answers the current value of the parameter, as text
487 private Object getCurrentValue()
489 return choice ? choicebox.getSelectedItem() : valueField.getText();
493 public int getBaseline(int width, int height)
499 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
500 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
502 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
504 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
507 public int getBoxHeight()
509 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
512 public ParameterI getParameter()
514 ParameterI prm = parameter.copy();
517 prm.setValue((String) choicebox.getSelectedItem());
521 prm.setValue(valueField.getText());
528 // reset the widget's initial value.
533 public void mouseClicked(MouseEvent e)
535 if (e.isPopupTrigger()) // for Windows
537 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
542 public void mouseEntered(MouseEvent e)
544 // TODO Auto-generated method stub
549 public void mouseExited(MouseEvent e)
551 // TODO Auto-generated method stub
556 public void mousePressed(MouseEvent e)
558 if (e.isPopupTrigger()) // for Mac
560 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
565 public void mouseReleased(MouseEvent e)
567 // TODO Auto-generated method stub
572 * Action on change of slider value
575 public void stateChanged(ChangeEvent e)
579 float value = slider.getSliderValue();
581 integ ? Integer.toString((int) value)
582 : Float.toString(value));
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 valueField = new JTextField();
602 valueField.addActionListener(this);
603 valueField.addKeyListener(new KeyListener()
607 public void keyTyped(KeyEvent e)
612 public void keyReleased(KeyEvent e)
616 if (valueField.getText().trim().length() > 0)
618 actionPerformed(null);
624 public void keyPressed(KeyEvent e)
628 valueField.setPreferredSize(new Dimension(60, 25));
629 slider = makeSlider(parm.getValidValue());
630 slider.addChangeListener(this);
632 controlPanel.add(slider, BorderLayout.WEST);
633 controlPanel.add(valueField, BorderLayout.EAST);
643 List<String> vals = parm.getPossibleValues();
644 for (String val : vals)
646 choicebox.addItem(val);
650 if (parm.getValue() != null)
652 choicebox.setSelectedItem(parm.getValue());
657 valueField.setText(parm.getValue());
660 lastVal = getCurrentValue();
664 private Slider makeSlider(ValueConstrainI validValue)
666 if (validValue != null)
668 final Number minValue = validValue.getMin();
669 final Number maxValue = validValue.getMax();
670 if (minValue != null && maxValue != null)
672 return new Slider(minValue.floatValue(), maxValue.floatValue(),
673 minValue.floatValue());
678 * otherwise, a nominal slider which will not be visible
680 return new Slider(0, 100, 50);
683 public void updateSliderFromValueField()
685 if (validator != null)
687 final Number minValue = validator.getMin();
688 final Number maxValue = validator.getMax();
694 valueField.setText(valueField.getText().trim());
695 iVal = Integer.valueOf(valueField.getText());
697 && minValue.intValue() > iVal)
699 iVal = minValue.intValue();
700 // TODO: provide visual indication that hard limit was reached for
703 if (maxValue != null && maxValue.intValue() < iVal)
705 iVal = maxValue.intValue();
707 } catch (NumberFormatException e)
709 System.err.println(e.toString());
711 if (minValue != null || maxValue != null)
713 valueField.setText(String.valueOf(iVal));
714 slider.setSliderValue(iVal);
718 slider.setVisible(false);
726 valueField.setText(valueField.getText().trim());
727 fVal = Float.valueOf(valueField.getText());
729 && minValue.floatValue() > fVal)
731 fVal = minValue.floatValue();
732 // TODO: provide visual indication that hard limit was reached for
734 // update value field to reflect any bound checking we performed.
735 valueField.setText("" + fVal);
738 && maxValue.floatValue() < fVal)
740 fVal = maxValue.floatValue();
741 // TODO: provide visual indication that hard limit was reached for
743 // update value field to reflect any bound checking we performed.
744 valueField.setText("" + fVal);
746 } catch (NumberFormatException e)
748 System.err.println(e.toString());
750 if (minValue != null && maxValue != null)
752 slider.setSliderModel(minValue.floatValue(),
753 maxValue.floatValue(), fVal);
757 slider.setVisible(false);
765 slider.setVisible(false);
771 public static final int PARAM_WIDTH = 340;
773 public static final int PARAM_HEIGHT = 150;
775 public static final int PARAM_CLOSEDHEIGHT = 80;
777 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
779 this(paramContainer, false);
782 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
785 poparent = paramContainer;
786 this.compact = compact;
789 public static void showUrlPopUp(JComponent invoker, final String finfo,
793 JPopupMenu mnu = new JPopupMenu();
794 JMenuItem mitem = new JMenuItem(
795 MessageManager.formatMessage("label.view_params", new String[]
797 mitem.addActionListener(new ActionListener()
801 public void actionPerformed(ActionEvent e)
803 Desktop.showUrl(finfo);
808 mnu.show(invoker, x, y);
811 URL linkImageURL = getClass().getResource("/images/link.gif");
813 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<>();
815 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<>();
817 public Map<String, OptionBox> getOptSet()
822 public void setOptSet(Map<String, OptionBox> optSet)
824 this.optSet = optSet;
827 public Map<String, ParamBox> getParamSet()
832 public void setParamSet(Map<String, ParamBox> paramSet)
834 this.paramSet = paramSet;
837 OptsParametersContainerI poparent;
839 OptionBox addOption(OptionI opt)
841 OptionBox cb = optSet.get(opt.getName());
844 cb = new OptionBox(opt);
845 optSet.put(opt.getName(), cb);
846 // jobOptions.add(cb, FlowLayout.LEFT);
851 ParamBox addParameter(ParameterI arg)
853 ParamBox pb = paramSet.get(arg.getName());
856 pb = new ParamBox(poparent, arg);
857 paramSet.put(arg.getName(), pb);
858 // paramList.add(pb);
861 // take the defaults from the parameter
862 pb.updateControls(arg);
866 void selectOption(OptionI option, String string)
868 OptionBox cb = optSet.get(option.getName());
871 cb = addOption(option);
873 cb.enabled.setSelected(string != null); // initial state for an option.
876 if (option.getPossibleValues().contains(string))
878 cb.val.setSelectedItem(string);
882 throw new Error(MessageManager.formatMessage(
883 "error.invalid_value_for_option", new String[]
884 { string, option.getName() }));
888 if (option.isRequired() && !cb.enabled.isSelected())
890 // TODO: indicate paramset is not valid.. option needs to be selected!
892 cb.setInitialValue();
895 void setParameter(ParameterI arg)
897 ParamBox pb = paramSet.get(arg.getName());
904 pb.updateControls(arg);
910 * recover options and parameters from GUI
914 public List<ArgumentI> getCurrentSettings()
916 List<ArgumentI> argSet = new ArrayList<>();
917 for (OptionBox opts : getOptSet().values())
919 OptionI opt = opts.getOptionIfEnabled();
925 for (ParamBox parambox : getParamSet().values())
927 ParameterI parm = parambox.getParameter();