1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 *******************************************************************************/
20 import jalview.ws.params.ArgumentI;
21 import jalview.ws.params.OptionI;
22 import jalview.ws.params.ParameterI;
23 import jalview.ws.params.ValueConstrainI;
24 import jalview.ws.params.ValueConstrainI.ValueType;
26 import java.awt.BorderLayout;
27 import java.awt.Component;
28 import java.awt.Dimension;
30 import java.awt.GridLayout;
31 import java.awt.Rectangle;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyEvent;
35 import java.awt.event.KeyListener;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
39 import java.util.ArrayList;
40 import java.util.Hashtable;
41 import java.util.List;
44 import javax.swing.JButton;
45 import javax.swing.JCheckBox;
46 import javax.swing.JComboBox;
47 import javax.swing.JComponent;
48 import javax.swing.JLabel;
49 import javax.swing.JMenuItem;
50 import javax.swing.JPanel;
51 import javax.swing.JPopupMenu;
52 import javax.swing.JScrollPane;
53 import javax.swing.JSlider;
54 import javax.swing.JTextArea;
55 import javax.swing.JTextField;
56 import javax.swing.border.TitledBorder;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
60 import net.miginfocom.swing.MigLayout;
63 * GUI generator/manager for options and parameters. Originally abstracted from
64 * the WsJobParameters dialog box.
69 public class OptsAndParamsPage
72 * compact or verbose style parameters
74 boolean compact = false;
76 public class OptionBox extends JPanel implements MouseListener,
79 JCheckBox enabled = new JCheckBox();
83 boolean hasLink = false;
85 boolean initEnabled = false;
87 String initVal = null;
91 JLabel optlabel = new JLabel();
93 JComboBox val = new JComboBox();
95 public OptionBox(OptionI opt)
98 setLayout(new BorderLayout());
99 enabled.setSelected(opt.isRequired()); // TODO: lock required options
100 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
102 enabled.setText(opt.getName());
103 enabled.addActionListener(this);
104 finfo = option.getFurtherDetails();
105 String desc = opt.getDescription();
110 enabled.setToolTipText("<html>"
112 .wrapTooltip(((desc == null) ? "see further details by right-clicking"
114 + "<br><img src=\"" + linkImageURL + "\"/>")
116 enabled.addMouseListener(this);
122 enabled.setToolTipText("<html>"
123 + JvSwingUtils.wrapTooltip(opt.getDescription())
127 add(enabled, BorderLayout.NORTH);
128 if (opt.getPossibleValues().size() > 1)
130 setLayout(new GridLayout(1, 2));
131 for (Object str : opt.getPossibleValues())
133 val.addItem((String) str);
135 val.setSelectedItem((String) opt.getValue());
136 val.addActionListener(this);
137 add(val, BorderLayout.SOUTH);
139 // TODO: add actionListeners for popup (to open further info),
140 // and to update list of parameters if an option is enabled
141 // that takes a value. JBPNote: is this TODO still valid ?
145 public void actionPerformed(ActionEvent e)
147 if (e.getSource() != enabled)
149 enabled.setSelected(true);
154 private void checkIfModified()
156 boolean notmod = (initEnabled == enabled.isSelected());
157 if (enabled.isSelected())
161 notmod &= initVal.equals(val.getSelectedItem());
165 // compare against default service setting
166 notmod &= option.getValue() == null
167 || option.getValue().equals(val.getSelectedItem());
172 notmod &= initVal == null;
174 poparent.argSetModified(this, !notmod);
177 public OptionI getOptionIfEnabled()
179 if (!enabled.isSelected())
183 OptionI opt = option.copy();
185 if (val.getSelectedItem() != null)
187 opt.setValue((String) val.getSelectedItem());
192 public void mouseClicked(MouseEvent e)
194 if (javax.swing.SwingUtilities.isRightMouseButton(e))
196 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
200 public void mouseEntered(MouseEvent e)
202 // TODO Auto-generated method stub
206 public void mouseExited(MouseEvent e)
208 // TODO Auto-generated method stub
212 public void mousePressed(MouseEvent e)
214 // TODO Auto-generated method stub
218 public void mouseReleased(MouseEvent e)
220 // TODO Auto-generated method stub
224 public void resetToDefault()
226 enabled.setSelected(false);
227 if (option.isRequired())
229 // Apply default value
230 selectOption(option, option.getValue());
234 public void setInitialValue()
236 initEnabled = enabled.isSelected();
237 if (option.getPossibleValues() != null
238 && option.getPossibleValues().size() > 1)
240 initVal = (String) val.getSelectedItem();
244 initVal = (initEnabled) ? option.getValue() : null;
250 public class ParamBox extends JPanel implements ChangeListener,
251 ActionListener, MouseListener
253 boolean adjusting = false;
255 boolean choice = false;
259 JPanel controlPanel = new JPanel();
261 boolean descisvisible = false;
263 JScrollPane descPanel = new JScrollPane();
267 boolean integ = false;
271 ParameterI parameter;
273 final OptsParametersContainerI pmdialogbox;
275 JPanel settingPanel = new JPanel();
277 JButton showDesc = new JButton();
279 JSlider slider = null;
281 JTextArea string = new JTextArea();
283 ValueConstrainI validator = null;
285 JTextField valueField = null;
287 public ParamBox(final OptsParametersContainerI pmlayout, ParameterI parm)
289 pmdialogbox = pmlayout;
290 finfo = parm.getFurtherDetails();
291 validator = parm.getValidValue();
293 if (validator != null)
295 integ = validator.getType() == ValueType.Integer;
299 if (parameter.getPossibleValues() != null)
307 makeExpanderParam(parm);
311 makeCompactParam(parm);
316 private void makeCompactParam(ParameterI parm)
318 setLayout(new MigLayout("","[][grow]"));
320 String ttipText=null;
322 controlPanel.setLayout(new BorderLayout());
324 if (parm.getDescription() != null
325 && parm.getDescription().trim().length() > 0)
327 // Only create description boxes if there actually is a description.
330 .wrapTooltip(parm.getDescription()+(finfo!=null ?"<br><img src=\""
332 + "\"/> Right click for further information.":""))
336 JvSwingUtils.mgAddtoLayout(this, ttipText, new JLabel(parm.getName()),controlPanel, "");
337 updateControls(parm);
341 private void makeExpanderParam(ParameterI parm)
343 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
344 setBorder(new TitledBorder(parm.getName()));
346 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
347 showDesc.setText("+");
348 string.setFont(new Font("Verdana", Font.PLAIN, 11));
349 string.setBackground(getBackground());
351 string.setEditable(false);
352 descPanel.getViewport().setView(string);
354 descPanel.setVisible(false);
356 JPanel firstrow = new JPanel();
357 firstrow.setLayout(null);
358 controlPanel.setLayout(new BorderLayout());
359 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
360 PARAM_CLOSEDHEIGHT - 50));
361 firstrow.add(controlPanel);
362 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
363 PARAM_CLOSEDHEIGHT - 30));
365 final ParamBox me = this;
367 if (parm.getDescription() != null
368 && parm.getDescription().trim().length() > 0)
370 // Only create description boxes if there actually is a description.
373 showDesc.setToolTipText("<html>"
375 .wrapTooltip("Click to show brief description<br><img src=\""
377 + "\"/> Right click for further information.")
379 showDesc.addMouseListener(this);
383 showDesc.setToolTipText("<html>"
385 .wrapTooltip("Click to show brief description.")
388 showDesc.addActionListener(new ActionListener()
391 public void actionPerformed(ActionEvent e)
393 descisvisible = !descisvisible;
394 descPanel.setVisible(descisvisible);
395 descPanel.getVerticalScrollBar().setValue(0);
396 me.setPreferredSize(new Dimension(PARAM_WIDTH,
397 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
399 pmdialogbox.refreshParamLayout();
402 string.setWrapStyleWord(true);
403 string.setLineWrap(true);
404 string.setColumns(32);
405 string.setText(parm.getDescription());
406 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
407 firstrow.add(showDesc);
410 validator = parm.getValidValue();
412 if (validator != null)
414 integ = validator.getType() == ValueType.Integer;
418 if (parameter.getPossibleValues() != null)
423 updateControls(parm);
424 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
425 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
430 public void actionPerformed(ActionEvent e)
438 updateSliderFromValueField();
443 private void checkIfModified()
445 Object cstate = updateSliderFromValueField();
446 boolean notmod = false;
447 if (cstate.getClass() == lastVal.getClass())
449 if (cstate instanceof int[])
451 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
453 else if (cstate instanceof float[])
455 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
457 else if (cstate instanceof String[])
459 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
462 pmdialogbox.argSetModified(this, !notmod);
466 public int getBaseline(int width, int height)
472 // http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
473 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
475 public Component.BaselineResizeBehavior getBaselineResizeBehavior()
477 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
480 public int getBoxHeight()
482 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
485 public ParameterI getParameter()
487 ParameterI prm = parameter.copy();
490 prm.setValue((String) choicebox.getSelectedItem());
494 prm.setValue(valueField.getText());
501 // reset the widget's initial value.
505 public void mouseClicked(MouseEvent e)
507 if (javax.swing.SwingUtilities.isRightMouseButton(e))
509 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
513 public void mouseEntered(MouseEvent e)
515 // TODO Auto-generated method stub
519 public void mouseExited(MouseEvent e)
521 // TODO Auto-generated method stub
525 public void mousePressed(MouseEvent e)
527 // TODO Auto-generated method stub
531 public void mouseReleased(MouseEvent e)
533 // TODO Auto-generated method stub
537 public void stateChanged(ChangeEvent e)
541 valueField.setText(""
542 + ((integ) ? ("" + (int) slider.getValue())
543 : ("" + (float) (slider.getValue() / 1000f))));
549 public void updateControls(ParameterI parm)
552 boolean init = (choicebox == null && valueField == null);
557 choicebox = new JComboBox();
558 choicebox.addActionListener(this);
559 controlPanel.add(choicebox, BorderLayout.CENTER);
563 slider = new JSlider();
564 slider.addChangeListener(this);
565 valueField = new JTextField();
566 valueField.addActionListener(this);
567 valueField.addKeyListener(new KeyListener()
571 public void keyTyped(KeyEvent e)
576 public void keyReleased(KeyEvent e)
578 if (valueField.getText().trim().length()>0)
580 actionPerformed(null);
585 public void keyPressed(KeyEvent e)
589 valueField.setPreferredSize(new Dimension(60, 25));
590 controlPanel.add(slider, BorderLayout.WEST);
591 controlPanel.add(valueField, BorderLayout.EAST);
602 List vals = parm.getPossibleValues();
603 for (Object val : vals)
605 choicebox.addItem(val);
609 if (parm.getValue() != null)
611 choicebox.setSelectedItem(parm.getValue());
616 valueField.setText(parm.getValue());
619 lastVal = updateSliderFromValueField();
623 public Object updateSliderFromValueField()
627 if (validator != null)
634 valueField.setText(valueField.getText().trim());
635 iVal = Integer.valueOf(valueField.getText());
636 if (validator.getMin() != null
637 && validator.getMin().intValue() > iVal)
639 iVal = validator.getMin().intValue();
640 // TODO: provide visual indication that hard limit was reached for
643 if (validator.getMax() != null
644 && validator.getMax().intValue() < iVal)
646 iVal = validator.getMax().intValue();
647 // TODO: provide visual indication that hard limit was reached for
650 } catch (Exception e)
654 if (validator.getMin() != null && validator.getMax() != null)
656 slider.getModel().setRangeProperties(iVal, 1,
657 validator.getMin().intValue(),
658 validator.getMax().intValue(), true);
662 slider.setVisible(false);
672 fVal = Float.valueOf(valueField.getText());
673 if (validator.getMin() != null
674 && validator.getMin().floatValue() > fVal)
676 fVal = validator.getMin().floatValue();
677 // TODO: provide visual indication that hard limit was reached for
680 if (validator.getMax() != null
681 && validator.getMax().floatValue() < fVal)
683 fVal = validator.getMax().floatValue();
684 // TODO: provide visual indication that hard limit was reached for
687 } catch (Exception e)
691 if (validator.getMin() != null && validator.getMax() != null)
693 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
694 (int) validator.getMin().floatValue() * 1000,
695 (int) validator.getMax().floatValue() * 1000, true);
699 slider.setVisible(false);
709 slider.setVisible(false);
711 { valueField.getText().trim() };
716 { (String) choicebox.getSelectedItem() };
723 public static final int PARAM_WIDTH = 340;
725 public static final int PARAM_HEIGHT = 150;
727 public static final int PARAM_CLOSEDHEIGHT = 80;
729 public OptsAndParamsPage(OptsParametersContainerI paramContainer)
731 this(paramContainer,false);
733 public OptsAndParamsPage(OptsParametersContainerI paramContainer, boolean compact)
735 poparent = paramContainer;
736 this.compact=compact;
739 public static void showUrlPopUp(JComponent invoker, final String finfo,
743 JPopupMenu mnu = new JPopupMenu();
744 JMenuItem mitem = new JMenuItem("View " + finfo);
745 mitem.addActionListener(new ActionListener()
749 public void actionPerformed(ActionEvent e)
751 Desktop.showUrl(finfo);
756 mnu.show(invoker, x, y);
759 URL linkImageURL = getClass().getResource("/images/link.gif");
761 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
763 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
765 public Map<String, OptionBox> getOptSet()
770 public void setOptSet(Map<String, OptionBox> optSet)
772 this.optSet = optSet;
775 public Map<String, ParamBox> getParamSet()
780 public void setParamSet(Map<String, ParamBox> paramSet)
782 this.paramSet = paramSet;
785 OptsParametersContainerI poparent;
787 OptionBox addOption(OptionI opt)
789 OptionBox cb = optSet.get(opt.getName());
792 cb = new OptionBox(opt);
793 optSet.put(opt.getName(), cb);
794 // jobOptions.add(cb, FlowLayout.LEFT);
799 ParamBox addParameter(ParameterI arg)
801 ParamBox pb = paramSet.get(arg.getName());
804 pb = new ParamBox(poparent, arg);
805 paramSet.put(arg.getName(), pb);
806 // paramList.add(pb);
809 // take the defaults from the parameter
810 pb.updateControls(arg);
814 void selectOption(OptionI option, String string)
816 OptionBox cb = optSet.get(option.getName());
819 cb = addOption(option);
821 cb.enabled.setSelected(string != null); // initial state for an option.
824 if (option.getPossibleValues().contains(string))
826 cb.val.setSelectedItem(string);
830 throw new Error("Invalid value " + string + " for option " + option);
834 if (option.isRequired() && !cb.enabled.isSelected())
836 // TODO: indicate paramset is not valid.. option needs to be selected!
838 cb.setInitialValue();
841 void setParameter(ParameterI arg)
843 ParamBox pb = paramSet.get(arg.getName());
850 pb.updateControls(arg);
856 * recover options and parameters from GUI
860 public List<ArgumentI> getCurrentSettings()
862 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
863 for (OptionBox opts : getOptSet().values())
865 OptionI opt = opts.getOptionIfEnabled();
871 for (ParamBox parambox : getParamSet().values())
873 ParameterI parm = parambox.getParameter();