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 static jalview.ws.params.simple.LogarithmicParameter.LOGSLIDERSCALE;
25 import jalview.util.MessageManager;
26 import jalview.ws.params.ArgumentI;
27 import jalview.ws.params.OptionI;
28 import jalview.ws.params.ParameterI;
29 import jalview.ws.params.ValueConstrainI;
30 import jalview.ws.params.ValueConstrainI.ValueType;
31 import jalview.ws.params.simple.LogarithmicParameter;
33 import java.awt.BorderLayout;
34 import java.awt.Component;
35 import java.awt.Dimension;
37 import java.awt.GridLayout;
38 import java.awt.Rectangle;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.KeyAdapter;
42 import java.awt.event.KeyEvent;
43 import java.awt.event.MouseEvent;
44 import java.awt.event.MouseListener;
46 import java.util.ArrayList;
47 import java.util.LinkedHashMap;
48 import java.util.List;
51 import javax.swing.JButton;
52 import javax.swing.JCheckBox;
53 import javax.swing.JComboBox;
54 import javax.swing.JComponent;
55 import javax.swing.JLabel;
56 import javax.swing.JMenuItem;
57 import javax.swing.JPanel;
58 import javax.swing.JPopupMenu;
59 import javax.swing.JScrollPane;
60 import javax.swing.JSlider;
61 import javax.swing.JTextArea;
62 import javax.swing.JTextField;
63 import javax.swing.border.TitledBorder;
64 import javax.swing.event.ChangeEvent;
65 import javax.swing.event.ChangeListener;
67 import net.miginfocom.swing.MigLayout;
70 * GUI generator/manager for options and parameters. Originally abstracted from
71 * the WsJobParameters dialog box.
76 public class OptsAndParamsPage
78 public static final int PARAM_WIDTH = 340;
80 public static final int PARAM_HEIGHT = 150;
82 public static final int PARAM_CLOSEDHEIGHT = 80;
84 URL linkImageURL = getClass().getResource("/images/link.gif");
86 Map<String, OptionBox> optSet = new LinkedHashMap<>();
88 Map<String, ParamBox> paramSet = new LinkedHashMap<>();
91 * compact or verbose style parameters
93 boolean compact = false;
95 OptsParametersContainerI poparent;
98 * A class that models a panel rendering a single option (checkbox or choice
101 public class OptionBox extends JPanel
102 implements MouseListener, ActionListener
104 JCheckBox enabled = new JCheckBox();
108 boolean hasLink = false;
110 boolean initEnabled = false;
112 String initVal = null;
116 JLabel optlabel = new JLabel();
118 JComboBox<String> val = new JComboBox<>();
120 public OptionBox(OptionI opt)
123 setLayout(new BorderLayout());
124 enabled.setSelected(opt.isRequired()); // TODO: lock required options
125 // enabled.setEnabled(!opt.isRequired());
126 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
128 enabled.setText(opt.getName());
129 enabled.addActionListener(this);
130 finfo = option.getFurtherDetails();
131 String desc = opt.getDescription();
136 enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
137 ((desc == null || desc.trim().length() == 0)
138 ? MessageManager.getString(
139 "label.opt_and_params_further_details")
140 : desc) + "<br><img src=\"" + linkImageURL
142 enabled.addMouseListener(this);
146 if (desc != null && desc.trim().length() > 0)
148 enabled.setToolTipText(
149 JvSwingUtils.wrapTooltip(true, opt.getDescription()));
152 add(enabled, BorderLayout.NORTH);
153 for (String str : opt.getPossibleValues())
157 val.setSelectedItem(opt.getValue());
158 if (opt.getPossibleValues().size() > 1)
160 setLayout(new GridLayout(1, 2));
161 val.addActionListener(this);
162 add(val, BorderLayout.SOUTH);
164 // TODO: add actionListeners for popup (to open further info),
165 // and to update list of parameters if an option is enabled
166 // that takes a value. JBPNote: is this TODO still valid ?
171 public void actionPerformed(ActionEvent e)
173 if (e.getSource() != enabled)
175 enabled.setSelected(true);
180 private void checkIfModified()
182 boolean notmod = (initEnabled == enabled.isSelected());
183 if (enabled.isSelected())
187 notmod &= initVal.equals(val.getSelectedItem());
191 // compare against default service setting
192 notmod &= option.getValue() == null
193 || option.getValue().equals(val.getSelectedItem());
198 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
199 : val.getSelectedItem() != initVal;
201 poparent.argSetModified(this, !notmod);
204 public OptionI getOptionIfEnabled()
206 if (!enabled.isSelected())
210 OptionI opt = option.copy();
211 if (opt.getPossibleValues() != null
212 && opt.getPossibleValues().size() == 1)
214 // Hack to make sure the default value for an enabled option with only
215 // one value is actually returned
216 opt.setValue(opt.getPossibleValues().get(0));
218 if (val.getSelectedItem() != null)
220 opt.setValue((String) val.getSelectedItem());
224 if (option.getValue() != null)
226 opt.setValue(option.getValue());
233 public void mouseClicked(MouseEvent e)
235 if (e.isPopupTrigger()) // for Windows
237 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
242 public void mouseEntered(MouseEvent e)
244 // TODO Auto-generated method stub
249 public void mouseExited(MouseEvent e)
251 // TODO Auto-generated method stub
256 public void mousePressed(MouseEvent e)
258 if (e.isPopupTrigger()) // Mac
260 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
265 public void mouseReleased(MouseEvent e)
269 public void resetToDefault(boolean setDefaultParams)
271 enabled.setSelected(false);
272 if (option.isRequired()
273 || (setDefaultParams && option.getValue() != null))
275 // Apply default value
276 selectOption(option, option.getValue());
280 public void setInitialValue()
282 initEnabled = enabled.isSelected();
283 if (option.getPossibleValues() != null
284 && option.getPossibleValues().size() > 1)
286 initVal = (String) val.getSelectedItem();
290 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
297 * A class that models a panel rendering a single parameter
299 public class ParamBox extends JPanel
300 implements ChangeListener, ActionListener, MouseListener
302 private static final float SLIDERSCALE = 1000f;
304 boolean isLogarithmic;
306 boolean adjusting = false;
308 boolean choice = false;
310 JComboBox<String> choicebox;
312 JPanel controlPanel = new JPanel();
314 boolean descisvisible = false;
316 JScrollPane descPanel = new JScrollPane();
320 boolean integ = false;
324 ParameterI parameter;
326 final OptsParametersContainerI pmdialogbox;
328 JPanel settingPanel = new JPanel();
330 JButton showDesc = new JButton();
332 JSlider slider = null;
334 JTextArea string = new JTextArea();
336 ValueConstrainI validator = null;
338 JTextField valueField = null;
340 public ParamBox(final OptsParametersContainerI pmlayout,
343 pmdialogbox = pmlayout;
344 finfo = parm.getFurtherDetails();
345 validator = parm.getValidValue();
347 if (validator != null)
349 integ = validator.getType() == ValueType.Integer;
353 if (parameter.getPossibleValues() != null)
358 if (parm instanceof LogarithmicParameter)
360 isLogarithmic = true;
365 makeExpanderParam(parm);
369 makeCompactParam(parm);
374 private void makeCompactParam(ParameterI parm)
376 setLayout(new MigLayout("", "[][grow]"));
378 String ttipText = null;
380 controlPanel.setLayout(new BorderLayout());
382 if (parm.getDescription() != null
383 && parm.getDescription().trim().length() > 0)
385 // Only create description boxes if there actually is a description.
386 ttipText = (JvSwingUtils.wrapTooltip(true,
387 parm.getDescription() + (finfo != null ? "<br><img src=\""
388 + linkImageURL + "\"/>"
389 + MessageManager.getString(
390 "label.opt_and_params_further_details")
394 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
396 updateControls(parm);
400 private void makeExpanderParam(ParameterI parm)
402 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
403 setBorder(new TitledBorder(parm.getName()));
405 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
406 showDesc.setText("+");
407 string.setFont(new Font("Verdana", Font.PLAIN, 11));
408 string.setBackground(getBackground());
410 string.setEditable(false);
411 descPanel.getViewport().setView(string);
413 descPanel.setVisible(false);
415 JPanel firstrow = new JPanel();
416 firstrow.setLayout(null);
417 controlPanel.setLayout(new BorderLayout());
418 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
419 PARAM_CLOSEDHEIGHT - 50));
420 firstrow.add(controlPanel);
421 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
422 PARAM_CLOSEDHEIGHT - 30));
424 final ParamBox me = this;
426 if (parm.getDescription() != null
427 && parm.getDescription().trim().length() > 0)
429 // Only create description boxes if there actually is a description.
432 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true,
433 MessageManager.formatMessage(
434 "label.opt_and_params_show_brief_desc_image_link",
436 { linkImageURL.toExternalForm() })));
437 showDesc.addMouseListener(this);
441 showDesc.setToolTipText(
442 JvSwingUtils.wrapTooltip(true, MessageManager.getString(
443 "label.opt_and_params_show_brief_desc")));
445 showDesc.addActionListener(new ActionListener()
449 public void actionPerformed(ActionEvent e)
451 descisvisible = !descisvisible;
452 descPanel.setVisible(descisvisible);
453 descPanel.getVerticalScrollBar().setValue(0);
454 me.setPreferredSize(new Dimension(PARAM_WIDTH,
455 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
457 pmdialogbox.refreshParamLayout();
460 string.setWrapStyleWord(true);
461 string.setLineWrap(true);
462 string.setColumns(32);
463 string.setText(parm.getDescription());
464 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
465 firstrow.add(showDesc);
468 validator = parm.getValidValue();
470 if (validator != null)
472 integ = validator.getType() == ValueType.Integer;
476 if (parameter.getPossibleValues() != null)
481 updateControls(parm);
482 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
483 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
489 public void actionPerformed(ActionEvent e)
497 updateSliderFromValueField();
502 private void checkIfModified()
504 Object cstate = updateSliderFromValueField();
505 boolean notmod = false;
506 if (cstate.getClass() == lastVal.getClass())
508 if (cstate instanceof int[])
510 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
512 else if (cstate instanceof float[])
514 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
516 else if (cstate instanceof String[])
518 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
521 pmdialogbox.argSetModified(this, !notmod);
525 public int getBaseline(int width, int height)
531 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
532 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
534 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
536 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
539 public int getBoxHeight()
541 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
544 public ParameterI getParameter()
546 ParameterI prm = parameter.copy();
549 prm.setValue((String) choicebox.getSelectedItem());
553 prm.setValue(valueField.getText());
560 // reset the widget's initial value.
565 public void mouseClicked(MouseEvent e)
567 if (e.isPopupTrigger()) // for Windows
569 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
574 public void mouseEntered(MouseEvent e)
576 // TODO Auto-generated method stub
581 public void mouseExited(MouseEvent e)
583 // TODO Auto-generated method stub
588 public void mousePressed(MouseEvent e)
590 if (e.isPopupTrigger()) // for Mac
592 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
597 public void mouseReleased(MouseEvent e)
599 // TODO Auto-generated method stub
604 public void stateChanged(ChangeEvent e)
611 * set (int or float formatted) text field value
613 valueField.setText(integ ? String.valueOf(slider.getValue())
614 : String.valueOf(slider.getValue() / SLIDERSCALE));
618 double base = ((LogarithmicParameter) parameter).getBase();
619 double value = Math.pow(base, slider.getValue() / LOGSLIDERSCALE);
620 valueField.setText(formatDouble(value));
628 * Answers the value formatted as a string to 3 decimal places - in
629 * scientific notation if the value is less than 0.001
634 public String formatDouble(double value)
636 String format = value < 0.001 ? "%3.3e" : "%3.3f";
637 return String.format(format, value);
640 public void updateControls(ParameterI parm)
643 boolean init = (choicebox == null && valueField == null);
648 choicebox = new JComboBox<>();
649 choicebox.addActionListener(this);
650 controlPanel.add(choicebox, BorderLayout.CENTER);
654 slider = new JSlider();
655 slider.addChangeListener(this);
656 valueField = new JTextField();
657 valueField.addActionListener(this);
658 valueField.addKeyListener(new KeyAdapter()
661 public void keyReleased(KeyEvent e)
665 if (valueField.getText().trim().length() > 0)
667 actionPerformed(null);
672 valueField.setPreferredSize(new Dimension(80, 25));
673 controlPanel.add(slider, BorderLayout.WEST);
674 controlPanel.add(valueField, BorderLayout.EAST);
684 for (String val : parm.getPossibleValues())
686 choicebox.addItem(val);
690 if (parm.getValue() != null)
692 choicebox.setSelectedItem(parm.getValue());
697 if (parm instanceof LogarithmicParameter)
699 double base = ((LogarithmicParameter) parm).getBase();
700 // double value = Math.pow(base,
701 // Double.parseDouble(parm.getValue()) / LOGSLIDERSCALE);
702 double value = Double.parseDouble(parm.getValue());
703 valueField.setText(formatDouble(value));
707 valueField.setText(parm.getValue());
711 lastVal = updateSliderFromValueField();
715 public Object updateSliderFromValueField()
720 if (validator != null)
727 valueField.setText(valueField.getText().trim());
728 iVal = Integer.valueOf(valueField.getText());
729 if (validator.getMin() != null
730 && validator.getMin().intValue() > iVal)
732 iVal = validator.getMin().intValue();
733 // TODO: provide visual indication that hard limit was reached for
736 if (validator.getMax() != null
737 && validator.getMax().intValue() < iVal)
739 iVal = validator.getMax().intValue();
740 // TODO: provide visual indication that hard limit was reached for
743 } catch (Exception e)
745 System.err.println(e.getMessage());
747 // update value field to reflect any bound checking we performed.
748 valueField.setText("" + iVal);
749 if (validator.getMin() != null && validator.getMax() != null)
751 slider.getModel().setRangeProperties(iVal, 1,
752 validator.getMin().intValue(),
753 validator.getMax().intValue() + 1, true);
757 slider.setVisible(false);
759 return new int[] { iVal };
761 else if (isLogarithmic)
766 valueField.setText(valueField.getText().trim());
767 double eValue = Double.valueOf(valueField.getText());
769 dVal = Math.log(eValue) / Math
770 .log(((LogarithmicParameter) parameter).getBase())
773 if (validator.getMin() != null
774 && validator.getMin().doubleValue() > dVal)
776 dVal = validator.getMin().doubleValue();
777 // TODO: provide visual indication that hard limit was reached for
779 // update value field to reflect any bound checking we performed.
780 valueField.setText(formatDouble(eValue));
782 if (validator.getMax() != null
783 && validator.getMax().doubleValue() < dVal)
785 dVal = validator.getMax().doubleValue();
786 // TODO: provide visual indication that hard limit was reached for
788 // update value field to reflect any bound checking we performed.
789 valueField.setText(formatDouble(eValue));
791 } catch (Exception e)
795 if (validator.getMin() != null && validator.getMax() != null)
797 slider.getModel().setRangeProperties((int) (dVal), 1,
798 (int) (validator.getMin().doubleValue()),
799 1 + (int) (validator.getMax().doubleValue()),
804 slider.setVisible(false);
806 return new double[] { dVal };
813 valueField.setText(valueField.getText().trim());
814 fVal = Float.valueOf(valueField.getText());
815 if (validator.getMin() != null
816 && validator.getMin().floatValue() > fVal)
818 fVal = validator.getMin().floatValue();
819 // TODO: provide visual indication that hard limit was reached for
821 // update value field to reflect any bound checking we performed.
822 valueField.setText("" + fVal);
824 if (validator.getMax() != null
825 && validator.getMax().floatValue() < fVal)
827 fVal = validator.getMax().floatValue();
828 // TODO: provide visual indication that hard limit was reached for
830 // update value field to reflect any bound checking we performed.
831 valueField.setText("" + fVal);
833 } catch (Exception e)
837 if (validator.getMin() != null && validator.getMax() != null)
839 slider.getModel().setRangeProperties((int) (fVal * SLIDERSCALE), 1,
840 (int) (validator.getMin().floatValue() * SLIDERSCALE),
841 1 + (int) (validator.getMax().floatValue() * SLIDERSCALE),
846 slider.setVisible(false);
848 return new float[] { fVal };
855 slider.setVisible(false);
856 return new String[] { valueField.getText().trim() };
860 return new String[] { (String) choicebox.getSelectedItem() };
867 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
869 this(paramContainer, false);
872 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
875 poparent = paramContainer;
876 this.compact = compact;
879 public static void showUrlPopUp(JComponent invoker, final String finfo,
883 JPopupMenu mnu = new JPopupMenu();
884 JMenuItem mitem = new JMenuItem(
885 MessageManager.formatMessage("label.view_params", new String[]
887 mitem.addActionListener(new ActionListener()
891 public void actionPerformed(ActionEvent e)
893 Desktop.showUrl(finfo);
898 mnu.show(invoker, x, y);
901 public Map<String, OptionBox> getOptSet()
906 public void setOptSet(Map<String, OptionBox> optSet)
908 this.optSet = optSet;
911 public Map<String, ParamBox> getParamSet()
916 public void setParamSet(Map<String, ParamBox> paramSet)
918 this.paramSet = paramSet;
921 OptionBox addOption(OptionI opt)
923 OptionBox cb = optSet.get(opt.getName());
926 cb = new OptionBox(opt);
927 optSet.put(opt.getName(), cb);
928 // jobOptions.add(cb, FlowLayout.LEFT);
933 ParamBox addParameter(ParameterI arg)
935 ParamBox pb = paramSet.get(arg.getName());
938 pb = new ParamBox(poparent, arg);
939 paramSet.put(arg.getName(), pb);
940 // paramList.add(pb);
943 // take the defaults from the parameter
944 pb.updateControls(arg);
948 void selectOption(OptionI option, String string)
950 OptionBox cb = optSet.get(option.getName());
953 cb = addOption(option);
955 cb.enabled.setSelected(string != null); // initial state for an option.
958 if (option.getPossibleValues().contains(string))
960 cb.val.setSelectedItem(string);
964 throw new Error(String.format("Invalid value '%s' for option '%s'",
965 string, option.getName()));
968 if (option.isRequired() && !cb.enabled.isSelected())
970 // TODO: indicate paramset is not valid.. option needs to be selected!
972 cb.setInitialValue();
975 void setParameter(ParameterI arg)
977 ParamBox pb = paramSet.get(arg.getName());
984 pb.updateControls(arg);
990 * recover options and parameters from GUI
994 public List<ArgumentI> getCurrentSettings()
996 List<ArgumentI> argSet = new ArrayList<>();
997 for (OptionBox opts : getOptSet().values())
999 OptionI opt = opts.getOptionIfEnabled();
1005 for (ParamBox parambox : getParamSet().values())
1007 ParameterI parm = parambox.getParameter();