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.JTable;
53 import javax.swing.JTextArea;
54 import javax.swing.JTextField;
55 import javax.swing.ListSelectionModel;
56 import javax.swing.SwingConstants;
57 import javax.swing.SwingUtilities;
58 import javax.swing.border.TitledBorder;
59 import javax.swing.event.CellEditorListener;
60 import javax.swing.event.ChangeEvent;
61 import javax.swing.event.ChangeListener;
62 import javax.swing.event.DocumentEvent;
63 import javax.swing.event.DocumentListener;
64 import javax.swing.table.*;
66 import compbio.metadata.Argument;
67 import compbio.metadata.Option;
68 import compbio.metadata.Parameter;
69 import compbio.metadata.Preset;
70 import compbio.metadata.PresetManager;
71 import compbio.metadata.RunnerConfig;
72 import compbio.metadata.ValueConstrain;
73 import compbio.metadata.WrongParameterException;
74 import compbio.metadata.ValueConstrain.Type;
76 import jalview.util.jarInputStreamProvider;
77 import jalview.ws.jws2.JabaParamStore;
78 import jalview.ws.jws2.JabaPreset;
79 import jalview.ws.jws2.Jws2Discoverer;
80 import jalview.ws.jws2.ParameterUtils;
81 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
82 import jalview.ws.jws2.dm.JabaOption;
83 import jalview.ws.jws2.dm.JabaParameter;
84 import jalview.ws.params.ArgumentI;
85 import jalview.ws.params.OptionI;
86 import jalview.ws.params.ParamDatastoreI;
87 import jalview.ws.params.ParameterI;
88 import jalview.ws.params.ValueConstrainI;
89 import jalview.ws.params.WsParamSetI;
92 * job parameter editing/browsing dialog box. User can browse existing settings
93 * (user + presets + Defaults), and any changes to parameters creates a modified
94 * user parameter set. LOGIC: If the parameter set is modified, and its name is
95 * a valid, non-existant user parameter set, then a save button is shown. If the
96 * parameter set is modified and its name is a valid, extant user parameter set,
97 * then an update button is shown. If user parameter set's name is edited, and
98 * old name exists as a writable user parameter set, then rename button is
99 * shown. If current parameter set is associated with a user defined parameter
100 * set, then : if set is modifed, a 'revert' button is shown. if set is not
101 * modified, a 'delete' button is shown.
106 public class WsJobParameters extends JPanel implements ItemListener,
107 ActionListener, DocumentListener
109 private static final String SVC_DEF = "Defaults"; // this is the null
110 // parameter set as shown to
113 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
114 PARAM_CLOSEDHEIGHT = 80;
116 private static final int OPTSET_HEIGHT = 30;
118 JPanel SetNamePanel = new JPanel();
120 JPanel setDetails = new JPanel();
122 JSplitPane settingsPanel = new JSplitPane();
124 JSplitPane jobPanel = new JSplitPane();
126 JPanel jobOptions = new JPanel();
128 JScrollPane jobOptionsPane = new JScrollPane();
130 JPanel jobParameters = new JPanel();
132 JButton createpref = new JButton();
134 JButton deletepref = new JButton();
136 JButton revertpref = new JButton();
138 JButton updatepref = new JButton();
140 JButton startjob = new JButton();
142 JButton canceljob = new JButton();
144 JComboBox setName = new JComboBox();
146 JTextArea setDescr = new JTextArea();
148 JScrollPane paramPane = new JScrollPane();
150 JPanel paramList = new JPanel();
152 RunnerConfig serviceOptions;
154 ParamDatastoreI paramStore;
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 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
222 (int) (deskr.getCenterY() - 250), 480, 500));
223 frame.setContentPane(this);
224 // should recover defaults from user prefs.
226 settingsPanel.setDividerLocation(0.4);
227 jobPanel.setDividerLocation(0.3);
228 frame.setVisible(true);
237 protected JButton makeButton(String label, String tooltip,
238 ActionListener action)
240 JButton button = new JButton();
241 button.setText(label);
242 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
243 button.setForeground(Color.black);
244 button.setHorizontalAlignment(SwingConstants.CENTER);
245 button.setToolTipText(tooltip);
246 button.addActionListener(action);
250 private void jbInit()
252 updatepref = makeButton("Update", "Update the stored user preference",
256 public void actionPerformed(ActionEvent e)
258 update_actionPerformed(e);
261 deletepref = makeButton("Delete", "Delete the user preference",
265 public void actionPerformed(ActionEvent e)
267 delete_actionPerformed(e);
270 createpref = makeButton("Create", "Create a new preference",
274 public void actionPerformed(ActionEvent e)
276 create_actionPerformed(e);
279 revertpref = makeButton("Revert",
280 "Undo all changes to the current set", new ActionListener()
283 public void actionPerformed(ActionEvent e)
285 revert_actionPerformed(e);
288 startjob.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
289 startjob.setText("Start");
290 startjob.setToolTipText("Start Job");
291 startjob.addActionListener(new ActionListener()
293 public void actionPerformed(ActionEvent e)
295 startjob_actionPerformed(e);
298 canceljob.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
299 canceljob.setText("Cancel");
300 canceljob.setToolTipText("Cancel Job");
301 canceljob.addActionListener(new ActionListener()
303 public void actionPerformed(ActionEvent e)
305 canceljob_actionPerformed(e);
309 setDetails.setBorder(new TitledBorder("Details"));
310 setDetails.setLayout(new BorderLayout());
311 setDescr.setColumns(40);
312 setDescr.setWrapStyleWord(true);
313 setDescr.setLineWrap(true);
314 setDescr.setBackground(getBackground());
315 setDescr.setEditable(true);
316 setDescr.getDocument().addDocumentListener(this);
317 JScrollPane setDescrView = new JScrollPane();
318 // setDescrView.setPreferredSize(new Dimension(350, 200));
319 setDescrView.getViewport().setView(setDescr);
320 setName.setEditable(true);
321 setName.addItemListener(this);
322 setName.getEditor().addActionListener(this);
323 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
324 SetNamePanel.setLayout(new GridLayout(2, 1));
325 SetNamePanel.setMinimumSize(new Dimension(300, 40));
326 JLabel setNameLabel = new JLabel("Parameter set: ");
327 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
328 // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
329 setNameInfo.add(setNameLabel);
330 setNameInfo.add(setName);
331 // initial button visibility
332 updatepref.setVisible(false);
333 deletepref.setVisible(false);
334 revertpref.setVisible(false);
335 createpref.setVisible(false);
336 JPanel setsavebuts = new JPanel();
337 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
338 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
339 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
340 setsavebuts.add(deletepref);
341 setsavebuts.add(revertpref);
342 setsavebuts.add(createpref);
343 setsavebuts.add(updatepref);
344 setsavebuts.setSize(new Dimension(150, 20));
345 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
346 buttonArea.add(setsavebuts);
347 SetNamePanel.add(setNameInfo);
348 SetNamePanel.add(buttonArea);
349 setDetails.add(setDescrView, BorderLayout.CENTER);
350 // setDetails.setPreferredSize(new Dimension(360, 100));
351 jobParameters.setBorder(new TitledBorder("Parameters"));
352 jobParameters.setLayout(new BorderLayout());
353 paramPane.setPreferredSize(new Dimension(360, 300));
354 paramPane.getVerticalScrollBar().setUnitIncrement(20);
355 // paramPanel.setPreferredSize(new Dimension(360, 300));
356 // TODO: relayout buttons nicely
357 paramPane.getViewport().setView(paramList);
358 jobParameters.add(paramPane, BorderLayout.CENTER);
359 JPanel jobOptionsPanel = new JPanel();
360 jobOptionsPanel.setLayout(new BorderLayout());
361 jobOptionsPanel.setBorder(new TitledBorder("Options"));
362 jobOptionsPane.getViewport().setView(jobOptions);
363 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
364 settingsPanel.setLeftComponent(jobOptionsPanel);
365 settingsPanel.setRightComponent(jobParameters);
366 settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
368 setLayout(new BorderLayout());
369 // setPreferredSize(new Dimension(400, 600));
370 // setSize(new Dimension(400, 600));
371 jobPanel.setLeftComponent(setDetails);
372 jobPanel.setRightComponent(settingsPanel);
373 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
374 add(SetNamePanel, BorderLayout.NORTH);
375 add(jobPanel, BorderLayout.CENTER);
376 JPanel dialogpanel = new JPanel();
377 dialogpanel.add(startjob);
378 dialogpanel.add(canceljob);
379 add(dialogpanel, BorderLayout.SOUTH);
382 protected void revert_actionPerformed(ActionEvent e)
384 reInitDialog(lastParmSet);
388 protected void update_actionPerformed(ActionEvent e)
392 String curname = ((String) setName.getSelectedItem()).trim();
393 _updatePreset(lastParmSet, curname);
394 lastParmSet = curname;
396 initArgSetModified();
397 syncSetNamesWithStore();
401 private void _deleteUserPreset(String lastParmSet2)
403 paramStore.deletePreset(lastParmSet2);
406 protected void delete_actionPerformed(ActionEvent e)
410 // delete current preset's saved entry
411 _deleteUserPreset(lastParmSet);
413 reInitDialog(null); // service default
416 protected void create_actionPerformed(ActionEvent e)
418 String curname = ((String) setName.getSelectedItem()).trim();
419 if (curname.length() > 0)
421 _storeCurrentPreset(curname);
422 lastParmSet = curname;
424 initArgSetModified();
428 // TODO: show warning
429 System.err.println("Invalid name. Not saved.");
433 protected void canceljob_actionPerformed(ActionEvent e)
438 frame.setVisible(false);
442 protected void startjob_actionPerformed(ActionEvent e)
447 frame.setVisible(false);
451 Jws2Instance service;
454 * list of service presets in the gui
456 Hashtable servicePresets = null;
459 * set if dialog is being set - so handlers will avoid spurious events
461 boolean settingDialog = false;
463 void initForService(Jws2Instance service, WsParamSetI jabap,
464 List<Argument> jabajobArgset)
466 WsParamSetI p = null;
467 List<ArgumentI> jobArgset = null;
468 settingDialog = true;
469 { // instantiate the abstract proxy for Jaba objects
470 jobArgset = jabajobArgset == null ? null : JabaParamStore
471 .getJwsArgsfromJaba(jabajobArgset);
472 p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
475 // TODO: Recover window geometry prefs for this service
476 // jobPanel.setDividerLocation(proportionalLocation)
477 // settingsPanel.setDividerLocation(proportionalLocation)
478 Hashtable exnames = new Hashtable();
479 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
481 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
483 servicePresets = new Hashtable();
484 // Add the default entry - if not present already.
485 if (!exnames.contains(SVC_DEF))
487 setName.addItem(SVC_DEF);
488 exnames.put(SVC_DEF, SVC_DEF);
489 servicePresets.put(SVC_DEF, SVC_DEF);
491 String curname = (p == null ? "" : p.getName());
492 for (WsParamSetI pr : paramStore.getPresets())
494 if (!pr.isModifiable())
496 servicePresets.put(pr.getName(), "preset");
501 if (!exnames.contains(pr.getName()))
503 setName.addItem(pr.getName());
506 // TODO: if initial jobArgset matches a given user setting or preset then
507 // should recover setting accordingly
508 // updateTable(p, jobArgset);
511 reInitDialog(p.getName());
512 initArgSetModified();
516 if (jobArgset != null && jobArgset.size() > 0)
518 curSetName = "Supplied Settings";
519 updateTable(p, jobArgset);
527 settingDialog = false;
531 @SuppressWarnings("unchecked")
532 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
534 // populate table from default parameter set.
535 List<ArgumentI> args = paramStore.getServiceParameters();
537 // split to params and required arguments
539 for (ArgumentI myarg : args)
541 // Ideally, Argument would implement isRequired !
542 if (myarg instanceof ParameterI)
544 ParameterI parm = (ParameterI) myarg;
549 if (myarg instanceof OptionI)
551 OptionI opt = (OptionI) myarg;
552 addOption(opt).resetToDefault();
556 System.err.println("Ignoring unknown service argument type "
557 + myarg.getClass().getName());
561 args = null; // no more args to process.
565 isUserPreset = false;
566 // initialise setname
567 setName.setSelectedItem(lastSetName = p.getName());
568 setDescr.setText(lastDescrText = p.getDescription());
572 args = p.getArguments();
573 } catch (Exception e)
577 // TODO: check if args should be unselected prior to resetting using the
582 if (lastParmSet == null)
584 isUserPreset = false;
585 // first call - so create a dummy name
587 setName.setSelectedItem(lastSetName = SVC_DEF);
591 if (jobArgset != null)
593 argSetModified(jobArgset, true);
596 // get setargs from current object
599 for (ArgumentI arg : args)
601 if (arg instanceof ParameterI)
603 setParameter((ParameterI) arg);
607 if (arg instanceof OptionI)
609 // System.out.println("Setting option "
610 // + System.identityHashCode(arg) + ":" + arg.getName()
611 // + " with " + arg.getDefaultValue());
612 selectOption((OptionI) arg, arg.getDefaultValue());
619 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
621 jobOptions.setLayout(new GridLayout(optSet.size(), 1));
622 refreshParamLayout();
623 paramPane.validate();
627 private boolean isModified()
629 return modifiedElements.size() > 0;
632 private Hashtable modifiedElements = new Hashtable();
635 * reset gui and modification state settings
637 private void initArgSetModified()
640 modifiedElements.clear();
641 updateButtonDisplay();
644 private void updateButtonDisplay()
646 boolean _update = false, _create = false, _delete = false, _revert = false;
647 if (modifiedElements.size() > 0)
651 _update = isUserPreset; // can only update user presets
652 if (!isUserPreset || modifiedElements.containsKey(setName))
654 // name modified - can create new preset
662 // can still delete a user preset
663 _delete = isUserPreset;
665 createpref.setVisible(_create);
666 updatepref.setVisible(_update);
667 deletepref.setVisible(_delete);
668 revertpref.setVisible(_revert);
672 private void argSetModified(Object modifiedElement, boolean b)
680 modifiedElements.remove(modifiedElement);
684 if (b && modifiedElement == setName
685 && modifiedElements.contains(modifiedElement))
687 // HACK! prevents iteration on makeSetNameValid
690 modifiedElements.put(modifiedElement, modifiedElement);
692 // set mod status based on presence of elements in table
693 if (b && modifiedElements.size() > 0)
695 makeSetNameValid(!isUserPreset);
696 SetNamePanel.revalidate();
698 updateButtonDisplay();
701 private boolean isServicePreset(String selectedItem)
703 return selectedItem.equals(SVC_DEF)
704 || servicePresets.containsKey(selectedItem);
708 * check if the current set name is a valid set name for saving, if not, then
711 private void makeSetNameValid(boolean newuserset)
713 boolean stn = settingDialog;
714 boolean renamed = false;
715 settingDialog = true;
716 String nm = (curSetName != null ? curSetName : (String) setName
718 // check if the name is reserved - if it is, rename it.
719 if (isServicePreset(nm))
728 while (paramStore.getPreset(tnm) != null)
730 tnm = nm + " (" + (++i) + ")";
739 boolean makeupdate = false;
740 // sync the gui with the preset database
741 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
743 String snm = (String) setName.getItemAt(i);
747 // setName.setSelectedIndex(i);
752 setName.addItem(curSetName = nm);
753 setName.setSelectedItem(curSetName);
757 settingDialog = false; // we need this name change to be registered.
758 argSetModified(setName, renamed);
763 private void addParameter(ParameterI arg)
765 ParamBox pb = paramSet.get(arg.getName());
768 pb = new ParamBox(this, arg);
769 paramSet.put(arg.getName(), pb);
773 // take the defaults from the parameter
774 pb.updateControls(arg);
777 private void setParameter(ParameterI arg)
779 ParamBox pb = paramSet.get(arg.getName());
786 pb.updateControls(arg);
791 private void selectOption(OptionI option, String string)
793 OptionBox cb = optSet.get(option.getName());
796 cb = addOption(option);
798 cb.enabled.setSelected(string != null); // initial state for an option.
801 if (option.getPossibleValues().contains(string))
803 cb.val.setSelectedItem(string);
807 throw new Error("Invalid value " + string + " for option " + option);
811 if (option.isRequired() && !cb.enabled.isSelected())
813 // TODO: indicate paramset is not valid.. option needs to be selected!
815 cb.setInitialValue();
818 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
820 public class ParamBox extends JPanel implements ChangeListener,
821 ActionListener, MouseListener
823 JButton showDesc = new JButton();
825 JTextArea string = new JTextArea();
827 JScrollPane descPanel = new JScrollPane();
829 JSlider slider = null;
831 JTextField valueField = null;
833 ValueConstrainI validator = null;
835 JPanel settingPanel = new JPanel();
837 JPanel controlPanel = new JPanel();
839 boolean integ = false;
841 boolean choice = false;
843 boolean descisvisible = false;
845 final WsJobParameters pmdialogbox;
849 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
851 pmdialogbox = pmlayout;
852 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
853 setBorder(new TitledBorder(parm.getName()));
855 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
856 showDesc.setText("+");
857 string.setFont(new Font("Verdana", Font.PLAIN, 11));
858 string.setBackground(getBackground());
859 // string.setSize(new Dimension(PARAM_WIDTH, 80));
860 string.setEditable(false);
861 descPanel.getViewport().setView(string);
862 // descPanel.setLocation(2,17);
863 descPanel.setVisible(false);
864 // string.setMinimumSize(new Dimension(140,80));
865 // string.setMaximumSize(new Dimension(280,80));
866 final ParamBox me = this;
867 finfo = parm.getFurtherDetails();
870 showDesc.setToolTipText("<html><p>Click to show brief description, and right click to open link for further information.</p></html>");
871 showDesc.addMouseListener(this);
875 showDesc.setToolTipText("<html><p>Click to show brief description.</p></html>");
877 showDesc.addActionListener(new ActionListener()
880 public void actionPerformed(ActionEvent e)
882 descisvisible = !descisvisible;
883 descPanel.setVisible(descisvisible);
884 me.setPreferredSize(new Dimension(PARAM_WIDTH,
885 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
887 pmlayout.refreshParamLayout();
890 string.setWrapStyleWord(true);
891 string.setLineWrap(true);
892 string.setColumns(32);
893 string.setText(parm.getDescription());
894 JPanel firstrow = new JPanel();
895 firstrow.setLayout(null);
896 controlPanel.setLayout(new BorderLayout());
897 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
898 PARAM_CLOSEDHEIGHT - 50));
899 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
900 firstrow.add(showDesc);
901 firstrow.add(controlPanel);
902 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
903 PARAM_CLOSEDHEIGHT - 30));
905 validator = parm.getValidValue();
907 if (validator != null)
909 integ = validator.getType() == Type.Integer;
913 if (parameter.getPossibleValues() != null)
918 updateControls(parm);
919 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
920 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
927 // reset the widget's initial value.
931 boolean adjusting = false;
933 ParameterI parameter;
937 public int getBoxHeight()
939 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
942 public void updateControls(ParameterI parm)
945 boolean init = (choicebox == null && valueField == null);
952 choicebox = new JComboBox();
953 choicebox.addActionListener(this);
954 controlPanel.add(choicebox, BorderLayout.CENTER);
958 slider = new JSlider();
959 slider.addChangeListener(this);
960 valueField = new JTextField();
961 valueField.addActionListener(this);
962 valueField.setPreferredSize(new Dimension(60, 25));
963 controlPanel.add(slider, BorderLayout.WEST);
964 controlPanel.add(valueField, BorderLayout.EAST);
975 List vals = parm.getPossibleValues();
976 for (Object val : vals)
978 choicebox.addItem(val);
982 if (parm.getDefaultValue() != null)
984 choicebox.setSelectedItem(parm.getDefaultValue());
989 valueField.setText(parm.getDefaultValue());
992 lastVal = updateSliderFromValueField();
998 public ParameterI getParameter()
1000 ParameterI prm = parameter.copy();
1003 prm.setDefaultValue((String) choicebox.getSelectedItem());
1007 prm.setDefaultValue(valueField.getText());
1012 public Object updateSliderFromValueField()
1016 if (validator != null)
1023 valueField.setText(valueField.getText().trim());
1024 iVal = Integer.valueOf(valueField.getText());
1025 if (validator.getMin() != null
1026 && validator.getMin().intValue() > iVal)
1028 iVal = validator.getMin().intValue();
1029 // TODO: provide visual indication that hard limit was reached for
1032 if (validator.getMax() != null
1033 && validator.getMax().intValue() < iVal)
1035 iVal = validator.getMax().intValue();
1036 // TODO: provide visual indication that hard limit was reached for
1039 } catch (Exception e)
1043 if (validator.getMin() != null && validator.getMax() != null)
1045 slider.getModel().setRangeProperties(iVal, 1,
1046 validator.getMin().intValue(),
1047 validator.getMax().intValue(), true);
1051 slider.setVisible(false);
1061 fVal = Float.valueOf(valueField.getText());
1062 if (validator.getMin() != null
1063 && validator.getMin().floatValue() > fVal)
1065 fVal = validator.getMin().floatValue();
1066 // TODO: provide visual indication that hard limit was reached for
1069 if (validator.getMax() != null
1070 && validator.getMax().floatValue() < fVal)
1072 fVal = validator.getMax().floatValue();
1073 // TODO: provide visual indication that hard limit was reached for
1076 } catch (Exception e)
1080 if (validator.getMin() != null && validator.getMax() != null)
1082 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1083 (int) validator.getMin().floatValue() * 1000,
1084 (int) validator.getMax().floatValue() * 1000, true);
1088 slider.setVisible(false);
1098 slider.setVisible(false);
1100 { valueField.getText().trim() };
1105 { (String) choicebox.getSelectedItem() };
1111 public void stateChanged(ChangeEvent e)
1115 valueField.setText(""
1116 + ((integ) ? ("" + (int) slider.getValue())
1117 : ("" + (float) (slider.getValue() / 1000f))));
1123 public void actionPerformed(ActionEvent e)
1131 updateSliderFromValueField();
1136 private void checkIfModified()
1138 Object cstate = updateSliderFromValueField();
1139 boolean notmod = false;
1140 if (cstate.getClass() == lastVal.getClass())
1142 if (cstate instanceof int[])
1144 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1146 else if (cstate instanceof float[])
1148 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1150 else if (cstate instanceof String[])
1152 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1155 pmdialogbox.argSetModified(this, !notmod);
1158 public void mouseClicked(MouseEvent e)
1160 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1162 Desktop.showUrl(finfo.toString());
1166 public void mousePressed(MouseEvent e)
1168 // TODO Auto-generated method stub
1172 public void mouseReleased(MouseEvent e)
1174 // TODO Auto-generated method stub
1178 public void mouseEntered(MouseEvent e)
1180 // TODO Auto-generated method stub
1184 public void mouseExited(MouseEvent e)
1186 // TODO Auto-generated method stub
1192 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1194 public class OptionBox extends JPanel implements MouseListener,
1197 JComboBox val = new JComboBox();
1199 JCheckBox enabled = new JCheckBox();
1201 JLabel optlabel = new JLabel();
1203 URL linkImageURL = getClass().getResource("/images/link.gif");
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><p>" + opt.getDescription()
1226 + "</p><img src=\"" + linkImageURL + "\"/></html>");
1227 // optlabel.addMouseListener(this);
1228 enabled.addMouseListener(this);
1232 // optlabel.setToolTipText(opt.getDescription());
1233 enabled.setToolTipText(opt.getDescription());
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 updateTable(null, null);
1762 lastParmSet = SVC_DEF;
1765 initArgSetModified();
1766 syncSetNamesWithStore();
1767 setName.setSelectedItem(lastParmSet);
1769 settingDialog = false;
1773 String curSetName = null;
1775 public void itemStateChanged(ItemEvent e)
1777 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1779 final String setname = (String) setName.getSelectedItem();
1780 System.out.println("Item state changed for " + setname
1781 + " (handling ? " + !settingDialog + ")");
1787 if (setname == null)
1791 javax.swing.SwingUtilities.invokeLater(new Runnable()
1795 doPreferenceComboStateChange(setname);
1801 private void doPreferenceComboStateChange(String setname)
1803 // user has selected a different item from combo-box
1806 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1807 if (lsetname.equals(setname))
1809 // setname was just edited - so ignore this event.
1812 settingDialog = true;
1813 System.out.println("Prompting to save " + lsetname);
1814 if (javax.swing.JOptionPane
1819 + "' is modifed, and your changes will be lost.\nReally change preset ?",
1820 "Warning: Unsaved Changes",
1821 javax.swing.JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
1823 // revert the combobox to the current item
1824 settingDialog = true;
1825 setName.setSelectedItem(lsetname);
1826 settingDialog = false;
1829 // System.out.println("Saving for " + lsetname);
1830 // _storeCurrentPreset(lsetname);
1834 settingDialog = true;
1835 reInitDialog(setname);
1836 settingDialog = false;
1840 private void _renameExistingPreset(String oldName, String curSetName2)
1842 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(),
1847 * store current settings as given name. You should then reset gui.
1849 * @param curSetName2
1851 private void _storeCurrentPreset(String curSetName2)
1853 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1856 private void _updatePreset(String lastParmSet2, String curname)
1858 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(),
1864 * last saved name for this user preset
1866 String lastSetName = null;
1869 * last saved value of the description text for this user preset
1871 String lastDescrText = null;
1873 public void actionPerformed(ActionEvent e)
1875 if (e.getSource() instanceof Component)
1877 Component src = (Component) e.getSource();
1878 if (src.getParent() == setName)
1880 // rename any existing records we know about for this set.
1881 String newname = (String) e.getActionCommand().trim();
1883 if (isServicePreset(newname))
1885 final String oldname = curSetName!=null ? curSetName : lastParmSet;
1886 final Component ourframe=this;
1888 setName.getEditor().setItem(oldname);
1889 settingDialog=false;
1890 javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {
1891 JOptionPane.showMessageDialog(ourframe,
1892 "Invalid name - preset already exists.", "Invalid name",
1893 JOptionPane.WARNING_MESSAGE);
1898 curSetName = newname;
1899 System.err.println("New name for user setting " + curSetName
1900 + " (was " + setName.getSelectedItem() + ")");
1901 if (curSetName.equals(setName.getSelectedItem()))
1905 if (curSetName != null)
1907 argSetModified(setName, true);
1915 private void checkDescrModified()
1922 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1923 : !setDescr.getText().equals(lastDescrText)));
1928 public void insertUpdate(DocumentEvent e)
1930 checkDescrModified();
1933 public void removeUpdate(DocumentEvent e)
1935 checkDescrModified();
1938 public void changedUpdate(DocumentEvent e)
1940 checkDescrModified();
1945 * @return null or the service preset selected by the user
1947 public WsParamSetI getPreset()
1949 if (isUserPreset || isModified()
1950 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
1956 return paramStore.getPreset(lastParmSet);