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();
128 JTabbedPane tabpanels=new JTabbedPane();
129 JPanel jobOptions = new JPanel();
131 JScrollPane jobOptionsPane = new JScrollPane();
133 JPanel jobParameters = new JPanel();
135 JButton createpref = new JButton();
137 JButton deletepref = new JButton();
139 JButton revertpref = new JButton();
141 JButton updatepref = new JButton();
143 JButton startjob = new JButton();
145 JButton canceljob = new JButton();
147 JComboBox setName = new JComboBox();
149 JTextArea setDescr = new JTextArea();
151 JScrollPane paramPane = new JScrollPane();
153 JPanel paramList = new JPanel();
155 RunnerConfig serviceOptions;
157 ParamDatastoreI paramStore;
159 WsJobParameters(Jws2Instance service)
164 public WsJobParameters(Jws2Instance service, WsParamSetI preset)
166 this(null, service, preset, null);
172 * - if null, create new JFrame outside of desktop
176 public WsJobParameters(JFrame parent, Jws2Instance service,
177 WsParamSetI preset, List<Argument> jobArgset)
179 this(parent, null, service, preset, jobArgset);
190 public WsJobParameters(JFrame parent, ParamDatastoreI paramStorei,
191 Jws2Instance service, WsParamSetI preset, List<Argument> jobArgset)
195 this.paramStore = paramStorei;
196 if (paramStore == null)
198 paramStore = service.getParamStore();
200 this.service = service;
201 // argSetModified(false);
202 // populate parameter table
203 initForService(service, preset, jobArgset);
204 // display in new JFrame attached to parent.
210 JDialog frame = null;
213 * shows a modal dialog containing the parameters.
217 public boolean showRunDialog()
220 frame = new JDialog(Desktop.instance, true);
222 frame.setTitle("Edit parameters for " + service.getActionText());
223 Rectangle deskr = Desktop.instance.getBounds();
224 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
225 (int) (deskr.getCenterY() - 250), 480, 500));
226 frame.setContentPane(this);
227 // should recover defaults from user prefs.
228 //settingsPanel.setDividerLocation(0.4);
229 //jobPanel.setDividerLocation(0.5);
232 javax.swing.SwingUtilities.invokeLater(new Runnable() {
234 settingsPanel.setDividerLocation(0.5);
235 jobPanel.setDividerLocation(setDescr.getLineCount()>3 ? 0.35 : 0.16 );
239 tabpanels.setSelectedComponent(jobOptionsPane);
241 frame.setVisible(true);
251 private void jbInit()
253 updatepref = JvSwingUtils.makeButton("Update", "Update this existing user parameter set.",
257 public void actionPerformed(ActionEvent e)
259 update_actionPerformed(e);
262 deletepref = JvSwingUtils.makeButton("Delete", "Delete the currently selected user parameter set.",
266 public void actionPerformed(ActionEvent e)
268 delete_actionPerformed(e);
271 createpref = JvSwingUtils.makeButton("Create", "Create a new parameter set with the current settings.",
275 public void actionPerformed(ActionEvent e)
277 create_actionPerformed(e);
280 revertpref = JvSwingUtils.makeButton("Revert",
281 "Undo all changes to the current parameter set", new ActionListener()
284 public void actionPerformed(ActionEvent e)
286 revert_actionPerformed(e);
289 startjob = JvSwingUtils.makeButton("Start Job", "Start Job with current settings.",new ActionListener()
291 public void actionPerformed(ActionEvent e)
293 startjob_actionPerformed(e);
296 canceljob = JvSwingUtils.makeButton("Cancel Job", "Close this dialog and cancel job.", new ActionListener()
298 public void actionPerformed(ActionEvent e)
300 canceljob_actionPerformed(e);
304 setDetails.setBorder(new TitledBorder("Details"));
305 setDetails.setLayout(new BorderLayout());
306 setDescr.setColumns(40);
307 setDescr.setWrapStyleWord(true);
308 setDescr.setLineWrap(true);
309 setDescr.setBackground(getBackground());
310 setDescr.setEditable(true);
311 setDescr.getDocument().addDocumentListener(this);
312 setDescr.setToolTipText("Click to edit the notes for this parameter set.");
313 JScrollPane setDescrView = new JScrollPane();
314 // setDescrView.setPreferredSize(new Dimension(350, 200));
315 setDescrView.getViewport().setView(setDescr);
316 setName.setEditable(true);
317 setName.addItemListener(this);
318 setName.getEditor().addActionListener(this);
319 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
320 SetNamePanel.setLayout(new GridLayout(2, 1));
321 SetNamePanel.setMinimumSize(new Dimension(300, 40));
322 JLabel setNameLabel = new JLabel("Current parameter set name :");
323 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
324 // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
325 setNameInfo.add(setNameLabel);
326 setNameInfo.add(setName);
327 // initial button visibility
328 updatepref.setVisible(false);
329 deletepref.setVisible(false);
330 revertpref.setVisible(false);
331 createpref.setVisible(false);
332 JPanel setsavebuts = new JPanel();
333 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
334 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
335 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
336 setsavebuts.add(deletepref);
337 setsavebuts.add(revertpref);
338 setsavebuts.add(createpref);
339 setsavebuts.add(updatepref);
340 setsavebuts.setSize(new Dimension(150, 20));
341 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
342 buttonArea.add(setsavebuts);
343 SetNamePanel.add(setNameInfo);
344 SetNamePanel.add(buttonArea);
345 setDetails.add(setDescrView, BorderLayout.CENTER);
346 //setDetails.setPreferredSize(new Dimension(360, 100));
347 jobParameters.setBorder(new TitledBorder("Parameters"));
348 jobParameters.setLayout(new BorderLayout());
349 paramPane.setPreferredSize(new Dimension(360, 300));
350 paramPane.getVerticalScrollBar().setUnitIncrement(20);
351 // paramPanel.setPreferredSize(new Dimension(360, 300));
352 // TODO: relayout buttons nicely
353 paramPane.getViewport().setView(paramList);
354 jobParameters.add(paramPane, BorderLayout.CENTER);
355 JPanel jobOptionsPanel = new JPanel();
356 jobOptionsPanel.setLayout(new BorderLayout());
357 jobOptionsPanel.setBorder(new TitledBorder("Options"));
358 jobOptionsPane.getViewport().setView(jobOptions);
359 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
361 setLayout(new BorderLayout());
362 jobPanel.setLeftComponent(setDetails);
364 settingsPanel.setLeftComponent(jobOptionsPanel);
365 settingsPanel.setRightComponent(jobParameters);
366 settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
367 jobPanel.setRightComponent(settingsPanel);
369 jobPanel.setRightComponent(tabpanels);
370 jobOptionsPane.setName("Options");
371 tabpanels.add(jobOptionsPane);
372 paramPane.setName("Parameters");
373 tabpanels.add(paramPane);
375 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
376 add(SetNamePanel, BorderLayout.NORTH);
377 add(jobPanel, BorderLayout.CENTER);
378 JPanel dialogpanel = new JPanel();
379 dialogpanel.add(startjob);
380 dialogpanel.add(canceljob);
381 add(dialogpanel, BorderLayout.SOUTH);
384 protected void revert_actionPerformed(ActionEvent e)
386 reInitDialog(lastParmSet);
390 protected void update_actionPerformed(ActionEvent e)
394 String curname = ((String) setName.getSelectedItem()).trim();
395 _updatePreset(lastParmSet, curname);
396 lastParmSet = curname;
398 initArgSetModified();
399 syncSetNamesWithStore();
403 private void _deleteUserPreset(String lastParmSet2)
405 paramStore.deletePreset(lastParmSet2);
408 protected void delete_actionPerformed(ActionEvent e)
412 // delete current preset's saved entry
413 _deleteUserPreset(lastParmSet);
415 reInitDialog(null); // service default
418 protected void create_actionPerformed(ActionEvent e)
420 String curname = ((String) setName.getSelectedItem()).trim();
421 if (curname.length() > 0)
423 _storeCurrentPreset(curname);
424 lastParmSet = curname;
426 initArgSetModified();
430 // TODO: show warning
431 System.err.println("Invalid name. Not saved.");
435 protected void canceljob_actionPerformed(ActionEvent e)
440 frame.setVisible(false);
444 protected void startjob_actionPerformed(ActionEvent e)
449 frame.setVisible(false);
453 Jws2Instance service;
456 * list of service presets in the gui
458 Hashtable servicePresets = null;
461 * set if dialog is being set - so handlers will avoid spurious events
463 boolean settingDialog = false;
465 void initForService(Jws2Instance service, WsParamSetI jabap,
466 List<Argument> jabajobArgset)
468 WsParamSetI p = null;
469 List<ArgumentI> jobArgset = null;
470 settingDialog = true;
471 { // instantiate the abstract proxy for Jaba objects
472 jobArgset = jabajobArgset == null ? null : JabaParamStore
473 .getJwsArgsfromJaba(jabajobArgset);
474 p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
477 // TODO: Recover window geometry prefs for this service
478 // jobPanel.setDividerLocation(proportionalLocation)
479 // settingsPanel.setDividerLocation(proportionalLocation)
480 Hashtable exnames = new Hashtable();
481 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
483 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
485 servicePresets = new Hashtable();
486 // Add the default entry - if not present already.
487 if (!exnames.contains(SVC_DEF))
489 setName.addItem(SVC_DEF);
490 exnames.put(SVC_DEF, SVC_DEF);
491 servicePresets.put(SVC_DEF, SVC_DEF);
493 String curname = (p == null ? "" : p.getName());
494 for (WsParamSetI pr : paramStore.getPresets())
496 if (!pr.isModifiable())
498 servicePresets.put(pr.getName(), "preset");
503 if (!exnames.contains(pr.getName()))
505 setName.addItem(pr.getName());
508 // TODO: if initial jobArgset matches a given user setting or preset then
509 // should recover setting accordingly
510 // updateTable(p, jobArgset);
513 reInitDialog(p.getName());
514 initArgSetModified();
518 if (jobArgset != null && jobArgset.size() > 0)
520 curSetName = "Supplied Settings";
521 updateTable(p, jobArgset);
529 settingDialog = false;
533 @SuppressWarnings("unchecked")
534 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
536 // populate table from default parameter set.
537 List<ArgumentI> args = paramStore.getServiceParameters();
539 // split to params and required arguments
541 for (ArgumentI myarg : args)
543 // Ideally, Argument would implement isRequired !
544 if (myarg instanceof ParameterI)
546 ParameterI parm = (ParameterI) myarg;
551 if (myarg instanceof OptionI)
553 OptionI opt = (OptionI) myarg;
554 addOption(opt).resetToDefault();
558 System.err.println("Ignoring unknown service argument type "
559 + myarg.getClass().getName());
563 args = null; // no more args to process.
567 isUserPreset = false;
568 // initialise setname
569 setName.setSelectedItem(lastSetName = p.getName());
570 setDescr.setText(lastDescrText = p.getDescription());
574 args = p.getArguments();
575 } catch (Exception e)
579 // TODO: check if args should be unselected prior to resetting using the
584 if (lastParmSet == null)
586 isUserPreset = false;
587 // first call - so create a dummy name
589 setName.setSelectedItem(lastSetName = SVC_DEF);
593 if (jobArgset != null)
595 argSetModified(jobArgset, true);
598 // get setargs from current object
601 for (ArgumentI arg : args)
603 if (arg instanceof ParameterI)
605 setParameter((ParameterI) arg);
609 if (arg instanceof OptionI)
611 // System.out.println("Setting option "
612 // + System.identityHashCode(arg) + ":" + arg.getName()
613 // + " with " + arg.getDefaultValue());
614 selectOption((OptionI) arg, arg.getDefaultValue());
621 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
623 jobOptions.setLayout(new GridLayout(optSet.size(), 1));
624 refreshParamLayout();
625 paramPane.validate();
629 private boolean isModified()
631 return modifiedElements.size() > 0;
634 private Hashtable modifiedElements = new Hashtable();
637 * reset gui and modification state settings
639 private void initArgSetModified()
642 modifiedElements.clear();
643 updateButtonDisplay();
646 private void updateButtonDisplay()
648 boolean _update = false, _create = false, _delete = false, _revert = false;
649 if (modifiedElements.size() > 0)
653 _update = isUserPreset; // can only update user presets
654 if (!isUserPreset || modifiedElements.containsKey(setName))
656 // name modified - can create new preset
664 // can still delete a user preset
665 _delete = isUserPreset;
667 createpref.setVisible(_create);
668 updatepref.setVisible(_update);
669 deletepref.setVisible(_delete);
670 revertpref.setVisible(_revert);
674 private void argSetModified(Object modifiedElement, boolean b)
682 modifiedElements.remove(modifiedElement);
686 if (b && modifiedElement == setName
687 && modifiedElements.contains(modifiedElement))
689 // HACK! prevents iteration on makeSetNameValid
692 modifiedElements.put(modifiedElement, modifiedElement);
694 // set mod status based on presence of elements in table
695 if (b && modifiedElements.size() > 0)
697 makeSetNameValid(!isUserPreset);
698 SetNamePanel.revalidate();
700 updateButtonDisplay();
703 private boolean isServicePreset(String selectedItem)
705 return selectedItem.equals(SVC_DEF)
706 || servicePresets.containsKey(selectedItem);
710 * check if the current set name is a valid set name for saving, if not, then
713 private void makeSetNameValid(boolean newuserset)
715 boolean stn = settingDialog;
716 boolean renamed = false;
717 settingDialog = true;
718 String nm = (curSetName != null ? curSetName : (String) setName
720 // check if the name is reserved - if it is, rename it.
721 if (isServicePreset(nm))
730 while (paramStore.getPreset(tnm) != null)
732 tnm = nm + " (" + (++i) + ")";
741 boolean makeupdate = false;
742 // sync the gui with the preset database
743 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
745 String snm = (String) setName.getItemAt(i);
749 // setName.setSelectedIndex(i);
754 setName.addItem(curSetName = nm);
755 setName.setSelectedItem(curSetName);
759 settingDialog = false; // we need this name change to be registered.
760 argSetModified(setName, renamed);
765 private void addParameter(ParameterI arg)
767 ParamBox pb = paramSet.get(arg.getName());
770 pb = new ParamBox(this, arg);
771 paramSet.put(arg.getName(), pb);
775 // take the defaults from the parameter
776 pb.updateControls(arg);
779 private void setParameter(ParameterI arg)
781 ParamBox pb = paramSet.get(arg.getName());
788 pb.updateControls(arg);
793 private void selectOption(OptionI option, String string)
795 OptionBox cb = optSet.get(option.getName());
798 cb = addOption(option);
800 cb.enabled.setSelected(string != null); // initial state for an option.
803 if (option.getPossibleValues().contains(string))
805 cb.val.setSelectedItem(string);
809 throw new Error("Invalid value " + string + " for option " + option);
813 if (option.isRequired() && !cb.enabled.isSelected())
815 // TODO: indicate paramset is not valid.. option needs to be selected!
817 cb.setInitialValue();
820 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
822 public class ParamBox extends JPanel implements ChangeListener,
823 ActionListener, MouseListener
825 JButton showDesc = new JButton();
827 JTextArea string = new JTextArea();
829 JScrollPane descPanel = new JScrollPane();
831 JSlider slider = null;
833 JTextField valueField = null;
835 ValueConstrainI validator = null;
837 JPanel settingPanel = new JPanel();
839 JPanel controlPanel = new JPanel();
841 boolean integ = false;
843 boolean choice = false;
845 boolean descisvisible = false;
847 final WsJobParameters pmdialogbox;
851 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
853 pmdialogbox = pmlayout;
854 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
855 setBorder(new TitledBorder(parm.getName()));
857 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
858 showDesc.setText("+");
859 string.setFont(new Font("Verdana", Font.PLAIN, 11));
860 string.setBackground(getBackground());
861 // string.setSize(new Dimension(PARAM_WIDTH, 80));
862 string.setEditable(false);
863 descPanel.getViewport().setView(string);
864 // descPanel.setLocation(2,17);
865 descPanel.setVisible(false);
866 // string.setMinimumSize(new Dimension(140,80));
867 // string.setMaximumSize(new Dimension(280,80));
868 final ParamBox me = this;
869 finfo = parm.getFurtherDetails();
872 showDesc.setToolTipText("<html>"+JvSwingUtils.wrapTooltip("Click to show brief description<br><img src=\"" + linkImageURL + "\"/> Right click for further information.")+"</html>");
873 showDesc.addMouseListener(this);
877 showDesc.setToolTipText("<html>"+JvSwingUtils.wrapTooltip("Click to show brief description.")+"</html>");
879 showDesc.addActionListener(new ActionListener()
882 public void actionPerformed(ActionEvent e)
884 descisvisible = !descisvisible;
885 descPanel.setVisible(descisvisible);
886 me.setPreferredSize(new Dimension(PARAM_WIDTH,
887 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
889 pmlayout.refreshParamLayout();
892 string.setWrapStyleWord(true);
893 string.setLineWrap(true);
894 string.setColumns(32);
895 string.setText(parm.getDescription());
896 JPanel firstrow = new JPanel();
897 firstrow.setLayout(null);
898 controlPanel.setLayout(new BorderLayout());
899 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
900 PARAM_CLOSEDHEIGHT - 50));
901 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
902 firstrow.add(showDesc);
903 firstrow.add(controlPanel);
904 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
905 PARAM_CLOSEDHEIGHT - 30));
907 validator = parm.getValidValue();
909 if (validator != null)
911 integ = validator.getType() == Type.Integer;
915 if (parameter.getPossibleValues() != null)
920 updateControls(parm);
921 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
922 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
929 // reset the widget's initial value.
933 boolean adjusting = false;
935 ParameterI parameter;
939 public int getBoxHeight()
941 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
944 public void updateControls(ParameterI parm)
947 boolean init = (choicebox == null && valueField == null);
954 choicebox = new JComboBox();
955 choicebox.addActionListener(this);
956 controlPanel.add(choicebox, BorderLayout.CENTER);
960 slider = new JSlider();
961 slider.addChangeListener(this);
962 valueField = new JTextField();
963 valueField.addActionListener(this);
964 valueField.setPreferredSize(new Dimension(60, 25));
965 controlPanel.add(slider, BorderLayout.WEST);
966 controlPanel.add(valueField, BorderLayout.EAST);
977 List vals = parm.getPossibleValues();
978 for (Object val : vals)
980 choicebox.addItem(val);
984 if (parm.getDefaultValue() != null)
986 choicebox.setSelectedItem(parm.getDefaultValue());
991 valueField.setText(parm.getDefaultValue());
994 lastVal = updateSliderFromValueField();
1000 public ParameterI getParameter()
1002 ParameterI prm = parameter.copy();
1005 prm.setDefaultValue((String) choicebox.getSelectedItem());
1009 prm.setDefaultValue(valueField.getText());
1014 public Object updateSliderFromValueField()
1018 if (validator != null)
1025 valueField.setText(valueField.getText().trim());
1026 iVal = Integer.valueOf(valueField.getText());
1027 if (validator.getMin() != null
1028 && validator.getMin().intValue() > iVal)
1030 iVal = validator.getMin().intValue();
1031 // TODO: provide visual indication that hard limit was reached for
1034 if (validator.getMax() != null
1035 && validator.getMax().intValue() < iVal)
1037 iVal = validator.getMax().intValue();
1038 // TODO: provide visual indication that hard limit was reached for
1041 } catch (Exception e)
1045 if (validator.getMin() != null && validator.getMax() != null)
1047 slider.getModel().setRangeProperties(iVal, 1,
1048 validator.getMin().intValue(),
1049 validator.getMax().intValue(), true);
1053 slider.setVisible(false);
1063 fVal = Float.valueOf(valueField.getText());
1064 if (validator.getMin() != null
1065 && validator.getMin().floatValue() > fVal)
1067 fVal = validator.getMin().floatValue();
1068 // TODO: provide visual indication that hard limit was reached for
1071 if (validator.getMax() != null
1072 && validator.getMax().floatValue() < fVal)
1074 fVal = validator.getMax().floatValue();
1075 // TODO: provide visual indication that hard limit was reached for
1078 } catch (Exception e)
1082 if (validator.getMin() != null && validator.getMax() != null)
1084 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1085 (int) validator.getMin().floatValue() * 1000,
1086 (int) validator.getMax().floatValue() * 1000, true);
1090 slider.setVisible(false);
1100 slider.setVisible(false);
1102 { valueField.getText().trim() };
1107 { (String) choicebox.getSelectedItem() };
1113 public void stateChanged(ChangeEvent e)
1117 valueField.setText(""
1118 + ((integ) ? ("" + (int) slider.getValue())
1119 : ("" + (float) (slider.getValue() / 1000f))));
1125 public void actionPerformed(ActionEvent e)
1133 updateSliderFromValueField();
1138 private void checkIfModified()
1140 Object cstate = updateSliderFromValueField();
1141 boolean notmod = false;
1142 if (cstate.getClass() == lastVal.getClass())
1144 if (cstate instanceof int[])
1146 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1148 else if (cstate instanceof float[])
1150 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1152 else if (cstate instanceof String[])
1154 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1157 pmdialogbox.argSetModified(this, !notmod);
1160 public void mouseClicked(MouseEvent e)
1162 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1164 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1168 public void mousePressed(MouseEvent e)
1170 // TODO Auto-generated method stub
1174 public void mouseReleased(MouseEvent e)
1176 // TODO Auto-generated method stub
1180 public void mouseEntered(MouseEvent e)
1182 // TODO Auto-generated method stub
1186 public void mouseExited(MouseEvent e)
1188 // TODO Auto-generated method stub
1194 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1196 public class OptionBox extends JPanel implements MouseListener,
1199 JComboBox val = new JComboBox();
1201 JCheckBox enabled = new JCheckBox();
1203 JLabel optlabel = new JLabel();
1207 boolean hasLink = false;
1211 public OptionBox(OptionI opt)
1214 setLayout(new BorderLayout());
1215 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1216 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1217 enabled.setText("");
1218 enabled.setText(opt.getName());
1219 enabled.addActionListener(this);
1220 finfo = option.getFurtherDetails();
1224 // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
1225 enabled.setToolTipText("<html>" + JvSwingUtils.wrapTooltip(opt.getDescription()+"<br><img src=\"" + linkImageURL + "\"/>")
1227 // optlabel.addMouseListener(this);
1228 enabled.addMouseListener(this);
1232 // optlabel.setToolTipText(opt.getDescription());
1233 enabled.setToolTipText("<html>"+JvSwingUtils.wrapTooltip(opt.getDescription())+"</html>");
1235 add(enabled, BorderLayout.NORTH);
1236 if (opt.getPossibleValues().size() > 1)
1238 setLayout(new GridLayout(1, 2));
1239 for (Object str : opt.getPossibleValues())
1241 val.addItem((String) str);
1243 val.setSelectedItem((String) opt.getDefaultValue());
1244 val.addActionListener(this);
1245 add(val, BorderLayout.SOUTH);
1247 // TODO: add actionListeners for popup (to open further info),
1248 // and to update list of parameters if an option is enabled
1249 // that takes a value.
1253 public void resetToDefault()
1255 enabled.setSelected(false);
1256 if (option.isRequired())
1258 // Apply default value
1259 selectOption(option, option.getDefaultValue());
1263 boolean initEnabled = false;
1265 String initVal = null;
1267 public void setInitialValue()
1269 initEnabled = enabled.isSelected();
1270 if (option.getPossibleValues() != null
1271 && option.getPossibleValues().size() > 1)
1273 initVal = (String) val.getSelectedItem();
1277 initVal = (initEnabled) ? option.getDefaultValue() : null;
1281 public OptionI getOptionIfEnabled()
1283 if (!enabled.isSelected())
1287 OptionI opt = option.copy();
1289 if (val.getSelectedItem() != null)
1291 opt.setDefaultValue((String) val.getSelectedItem());
1296 public void actionPerformed(ActionEvent e)
1298 if (e.getSource() != enabled)
1300 enabled.setSelected(true);
1305 private void checkIfModified()
1307 boolean notmod = (initEnabled == enabled.isSelected());
1308 if (enabled.isSelected())
1310 if (initVal != null)
1312 notmod &= initVal.equals(val.getSelectedItem());
1316 // compare against default service setting
1317 notmod &= option.getDefaultValue() == null
1318 || option.getDefaultValue().equals(val.getSelectedItem());
1323 notmod &= initVal == null;
1325 argSetModified(this, !notmod);
1328 public void mouseClicked(MouseEvent e)
1330 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1332 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1336 public void mousePressed(MouseEvent e)
1338 // TODO Auto-generated method stub
1342 public void mouseReleased(MouseEvent e)
1344 // TODO Auto-generated method stub
1348 public void mouseEntered(MouseEvent e)
1350 // TODO Auto-generated method stub
1354 public void mouseExited(MouseEvent e)
1356 // TODO Auto-generated method stub
1362 private OptionBox addOption(OptionI opt)
1364 OptionBox cb = optSet.get(opt.getName());
1367 cb = new OptionBox(opt);
1368 optSet.put(opt.getName(), cb);
1374 public static void showUrlPopUp(JComponent invoker, final String finfo,
1378 JPopupMenu mnu = new JPopupMenu();
1379 JMenuItem mitem = new JMenuItem("View " + finfo);
1380 mitem.addActionListener(new ActionListener()
1384 public void actionPerformed(ActionEvent e)
1386 Desktop.showUrl(finfo);
1391 mnu.show(invoker, x, y);
1394 protected void refreshParamLayout()
1396 FlowLayout fl = new FlowLayout();
1397 paramList.setLayout(fl);
1398 int s = 2 * fl.getVgap();
1399 for (ParamBox pbox : paramSet.values())
1401 s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
1403 paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
1408 * testing method - grab a service and parameter set and show the window
1412 public static void main(String[] args)
1414 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1417 if (args.length > 3)
1419 Vector<String> services = new Vector<String>();
1420 services.addElement(args[p++]);
1421 Jws2Discoverer.setServiceUrls(services);
1426 } catch (Exception e)
1428 System.err.println("Aborting. Problem discovering services.");
1429 e.printStackTrace();
1432 Jws2Discoverer.Jws2Instance lastserv = null;
1433 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1436 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1438 if (lastserv != null)
1440 List<Preset> prl = null;
1442 if (++p < args.length)
1444 PresetManager prman = lastserv.getPresets();
1447 pr = prman.getPresetByName(args[p]);
1450 // just grab the last preset.
1451 prl = prman.getPresets();
1457 PresetManager prman = lastserv.getPresets();
1460 prl = prman.getPresets();
1463 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1464 while (en != null && en.hasNext())
1470 en = prl.iterator();
1475 System.out.println("Testing opts dupes for "
1476 + lastserv.getUri() + " : "
1477 + lastserv.getActionText() + ":" + pr.getName());
1478 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1483 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1484 } catch (Exception e)
1486 System.err.println("Failed to copy " + o.getName());
1487 e.printStackTrace();
1490 System.err.println("Failed to copy " + o.getName());
1491 e.printStackTrace();
1496 System.out.println("Testing param dupes:");
1497 List<Parameter> rg = lastserv.getRunnerConfig()
1499 for (Parameter o : rg)
1503 Parameter cpy = jalview.ws.jws2.ParameterUtils
1505 } catch (Exception e)
1507 System.err.println("Failed to copy " + o.getName());
1508 e.printStackTrace();
1511 System.err.println("Failed to copy " + o.getName());
1512 e.printStackTrace();
1517 System.out.println("Testing param write:");
1518 List<String> writeparam = null, readparam = null;
1521 writeparam = jalview.ws.jws2.ParameterUtils
1523 pr.getArguments(lastserv.getRunnerConfig()),
1525 System.out.println("Testing param read :");
1526 List<Option> pset = jalview.ws.jws2.ParameterUtils
1527 .processParameters(writeparam,
1528 lastserv.getRunnerConfig(), " ");
1529 readparam = jalview.ws.jws2.ParameterUtils
1530 .writeParameterSet(pset, " ");
1531 Iterator<String> o = pr.getOptions().iterator(), s = writeparam
1532 .iterator(), t = readparam.iterator();
1533 boolean failed = false;
1534 while (s.hasNext() && t.hasNext())
1536 String on = o.next(), sn = s.next(), st = t.next();
1539 System.out.println("Original was " + on
1540 + " Phase 1 wrote " + sn + "\tPhase 2 wrote "
1547 System.out.println("Original parameters:\n"
1549 System.out.println("Wrote parameters in first set:\n"
1551 System.out.println("Wrote parameters in second set:\n"
1555 } catch (Exception e)
1557 e.printStackTrace();
1560 WsJobParameters pgui = new WsJobParameters(lastserv,
1561 new JabaPreset(lastserv, pr));
1562 JFrame jf = new JFrame("Parameters for "
1563 + lastserv.getActionText());
1564 JPanel cont = new JPanel();
1565 // jf.setPreferredSize(new Dimension(600, 800));
1568 final Thread thr = Thread.currentThread();
1569 jf.addWindowListener(new WindowListener()
1572 public void windowActivated(WindowEvent e)
1574 // TODO Auto-generated method stub
1578 public void windowClosed(WindowEvent e)
1582 public void windowClosing(WindowEvent e)
1588 public void windowDeactivated(WindowEvent e)
1590 // TODO Auto-generated method stub
1594 public void windowDeiconified(WindowEvent e)
1596 // TODO Auto-generated method stub
1600 public void windowIconified(WindowEvent e)
1602 // TODO Auto-generated method stub
1606 public void windowOpened(WindowEvent e)
1608 // TODO Auto-generated method stub
1613 jf.setVisible(true);
1614 boolean inter = false;
1619 Thread.sleep(10000);
1620 } catch (Exception e)
1633 public List<ArgumentI> getJobParams()
1635 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
1636 // recover options and parameters from GUI
1637 for (OptionBox opts : optSet.values())
1639 OptionI opt = opts.getOptionIfEnabled();
1645 for (ParamBox parambox : paramSet.values())
1647 ParameterI parm = parambox.getParameter();
1657 String lastParmSet = null;
1660 * Hashtable<String, Object[]> editedParams = new Hashtable<String,
1663 * store the given parameters in the user parameter set database.
1665 * @param storeSetName - lastParmSet
1667 * @param descr - setDescr.getText()
1669 * @param jobParams - getJobParams()
1671 * private void _storeUserPreset(String storeSetName, String descr,
1672 * List<ArgumentI> jobParams) { // this is a simple hash store. Object[] pset;
1673 * editedParams.put(storeSetName, pset = new Object[3]); pset[0] =
1674 * storeSetName; pset[1] = descr; pset[2] = jobParams; // writeParam("Saving "
1675 * + storeSetName + ": ", jobParams); }
1677 * private void writeParam(String nm, List<ArgumentI> params) { for (ArgumentI
1678 * p : params) { System.out.println(nm + ":" + System.identityHashCode(p) +
1679 * " Name: " + p.getName() + " Value: " + p.getDefaultValue()); } }
1681 * private Object[] _getUserPreset(String setName) { Object[] pset =
1682 * editedParams.get(setName); // if (pset != null) // writeParam("Retrieving "
1683 * + setName + ": ", (List<Argument>) pset[2]); return pset; }
1685 * * remove the given user preset from the preset stash
1689 * private void _deleteUserPreset(String setName) {
1690 * editedParams.remove(setName); }
1693 private void syncSetNamesWithStore()
1696 // remove any set names in the drop down menu that aren't either a reserved
1697 // setting, or a user defined or service preset.
1698 Vector items = new Vector();
1699 while (n < setName.getItemCount())
1701 String item = (String) setName.getItemAt(n);
1702 if (!item.equals(SVC_DEF) && !paramStore.presetExists(item))
1704 setName.removeItemAt(n);
1708 items.addElement(item);
1712 if (!items.contains(SVC_DEF))
1714 setName.addItem(SVC_DEF);
1716 for (WsParamSetI upn : paramStore.getPresets())
1718 if (!items.contains(upn.getName()))
1720 setName.addItem(upn.getName());
1726 * true if lastParmSet is a user preset
1728 boolean isUserPreset = false;
1730 private void reInitDialog(String nextPreset)
1732 settingDialog = true;
1733 // updateTable(null,null); // first reset to defaults
1734 WsParamSetI pset = null;
1735 if (nextPreset != null && nextPreset.length() > 0)
1737 pset = paramStore.getPreset(nextPreset);
1741 if (pset.isModifiable())
1743 isUserPreset = true;
1744 setDescr.setText(pset.getDescription());
1745 updateTable(null, pset.getArguments());
1746 lastParmSet = nextPreset;
1750 isUserPreset = false;
1751 setDescr.setText("");
1752 // must be a default preset from service
1753 updateTable(pset, null);
1754 lastParmSet = nextPreset;
1759 isUserPreset = false;
1761 setDescr.setText("");
1762 updateTable(null, null);
1763 lastParmSet = SVC_DEF;
1766 initArgSetModified();
1767 syncSetNamesWithStore();
1768 setName.setSelectedItem(lastParmSet);
1770 settingDialog = false;
1774 String curSetName = null;
1776 public void itemStateChanged(ItemEvent e)
1778 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1780 final String setname = (String) setName.getSelectedItem();
1781 System.out.println("Item state changed for " + setname
1782 + " (handling ? " + !settingDialog + ")");
1788 if (setname == null)
1792 javax.swing.SwingUtilities.invokeLater(new Runnable()
1796 doPreferenceComboStateChange(setname);
1802 private void doPreferenceComboStateChange(String setname)
1804 // user has selected a different item from combo-box
1807 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1808 if (lsetname.equals(setname))
1810 // setname was just edited - so ignore this event.
1813 settingDialog = true;
1814 System.out.println("Prompting to save " + lsetname);
1815 if (javax.swing.JOptionPane
1820 + "' is modifed, and your changes will be lost.\nReally change preset ?",
1821 "Warning: Unsaved Changes",
1822 javax.swing.JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
1824 // revert the combobox to the current item
1825 settingDialog = true;
1826 setName.setSelectedItem(lsetname);
1827 settingDialog = false;
1830 // System.out.println("Saving for " + lsetname);
1831 // _storeCurrentPreset(lsetname);
1835 settingDialog = true;
1836 reInitDialog(setname);
1837 settingDialog = false;
1841 private void _renameExistingPreset(String oldName, String curSetName2)
1843 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(),
1848 * store current settings as given name. You should then reset gui.
1850 * @param curSetName2
1852 private void _storeCurrentPreset(String curSetName2)
1854 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1857 private void _updatePreset(String lastParmSet2, String curname)
1859 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(),
1865 * last saved name for this user preset
1867 String lastSetName = null;
1870 * last saved value of the description text for this user preset
1872 String lastDescrText = null;
1874 public void actionPerformed(ActionEvent e)
1876 if (e.getSource() instanceof Component)
1878 Component src = (Component) e.getSource();
1879 if (src.getParent() == setName)
1881 // rename any existing records we know about for this set.
1882 String newname = (String) e.getActionCommand().trim();
1884 if (isServicePreset(newname))
1886 final String oldname = curSetName!=null ? curSetName : lastParmSet;
1887 final Component ourframe=this;
1889 setName.getEditor().setItem(oldname);
1890 settingDialog=false;
1891 javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {
1892 JOptionPane.showMessageDialog(ourframe,
1893 "Invalid name - preset already exists.", "Invalid name",
1894 JOptionPane.WARNING_MESSAGE);
1899 curSetName = newname;
1900 System.err.println("New name for user setting " + curSetName
1901 + " (was " + setName.getSelectedItem() + ")");
1902 if (curSetName.equals(setName.getSelectedItem()))
1906 if (curSetName != null)
1908 argSetModified(setName, true);
1916 private void checkDescrModified()
1923 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1924 : !setDescr.getText().equals(lastDescrText)));
1929 public void insertUpdate(DocumentEvent e)
1931 checkDescrModified();
1934 public void removeUpdate(DocumentEvent e)
1936 checkDescrModified();
1939 public void changedUpdate(DocumentEvent e)
1941 checkDescrModified();
1946 * @return null or the service preset selected by the user
1948 public WsParamSetI getPreset()
1950 if (isUserPreset || isModified()
1951 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
1957 return paramStore.getPreset(lastParmSet);