2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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/>.
20 import jalview.ws.jws2.JabaParamStore;
21 import jalview.ws.jws2.JabaPreset;
22 import jalview.ws.jws2.Jws2Discoverer;
23 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
24 import jalview.ws.params.ArgumentI;
25 import jalview.ws.params.OptionI;
26 import jalview.ws.params.ParamDatastoreI;
27 import jalview.ws.params.ParameterI;
28 import jalview.ws.params.ValueConstrainI;
29 import jalview.ws.params.WsParamSetI;
31 import java.awt.BorderLayout;
32 import java.awt.Component;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
36 import java.awt.GridBagConstraints;
37 import java.awt.GridBagLayout;
38 import java.awt.GridLayout;
39 import java.awt.Rectangle;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.ItemEvent;
43 import java.awt.event.ItemListener;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
46 import java.awt.event.WindowEvent;
47 import java.awt.event.WindowListener;
49 import java.util.ArrayList;
50 import java.util.Hashtable;
51 import java.util.Iterator;
52 import java.util.List;
54 import java.util.Vector;
56 import javax.swing.JButton;
57 import javax.swing.JCheckBox;
58 import javax.swing.JComboBox;
59 import javax.swing.JComponent;
60 import javax.swing.JDialog;
61 import javax.swing.JFrame;
62 import javax.swing.JLabel;
63 import javax.swing.JMenuItem;
64 import javax.swing.JOptionPane;
65 import javax.swing.JPanel;
66 import javax.swing.JPopupMenu;
67 import javax.swing.JScrollPane;
68 import javax.swing.JSlider;
69 import javax.swing.JSplitPane;
70 import javax.swing.JTabbedPane;
71 import javax.swing.JTextArea;
72 import javax.swing.JTextField;
73 import javax.swing.border.TitledBorder;
74 import javax.swing.event.ChangeEvent;
75 import javax.swing.event.ChangeListener;
76 import javax.swing.event.DocumentEvent;
77 import javax.swing.event.DocumentListener;
79 import compbio.metadata.Argument;
80 import compbio.metadata.Option;
81 import compbio.metadata.Parameter;
82 import compbio.metadata.Preset;
83 import compbio.metadata.PresetManager;
84 import compbio.metadata.RunnerConfig;
85 import compbio.metadata.ValueConstrain.Type;
88 * job parameter editing/browsing dialog box. User can browse existing settings
89 * (user + presets + Defaults), and any changes to parameters creates a modified
90 * user parameter set. LOGIC: If the parameter set is modified, and its name is
91 * a valid, non-existant user parameter set, then a save button is shown. If the
92 * parameter set is modified and its name is a valid, extant user parameter set,
93 * then an update button is shown. If user parameter set's name is edited, and
94 * old name exists as a writable user parameter set, then rename button is
95 * shown. If current parameter set is associated with a user defined parameter
96 * set, then : if set is modifed, a 'revert' button is shown. if set is not
97 * modified, a 'delete' button is shown.
102 public class WsJobParameters extends JPanel implements ItemListener,
103 ActionListener, DocumentListener
105 URL linkImageURL = getClass().getResource("/images/link.gif");
107 private static final String SVC_DEF = "Defaults"; // this is the null
108 // parameter set as shown to
111 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
112 PARAM_CLOSEDHEIGHT = 80;
114 JPanel SetNamePanel = new JPanel();
116 JPanel setDetails = new JPanel();
118 JSplitPane settingsPanel = new JSplitPane();
120 JSplitPane jobPanel = new JSplitPane();
122 JPanel jobOptions = new JPanel();
124 JScrollPane jobOptionsPane = new JScrollPane();
126 JPanel jobParameters = new JPanel();
128 JButton createpref = new JButton();
130 JButton deletepref = new JButton();
132 JButton revertpref = new JButton();
134 JButton updatepref = new JButton();
136 JButton startjob = new JButton();
138 JButton canceljob = new JButton();
140 JComboBox setName = new JComboBox();
142 JTextArea setDescr = new JTextArea();
144 JScrollPane paramPane = new JScrollPane();
146 JPanel paramList = new JPanel();
148 JPanel optsAndparams = new JPanel();
150 RunnerConfig serviceOptions;
152 ParamDatastoreI paramStore;
154 private int MAX_OPTWIDTH = 200;
156 WsJobParameters(Jws2Instance service)
161 public WsJobParameters(Jws2Instance service, WsParamSetI preset)
163 this(null, service, preset, null);
169 * - if null, create new JFrame outside of desktop
173 public WsJobParameters(JFrame parent, Jws2Instance service,
174 WsParamSetI preset, List<Argument> jobArgset)
176 this(parent, null, service, preset, jobArgset);
187 public WsJobParameters(JFrame parent, ParamDatastoreI paramStorei,
188 Jws2Instance service, WsParamSetI preset, List<Argument> jobArgset)
192 this.paramStore = paramStorei;
193 if (paramStore == null)
195 paramStore = service.getParamStore();
197 this.service = service;
198 // argSetModified(false);
199 // populate parameter table
200 initForService(service, preset, jobArgset);
201 // display in new JFrame attached to parent.
207 JDialog frame = null;
210 * shows a modal dialog containing the parameters.
214 public boolean showRunDialog()
217 frame = new JDialog(Desktop.instance, true);
219 frame.setTitle("Edit parameters for " + service.getActionText());
220 Rectangle deskr = Desktop.instance.getBounds();
221 Dimension pref = this.getPreferredSize();
222 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - pref.width/2),
223 (int) (deskr.getCenterY() - pref.height/2), pref.width, pref.height));
224 frame.setContentPane(this);
226 // should perhaps recover defaults from user prefs.
229 javax.swing.SwingUtilities.invokeLater(new Runnable()
233 jobPanel.setDividerLocation(0.25);
237 frame.setVisible(true);
246 private void jbInit()
248 updatepref = JvSwingUtils.makeButton("Update",
249 "Update this existing user parameter set.",
253 public void actionPerformed(ActionEvent e)
255 update_actionPerformed(e);
258 deletepref = JvSwingUtils.makeButton("Delete",
259 "Delete the currently selected user parameter set.",
263 public void actionPerformed(ActionEvent e)
265 delete_actionPerformed(e);
268 createpref = JvSwingUtils.makeButton("Create",
269 "Create a new parameter set with the current settings.",
273 public void actionPerformed(ActionEvent e)
275 create_actionPerformed(e);
278 revertpref = JvSwingUtils.makeButton("Revert",
279 "Undo all changes to the current parameter set",
283 public void actionPerformed(ActionEvent e)
285 revert_actionPerformed(e);
288 startjob = JvSwingUtils.makeButton("Start Job",
289 "Start Job with current settings.", new ActionListener()
291 public void actionPerformed(ActionEvent e)
293 startjob_actionPerformed(e);
296 canceljob = JvSwingUtils.makeButton("Cancel Job",
297 "Close this dialog and cancel job.", new ActionListener()
299 public void actionPerformed(ActionEvent e)
301 canceljob_actionPerformed(e);
305 setDetails.setBorder(new TitledBorder("Details"));
306 setDetails.setLayout(new BorderLayout());
307 setDescr.setColumns(40);
308 setDescr.setWrapStyleWord(true);
309 setDescr.setLineWrap(true);
310 setDescr.setBackground(getBackground());
311 setDescr.setEditable(true);
312 setDescr.getDocument().addDocumentListener(this);
313 setDescr.setToolTipText("Click to edit the notes for this parameter set.");
314 JScrollPane setDescrView = new JScrollPane();
315 // setDescrView.setPreferredSize(new Dimension(350, 200));
316 setDescrView.getViewport().setView(setDescr);
317 setName.setEditable(true);
318 setName.addItemListener(this);
319 setName.getEditor().addActionListener(this);
320 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
321 GridBagLayout gbl = new GridBagLayout();
322 SetNamePanel.setLayout(gbl);
324 JLabel setNameLabel = new JLabel("Current parameter set name :");
325 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
327 setNameInfo.add(setNameLabel);
328 setNameInfo.add(setName);
330 // initial button visibility
331 updatepref.setVisible(false);
332 deletepref.setVisible(false);
333 revertpref.setVisible(false);
334 createpref.setVisible(false);
335 JPanel setsavebuts = new JPanel();
336 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
337 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
338 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
339 JPanel spacer = new JPanel();
340 spacer.setPreferredSize(new Dimension(2,30));
341 setsavebuts.add(spacer);
342 setsavebuts.add(deletepref);
343 setsavebuts.add(revertpref);
344 setsavebuts.add(createpref);
345 setsavebuts.add(updatepref);
346 // setsavebuts.setSize(new Dimension(150, 30));
347 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
348 buttonArea.add(setsavebuts);
349 SetNamePanel.add(setNameInfo);
350 GridBagConstraints gbc = new GridBagConstraints();
352 gbl.setConstraints(setNameInfo, gbc);
353 SetNamePanel.add(buttonArea);
354 gbc = new GridBagConstraints();
358 gbl.setConstraints(buttonArea, gbc);
359 setDetails.add(setDescrView, BorderLayout.CENTER);
361 jobParameters.setBorder(new TitledBorder("Parameters"));
362 paramPane.setPreferredSize(new Dimension(360, 400));
363 jobOptions.setBorder(new TitledBorder("Options"));
365 paramList.setBorder(new TitledBorder("Parameters"));
367 JPanel bjo=new JPanel(new BorderLayout()),bjp=new JPanel(new BorderLayout());
368 bjo.add(jobOptions, BorderLayout.CENTER);
369 bjp.add(paramList, BorderLayout.CENTER);
370 optsAndparams.setLayout(new BorderLayout());
372 optsAndparams.add(bjo, BorderLayout.CENTER);
373 optsAndparams.add(bjp, BorderLayout.SOUTH);
374 paramPane.setViewportView(optsAndparams);
376 setLayout(new BorderLayout());
377 jobPanel.setLeftComponent(setDetails);
378 jobPanel.setRightComponent(paramPane);
379 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
381 add(SetNamePanel, BorderLayout.NORTH);
382 add(jobPanel, BorderLayout.CENTER);
384 JPanel dialogpanel = new JPanel();
385 dialogpanel.add(startjob);
386 dialogpanel.add(canceljob);
387 add(dialogpanel, BorderLayout.SOUTH);
390 protected void revert_actionPerformed(ActionEvent e)
392 reInitDialog(lastParmSet);
396 protected void update_actionPerformed(ActionEvent e)
400 String curname = ((String) setName.getSelectedItem()).trim();
401 _updatePreset(lastParmSet, curname);
402 lastParmSet = curname;
404 initArgSetModified();
405 syncSetNamesWithStore();
409 private void _deleteUserPreset(String lastParmSet2)
411 paramStore.deletePreset(lastParmSet2);
414 protected void delete_actionPerformed(ActionEvent e)
418 // delete current preset's saved entry
419 _deleteUserPreset(lastParmSet);
421 reInitDialog(null); // service default
424 protected void create_actionPerformed(ActionEvent e)
426 String curname = ((String) setName.getSelectedItem()).trim();
427 if (curname.length() > 0)
429 _storeCurrentPreset(curname);
430 lastParmSet = curname;
432 initArgSetModified();
436 // TODO: show warning
437 System.err.println("Invalid name. Not saved.");
441 protected void canceljob_actionPerformed(ActionEvent e)
446 frame.setVisible(false);
450 protected void startjob_actionPerformed(ActionEvent e)
455 frame.setVisible(false);
459 Jws2Instance service;
462 * list of service presets in the gui
464 Hashtable servicePresets = null;
467 * set if dialog is being set - so handlers will avoid spurious events
469 boolean settingDialog = false;
471 void initForService(Jws2Instance service, WsParamSetI jabap,
472 List<Argument> jabajobArgset)
474 WsParamSetI p = null;
475 List<ArgumentI> jobArgset = null;
476 settingDialog = true;
477 { // instantiate the abstract proxy for Jaba objects
478 jobArgset = jabajobArgset == null ? null : JabaParamStore
479 .getJwsArgsfromJaba(jabajobArgset);
480 p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
484 Hashtable exnames = new Hashtable();
485 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
487 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
489 servicePresets = new Hashtable();
490 // Add the default entry - if not present already.
491 if (!exnames.contains(SVC_DEF))
493 setName.addItem(SVC_DEF);
494 exnames.put(SVC_DEF, SVC_DEF);
495 servicePresets.put(SVC_DEF, SVC_DEF);
497 String curname = (p == null ? "" : p.getName());
498 for (WsParamSetI pr : paramStore.getPresets())
500 if (!pr.isModifiable())
502 servicePresets.put(pr.getName(), "preset");
507 if (!exnames.contains(pr.getName()))
509 setName.addItem(pr.getName());
512 // TODO: if initial jobArgset matches a given user setting or preset then
513 // should recover setting accordingly
514 // updateTable(p, jobArgset);
517 reInitDialog(p.getName());
518 initArgSetModified();
522 if (jobArgset != null && jobArgset.size() > 0)
524 curSetName = "Supplied Settings";
525 updateTable(p, jobArgset);
533 settingDialog = false;
537 @SuppressWarnings("unchecked")
538 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
540 // populate table from default parameter set.
541 List<ArgumentI> args = paramStore.getServiceParameters();
543 // split to params and required arguments
545 for (ArgumentI myarg : args)
547 // Ideally, Argument would implement isRequired !
548 if (myarg instanceof ParameterI)
550 ParameterI parm = (ParameterI) myarg;
555 if (myarg instanceof OptionI)
557 OptionI opt = (OptionI) myarg;
558 OptionBox ob = addOption(opt);
560 if (MAX_OPTWIDTH < ob.getPreferredSize().width)
562 MAX_OPTWIDTH = ob.getPreferredSize().width;
568 System.err.println("Ignoring unknown service argument type "
569 + myarg.getClass().getName());
573 args = null; // no more args to process.
577 isUserPreset = false;
578 // initialise setname
579 setName.setSelectedItem(lastSetName = p.getName());
580 setDescr.setText(lastDescrText = p.getDescription());
584 args = p.getArguments();
585 } catch (Exception e)
589 // TODO: check if args should be unselected prior to resetting using the
594 if (lastParmSet == null)
596 isUserPreset = false;
597 // first call - so create a dummy name
599 setName.setSelectedItem(lastSetName = SVC_DEF);
603 if (jobArgset != null)
605 argSetModified(jobArgset, true);
608 // get setargs from current object
611 for (ArgumentI arg : args)
613 if (arg instanceof ParameterI)
615 setParameter((ParameterI) arg);
619 if (arg instanceof OptionI)
621 // System.out.println("Setting option "
622 // + System.identityHashCode(arg) + ":" + arg.getName()
623 // + " with " + arg.getDefaultValue());
624 selectOption((OptionI) arg, arg.getDefaultValue());
631 refreshParamLayout();
635 private boolean isModified()
637 return modifiedElements.size() > 0;
640 private Hashtable modifiedElements = new Hashtable();
643 * reset gui and modification state settings
645 private void initArgSetModified()
648 modifiedElements.clear();
649 updateButtonDisplay();
652 private void updateButtonDisplay()
654 boolean _update = false, _create = false, _delete = false, _revert = false;
655 if (modifiedElements.size() > 0)
659 _update = isUserPreset; // can only update user presets
660 if (!isUserPreset || modifiedElements.containsKey(setName))
662 // name modified - can create new preset
670 // can still delete a user preset
671 _delete = isUserPreset;
673 createpref.setVisible(_create);
674 updatepref.setVisible(_update);
675 deletepref.setVisible(_delete);
676 revertpref.setVisible(_revert);
680 private void argSetModified(Object modifiedElement, boolean b)
688 modifiedElements.remove(modifiedElement);
692 if (b && modifiedElement == setName
693 && modifiedElements.contains(modifiedElement))
695 // HACK! prevents iteration on makeSetNameValid
698 modifiedElements.put(modifiedElement, modifiedElement);
700 // set mod status based on presence of elements in table
701 if (b && modifiedElements.size() > 0)
703 makeSetNameValid(!isUserPreset);
704 SetNamePanel.revalidate();
706 updateButtonDisplay();
709 private boolean isServicePreset(String selectedItem)
711 return selectedItem.equals(SVC_DEF)
712 || servicePresets.containsKey(selectedItem);
716 * check if the current set name is a valid set name for saving, if not, then
719 private void makeSetNameValid(boolean newuserset)
721 boolean stn = settingDialog;
722 boolean renamed = false;
723 settingDialog = true;
724 String nm = (curSetName != null ? curSetName : (String) setName
726 // check if the name is reserved - if it is, rename it.
727 if (isServicePreset(nm))
736 while (paramStore.getPreset(tnm) != null)
738 tnm = nm + " (" + (++i) + ")";
747 boolean makeupdate = false;
748 // sync the gui with the preset database
749 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
751 String snm = (String) setName.getItemAt(i);
755 // setName.setSelectedIndex(i);
760 setName.addItem(curSetName = nm);
761 setName.setSelectedItem(curSetName);
765 settingDialog = false; // we need this name change to be registered.
766 argSetModified(setName, renamed);
771 private void addParameter(ParameterI arg)
773 ParamBox pb = paramSet.get(arg.getName());
776 pb = new ParamBox(this, arg);
777 paramSet.put(arg.getName(), pb);
781 // take the defaults from the parameter
782 pb.updateControls(arg);
785 private void setParameter(ParameterI arg)
787 ParamBox pb = paramSet.get(arg.getName());
794 pb.updateControls(arg);
799 private void selectOption(OptionI option, String string)
801 OptionBox cb = optSet.get(option.getName());
804 cb = addOption(option);
806 cb.enabled.setSelected(string != null); // initial state for an option.
809 if (option.getPossibleValues().contains(string))
811 cb.val.setSelectedItem(string);
815 throw new Error("Invalid value " + string + " for option " + option);
819 if (option.isRequired() && !cb.enabled.isSelected())
821 // TODO: indicate paramset is not valid.. option needs to be selected!
823 cb.setInitialValue();
826 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
828 public class ParamBox extends JPanel implements ChangeListener,
829 ActionListener, MouseListener
831 JButton showDesc = new JButton();
833 JTextArea string = new JTextArea();
835 JScrollPane descPanel = new JScrollPane();
837 JSlider slider = null;
839 JTextField valueField = null;
841 ValueConstrainI validator = null;
843 JPanel settingPanel = new JPanel();
845 JPanel controlPanel = new JPanel();
847 boolean integ = false;
849 boolean choice = false;
851 boolean descisvisible = false;
853 final WsJobParameters pmdialogbox;
857 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
859 pmdialogbox = pmlayout;
860 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
861 setBorder(new TitledBorder(parm.getName()));
863 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
864 showDesc.setText("+");
865 string.setFont(new Font("Verdana", Font.PLAIN, 11));
866 string.setBackground(getBackground());
868 string.setEditable(false);
869 descPanel.getViewport().setView(string);
871 descPanel.setVisible(false);
873 final ParamBox me = this;
874 finfo = parm.getFurtherDetails();
877 showDesc.setToolTipText("<html>"
879 .wrapTooltip("Click to show brief description<br><img src=\""
881 + "\"/> Right click for further information.")
883 showDesc.addMouseListener(this);
887 showDesc.setToolTipText("<html>"
889 .wrapTooltip("Click to show brief description.")
892 showDesc.addActionListener(new ActionListener()
895 public void actionPerformed(ActionEvent e)
897 descisvisible = !descisvisible;
898 descPanel.setVisible(descisvisible);
899 descPanel.getVerticalScrollBar().setValue(0);
900 me.setPreferredSize(new Dimension(PARAM_WIDTH,
901 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
903 pmlayout.refreshParamLayout();
906 string.setWrapStyleWord(true);
907 string.setLineWrap(true);
908 string.setColumns(32);
909 string.setText(parm.getDescription());
910 JPanel firstrow = new JPanel();
911 firstrow.setLayout(null);
912 controlPanel.setLayout(new BorderLayout());
913 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
914 PARAM_CLOSEDHEIGHT - 50));
915 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
916 firstrow.add(showDesc);
917 firstrow.add(controlPanel);
918 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
919 PARAM_CLOSEDHEIGHT - 30));
921 validator = parm.getValidValue();
923 if (validator != null)
925 integ = validator.getType() == Type.Integer;
929 if (parameter.getPossibleValues() != null)
934 updateControls(parm);
935 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
936 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
943 // reset the widget's initial value.
947 boolean adjusting = false;
949 ParameterI parameter;
953 public int getBoxHeight()
955 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
958 public void updateControls(ParameterI parm)
961 boolean init = (choicebox == null && valueField == null);
966 choicebox = new JComboBox();
967 choicebox.addActionListener(this);
968 controlPanel.add(choicebox, BorderLayout.CENTER);
972 slider = new JSlider();
973 slider.addChangeListener(this);
974 valueField = new JTextField();
975 valueField.addActionListener(this);
976 valueField.setPreferredSize(new Dimension(60, 25));
977 controlPanel.add(slider, BorderLayout.WEST);
978 controlPanel.add(valueField, BorderLayout.EAST);
989 List vals = parm.getPossibleValues();
990 for (Object val : vals)
992 choicebox.addItem(val);
996 if (parm.getDefaultValue() != null)
998 choicebox.setSelectedItem(parm.getDefaultValue());
1003 valueField.setText(parm.getDefaultValue());
1006 lastVal = updateSliderFromValueField();
1012 public ParameterI getParameter()
1014 ParameterI prm = parameter.copy();
1017 prm.setDefaultValue((String) choicebox.getSelectedItem());
1021 prm.setDefaultValue(valueField.getText());
1026 public Object updateSliderFromValueField()
1030 if (validator != null)
1037 valueField.setText(valueField.getText().trim());
1038 iVal = Integer.valueOf(valueField.getText());
1039 if (validator.getMin() != null
1040 && validator.getMin().intValue() > iVal)
1042 iVal = validator.getMin().intValue();
1043 // TODO: provide visual indication that hard limit was reached for
1046 if (validator.getMax() != null
1047 && validator.getMax().intValue() < iVal)
1049 iVal = validator.getMax().intValue();
1050 // TODO: provide visual indication that hard limit was reached for
1053 } catch (Exception e)
1057 if (validator.getMin() != null && validator.getMax() != null)
1059 slider.getModel().setRangeProperties(iVal, 1,
1060 validator.getMin().intValue(),
1061 validator.getMax().intValue(), true);
1065 slider.setVisible(false);
1075 fVal = Float.valueOf(valueField.getText());
1076 if (validator.getMin() != null
1077 && validator.getMin().floatValue() > fVal)
1079 fVal = validator.getMin().floatValue();
1080 // TODO: provide visual indication that hard limit was reached for
1083 if (validator.getMax() != null
1084 && validator.getMax().floatValue() < fVal)
1086 fVal = validator.getMax().floatValue();
1087 // TODO: provide visual indication that hard limit was reached for
1090 } catch (Exception e)
1094 if (validator.getMin() != null && validator.getMax() != null)
1096 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1097 (int) validator.getMin().floatValue() * 1000,
1098 (int) validator.getMax().floatValue() * 1000, true);
1102 slider.setVisible(false);
1112 slider.setVisible(false);
1114 { valueField.getText().trim() };
1119 { (String) choicebox.getSelectedItem() };
1125 public void stateChanged(ChangeEvent e)
1129 valueField.setText(""
1130 + ((integ) ? ("" + (int) slider.getValue())
1131 : ("" + (float) (slider.getValue() / 1000f))));
1137 public void actionPerformed(ActionEvent e)
1145 updateSliderFromValueField();
1150 private void checkIfModified()
1152 Object cstate = updateSliderFromValueField();
1153 boolean notmod = false;
1154 if (cstate.getClass() == lastVal.getClass())
1156 if (cstate instanceof int[])
1158 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1160 else if (cstate instanceof float[])
1162 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1164 else if (cstate instanceof String[])
1166 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1169 pmdialogbox.argSetModified(this, !notmod);
1172 public void mouseClicked(MouseEvent e)
1174 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1176 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1180 public void mousePressed(MouseEvent e)
1182 // TODO Auto-generated method stub
1186 public void mouseReleased(MouseEvent e)
1188 // TODO Auto-generated method stub
1192 public void mouseEntered(MouseEvent e)
1194 // TODO Auto-generated method stub
1198 public void mouseExited(MouseEvent e)
1200 // TODO Auto-generated method stub
1203 // from http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
1204 // helpful hint of using the Java 1.6 alignBaseLine property of FlowLayout
1206 public Component.BaselineResizeBehavior getBaselineResizeBehavior() {
1207 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
1211 public int getBaseline(int width, int height) {
1216 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1218 public class OptionBox extends JPanel implements MouseListener,
1221 JComboBox val = new JComboBox();
1223 JCheckBox enabled = new JCheckBox();
1225 JLabel optlabel = new JLabel();
1229 boolean hasLink = false;
1233 public OptionBox(OptionI opt)
1236 setLayout(new BorderLayout());
1237 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1238 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1239 enabled.setText("");
1240 enabled.setText(opt.getName());
1241 enabled.addActionListener(this);
1242 finfo = option.getFurtherDetails();
1246 enabled.setToolTipText("<html>"
1247 + JvSwingUtils.wrapTooltip(opt.getDescription()
1248 + "<br><img src=\"" + linkImageURL + "\"/>")
1250 enabled.addMouseListener(this);
1254 enabled.setToolTipText("<html>"
1255 + JvSwingUtils.wrapTooltip(opt.getDescription())
1258 add(enabled, BorderLayout.NORTH);
1259 if (opt.getPossibleValues().size() > 1)
1261 setLayout(new GridLayout(1, 2));
1262 for (Object str : opt.getPossibleValues())
1264 val.addItem((String) str);
1266 val.setSelectedItem((String) opt.getDefaultValue());
1267 val.addActionListener(this);
1268 add(val, BorderLayout.SOUTH);
1270 // TODO: add actionListeners for popup (to open further info),
1271 // and to update list of parameters if an option is enabled
1272 // that takes a value. JBPNote: is this TODO still valid ?
1276 public void resetToDefault()
1278 enabled.setSelected(false);
1279 if (option.isRequired())
1281 // Apply default value
1282 selectOption(option, option.getDefaultValue());
1286 boolean initEnabled = false;
1288 String initVal = null;
1290 public void setInitialValue()
1292 initEnabled = enabled.isSelected();
1293 if (option.getPossibleValues() != null
1294 && option.getPossibleValues().size() > 1)
1296 initVal = (String) val.getSelectedItem();
1300 initVal = (initEnabled) ? option.getDefaultValue() : null;
1304 public OptionI getOptionIfEnabled()
1306 if (!enabled.isSelected())
1310 OptionI opt = option.copy();
1312 if (val.getSelectedItem() != null)
1314 opt.setDefaultValue((String) val.getSelectedItem());
1319 public void actionPerformed(ActionEvent e)
1321 if (e.getSource() != enabled)
1323 enabled.setSelected(true);
1328 private void checkIfModified()
1330 boolean notmod = (initEnabled == enabled.isSelected());
1331 if (enabled.isSelected())
1333 if (initVal != null)
1335 notmod &= initVal.equals(val.getSelectedItem());
1339 // compare against default service setting
1340 notmod &= option.getDefaultValue() == null
1341 || option.getDefaultValue().equals(val.getSelectedItem());
1346 notmod &= initVal == null;
1348 argSetModified(this, !notmod);
1351 public void mouseClicked(MouseEvent e)
1353 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1355 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1359 public void mousePressed(MouseEvent e)
1361 // TODO Auto-generated method stub
1365 public void mouseReleased(MouseEvent e)
1367 // TODO Auto-generated method stub
1371 public void mouseEntered(MouseEvent e)
1373 // TODO Auto-generated method stub
1377 public void mouseExited(MouseEvent e)
1379 // TODO Auto-generated method stub
1385 private OptionBox addOption(OptionI opt)
1387 OptionBox cb = optSet.get(opt.getName());
1390 cb = new OptionBox(opt);
1391 optSet.put(opt.getName(), cb);
1392 jobOptions.add(cb, FlowLayout.LEFT);
1397 public static void showUrlPopUp(JComponent invoker, final String finfo,
1401 JPopupMenu mnu = new JPopupMenu();
1402 JMenuItem mitem = new JMenuItem("View " + finfo);
1403 mitem.addActionListener(new ActionListener()
1407 public void actionPerformed(ActionEvent e)
1409 Desktop.showUrl(finfo);
1414 mnu.show(invoker, x, y);
1417 protected void refreshParamLayout()
1419 FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
1420 int sep=fl.getVgap();
1421 int os=0,s = jobOptions.getBorder().getBorderInsets(jobOptions).bottom+jobOptions.getBorder().getBorderInsets(jobOptions).top+2 * sep;
1422 int w = 2 * fl.getHgap() + (MAX_OPTWIDTH > PARAM_WIDTH ? MAX_OPTWIDTH : PARAM_WIDTH);
1423 jobOptions.setLayout(fl);
1424 if (optSet.size() > 0)
1427 for (OptionBox pbox : optSet.values())
1430 s += sep + pbox.getPreferredSize().height;
1432 jobOptions.setPreferredSize(new Dimension(w, s));
1433 jobOptions.setLayout(new GridLayout(optSet.size(),1));
1438 jobOptions.setVisible(false);
1441 // Now layout the parameters assuming they occupy one column - to calculate total height of options+parameters
1442 fl = new FlowLayout(FlowLayout.CENTER);
1443 // helpful hint from http://stackoverflow.com/questions/2743177/top-alignment-for-flowlayout
1444 fl.setAlignOnBaseline(true);
1445 if (paramSet.size() > 0)
1447 paramList.setLayout(fl);
1450 for (ParamBox pbox : paramSet.values())
1453 s += sep + pbox.getPreferredSize().height+pbox.getBorder().getBorderInsets(pbox).bottom;
1456 paramList.setPreferredSize(new Dimension(w, s));
1457 os+=s+2*sep+paramList.getBorder().getBorderInsets(paramList).bottom+paramList.getBorder().getBorderInsets(paramList).top;
1461 paramList.setVisible(false);
1463 // TODO: waste some time trying to eliminate any unnecessary .validate calls here
1464 paramList.validate();
1465 jobOptions.validate();
1466 // System.out.println("Size will be : "+w+","+os);
1467 optsAndparams.validate();
1468 paramPane.getViewport().validate();
1469 paramPane.getVerticalScrollBar().setBlockIncrement(PARAM_CLOSEDHEIGHT*2);
1470 paramPane.validate();
1475 * testing method - grab a service and parameter set and show the window
1479 public static void main(String[] args)
1481 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1484 if (args.length > 0)
1486 Vector<String> services = new Vector<String>();
1487 services.addElement(args[p++]);
1488 Jws2Discoverer.setServiceUrls(services);
1493 } catch (Exception e)
1495 System.err.println("Aborting. Problem discovering services.");
1496 e.printStackTrace();
1499 Jws2Discoverer.Jws2Instance lastserv = null;
1500 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1503 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1505 if (lastserv != null)
1507 List<Preset> prl = null;
1509 if (++p < args.length)
1511 PresetManager prman = lastserv.getPresets();
1514 pr = prman.getPresetByName(args[p]);
1517 // just grab the last preset.
1518 prl = prman.getPresets();
1524 PresetManager prman = lastserv.getPresets();
1527 prl = prman.getPresets();
1530 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1531 while (en != null && en.hasNext())
1537 en = prl.iterator();
1542 System.out.println("Testing opts dupes for "
1543 + lastserv.getUri() + " : "
1544 + lastserv.getActionText() + ":" + pr.getName());
1545 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1550 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1551 } catch (Exception e)
1553 System.err.println("Failed to copy " + o.getName());
1554 e.printStackTrace();
1557 System.err.println("Failed to copy " + o.getName());
1558 e.printStackTrace();
1563 System.out.println("Testing param dupes:");
1564 List<Parameter> rg = lastserv.getRunnerConfig()
1566 for (Parameter o : rg)
1570 Parameter cpy = jalview.ws.jws2.ParameterUtils
1572 } catch (Exception e)
1574 System.err.println("Failed to copy " + o.getName());
1575 e.printStackTrace();
1578 System.err.println("Failed to copy " + o.getName());
1579 e.printStackTrace();
1584 System.out.println("Testing param write:");
1585 List<String> writeparam = null, readparam = null;
1588 writeparam = jalview.ws.jws2.ParameterUtils
1590 pr.getArguments(lastserv.getRunnerConfig()),
1592 System.out.println("Testing param read :");
1593 List<Option> pset = jalview.ws.jws2.ParameterUtils
1594 .processParameters(writeparam,
1595 lastserv.getRunnerConfig(), " ");
1596 readparam = jalview.ws.jws2.ParameterUtils
1597 .writeParameterSet(pset, " ");
1598 Iterator<String> o = pr.getOptions().iterator(), s = writeparam
1599 .iterator(), t = readparam.iterator();
1600 boolean failed = false;
1601 while (s.hasNext() && t.hasNext())
1603 String on = o.next(), sn = s.next(), st = t.next();
1606 System.out.println("Original was " + on
1607 + " Phase 1 wrote " + sn + "\tPhase 2 wrote "
1614 System.out.println("Original parameters:\n"
1616 System.out.println("Wrote parameters in first set:\n"
1618 System.out.println("Wrote parameters in second set:\n"
1622 } catch (Exception e)
1624 e.printStackTrace();
1627 WsJobParameters pgui = new WsJobParameters(lastserv,
1628 new JabaPreset(lastserv, pr));
1629 JFrame jf = new JFrame("Parameters for "
1630 + lastserv.getActionText());
1631 JPanel cont = new JPanel(new BorderLayout());
1633 cont.setPreferredSize(pgui.getPreferredSize());
1634 cont.add(pgui, BorderLayout.CENTER);
1635 jf.setLayout(new BorderLayout());
1636 jf.add(cont, BorderLayout.CENTER);
1638 final Thread thr = Thread.currentThread();
1639 jf.addWindowListener(new WindowListener()
1642 public void windowActivated(WindowEvent e)
1644 // TODO Auto-generated method stub
1648 public void windowClosed(WindowEvent e)
1652 public void windowClosing(WindowEvent e)
1658 public void windowDeactivated(WindowEvent e)
1660 // TODO Auto-generated method stub
1664 public void windowDeiconified(WindowEvent e)
1666 // TODO Auto-generated method stub
1670 public void windowIconified(WindowEvent e)
1672 // TODO Auto-generated method stub
1676 public void windowOpened(WindowEvent e)
1678 // TODO Auto-generated method stub
1683 jf.setVisible(true);
1684 boolean inter = false;
1689 Thread.sleep(10000);
1690 } catch (Exception e)
1703 public List<ArgumentI> getJobParams()
1705 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
1706 // recover options and parameters from GUI
1707 for (OptionBox opts : optSet.values())
1709 OptionI opt = opts.getOptionIfEnabled();
1715 for (ParamBox parambox : paramSet.values())
1717 ParameterI parm = parambox.getParameter();
1727 String lastParmSet = null;
1730 * Hashtable<String, Object[]> editedParams = new Hashtable<String,
1733 * store the given parameters in the user parameter set database.
1735 * @param storeSetName - lastParmSet
1737 * @param descr - setDescr.getText()
1739 * @param jobParams - getJobParams()
1741 * private void _storeUserPreset(String storeSetName, String descr,
1742 * List<ArgumentI> jobParams) { // this is a simple hash store. Object[] pset;
1743 * editedParams.put(storeSetName, pset = new Object[3]); pset[0] =
1744 * storeSetName; pset[1] = descr; pset[2] = jobParams; // writeParam("Saving "
1745 * + storeSetName + ": ", jobParams); }
1747 * private void writeParam(String nm, List<ArgumentI> params) { for (ArgumentI
1748 * p : params) { System.out.println(nm + ":" + System.identityHashCode(p) +
1749 * " Name: " + p.getName() + " Value: " + p.getDefaultValue()); } }
1751 * private Object[] _getUserPreset(String setName) { Object[] pset =
1752 * editedParams.get(setName); // if (pset != null) // writeParam("Retrieving "
1753 * + setName + ": ", (List<Argument>) pset[2]); return pset; }
1755 * * remove the given user preset from the preset stash
1759 * private void _deleteUserPreset(String setName) {
1760 * editedParams.remove(setName); }
1763 private void syncSetNamesWithStore()
1766 // remove any set names in the drop down menu that aren't either a reserved
1767 // setting, or a user defined or service preset.
1768 Vector items = new Vector();
1769 while (n < setName.getItemCount())
1771 String item = (String) setName.getItemAt(n);
1772 if (!item.equals(SVC_DEF) && !paramStore.presetExists(item))
1774 setName.removeItemAt(n);
1778 items.addElement(item);
1782 if (!items.contains(SVC_DEF))
1784 setName.addItem(SVC_DEF);
1786 for (WsParamSetI upn : paramStore.getPresets())
1788 if (!items.contains(upn.getName()))
1790 setName.addItem(upn.getName());
1796 * true if lastParmSet is a user preset
1798 boolean isUserPreset = false;
1800 private void reInitDialog(String nextPreset)
1802 settingDialog = true;
1803 // updateTable(null,null); // first reset to defaults
1804 WsParamSetI pset = null;
1805 if (nextPreset != null && nextPreset.length() > 0)
1807 pset = paramStore.getPreset(nextPreset);
1811 if (pset.isModifiable())
1813 isUserPreset = true;
1814 setDescr.setText(pset.getDescription());
1815 updateTable(null, pset.getArguments());
1816 lastParmSet = nextPreset;
1820 isUserPreset = false;
1821 setDescr.setText("");
1822 // must be a default preset from service
1823 updateTable(pset, null);
1824 lastParmSet = nextPreset;
1829 isUserPreset = false;
1831 setDescr.setText("");
1832 updateTable(null, null);
1833 lastParmSet = SVC_DEF;
1836 initArgSetModified();
1837 syncSetNamesWithStore();
1838 setName.setSelectedItem(lastParmSet);
1839 SetNamePanel.validate();
1841 settingDialog = false;
1845 String curSetName = null;
1847 public void itemStateChanged(ItemEvent e)
1849 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1851 final String setname = (String) setName.getSelectedItem();
1852 System.out.println("Item state changed for " + setname
1853 + " (handling ? " + !settingDialog + ")");
1859 if (setname == null)
1863 javax.swing.SwingUtilities.invokeLater(new Runnable()
1867 doPreferenceComboStateChange(setname);
1873 private void doPreferenceComboStateChange(String setname)
1875 // user has selected a different item from combo-box
1878 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1879 if (lsetname.equals(setname))
1881 // setname was just edited - so ignore this event.
1884 settingDialog = true;
1885 System.out.println("Prompting to save " + lsetname);
1886 if (javax.swing.JOptionPane
1891 + "' is modifed, and your changes will be lost.\nReally change preset ?",
1892 "Warning: Unsaved Changes",
1893 javax.swing.JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
1895 // revert the combobox to the current item
1896 settingDialog = true;
1897 setName.setSelectedItem(lsetname);
1898 settingDialog = false;
1901 // System.out.println("Saving for " + lsetname);
1902 // _storeCurrentPreset(lsetname);
1906 settingDialog = true;
1907 reInitDialog(setname);
1908 settingDialog = false;
1912 private void _renameExistingPreset(String oldName, String curSetName2)
1914 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(),
1919 * store current settings as given name. You should then reset gui.
1921 * @param curSetName2
1923 private void _storeCurrentPreset(String curSetName2)
1925 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1928 private void _updatePreset(String lastParmSet2, String curname)
1930 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(),
1936 * last saved name for this user preset
1938 String lastSetName = null;
1941 * last saved value of the description text for this user preset
1943 String lastDescrText = null;
1945 public void actionPerformed(ActionEvent e)
1947 if (e.getSource() instanceof Component)
1949 Component src = (Component) e.getSource();
1950 if (src.getParent() == setName)
1952 // rename any existing records we know about for this set.
1953 String newname = (String) e.getActionCommand().trim();
1955 if (isServicePreset(newname))
1957 final String oldname = curSetName != null ? curSetName
1959 final Component ourframe = this;
1960 settingDialog = true;
1961 setName.getEditor().setItem(oldname);
1962 settingDialog = false;
1963 javax.swing.SwingUtilities.invokeLater(new Runnable()
1967 JOptionPane.showMessageDialog(ourframe,
1968 "Invalid name - preset already exists.",
1969 "Invalid name", JOptionPane.WARNING_MESSAGE);
1975 curSetName = newname;
1976 System.err.println("New name for user setting " + curSetName
1977 + " (was " + setName.getSelectedItem() + ")");
1978 if (curSetName.equals(setName.getSelectedItem()))
1982 if (curSetName != null)
1984 argSetModified(setName, true);
1992 private void checkDescrModified()
1999 (lastDescrText == null ? setDescr.getText().trim().length() > 0
2000 : !setDescr.getText().equals(lastDescrText)));
2005 public void insertUpdate(DocumentEvent e)
2007 checkDescrModified();
2010 public void removeUpdate(DocumentEvent e)
2012 checkDescrModified();
2015 public void changedUpdate(DocumentEvent e)
2017 checkDescrModified();
2022 * @return null or the service preset selected by the user
2024 public WsParamSetI getPreset()
2026 if (isUserPreset || isModified()
2027 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
2033 return paramStore.getPreset(lastParmSet);