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;
29 import jalview.ws.params.simple.LogarithmicParameter;
31 import java.awt.BorderLayout;
32 import java.awt.Component;
33 import java.awt.Dimension;
35 import java.awt.GridLayout;
36 import java.awt.Rectangle;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.KeyEvent;
40 import java.awt.event.KeyListener;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
44 import java.util.ArrayList;
45 import java.util.List;
48 import javax.swing.JButton;
49 import javax.swing.JCheckBox;
50 import javax.swing.JComboBox;
51 import javax.swing.JComponent;
52 import javax.swing.JLabel;
53 import javax.swing.JMenuItem;
54 import javax.swing.JPanel;
55 import javax.swing.JPopupMenu;
56 import javax.swing.JScrollPane;
57 import javax.swing.JSlider;
58 import javax.swing.JTextArea;
59 import javax.swing.JTextField;
60 import javax.swing.border.TitledBorder;
61 import javax.swing.event.ChangeEvent;
62 import javax.swing.event.ChangeListener;
64 import net.miginfocom.swing.MigLayout;
67 * GUI generator/manager for options and parameters. Originally abstracted from
68 * the WsJobParameters dialog box.
73 public class OptsAndParamsPage
76 * compact or verbose style parameters
78 boolean compact = false;
80 public class OptionBox extends JPanel
81 implements MouseListener, ActionListener
83 JCheckBox enabled = new JCheckBox();
87 boolean hasLink = false;
89 boolean initEnabled = false;
91 String initVal = null;
95 JLabel optlabel = new JLabel();
97 JComboBox val = new JComboBox();
99 public OptionBox(OptionI opt)
102 setLayout(new BorderLayout());
103 enabled.setSelected(opt.isRequired()); // TODO: lock required options
104 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
106 enabled.setText(opt.getName());
107 enabled.addActionListener(this);
108 finfo = option.getFurtherDetails();
109 String desc = opt.getDescription();
114 enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
115 ((desc == null || desc.trim().length() == 0)
116 ? MessageManager.getString(
117 "label.opt_and_params_further_details")
118 : desc) + "<br><img src=\"" + linkImageURL
120 enabled.addMouseListener(this);
124 if (desc != null && desc.trim().length() > 0)
126 enabled.setToolTipText(
127 JvSwingUtils.wrapTooltip(true, 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
275 implements ChangeListener, ActionListener, MouseListener
278 boolean isLogarithmic;
280 boolean adjusting = false;
282 boolean choice = false;
286 JPanel controlPanel = new JPanel();
288 boolean descisvisible = false;
290 JScrollPane descPanel = new JScrollPane();
294 boolean integ = false;
298 ParameterI parameter;
300 final OptsParametersContainerI pmdialogbox;
302 JPanel settingPanel = new JPanel();
304 JButton showDesc = new JButton();
306 JSlider slider = null;
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)
332 if (parm instanceof LogarithmicParameter)
334 isLogarithmic = true;
339 makeExpanderParam(parm);
343 makeCompactParam(parm);
348 private void makeCompactParam(ParameterI parm)
350 setLayout(new MigLayout("", "[][grow]"));
352 String ttipText = null;
354 controlPanel.setLayout(new BorderLayout());
356 if (parm.getDescription() != null
357 && parm.getDescription().trim().length() > 0)
359 // Only create description boxes if there actually is a description.
360 ttipText = (JvSwingUtils.wrapTooltip(true,
361 parm.getDescription() + (finfo != null ? "<br><img src=\""
362 + linkImageURL + "\"/>"
363 + MessageManager.getString(
364 "label.opt_and_params_further_details")
368 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
370 updateControls(parm);
374 private void makeExpanderParam(ParameterI parm)
376 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
377 setBorder(new TitledBorder(parm.getName()));
379 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
380 showDesc.setText("+");
381 string.setFont(new Font("Verdana", Font.PLAIN, 11));
382 string.setBackground(getBackground());
384 string.setEditable(false);
385 descPanel.getViewport().setView(string);
387 descPanel.setVisible(false);
389 JPanel firstrow = new JPanel();
390 firstrow.setLayout(null);
391 controlPanel.setLayout(new BorderLayout());
392 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
393 PARAM_CLOSEDHEIGHT - 50));
394 firstrow.add(controlPanel);
395 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
396 PARAM_CLOSEDHEIGHT - 30));
398 final ParamBox me = this;
400 if (parm.getDescription() != null
401 && parm.getDescription().trim().length() > 0)
403 // Only create description boxes if there actually is a description.
406 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true,
407 MessageManager.formatMessage(
408 "label.opt_and_params_show_brief_desc_image_link",
410 { linkImageURL.toExternalForm() })));
411 showDesc.addMouseListener(this);
415 showDesc.setToolTipText(
416 JvSwingUtils.wrapTooltip(true, MessageManager.getString(
417 "label.opt_and_params_show_brief_desc")));
419 showDesc.addActionListener(new ActionListener()
423 public void actionPerformed(ActionEvent e)
425 descisvisible = !descisvisible;
426 descPanel.setVisible(descisvisible);
427 descPanel.getVerticalScrollBar().setValue(0);
428 me.setPreferredSize(new Dimension(PARAM_WIDTH,
429 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
431 pmdialogbox.refreshParamLayout();
434 string.setWrapStyleWord(true);
435 string.setLineWrap(true);
436 string.setColumns(32);
437 string.setText(parm.getDescription());
438 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
439 firstrow.add(showDesc);
442 validator = parm.getValidValue();
444 if (validator != null)
446 integ = validator.getType() == ValueType.Integer;
450 if (parameter.getPossibleValues() != null)
455 updateControls(parm);
456 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
457 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
463 public void actionPerformed(ActionEvent e)
471 updateSliderFromValueField();
476 private void checkIfModified()
478 Object cstate = updateSliderFromValueField();
479 boolean notmod = false;
480 if (cstate.getClass() == lastVal.getClass())
482 if (cstate instanceof int[])
484 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
486 else if (cstate instanceof float[])
488 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
490 else if (cstate instanceof String[])
492 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
495 pmdialogbox.argSetModified(this, !notmod);
499 public int getBaseline(int width, int height)
505 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
506 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
508 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
510 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
513 public int getBoxHeight()
515 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
518 public ParameterI getParameter()
520 ParameterI prm = parameter.copy();
523 prm.setValue((String) choicebox.getSelectedItem());
527 prm.setValue(valueField.getText());
534 // reset the widget's initial value.
539 public void mouseClicked(MouseEvent e)
541 if (e.isPopupTrigger()) // for Windows
543 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
548 public void mouseEntered(MouseEvent e)
550 // TODO Auto-generated method stub
555 public void mouseExited(MouseEvent e)
557 // TODO Auto-generated method stub
562 public void mousePressed(MouseEvent e)
564 if (e.isPopupTrigger()) // for Mac
566 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
571 public void mouseReleased(MouseEvent e)
573 // TODO Auto-generated method stub
578 public void stateChanged(ChangeEvent e)
584 valueField.setText("" + ((integ) ? ("" + slider.getValue())
585 : ("" + slider.getValue() / 1000f)));
589 Double base = ((LogarithmicParameter) parameter).getBase();
590 Double value = Math.pow(
593 valueField.setText(formatDouble(value));
600 public String formatDouble(Double value)
605 string = String.format("%3.3e", value);
609 string = value.toString();
614 public void updateControls(ParameterI parm)
617 boolean init = (choicebox == null && valueField == null);
622 choicebox = new JComboBox();
623 choicebox.addActionListener(this);
624 controlPanel.add(choicebox, BorderLayout.CENTER);
628 slider = new JSlider();
629 slider.addChangeListener(this);
630 valueField = new JTextField();
631 valueField.addActionListener(this);
632 valueField.addKeyListener(new KeyListener()
636 public void keyTyped(KeyEvent e)
641 public void keyReleased(KeyEvent e)
645 if (valueField.getText().trim().length() > 0)
647 actionPerformed(null);
653 public void keyPressed(KeyEvent e)
657 valueField.setPreferredSize(new Dimension(60, 25));
658 controlPanel.add(slider, BorderLayout.WEST);
659 controlPanel.add(valueField, BorderLayout.EAST);
670 List vals = parm.getPossibleValues();
671 for (Object val : vals)
673 choicebox.addItem(val);
677 if (parm.getValue() != null)
679 choicebox.setSelectedItem(parm.getValue());
684 valueField.setText(parm.getValue());
687 lastVal = updateSliderFromValueField();
691 public Object updateSliderFromValueField()
696 if (validator != null)
703 valueField.setText(valueField.getText().trim());
704 iVal = Integer.valueOf(valueField.getText());
705 if (validator.getMin() != null
706 && validator.getMin().intValue() > iVal)
708 iVal = validator.getMin().intValue();
709 // TODO: provide visual indication that hard limit was reached for
712 if (validator.getMax() != null
713 && validator.getMax().intValue() < iVal)
715 iVal = validator.getMax().intValue();
716 // TODO: provide visual indication that hard limit was reached for
719 } catch (Exception e)
723 // update value field to reflect any bound checking we performed.
724 valueField.setText("" + iVal);
725 if (validator.getMin() != null && validator.getMax() != null)
727 slider.getModel().setRangeProperties(iVal, 1,
728 validator.getMin().intValue(),
729 validator.getMax().intValue() + 1, true);
733 slider.setVisible(false);
735 return new int[] { iVal };
737 else if (isLogarithmic)
743 valueField.setText(valueField.getText().trim());
744 eValue = Double.valueOf(valueField.getText());
746 dVal = Math.log(eValue) / Math
747 .log(((LogarithmicParameter) parameter).getBase());
749 if (validator.getMin() != null
750 && validator.getMin().doubleValue() > dVal)
752 dVal = validator.getMin().doubleValue();
753 // TODO: provide visual indication that hard limit was reached for
755 // update value field to reflect any bound checking we performed.
756 valueField.setText("" + formatDouble(eValue));
758 if (validator.getMax() != null
759 && validator.getMax().doubleValue() < dVal)
761 dVal = validator.getMax().doubleValue();
762 // TODO: provide visual indication that hard limit was reached for
764 // update value field to reflect any bound checking we performed.
765 valueField.setText("" + formatDouble(eValue));
767 } catch (Exception e)
771 if (validator.getMin() != null && validator.getMax() != null)
773 slider.getModel().setRangeProperties((int) (dVal), 1,
774 (int) (validator.getMin().doubleValue()),
775 1 + (int) (validator.getMax().doubleValue()),
780 slider.setVisible(false);
782 return new double[] { dVal };
789 valueField.setText(valueField.getText().trim());
790 fVal = Float.valueOf(valueField.getText());
791 if (validator.getMin() != null
792 && validator.getMin().floatValue() > fVal)
794 fVal = validator.getMin().floatValue();
795 // TODO: provide visual indication that hard limit was reached for
797 // update value field to reflect any bound checking we performed.
798 valueField.setText("" + fVal);
800 if (validator.getMax() != null
801 && validator.getMax().floatValue() < fVal)
803 fVal = validator.getMax().floatValue();
804 // TODO: provide visual indication that hard limit was reached for
806 // update value field to reflect any bound checking we performed.
807 valueField.setText("" + fVal);
809 } catch (Exception e)
813 if (validator.getMin() != null && validator.getMax() != null)
815 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
816 (int) (validator.getMin().floatValue() * 1000f),
817 1 + (int) (validator.getMax().floatValue() * 1000f),
822 slider.setVisible(false);
824 return new float[] { fVal };
831 slider.setVisible(false);
832 return new String[] { valueField.getText().trim() };
836 return new String[] { (String) choicebox.getSelectedItem() };
843 public static final int PARAM_WIDTH = 340;
845 public static final int PARAM_HEIGHT = 150;
847 public static final int PARAM_CLOSEDHEIGHT = 80;
849 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
851 this(paramContainer, false);
854 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
857 poparent = paramContainer;
858 this.compact = compact;
861 public static void showUrlPopUp(JComponent invoker, final String finfo,
865 JPopupMenu mnu = new JPopupMenu();
866 JMenuItem mitem = new JMenuItem(
867 MessageManager.formatMessage("label.view_params", new String[]
869 mitem.addActionListener(new ActionListener()
873 public void actionPerformed(ActionEvent e)
875 Desktop.showUrl(finfo);
880 mnu.show(invoker, x, y);
883 URL linkImageURL = getClass().getResource("/images/link.gif");
885 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<>();
887 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<>();
889 public Map<String, OptionBox> getOptSet()
894 public void setOptSet(Map<String, OptionBox> optSet)
896 this.optSet = optSet;
899 public Map<String, ParamBox> getParamSet()
904 public void setParamSet(Map<String, ParamBox> paramSet)
906 this.paramSet = paramSet;
909 OptsParametersContainerI poparent;
911 OptionBox addOption(OptionI opt)
913 OptionBox cb = optSet.get(opt.getName());
916 cb = new OptionBox(opt);
917 optSet.put(opt.getName(), cb);
918 // jobOptions.add(cb, FlowLayout.LEFT);
923 ParamBox addParameter(ParameterI arg)
925 ParamBox pb = paramSet.get(arg.getName());
928 pb = new ParamBox(poparent, arg);
929 paramSet.put(arg.getName(), pb);
930 // paramList.add(pb);
933 // take the defaults from the parameter
934 pb.updateControls(arg);
938 void selectOption(OptionI option, String string)
940 OptionBox cb = optSet.get(option.getName());
943 cb = addOption(option);
945 cb.enabled.setSelected(string != null); // initial state for an option.
948 if (option.getPossibleValues().contains(string))
950 cb.val.setSelectedItem(string);
954 throw new Error(MessageManager.formatMessage(
955 "error.invalid_value_for_option", new String[]
956 { string, option.getName() }));
960 if (option.isRequired() && !cb.enabled.isSelected())
962 // TODO: indicate paramset is not valid.. option needs to be selected!
964 cb.setInitialValue();
967 void setParameter(ParameterI arg)
969 ParamBox pb = paramSet.get(arg.getName());
976 pb.updateControls(arg);
982 * recover options and parameters from GUI
986 public List<ArgumentI> getCurrentSettings()
988 List<ArgumentI> argSet = new ArrayList<>();
989 for (OptionBox opts : getOptSet().values())
991 OptionI opt = opts.getOptionIfEnabled();
997 for (ParamBox parambox : getParamSet().values())
999 ParameterI parm = parambox.getParameter();