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.BooleanOption;
30 import jalview.ws.params.simple.LogarithmicParameter;
32 import java.awt.BorderLayout;
33 import java.awt.Component;
34 import java.awt.Dimension;
36 import java.awt.GridLayout;
37 import java.awt.Rectangle;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.KeyEvent;
41 import java.awt.event.KeyListener;
42 import java.awt.event.MouseEvent;
43 import java.awt.event.MouseListener;
45 import java.util.ArrayList;
46 import java.util.List;
49 import javax.swing.JButton;
50 import javax.swing.JCheckBox;
51 import javax.swing.JComboBox;
52 import javax.swing.JComponent;
53 import javax.swing.JLabel;
54 import javax.swing.JMenuItem;
55 import javax.swing.JPanel;
56 import javax.swing.JPopupMenu;
57 import javax.swing.JScrollPane;
58 import javax.swing.JSlider;
59 import javax.swing.JTextArea;
60 import javax.swing.JTextField;
61 import javax.swing.border.TitledBorder;
62 import javax.swing.event.ChangeEvent;
63 import javax.swing.event.ChangeListener;
65 import net.miginfocom.swing.MigLayout;
68 * GUI generator/manager for options and parameters. Originally abstracted from
69 * the WsJobParameters dialog box.
74 public class OptsAndParamsPage
77 * compact or verbose style parameters
79 boolean compact = false;
81 public class OptionBox extends JPanel
82 implements MouseListener, ActionListener
84 JCheckBox enabled = new JCheckBox();
88 boolean hasLink = false;
90 boolean initEnabled = false;
92 String initVal = null;
96 JLabel optlabel = new JLabel();
98 JComboBox val = new JComboBox();
100 public OptionBox(OptionI opt)
103 setLayout(new BorderLayout());
104 enabled.setSelected(opt.isRequired()); // TODO: lock required options
105 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
107 enabled.setText(opt.getName());
108 enabled.addActionListener(this);
109 finfo = option.getFurtherDetails();
110 String desc = opt.getDescription();
115 enabled.setToolTipText(JvSwingUtils.wrapTooltip(true,
116 ((desc == null || desc.trim().length() == 0)
117 ? MessageManager.getString(
118 "label.opt_and_params_further_details")
119 : desc) + "<br><img src=\"" + linkImageURL
121 enabled.addMouseListener(this);
125 if (desc != null && desc.trim().length() > 0)
127 enabled.setToolTipText(
128 JvSwingUtils.wrapTooltip(true, opt.getDescription()));
131 add(enabled, BorderLayout.NORTH);
132 for (Object str : opt.getPossibleValues())
136 val.setSelectedItem(opt.getValue());
137 if (opt.getPossibleValues().size() > 1)
139 setLayout(new GridLayout(1, 2));
140 val.addActionListener(this);
141 add(val, BorderLayout.SOUTH);
143 // TODO: add actionListeners for popup (to open further info),
144 // and to update list of parameters if an option is enabled
145 // that takes a value. JBPNote: is this TODO still valid ?
150 public void actionPerformed(ActionEvent e)
152 if (e.getSource() != enabled)
154 enabled.setSelected(true);
159 private void checkIfModified()
161 boolean notmod = (initEnabled == enabled.isSelected());
162 if (enabled.isSelected())
166 notmod &= initVal.equals(val.getSelectedItem());
170 // compare against default service setting
171 notmod &= option.getValue() == null
172 || option.getValue().equals(val.getSelectedItem());
177 notmod &= (initVal != null) ? initVal.equals(val.getSelectedItem())
178 : val.getSelectedItem() != initVal;
180 poparent.argSetModified(this, !notmod);
183 public OptionI getOptionIfEnabled()
185 if (!enabled.isSelected())
189 OptionI opt = option.copy();
190 if (opt.getPossibleValues() != null
191 && opt.getPossibleValues().size() == 1)
193 // Hack to make sure the default value for an enabled option with only
194 // one value is actually returned
195 opt.setValue(opt.getPossibleValues().get(0));
197 if (val.getSelectedItem() != null)
199 opt.setValue((String) val.getSelectedItem());
203 if (option.getValue() != null)
205 opt.setValue(option.getValue());
212 public void mouseClicked(MouseEvent e)
214 if (e.isPopupTrigger()) // for Windows
216 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
221 public void mouseEntered(MouseEvent e)
223 // TODO Auto-generated method stub
228 public void mouseExited(MouseEvent e)
230 // TODO Auto-generated method stub
235 public void mousePressed(MouseEvent e)
237 if (e.isPopupTrigger()) // Mac
239 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
244 public void mouseReleased(MouseEvent e)
248 public void resetToDefault(boolean setDefaultParams)
250 enabled.setSelected(false);
251 if (option.isRequired()
252 || (setDefaultParams && option.getValue() != null))
254 // Apply default value
255 selectOption(option, option.getValue());
259 public void setInitialValue()
261 initEnabled = enabled.isSelected();
262 if (option.getPossibleValues() != null
263 && option.getPossibleValues().size() > 1)
265 initVal = (String) val.getSelectedItem();
269 initVal = (initEnabled) ? (String) val.getSelectedItem() : null;
275 public class ParamBox extends JPanel
276 implements ChangeListener, ActionListener, MouseListener
279 boolean isLogarithmic;
281 boolean adjusting = false;
283 boolean choice = false;
287 JPanel controlPanel = new JPanel();
289 boolean descisvisible = false;
291 JScrollPane descPanel = new JScrollPane();
295 boolean integ = false;
299 ParameterI parameter;
301 final OptsParametersContainerI pmdialogbox;
303 JPanel settingPanel = new JPanel();
305 JButton showDesc = new JButton();
307 JSlider slider = null;
309 JTextArea string = new JTextArea();
311 ValueConstrainI validator = null;
313 JTextField valueField = null;
315 public ParamBox(final OptsParametersContainerI pmlayout,
318 pmdialogbox = pmlayout;
319 finfo = parm.getFurtherDetails();
320 validator = parm.getValidValue();
322 if (validator != null)
324 integ = validator.getType() == ValueType.Integer;
328 if (parameter.getPossibleValues() != null)
333 if (parm instanceof LogarithmicParameter)
335 isLogarithmic = true;
340 makeExpanderParam(parm);
344 makeCompactParam(parm);
349 private void makeCompactParam(ParameterI parm)
351 setLayout(new MigLayout("", "[][grow]"));
353 String ttipText = null;
355 controlPanel.setLayout(new BorderLayout());
357 if (parm.getDescription() != null
358 && parm.getDescription().trim().length() > 0)
360 // Only create description boxes if there actually is a description.
361 ttipText = (JvSwingUtils.wrapTooltip(true,
362 parm.getDescription() + (finfo != null ? "<br><img src=\""
363 + linkImageURL + "\"/>"
364 + MessageManager.getString(
365 "label.opt_and_params_further_details")
369 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),
371 updateControls(parm);
375 private void makeExpanderParam(ParameterI parm)
377 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
378 setBorder(new TitledBorder(parm.getName()));
380 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
381 showDesc.setText("+");
382 string.setFont(new Font("Verdana", Font.PLAIN, 11));
383 string.setBackground(getBackground());
385 string.setEditable(false);
386 descPanel.getViewport().setView(string);
388 descPanel.setVisible(false);
390 JPanel firstrow = new JPanel();
391 firstrow.setLayout(null);
392 controlPanel.setLayout(new BorderLayout());
393 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
394 PARAM_CLOSEDHEIGHT - 50));
395 firstrow.add(controlPanel);
396 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
397 PARAM_CLOSEDHEIGHT - 30));
399 final ParamBox me = this;
401 if (parm.getDescription() != null
402 && parm.getDescription().trim().length() > 0)
404 // Only create description boxes if there actually is a description.
407 showDesc.setToolTipText(JvSwingUtils.wrapTooltip(true,
408 MessageManager.formatMessage(
409 "label.opt_and_params_show_brief_desc_image_link",
411 { linkImageURL.toExternalForm() })));
412 showDesc.addMouseListener(this);
416 showDesc.setToolTipText(
417 JvSwingUtils.wrapTooltip(true, MessageManager.getString(
418 "label.opt_and_params_show_brief_desc")));
420 showDesc.addActionListener(new ActionListener()
424 public void actionPerformed(ActionEvent e)
426 descisvisible = !descisvisible;
427 descPanel.setVisible(descisvisible);
428 descPanel.getVerticalScrollBar().setValue(0);
429 me.setPreferredSize(new Dimension(PARAM_WIDTH,
430 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
432 pmdialogbox.refreshParamLayout();
435 string.setWrapStyleWord(true);
436 string.setLineWrap(true);
437 string.setColumns(32);
438 string.setText(parm.getDescription());
439 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
440 firstrow.add(showDesc);
443 validator = parm.getValidValue();
445 if (validator != null)
447 integ = validator.getType() == ValueType.Integer;
451 if (parameter.getPossibleValues() != null)
456 updateControls(parm);
457 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
458 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
464 public void actionPerformed(ActionEvent e)
472 updateSliderFromValueField();
477 private void checkIfModified()
479 Object cstate = updateSliderFromValueField();
480 boolean notmod = false;
481 if (cstate.getClass() == lastVal.getClass())
483 if (cstate instanceof int[])
485 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
487 else if (cstate instanceof float[])
489 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
491 else if (cstate instanceof String[])
493 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
496 pmdialogbox.argSetModified(this, !notmod);
500 public int getBaseline(int width, int height)
506 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
507 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
509 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
511 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
514 public int getBoxHeight()
516 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
519 public ParameterI getParameter()
521 ParameterI prm = parameter.copy();
524 prm.setValue((String) choicebox.getSelectedItem());
528 prm.setValue(valueField.getText());
535 // reset the widget's initial value.
540 public void mouseClicked(MouseEvent e)
542 if (e.isPopupTrigger()) // for Windows
544 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
549 public void mouseEntered(MouseEvent e)
551 // TODO Auto-generated method stub
556 public void mouseExited(MouseEvent e)
558 // TODO Auto-generated method stub
563 public void mousePressed(MouseEvent e)
565 if (e.isPopupTrigger()) // for Mac
567 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
572 public void mouseReleased(MouseEvent e)
574 // TODO Auto-generated method stub
579 public void stateChanged(ChangeEvent e)
585 valueField.setText("" + ((integ) ? ("" + slider.getValue())
586 : ("" + slider.getValue() / 1000f)));
590 Double base = ((LogarithmicParameter) parameter).getBase();
591 Double value = Math.pow(
593 slider.getValue() / 1000000f);
594 valueField.setText(formatDouble(value));
601 public String formatDouble(Double value)
603 String string = String.format("%3.3f", value);
606 string = String.format("%3.3e", value);
611 public void updateControls(ParameterI parm)
614 boolean init = (choicebox == null && valueField == null);
619 choicebox = new JComboBox();
620 choicebox.addActionListener(this);
621 controlPanel.add(choicebox, BorderLayout.CENTER);
625 slider = new JSlider();
626 slider.addChangeListener(this);
627 valueField = new JTextField();
628 valueField.addActionListener(this);
629 valueField.addKeyListener(new KeyListener()
633 public void keyTyped(KeyEvent e)
638 public void keyReleased(KeyEvent e)
642 if (valueField.getText().trim().length() > 0)
644 actionPerformed(null);
650 public void keyPressed(KeyEvent e)
654 valueField.setPreferredSize(new Dimension(60, 25));
655 controlPanel.add(slider, BorderLayout.WEST);
656 controlPanel.add(valueField, BorderLayout.EAST);
667 List vals = parm.getPossibleValues();
668 for (Object val : vals)
670 choicebox.addItem(val);
674 if (parm.getValue() != null)
676 choicebox.setSelectedItem(parm.getValue());
681 if (parm instanceof LogarithmicParameter)
683 Double base = ((LogarithmicParameter) parm).getBase();
684 Double value = Math.pow(base,
685 Double.parseDouble(parm.getValue()) / 1000000);
686 valueField.setText(formatDouble(value));
690 valueField.setText(parm.getValue());
694 lastVal = updateSliderFromValueField();
698 public Object updateSliderFromValueField()
703 if (validator != null)
710 valueField.setText(valueField.getText().trim());
711 iVal = Integer.valueOf(valueField.getText());
712 if (validator.getMin() != null
713 && validator.getMin().intValue() > iVal)
715 iVal = validator.getMin().intValue();
716 // TODO: provide visual indication that hard limit was reached for
719 if (validator.getMax() != null
720 && validator.getMax().intValue() < iVal)
722 iVal = validator.getMax().intValue();
723 // TODO: provide visual indication that hard limit was reached for
726 } catch (Exception e)
730 // update value field to reflect any bound checking we performed.
731 valueField.setText("" + iVal);
732 if (validator.getMin() != null && validator.getMax() != null)
734 slider.getModel().setRangeProperties(iVal, 1,
735 validator.getMin().intValue(),
736 validator.getMax().intValue() + 1, true);
740 slider.setVisible(false);
742 return new int[] { iVal };
744 else if (isLogarithmic)
750 valueField.setText(valueField.getText().trim());
751 eValue = Double.valueOf(valueField.getText());
753 dVal = Math.log(eValue) / Math
754 .log(((LogarithmicParameter) parameter).getBase())
757 if (validator.getMin() != null
758 && validator.getMin().doubleValue() > dVal)
760 dVal = validator.getMin().doubleValue();
761 // TODO: provide visual indication that hard limit was reached for
763 // update value field to reflect any bound checking we performed.
764 valueField.setText("" + formatDouble(eValue));
766 if (validator.getMax() != null
767 && validator.getMax().doubleValue() < dVal)
769 dVal = validator.getMax().doubleValue();
770 // TODO: provide visual indication that hard limit was reached for
772 // update value field to reflect any bound checking we performed.
773 valueField.setText("" + formatDouble(eValue));
775 } catch (Exception e)
779 if (validator.getMin() != null && validator.getMax() != null)
781 slider.getModel().setRangeProperties((int) (dVal), 1,
782 (int) (validator.getMin().doubleValue()),
783 1 + (int) (validator.getMax().doubleValue()),
788 slider.setVisible(false);
790 return new double[] { dVal };
797 valueField.setText(valueField.getText().trim());
798 fVal = Float.valueOf(valueField.getText());
799 if (validator.getMin() != null
800 && validator.getMin().floatValue() > fVal)
802 fVal = validator.getMin().floatValue();
803 // TODO: provide visual indication that hard limit was reached for
805 // update value field to reflect any bound checking we performed.
806 valueField.setText("" + fVal);
808 if (validator.getMax() != null
809 && validator.getMax().floatValue() < fVal)
811 fVal = validator.getMax().floatValue();
812 // TODO: provide visual indication that hard limit was reached for
814 // update value field to reflect any bound checking we performed.
815 valueField.setText("" + fVal);
817 } catch (Exception e)
821 if (validator.getMin() != null && validator.getMax() != null)
823 slider.getModel().setRangeProperties((int) (fVal * 1000f), 1,
824 (int) (validator.getMin().floatValue() * 1000f),
825 1 + (int) (validator.getMax().floatValue() * 1000f),
830 slider.setVisible(false);
832 return new float[] { fVal };
839 slider.setVisible(false);
840 return new String[] { valueField.getText().trim() };
844 return new String[] { (String) choicebox.getSelectedItem() };
851 public static final int PARAM_WIDTH = 340;
853 public static final int PARAM_HEIGHT = 150;
855 public static final int PARAM_CLOSEDHEIGHT = 80;
857 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
859 this(paramContainer, false);
862 public OptsAndParamsPage(OptsParametersContainerI paramContainer,
865 poparent = paramContainer;
866 this.compact = compact;
869 public static void showUrlPopUp(JComponent invoker, final String finfo,
873 JPopupMenu mnu = new JPopupMenu();
874 JMenuItem mitem = new JMenuItem(
875 MessageManager.formatMessage("label.view_params", new String[]
877 mitem.addActionListener(new ActionListener()
881 public void actionPerformed(ActionEvent e)
883 Desktop.showUrl(finfo);
888 mnu.show(invoker, x, y);
891 URL linkImageURL = getClass().getResource("/images/link.gif");
893 Map<String, OptionBox> optSet = new java.util.LinkedHashMap<>();
895 Map<String, ParamBox> paramSet = new java.util.LinkedHashMap<>();
897 public Map<String, OptionBox> getOptSet()
902 public void setOptSet(Map<String, OptionBox> optSet)
904 this.optSet = optSet;
907 public Map<String, ParamBox> getParamSet()
912 public void setParamSet(Map<String, ParamBox> paramSet)
914 this.paramSet = paramSet;
917 OptsParametersContainerI poparent;
919 OptionBox addOption(OptionI opt)
921 OptionBox cb = optSet.get(opt.getName());
924 cb = new OptionBox(opt);
925 optSet.put(opt.getName(), cb);
926 // jobOptions.add(cb, FlowLayout.LEFT);
931 ParamBox addParameter(ParameterI arg)
933 ParamBox pb = paramSet.get(arg.getName());
936 pb = new ParamBox(poparent, arg);
937 paramSet.put(arg.getName(), pb);
938 // paramList.add(pb);
941 // take the defaults from the parameter
942 pb.updateControls(arg);
946 void selectOption(OptionI option, String string)
948 OptionBox cb = optSet.get(option.getName());
951 cb = addOption(option);
953 cb.enabled.setSelected(string != null); // initial state for an option.
956 if (option.getPossibleValues().contains(string))
958 cb.val.setSelectedItem(string);
962 throw new Error(String.format("Invalid value '%s' for option '%s'",
963 string, option.getName()));
966 if (option.isRequired() && !cb.enabled.isSelected())
968 // TODO: indicate paramset is not valid.. option needs to be selected!
970 cb.setInitialValue();
973 void setParameter(ParameterI arg)
975 ParamBox pb = paramSet.get(arg.getName());
982 pb.updateControls(arg);
988 * recover options and parameters from GUI
992 public List<ArgumentI> getCurrentSettings()
994 List<ArgumentI> argSet = new ArrayList<>();
995 for (OptionBox opts : getOptSet().values())
997 OptionI opt = opts.getOptionIfEnabled();
1003 for (ParamBox parambox : getParamSet().values())
1005 ParameterI parm = parambox.getParameter();