3 import java.awt.BorderLayout;
5 import java.awt.Component;
6 import java.awt.Dimension;
7 import java.awt.FlowLayout;
9 import java.awt.GridLayout;
10 import java.awt.Rectangle;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.awt.event.ComponentEvent;
14 import java.awt.event.ComponentListener;
15 import java.awt.event.ContainerEvent;
16 import java.awt.event.ContainerListener;
17 import java.awt.event.ItemEvent;
18 import java.awt.event.ItemListener;
19 import java.awt.event.KeyEvent;
20 import java.awt.event.KeyListener;
21 import java.awt.event.MouseEvent;
22 import java.awt.event.MouseListener;
23 import java.awt.event.WindowEvent;
24 import java.awt.event.WindowListener;
25 import java.awt.event.WindowStateListener;
27 import java.util.ArrayList;
28 import java.util.EventObject;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.Vector;
36 import javax.swing.InputVerifier;
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JComboBox;
40 import javax.swing.JComponent;
41 import javax.swing.JDialog;
42 import javax.swing.JFrame;
43 import javax.swing.JLabel;
44 import javax.swing.JList;
45 import javax.swing.JMenuItem;
46 import javax.swing.JOptionPane;
47 import javax.swing.JPanel;
48 import javax.swing.JPopupMenu;
49 import javax.swing.JScrollPane;
50 import javax.swing.JSlider;
51 import javax.swing.JSplitPane;
52 import javax.swing.JTabbedPane;
53 import javax.swing.JTable;
54 import javax.swing.JTextArea;
55 import javax.swing.JTextField;
56 import javax.swing.ListSelectionModel;
57 import javax.swing.SwingConstants;
58 import javax.swing.SwingUtilities;
59 import javax.swing.border.TitledBorder;
60 import javax.swing.event.CellEditorListener;
61 import javax.swing.event.ChangeEvent;
62 import javax.swing.event.ChangeListener;
63 import javax.swing.event.DocumentEvent;
64 import javax.swing.event.DocumentListener;
65 import javax.swing.table.*;
67 import compbio.metadata.Argument;
68 import compbio.metadata.Option;
69 import compbio.metadata.Parameter;
70 import compbio.metadata.Preset;
71 import compbio.metadata.PresetManager;
72 import compbio.metadata.RunnerConfig;
73 import compbio.metadata.ValueConstrain;
74 import compbio.metadata.WrongParameterException;
75 import compbio.metadata.ValueConstrain.Type;
77 import jalview.util.jarInputStreamProvider;
78 import jalview.ws.jws2.JabaParamStore;
79 import jalview.ws.jws2.JabaPreset;
80 import jalview.ws.jws2.Jws2Discoverer;
81 import jalview.ws.jws2.ParameterUtils;
82 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
83 import jalview.ws.jws2.dm.JabaOption;
84 import jalview.ws.jws2.dm.JabaParameter;
85 import jalview.ws.params.ArgumentI;
86 import jalview.ws.params.OptionI;
87 import jalview.ws.params.ParamDatastoreI;
88 import jalview.ws.params.ParameterI;
89 import jalview.ws.params.ValueConstrainI;
90 import jalview.ws.params.WsParamSetI;
93 * job parameter editing/browsing dialog box. User can browse existing settings
94 * (user + presets + Defaults), and any changes to parameters creates a modified
95 * user parameter set. LOGIC: If the parameter set is modified, and its name is
96 * a valid, non-existant user parameter set, then a save button is shown. If the
97 * parameter set is modified and its name is a valid, extant user parameter set,
98 * then an update button is shown. If user parameter set's name is edited, and
99 * old name exists as a writable user parameter set, then rename button is
100 * shown. If current parameter set is associated with a user defined parameter
101 * set, then : if set is modifed, a 'revert' button is shown. if set is not
102 * modified, a 'delete' button is shown.
107 public class WsJobParameters extends JPanel implements ItemListener,
108 ActionListener, DocumentListener
110 URL linkImageURL = getClass().getResource("/images/link.gif");
111 private static final String SVC_DEF = "Defaults"; // this is the null
112 // parameter set as shown to
115 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
116 PARAM_CLOSEDHEIGHT = 80;
118 private static final int OPTSET_HEIGHT = 30;
120 JPanel SetNamePanel = new JPanel();
122 JPanel setDetails = new JPanel();
124 JSplitPane settingsPanel = new JSplitPane();
126 JSplitPane jobPanel = new JSplitPane();
127 JTabbedPane tabpanels=new JTabbedPane();
128 JPanel jobOptions = new JPanel();
130 JScrollPane jobOptionsPane = new JScrollPane();
132 JPanel jobParameters = new JPanel();
134 JButton createpref = new JButton();
136 JButton deletepref = new JButton();
138 JButton revertpref = new JButton();
140 JButton updatepref = new JButton();
142 JButton startjob = new JButton();
144 JButton canceljob = new JButton();
146 JComboBox setName = new JComboBox();
148 JTextArea setDescr = new JTextArea();
150 JScrollPane paramPane = new JScrollPane();
152 JPanel paramList = new JPanel();
154 RunnerConfig serviceOptions;
156 ParamDatastoreI paramStore;
158 WsJobParameters(Jws2Instance service)
163 public WsJobParameters(Jws2Instance service, WsParamSetI preset)
165 this(null, service, preset, null);
171 * - if null, create new JFrame outside of desktop
175 public WsJobParameters(JFrame parent, Jws2Instance service,
176 WsParamSetI preset, List<Argument> jobArgset)
178 this(parent, null, service, preset, jobArgset);
189 public WsJobParameters(JFrame parent, ParamDatastoreI paramStorei,
190 Jws2Instance service, WsParamSetI preset, List<Argument> jobArgset)
194 this.paramStore = paramStorei;
195 if (paramStore == null)
197 paramStore = service.getParamStore();
199 this.service = service;
200 // argSetModified(false);
201 // populate parameter table
202 initForService(service, preset, jobArgset);
203 // display in new JFrame attached to parent.
209 JDialog frame = null;
212 * shows a modal dialog containing the parameters.
216 public boolean showRunDialog()
219 frame = new JDialog(Desktop.instance, true);
221 frame.setTitle("Edit parameters for " + service.getActionText());
222 Rectangle deskr = Desktop.instance.getBounds();
223 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
224 (int) (deskr.getCenterY() - 250), 480, 500));
225 frame.setContentPane(this);
226 // should recover defaults from user prefs.
227 //settingsPanel.setDividerLocation(0.4);
228 //jobPanel.setDividerLocation(0.5);
229 tabpanels.setSelectedComponent(jobOptionsPane);
231 frame.setVisible(true);
241 private void jbInit()
243 updatepref = JvSwingUtils.makeButton("Update", "Update the stored user preference",
247 public void actionPerformed(ActionEvent e)
249 update_actionPerformed(e);
252 deletepref = JvSwingUtils.makeButton("Delete", "Delete the user preference",
256 public void actionPerformed(ActionEvent e)
258 delete_actionPerformed(e);
261 createpref = JvSwingUtils.makeButton("Create", "Create a new preference",
265 public void actionPerformed(ActionEvent e)
267 create_actionPerformed(e);
270 revertpref = JvSwingUtils.makeButton("Revert",
271 "Undo all changes to the current set", new ActionListener()
274 public void actionPerformed(ActionEvent e)
276 revert_actionPerformed(e);
279 startjob = JvSwingUtils.makeButton("Start", "Start Job",new ActionListener()
281 public void actionPerformed(ActionEvent e)
283 startjob_actionPerformed(e);
286 canceljob = JvSwingUtils.makeButton("Cancel", "Cancel Job", new ActionListener()
288 public void actionPerformed(ActionEvent e)
290 canceljob_actionPerformed(e);
294 setDetails.setBorder(new TitledBorder("Details"));
295 setDetails.setLayout(new BorderLayout());
296 setDescr.setColumns(40);
297 setDescr.setWrapStyleWord(true);
298 setDescr.setLineWrap(true);
299 setDescr.setBackground(getBackground());
300 setDescr.setEditable(true);
301 setDescr.getDocument().addDocumentListener(this);
302 JScrollPane setDescrView = new JScrollPane();
303 // setDescrView.setPreferredSize(new Dimension(350, 200));
304 setDescrView.getViewport().setView(setDescr);
305 setName.setEditable(true);
306 setName.addItemListener(this);
307 setName.getEditor().addActionListener(this);
308 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
309 SetNamePanel.setLayout(new GridLayout(2, 1));
310 SetNamePanel.setMinimumSize(new Dimension(300, 40));
311 JLabel setNameLabel = new JLabel("Parameter set: ");
312 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
313 // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
314 setNameInfo.add(setNameLabel);
315 setNameInfo.add(setName);
316 // initial button visibility
317 updatepref.setVisible(false);
318 deletepref.setVisible(false);
319 revertpref.setVisible(false);
320 createpref.setVisible(false);
321 JPanel setsavebuts = new JPanel();
322 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
323 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
324 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
325 setsavebuts.add(deletepref);
326 setsavebuts.add(revertpref);
327 setsavebuts.add(createpref);
328 setsavebuts.add(updatepref);
329 setsavebuts.setSize(new Dimension(150, 20));
330 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
331 buttonArea.add(setsavebuts);
332 SetNamePanel.add(setNameInfo);
333 SetNamePanel.add(buttonArea);
334 setDetails.add(setDescrView, BorderLayout.CENTER);
335 //setDetails.setPreferredSize(new Dimension(360, 100));
336 jobParameters.setBorder(new TitledBorder("Parameters"));
337 jobParameters.setLayout(new BorderLayout());
338 paramPane.setPreferredSize(new Dimension(360, 300));
339 paramPane.getVerticalScrollBar().setUnitIncrement(20);
340 // paramPanel.setPreferredSize(new Dimension(360, 300));
341 // TODO: relayout buttons nicely
342 paramPane.getViewport().setView(paramList);
343 jobParameters.add(paramPane, BorderLayout.CENTER);
344 JPanel jobOptionsPanel = new JPanel();
345 jobOptionsPanel.setLayout(new BorderLayout());
346 jobOptionsPanel.setBorder(new TitledBorder("Options"));
347 jobOptionsPane.getViewport().setView(jobOptions);
348 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
349 //settingsPanel.setLeftComponent(jobOptionsPanel);
350 //settingsPanel.setRightComponent(jobParameters);
351 //settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
353 setLayout(new BorderLayout());
354 jobPanel.setLeftComponent(setDetails);
355 jobPanel.setRightComponent(tabpanels);
356 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
357 add(SetNamePanel, BorderLayout.NORTH);
358 //add(jobPanel, BorderLayout.CENTER);
359 //setDescrView.setName("Description");
360 //tabpanels.add(setDescrView);
361 jobOptionsPane.setName("Options");
362 tabpanels.add(jobOptionsPane);
363 paramPane.setName("Parameters");
364 tabpanels.add(paramPane);
365 add(jobPanel, BorderLayout.CENTER);
366 JPanel dialogpanel = new JPanel();
367 dialogpanel.add(startjob);
368 dialogpanel.add(canceljob);
369 add(dialogpanel, BorderLayout.SOUTH);
372 protected void revert_actionPerformed(ActionEvent e)
374 reInitDialog(lastParmSet);
378 protected void update_actionPerformed(ActionEvent e)
382 String curname = ((String) setName.getSelectedItem()).trim();
383 _updatePreset(lastParmSet, curname);
384 lastParmSet = curname;
386 initArgSetModified();
387 syncSetNamesWithStore();
391 private void _deleteUserPreset(String lastParmSet2)
393 paramStore.deletePreset(lastParmSet2);
396 protected void delete_actionPerformed(ActionEvent e)
400 // delete current preset's saved entry
401 _deleteUserPreset(lastParmSet);
403 reInitDialog(null); // service default
406 protected void create_actionPerformed(ActionEvent e)
408 String curname = ((String) setName.getSelectedItem()).trim();
409 if (curname.length() > 0)
411 _storeCurrentPreset(curname);
412 lastParmSet = curname;
414 initArgSetModified();
418 // TODO: show warning
419 System.err.println("Invalid name. Not saved.");
423 protected void canceljob_actionPerformed(ActionEvent e)
428 frame.setVisible(false);
432 protected void startjob_actionPerformed(ActionEvent e)
437 frame.setVisible(false);
441 Jws2Instance service;
444 * list of service presets in the gui
446 Hashtable servicePresets = null;
449 * set if dialog is being set - so handlers will avoid spurious events
451 boolean settingDialog = false;
453 void initForService(Jws2Instance service, WsParamSetI jabap,
454 List<Argument> jabajobArgset)
456 WsParamSetI p = null;
457 List<ArgumentI> jobArgset = null;
458 settingDialog = true;
459 { // instantiate the abstract proxy for Jaba objects
460 jobArgset = jabajobArgset == null ? null : JabaParamStore
461 .getJwsArgsfromJaba(jabajobArgset);
462 p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
465 // TODO: Recover window geometry prefs for this service
466 // jobPanel.setDividerLocation(proportionalLocation)
467 // settingsPanel.setDividerLocation(proportionalLocation)
468 Hashtable exnames = new Hashtable();
469 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
471 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
473 servicePresets = new Hashtable();
474 // Add the default entry - if not present already.
475 if (!exnames.contains(SVC_DEF))
477 setName.addItem(SVC_DEF);
478 exnames.put(SVC_DEF, SVC_DEF);
479 servicePresets.put(SVC_DEF, SVC_DEF);
481 String curname = (p == null ? "" : p.getName());
482 for (WsParamSetI pr : paramStore.getPresets())
484 if (!pr.isModifiable())
486 servicePresets.put(pr.getName(), "preset");
491 if (!exnames.contains(pr.getName()))
493 setName.addItem(pr.getName());
496 // TODO: if initial jobArgset matches a given user setting or preset then
497 // should recover setting accordingly
498 // updateTable(p, jobArgset);
501 reInitDialog(p.getName());
502 initArgSetModified();
506 if (jobArgset != null && jobArgset.size() > 0)
508 curSetName = "Supplied Settings";
509 updateTable(p, jobArgset);
517 settingDialog = false;
521 @SuppressWarnings("unchecked")
522 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
524 // populate table from default parameter set.
525 List<ArgumentI> args = paramStore.getServiceParameters();
527 // split to params and required arguments
529 for (ArgumentI myarg : args)
531 // Ideally, Argument would implement isRequired !
532 if (myarg instanceof ParameterI)
534 ParameterI parm = (ParameterI) myarg;
539 if (myarg instanceof OptionI)
541 OptionI opt = (OptionI) myarg;
542 addOption(opt).resetToDefault();
546 System.err.println("Ignoring unknown service argument type "
547 + myarg.getClass().getName());
551 args = null; // no more args to process.
555 isUserPreset = false;
556 // initialise setname
557 setName.setSelectedItem(lastSetName = p.getName());
558 setDescr.setText(lastDescrText = p.getDescription());
562 args = p.getArguments();
563 } catch (Exception e)
567 // TODO: check if args should be unselected prior to resetting using the
572 if (lastParmSet == null)
574 isUserPreset = false;
575 // first call - so create a dummy name
577 setName.setSelectedItem(lastSetName = SVC_DEF);
581 if (jobArgset != null)
583 argSetModified(jobArgset, true);
586 // get setargs from current object
589 for (ArgumentI arg : args)
591 if (arg instanceof ParameterI)
593 setParameter((ParameterI) arg);
597 if (arg instanceof OptionI)
599 // System.out.println("Setting option "
600 // + System.identityHashCode(arg) + ":" + arg.getName()
601 // + " with " + arg.getDefaultValue());
602 selectOption((OptionI) arg, arg.getDefaultValue());
609 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
611 jobOptions.setLayout(new GridLayout(optSet.size(), 1));
612 refreshParamLayout();
613 paramPane.validate();
617 private boolean isModified()
619 return modifiedElements.size() > 0;
622 private Hashtable modifiedElements = new Hashtable();
625 * reset gui and modification state settings
627 private void initArgSetModified()
630 modifiedElements.clear();
631 updateButtonDisplay();
634 private void updateButtonDisplay()
636 boolean _update = false, _create = false, _delete = false, _revert = false;
637 if (modifiedElements.size() > 0)
641 _update = isUserPreset; // can only update user presets
642 if (!isUserPreset || modifiedElements.containsKey(setName))
644 // name modified - can create new preset
652 // can still delete a user preset
653 _delete = isUserPreset;
655 createpref.setVisible(_create);
656 updatepref.setVisible(_update);
657 deletepref.setVisible(_delete);
658 revertpref.setVisible(_revert);
662 private void argSetModified(Object modifiedElement, boolean b)
670 modifiedElements.remove(modifiedElement);
674 if (b && modifiedElement == setName
675 && modifiedElements.contains(modifiedElement))
677 // HACK! prevents iteration on makeSetNameValid
680 modifiedElements.put(modifiedElement, modifiedElement);
682 // set mod status based on presence of elements in table
683 if (b && modifiedElements.size() > 0)
685 makeSetNameValid(!isUserPreset);
686 SetNamePanel.revalidate();
688 updateButtonDisplay();
691 private boolean isServicePreset(String selectedItem)
693 return selectedItem.equals(SVC_DEF)
694 || servicePresets.containsKey(selectedItem);
698 * check if the current set name is a valid set name for saving, if not, then
701 private void makeSetNameValid(boolean newuserset)
703 boolean stn = settingDialog;
704 boolean renamed = false;
705 settingDialog = true;
706 String nm = (curSetName != null ? curSetName : (String) setName
708 // check if the name is reserved - if it is, rename it.
709 if (isServicePreset(nm))
718 while (paramStore.getPreset(tnm) != null)
720 tnm = nm + " (" + (++i) + ")";
729 boolean makeupdate = false;
730 // sync the gui with the preset database
731 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
733 String snm = (String) setName.getItemAt(i);
737 // setName.setSelectedIndex(i);
742 setName.addItem(curSetName = nm);
743 setName.setSelectedItem(curSetName);
747 settingDialog = false; // we need this name change to be registered.
748 argSetModified(setName, renamed);
753 private void addParameter(ParameterI arg)
755 ParamBox pb = paramSet.get(arg.getName());
758 pb = new ParamBox(this, arg);
759 paramSet.put(arg.getName(), pb);
763 // take the defaults from the parameter
764 pb.updateControls(arg);
767 private void setParameter(ParameterI arg)
769 ParamBox pb = paramSet.get(arg.getName());
776 pb.updateControls(arg);
781 private void selectOption(OptionI option, String string)
783 OptionBox cb = optSet.get(option.getName());
786 cb = addOption(option);
788 cb.enabled.setSelected(string != null); // initial state for an option.
791 if (option.getPossibleValues().contains(string))
793 cb.val.setSelectedItem(string);
797 throw new Error("Invalid value " + string + " for option " + option);
801 if (option.isRequired() && !cb.enabled.isSelected())
803 // TODO: indicate paramset is not valid.. option needs to be selected!
805 cb.setInitialValue();
808 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
810 public class ParamBox extends JPanel implements ChangeListener,
811 ActionListener, MouseListener
813 JButton showDesc = new JButton();
815 JTextArea string = new JTextArea();
817 JScrollPane descPanel = new JScrollPane();
819 JSlider slider = null;
821 JTextField valueField = null;
823 ValueConstrainI validator = null;
825 JPanel settingPanel = new JPanel();
827 JPanel controlPanel = new JPanel();
829 boolean integ = false;
831 boolean choice = false;
833 boolean descisvisible = false;
835 final WsJobParameters pmdialogbox;
839 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
841 pmdialogbox = pmlayout;
842 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
843 setBorder(new TitledBorder(parm.getName()));
845 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
846 showDesc.setText("+");
847 string.setFont(new Font("Verdana", Font.PLAIN, 11));
848 string.setBackground(getBackground());
849 // string.setSize(new Dimension(PARAM_WIDTH, 80));
850 string.setEditable(false);
851 descPanel.getViewport().setView(string);
852 // descPanel.setLocation(2,17);
853 descPanel.setVisible(false);
854 // string.setMinimumSize(new Dimension(140,80));
855 // string.setMaximumSize(new Dimension(280,80));
856 final ParamBox me = this;
857 finfo = parm.getFurtherDetails();
860 showDesc.setToolTipText("<html>"+JvSwingUtils.wrapTooltip("Click to show brief description<br><img src=\"" + linkImageURL + "\"/> Right click for further information.")+"</html>");
861 showDesc.addMouseListener(this);
865 showDesc.setToolTipText("<html>"+JvSwingUtils.wrapTooltip("Click to show brief description.")+"</html>");
867 showDesc.addActionListener(new ActionListener()
870 public void actionPerformed(ActionEvent e)
872 descisvisible = !descisvisible;
873 descPanel.setVisible(descisvisible);
874 me.setPreferredSize(new Dimension(PARAM_WIDTH,
875 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
877 pmlayout.refreshParamLayout();
880 string.setWrapStyleWord(true);
881 string.setLineWrap(true);
882 string.setColumns(32);
883 string.setText(parm.getDescription());
884 JPanel firstrow = new JPanel();
885 firstrow.setLayout(null);
886 controlPanel.setLayout(new BorderLayout());
887 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
888 PARAM_CLOSEDHEIGHT - 50));
889 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
890 firstrow.add(showDesc);
891 firstrow.add(controlPanel);
892 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
893 PARAM_CLOSEDHEIGHT - 30));
895 validator = parm.getValidValue();
897 if (validator != null)
899 integ = validator.getType() == Type.Integer;
903 if (parameter.getPossibleValues() != null)
908 updateControls(parm);
909 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
910 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
917 // reset the widget's initial value.
921 boolean adjusting = false;
923 ParameterI parameter;
927 public int getBoxHeight()
929 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
932 public void updateControls(ParameterI parm)
935 boolean init = (choicebox == null && valueField == null);
942 choicebox = new JComboBox();
943 choicebox.addActionListener(this);
944 controlPanel.add(choicebox, BorderLayout.CENTER);
948 slider = new JSlider();
949 slider.addChangeListener(this);
950 valueField = new JTextField();
951 valueField.addActionListener(this);
952 valueField.setPreferredSize(new Dimension(60, 25));
953 controlPanel.add(slider, BorderLayout.WEST);
954 controlPanel.add(valueField, BorderLayout.EAST);
965 List vals = parm.getPossibleValues();
966 for (Object val : vals)
968 choicebox.addItem(val);
972 if (parm.getDefaultValue() != null)
974 choicebox.setSelectedItem(parm.getDefaultValue());
979 valueField.setText(parm.getDefaultValue());
982 lastVal = updateSliderFromValueField();
988 public ParameterI getParameter()
990 ParameterI prm = parameter.copy();
993 prm.setDefaultValue((String) choicebox.getSelectedItem());
997 prm.setDefaultValue(valueField.getText());
1002 public Object updateSliderFromValueField()
1006 if (validator != null)
1013 valueField.setText(valueField.getText().trim());
1014 iVal = Integer.valueOf(valueField.getText());
1015 if (validator.getMin() != null
1016 && validator.getMin().intValue() > iVal)
1018 iVal = validator.getMin().intValue();
1019 // TODO: provide visual indication that hard limit was reached for
1022 if (validator.getMax() != null
1023 && validator.getMax().intValue() < iVal)
1025 iVal = validator.getMax().intValue();
1026 // TODO: provide visual indication that hard limit was reached for
1029 } catch (Exception e)
1033 if (validator.getMin() != null && validator.getMax() != null)
1035 slider.getModel().setRangeProperties(iVal, 1,
1036 validator.getMin().intValue(),
1037 validator.getMax().intValue(), true);
1041 slider.setVisible(false);
1051 fVal = Float.valueOf(valueField.getText());
1052 if (validator.getMin() != null
1053 && validator.getMin().floatValue() > fVal)
1055 fVal = validator.getMin().floatValue();
1056 // TODO: provide visual indication that hard limit was reached for
1059 if (validator.getMax() != null
1060 && validator.getMax().floatValue() < fVal)
1062 fVal = validator.getMax().floatValue();
1063 // TODO: provide visual indication that hard limit was reached for
1066 } catch (Exception e)
1070 if (validator.getMin() != null && validator.getMax() != null)
1072 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1073 (int) validator.getMin().floatValue() * 1000,
1074 (int) validator.getMax().floatValue() * 1000, true);
1078 slider.setVisible(false);
1088 slider.setVisible(false);
1090 { valueField.getText().trim() };
1095 { (String) choicebox.getSelectedItem() };
1101 public void stateChanged(ChangeEvent e)
1105 valueField.setText(""
1106 + ((integ) ? ("" + (int) slider.getValue())
1107 : ("" + (float) (slider.getValue() / 1000f))));
1113 public void actionPerformed(ActionEvent e)
1121 updateSliderFromValueField();
1126 private void checkIfModified()
1128 Object cstate = updateSliderFromValueField();
1129 boolean notmod = false;
1130 if (cstate.getClass() == lastVal.getClass())
1132 if (cstate instanceof int[])
1134 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1136 else if (cstate instanceof float[])
1138 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1140 else if (cstate instanceof String[])
1142 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1145 pmdialogbox.argSetModified(this, !notmod);
1148 public void mouseClicked(MouseEvent e)
1150 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1152 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1156 public void mousePressed(MouseEvent e)
1158 // TODO Auto-generated method stub
1162 public void mouseReleased(MouseEvent e)
1164 // TODO Auto-generated method stub
1168 public void mouseEntered(MouseEvent e)
1170 // TODO Auto-generated method stub
1174 public void mouseExited(MouseEvent e)
1176 // TODO Auto-generated method stub
1182 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1184 public class OptionBox extends JPanel implements MouseListener,
1187 JComboBox val = new JComboBox();
1189 JCheckBox enabled = new JCheckBox();
1191 JLabel optlabel = new JLabel();
1195 boolean hasLink = false;
1199 public OptionBox(OptionI opt)
1202 setLayout(new BorderLayout());
1203 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1204 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1205 enabled.setText("");
1206 enabled.setText(opt.getName());
1207 enabled.addActionListener(this);
1208 finfo = option.getFurtherDetails();
1212 // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
1213 enabled.setToolTipText("<html>" + JvSwingUtils.wrapTooltip(opt.getDescription()+"<br><img src=\"" + linkImageURL + "\"/>")
1215 // optlabel.addMouseListener(this);
1216 enabled.addMouseListener(this);
1220 // optlabel.setToolTipText(opt.getDescription());
1221 enabled.setToolTipText("<html>"+JvSwingUtils.wrapTooltip(opt.getDescription())+"</html>");
1223 add(enabled, BorderLayout.NORTH);
1224 if (opt.getPossibleValues().size() > 1)
1226 setLayout(new GridLayout(1, 2));
1227 for (Object str : opt.getPossibleValues())
1229 val.addItem((String) str);
1231 val.setSelectedItem((String) opt.getDefaultValue());
1232 val.addActionListener(this);
1233 add(val, BorderLayout.SOUTH);
1235 // TODO: add actionListeners for popup (to open further info),
1236 // and to update list of parameters if an option is enabled
1237 // that takes a value.
1241 public void resetToDefault()
1243 enabled.setSelected(false);
1244 if (option.isRequired())
1246 // Apply default value
1247 selectOption(option, option.getDefaultValue());
1251 boolean initEnabled = false;
1253 String initVal = null;
1255 public void setInitialValue()
1257 initEnabled = enabled.isSelected();
1258 if (option.getPossibleValues() != null
1259 && option.getPossibleValues().size() > 1)
1261 initVal = (String) val.getSelectedItem();
1265 initVal = (initEnabled) ? option.getDefaultValue() : null;
1269 public OptionI getOptionIfEnabled()
1271 if (!enabled.isSelected())
1275 OptionI opt = option.copy();
1277 if (val.getSelectedItem() != null)
1279 opt.setDefaultValue((String) val.getSelectedItem());
1284 public void actionPerformed(ActionEvent e)
1286 if (e.getSource() != enabled)
1288 enabled.setSelected(true);
1293 private void checkIfModified()
1295 boolean notmod = (initEnabled == enabled.isSelected());
1296 if (enabled.isSelected())
1298 if (initVal != null)
1300 notmod &= initVal.equals(val.getSelectedItem());
1304 // compare against default service setting
1305 notmod &= option.getDefaultValue() == null
1306 || option.getDefaultValue().equals(val.getSelectedItem());
1311 notmod &= initVal == null;
1313 argSetModified(this, !notmod);
1316 public void mouseClicked(MouseEvent e)
1318 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1320 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1324 public void mousePressed(MouseEvent e)
1326 // TODO Auto-generated method stub
1330 public void mouseReleased(MouseEvent e)
1332 // TODO Auto-generated method stub
1336 public void mouseEntered(MouseEvent e)
1338 // TODO Auto-generated method stub
1342 public void mouseExited(MouseEvent e)
1344 // TODO Auto-generated method stub
1350 private OptionBox addOption(OptionI opt)
1352 OptionBox cb = optSet.get(opt.getName());
1355 cb = new OptionBox(opt);
1356 optSet.put(opt.getName(), cb);
1362 public static void showUrlPopUp(JComponent invoker, final String finfo,
1366 JPopupMenu mnu = new JPopupMenu();
1367 JMenuItem mitem = new JMenuItem("View " + finfo);
1368 mitem.addActionListener(new ActionListener()
1372 public void actionPerformed(ActionEvent e)
1374 Desktop.showUrl(finfo);
1379 mnu.show(invoker, x, y);
1382 protected void refreshParamLayout()
1384 FlowLayout fl = new FlowLayout();
1385 paramList.setLayout(fl);
1386 int s = 2 * fl.getVgap();
1387 for (ParamBox pbox : paramSet.values())
1389 s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
1391 paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
1396 * testing method - grab a service and parameter set and show the window
1400 public static void main(String[] args)
1402 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1405 if (args.length > 3)
1407 Vector<String> services = new Vector<String>();
1408 services.addElement(args[p++]);
1409 Jws2Discoverer.setServiceUrls(services);
1414 } catch (Exception e)
1416 System.err.println("Aborting. Problem discovering services.");
1417 e.printStackTrace();
1420 Jws2Discoverer.Jws2Instance lastserv = null;
1421 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1424 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1426 if (lastserv != null)
1428 List<Preset> prl = null;
1430 if (++p < args.length)
1432 PresetManager prman = lastserv.getPresets();
1435 pr = prman.getPresetByName(args[p]);
1438 // just grab the last preset.
1439 prl = prman.getPresets();
1445 PresetManager prman = lastserv.getPresets();
1448 prl = prman.getPresets();
1451 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1452 while (en != null && en.hasNext())
1458 en = prl.iterator();
1463 System.out.println("Testing opts dupes for "
1464 + lastserv.getUri() + " : "
1465 + lastserv.getActionText() + ":" + pr.getName());
1466 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1471 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1472 } catch (Exception e)
1474 System.err.println("Failed to copy " + o.getName());
1475 e.printStackTrace();
1478 System.err.println("Failed to copy " + o.getName());
1479 e.printStackTrace();
1484 System.out.println("Testing param dupes:");
1485 List<Parameter> rg = lastserv.getRunnerConfig()
1487 for (Parameter o : rg)
1491 Parameter cpy = jalview.ws.jws2.ParameterUtils
1493 } catch (Exception e)
1495 System.err.println("Failed to copy " + o.getName());
1496 e.printStackTrace();
1499 System.err.println("Failed to copy " + o.getName());
1500 e.printStackTrace();
1505 System.out.println("Testing param write:");
1506 List<String> writeparam = null, readparam = null;
1509 writeparam = jalview.ws.jws2.ParameterUtils
1511 pr.getArguments(lastserv.getRunnerConfig()),
1513 System.out.println("Testing param read :");
1514 List<Option> pset = jalview.ws.jws2.ParameterUtils
1515 .processParameters(writeparam,
1516 lastserv.getRunnerConfig(), " ");
1517 readparam = jalview.ws.jws2.ParameterUtils
1518 .writeParameterSet(pset, " ");
1519 Iterator<String> o = pr.getOptions().iterator(), s = writeparam
1520 .iterator(), t = readparam.iterator();
1521 boolean failed = false;
1522 while (s.hasNext() && t.hasNext())
1524 String on = o.next(), sn = s.next(), st = t.next();
1527 System.out.println("Original was " + on
1528 + " Phase 1 wrote " + sn + "\tPhase 2 wrote "
1535 System.out.println("Original parameters:\n"
1537 System.out.println("Wrote parameters in first set:\n"
1539 System.out.println("Wrote parameters in second set:\n"
1543 } catch (Exception e)
1545 e.printStackTrace();
1548 WsJobParameters pgui = new WsJobParameters(lastserv,
1549 new JabaPreset(lastserv, pr));
1550 JFrame jf = new JFrame("Parameters for "
1551 + lastserv.getActionText());
1552 JPanel cont = new JPanel();
1553 // jf.setPreferredSize(new Dimension(600, 800));
1556 final Thread thr = Thread.currentThread();
1557 jf.addWindowListener(new WindowListener()
1560 public void windowActivated(WindowEvent e)
1562 // TODO Auto-generated method stub
1566 public void windowClosed(WindowEvent e)
1570 public void windowClosing(WindowEvent e)
1576 public void windowDeactivated(WindowEvent e)
1578 // TODO Auto-generated method stub
1582 public void windowDeiconified(WindowEvent e)
1584 // TODO Auto-generated method stub
1588 public void windowIconified(WindowEvent e)
1590 // TODO Auto-generated method stub
1594 public void windowOpened(WindowEvent e)
1596 // TODO Auto-generated method stub
1601 jf.setVisible(true);
1602 boolean inter = false;
1607 Thread.sleep(10000);
1608 } catch (Exception e)
1621 public List<ArgumentI> getJobParams()
1623 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
1624 // recover options and parameters from GUI
1625 for (OptionBox opts : optSet.values())
1627 OptionI opt = opts.getOptionIfEnabled();
1633 for (ParamBox parambox : paramSet.values())
1635 ParameterI parm = parambox.getParameter();
1645 String lastParmSet = null;
1648 * Hashtable<String, Object[]> editedParams = new Hashtable<String,
1651 * store the given parameters in the user parameter set database.
1653 * @param storeSetName - lastParmSet
1655 * @param descr - setDescr.getText()
1657 * @param jobParams - getJobParams()
1659 * private void _storeUserPreset(String storeSetName, String descr,
1660 * List<ArgumentI> jobParams) { // this is a simple hash store. Object[] pset;
1661 * editedParams.put(storeSetName, pset = new Object[3]); pset[0] =
1662 * storeSetName; pset[1] = descr; pset[2] = jobParams; // writeParam("Saving "
1663 * + storeSetName + ": ", jobParams); }
1665 * private void writeParam(String nm, List<ArgumentI> params) { for (ArgumentI
1666 * p : params) { System.out.println(nm + ":" + System.identityHashCode(p) +
1667 * " Name: " + p.getName() + " Value: " + p.getDefaultValue()); } }
1669 * private Object[] _getUserPreset(String setName) { Object[] pset =
1670 * editedParams.get(setName); // if (pset != null) // writeParam("Retrieving "
1671 * + setName + ": ", (List<Argument>) pset[2]); return pset; }
1673 * * remove the given user preset from the preset stash
1677 * private void _deleteUserPreset(String setName) {
1678 * editedParams.remove(setName); }
1681 private void syncSetNamesWithStore()
1684 // remove any set names in the drop down menu that aren't either a reserved
1685 // setting, or a user defined or service preset.
1686 Vector items = new Vector();
1687 while (n < setName.getItemCount())
1689 String item = (String) setName.getItemAt(n);
1690 if (!item.equals(SVC_DEF) && !paramStore.presetExists(item))
1692 setName.removeItemAt(n);
1696 items.addElement(item);
1700 if (!items.contains(SVC_DEF))
1702 setName.addItem(SVC_DEF);
1704 for (WsParamSetI upn : paramStore.getPresets())
1706 if (!items.contains(upn.getName()))
1708 setName.addItem(upn.getName());
1714 * true if lastParmSet is a user preset
1716 boolean isUserPreset = false;
1718 private void reInitDialog(String nextPreset)
1720 settingDialog = true;
1721 // updateTable(null,null); // first reset to defaults
1722 WsParamSetI pset = null;
1723 if (nextPreset != null && nextPreset.length() > 0)
1725 pset = paramStore.getPreset(nextPreset);
1729 if (pset.isModifiable())
1731 isUserPreset = true;
1732 setDescr.setText(pset.getDescription());
1733 updateTable(null, pset.getArguments());
1734 lastParmSet = nextPreset;
1738 isUserPreset = false;
1739 setDescr.setText("");
1740 // must be a default preset from service
1741 updateTable(pset, null);
1742 lastParmSet = nextPreset;
1747 isUserPreset = false;
1749 setDescr.setText("");
1750 updateTable(null, null);
1751 lastParmSet = SVC_DEF;
1754 initArgSetModified();
1755 syncSetNamesWithStore();
1756 setName.setSelectedItem(lastParmSet);
1758 settingDialog = false;
1762 String curSetName = null;
1764 public void itemStateChanged(ItemEvent e)
1766 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1768 final String setname = (String) setName.getSelectedItem();
1769 System.out.println("Item state changed for " + setname
1770 + " (handling ? " + !settingDialog + ")");
1776 if (setname == null)
1780 javax.swing.SwingUtilities.invokeLater(new Runnable()
1784 doPreferenceComboStateChange(setname);
1790 private void doPreferenceComboStateChange(String setname)
1792 // user has selected a different item from combo-box
1795 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1796 if (lsetname.equals(setname))
1798 // setname was just edited - so ignore this event.
1801 settingDialog = true;
1802 System.out.println("Prompting to save " + lsetname);
1803 if (javax.swing.JOptionPane
1808 + "' is modifed, and your changes will be lost.\nReally change preset ?",
1809 "Warning: Unsaved Changes",
1810 javax.swing.JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
1812 // revert the combobox to the current item
1813 settingDialog = true;
1814 setName.setSelectedItem(lsetname);
1815 settingDialog = false;
1818 // System.out.println("Saving for " + lsetname);
1819 // _storeCurrentPreset(lsetname);
1823 settingDialog = true;
1824 reInitDialog(setname);
1825 settingDialog = false;
1829 private void _renameExistingPreset(String oldName, String curSetName2)
1831 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(),
1836 * store current settings as given name. You should then reset gui.
1838 * @param curSetName2
1840 private void _storeCurrentPreset(String curSetName2)
1842 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1845 private void _updatePreset(String lastParmSet2, String curname)
1847 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(),
1853 * last saved name for this user preset
1855 String lastSetName = null;
1858 * last saved value of the description text for this user preset
1860 String lastDescrText = null;
1862 public void actionPerformed(ActionEvent e)
1864 if (e.getSource() instanceof Component)
1866 Component src = (Component) e.getSource();
1867 if (src.getParent() == setName)
1869 // rename any existing records we know about for this set.
1870 String newname = (String) e.getActionCommand().trim();
1872 if (isServicePreset(newname))
1874 final String oldname = curSetName!=null ? curSetName : lastParmSet;
1875 final Component ourframe=this;
1877 setName.getEditor().setItem(oldname);
1878 settingDialog=false;
1879 javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {
1880 JOptionPane.showMessageDialog(ourframe,
1881 "Invalid name - preset already exists.", "Invalid name",
1882 JOptionPane.WARNING_MESSAGE);
1887 curSetName = newname;
1888 System.err.println("New name for user setting " + curSetName
1889 + " (was " + setName.getSelectedItem() + ")");
1890 if (curSetName.equals(setName.getSelectedItem()))
1894 if (curSetName != null)
1896 argSetModified(setName, true);
1904 private void checkDescrModified()
1911 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1912 : !setDescr.getText().equals(lastDescrText)));
1917 public void insertUpdate(DocumentEvent e)
1919 checkDescrModified();
1922 public void removeUpdate(DocumentEvent e)
1924 checkDescrModified();
1927 public void changedUpdate(DocumentEvent e)
1929 checkDescrModified();
1934 * @return null or the service preset selected by the user
1936 public WsParamSetI getPreset()
1938 if (isUserPreset || isModified()
1939 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
1945 return paramStore.getPreset(lastParmSet);