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(
592 slider.getValue() / 1000000f);
593 valueField.setText(formatDouble(value));
600 public String formatDouble(Double value)
602 String string = String.format("%3.3f", value);
605 string = String.format("%3.3e", value);
610 public void updateControls(ParameterI parm)
613 boolean init = (choicebox == null && valueField == null);
618 choicebox = new JComboBox();
619 choicebox.addActionListener(this);
620 controlPanel.add(choicebox, BorderLayout.CENTER);
624 slider = new JSlider();
625 slider.addChangeListener(this);
626 valueField = new JTextField();
627 valueField.addActionListener(this);
628 valueField.addKeyListener(new KeyListener()
632 public void keyTyped(KeyEvent e)
637 public void keyReleased(KeyEvent e)
641 if (valueField.getText().trim().length() > 0)
643 actionPerformed(null);
649 public void keyPressed(KeyEvent e)
653 valueField.setPreferredSize(new Dimension(60, 25));
654 controlPanel.add(slider, BorderLayout.WEST);
655 controlPanel.add(valueField, BorderLayout.EAST);
666 List vals = parm.getPossibleValues();
667 for (Object val : vals)
669 choicebox.addItem(val);
673 if (parm.getValue() != null)
675 choicebox.setSelectedItem(parm.getValue());
680 if (parm instanceof LogarithmicParameter)
682 Double base = ((LogarithmicParameter) parm).getBase();
683 Double value = Math.pow(base,
684 Double.parseDouble(parm.getValue()) / 1000000);
685 valueField.setText(formatDouble(value));
689 valueField.setText(parm.getValue());
693 lastVal = updateSliderFromValueField();
697 public Object updateSliderFromValueField()
702 if (validator != null)
709 valueField.setText(valueField.getText().trim());
710 iVal = Integer.valueOf(valueField.getText());
711 if (validator.getMin() != null
712 && validator.getMin().intValue() > iVal)
714 iVal = validator.getMin().intValue();
715 // TODO: provide visual indication that hard limit was reached for
718 if (validator.getMax() != null
719 && validator.getMax().intValue() < iVal)
721 iVal = validator.getMax().intValue();
722 // TODO: provide visual indication that hard limit was reached for
725 } catch (Exception e)
729 // update value field to reflect any bound checking we performed.
730 valueField.setText("" + iVal);
731 if (validator.getMin() != null && validator.getMax() != null)
733 slider.getModel().setRangeProperties(iVal, 1,
734 validator.getMin().intValue(),
735 validator.getMax().intValue() + 1, true);
739 slider.setVisible(false);
741 return new int[] { iVal };
743 else if (isLogarithmic)
749 valueField.setText(valueField.getText().trim());
750 eValue = Double.valueOf(valueField.getText());
752 dVal = Math.log(eValue) / Math
753 .log(((LogarithmicParameter) parameter).getBase())
756 if (validator.getMin() != null
757 && validator.getMin().doubleValue() > dVal)
759 dVal = validator.getMin().doubleValue();
760 // TODO: provide visual indication that hard limit was reached for
762 // update value field to reflect any bound checking we performed.
763 valueField.setText("" + formatDouble(eValue));
765 if (validator.getMax() != null
766 && validator.getMax().doubleValue() < dVal)
768 dVal = validator.getMax().doubleValue();
769 // TODO: provide visual indication that hard limit was reached for
771 // update value field to reflect any bound checking we performed.
772 valueField.setText("" + formatDouble(eValue));
774 } catch (Exception e)
778 if (validator.getMin() != null && validator.getMax() != null)
780 slider.getModel().setRangeProperties((int) (dVal), 1,
781 (int) (validator.getMin().doubleValue()),
782 1 + (int) (validator.getMax().doubleValue()),
787 slider.setVisible(false);
789 return new double[] { dVal };
796 valueField.setText(valueField.getText().trim());
797 fVal = Float.valueOf(valueField.getText());
798 if (validator.getMin() != null
799 && validator.getMin().floatValue() > fVal)
801 fVal = validator.getMin().floatValue();
802 // TODO: provide visual indication that hard limit was reached for
804 // update value field to reflect any bound checking we performed.
805 valueField.setText("" + fVal);
807 if (validator.getMax() != null
808 && validator.getMax().floatValue() < fVal)
810 fVal = validator.getMax().floatValue();
811 // TODO: provide visual indication that hard limit was reached for
813 // update value field to reflect any bound checking we performed.
814 valueField.setText("" + fVal);
816 } catch (Exception e)
820 if (validator.getMin() != null && validator.getMax() != null)
822 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
823 (int) (validator.getMin().floatValue() * 1000f),
824 1 + (int) (validator.getMax().floatValue() * 1000f),
829 slider.setVisible(false);
831 return new float[] { fVal };
838 slider.setVisible(false);
839 return new String[] { valueField.getText().trim() };
843 return new String[] { (String) choicebox.getSelectedItem() };
850 public static final int PARAM_WIDTH = 340;
852 public static final int PARAM_HEIGHT = 150;
854 public static final int PARAM_CLOSEDHEIGHT = 80;
856 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
858 this(paramContainer, false);
861 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
864 poparent = paramContainer;
865 this.compact = compact;
868 public static void showUrlPopUp(JComponent invoker, final String finfo,
872 JPopupMenu mnu = new JPopupMenu();
873 JMenuItem mitem = new JMenuItem(
874 MessageManager.formatMessage("label.view_params", new String[]
876 mitem.addActionListener(new ActionListener()
880 public void actionPerformed(ActionEvent e)
882 Desktop.showUrl(finfo);
887 mnu.show(invoker, x, y);
890 URL linkImageURL = getClass().getResource("/images/link.gif");
892 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<>();
894 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<>();
896 public Map<String, OptionBox> getOptSet()
901 public void setOptSet(Map<String, OptionBox> optSet)
903 this.optSet = optSet;
906 public Map<String, ParamBox> getParamSet()
911 public void setParamSet(Map<String, ParamBox> paramSet)
913 this.paramSet = paramSet;
916 OptsParametersContainerI poparent;
918 OptionBox addOption(OptionI opt)
920 OptionBox cb = optSet.get(opt.getName());
923 cb = new OptionBox(opt);
924 optSet.put(opt.getName(), cb);
925 // jobOptions.add(cb, FlowLayout.LEFT);
930 ParamBox addParameter(ParameterI arg)
932 ParamBox pb = paramSet.get(arg.getName());
935 pb = new ParamBox(poparent, arg);
936 paramSet.put(arg.getName(), pb);
937 // paramList.add(pb);
940 // take the defaults from the parameter
941 pb.updateControls(arg);
945 void selectOption(OptionI option, String string)
947 OptionBox cb = optSet.get(option.getName());
950 cb = addOption(option);
952 cb.enabled.setSelected(string != null); // initial state for an option.
955 if (option.getPossibleValues().contains(string))
957 cb.val.setSelectedItem(string);
961 throw new Error(MessageManager.formatMessage(
962 "error.invalid_value_for_option", new String[]
963 { string, option.getName() }));
967 if (option.isRequired() && !cb.enabled.isSelected())
969 // TODO: indicate paramset is not valid.. option needs to be selected!
971 cb.setInitialValue();
974 void setParameter(ParameterI arg)
976 ParamBox pb = paramSet.get(arg.getName());
983 pb.updateControls(arg);
989 * recover options and parameters from GUI
993 public List<ArgumentI> getCurrentSettings()
995 List<ArgumentI> argSet = new ArrayList<>();
996 for (OptionBox opts : getOptSet().values())
998 OptionI opt = opts.getOptionIfEnabled();
1004 for (ParamBox parambox : getParamSet().values())
1006 ParameterI parm = parambox.getParameter();