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.JButton;
37 import javax.swing.JCheckBox;
38 import javax.swing.JComboBox;
39 import javax.swing.JComponent;
40 import javax.swing.JDialog;
41 import javax.swing.JFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JList;
44 import javax.swing.JMenuItem;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JPopupMenu;
48 import javax.swing.JScrollPane;
49 import javax.swing.JSlider;
50 import javax.swing.JSplitPane;
51 import javax.swing.JTable;
52 import javax.swing.JTextArea;
53 import javax.swing.JTextField;
54 import javax.swing.ListSelectionModel;
55 import javax.swing.SwingConstants;
56 import javax.swing.SwingUtilities;
57 import javax.swing.border.TitledBorder;
58 import javax.swing.event.CellEditorListener;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
61 import javax.swing.event.DocumentEvent;
62 import javax.swing.event.DocumentListener;
63 import javax.swing.table.*;
65 import compbio.metadata.Argument;
66 import compbio.metadata.Option;
67 import compbio.metadata.Parameter;
68 import compbio.metadata.Preset;
69 import compbio.metadata.PresetManager;
70 import compbio.metadata.RunnerConfig;
71 import compbio.metadata.ValueConstrain;
72 import compbio.metadata.WrongParameterException;
73 import compbio.metadata.ValueConstrain.Type;
75 import jalview.util.jarInputStreamProvider;
76 import jalview.ws.jws2.JabaParamStore;
77 import jalview.ws.jws2.JabaPreset;
78 import jalview.ws.jws2.Jws2Discoverer;
79 import jalview.ws.jws2.ParameterUtils;
80 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
81 import jalview.ws.jws2.dm.JabaOption;
82 import jalview.ws.jws2.dm.JabaParameter;
83 import jalview.ws.params.ArgumentI;
84 import jalview.ws.params.OptionI;
85 import jalview.ws.params.ParamDatastoreI;
86 import jalview.ws.params.ParameterI;
87 import jalview.ws.params.ValueConstrainI;
88 import jalview.ws.params.WsParamSetI;
91 * job parameter editing/browsing dialog box. User can browse existing settings
92 * (user + presets + Defaults), and any changes to parameters creates a modified
93 * user parameter set. LOGIC: If the parameter set is modified, and its name is
94 * a valid, non-existant user parameter set, then a save button is shown. If the
95 * parameter set is modified and its name is a valid, extant user parameter set,
96 * then an update button is shown. If user parameter set's name is edited, and
97 * old name exists as a writable user parameter set, then rename button is
98 * shown. If current parameter set is associated with a user defined parameter
99 * set, then : if set is modifed, a 'revert' button is shown. if set is not
100 * modified, a 'delete' button is shown.
105 public class WsJobParameters extends JPanel implements ItemListener,
106 ActionListener, DocumentListener
108 private static final String SVC_DEF = "Defaults"; // this is the null
109 // parameter set as shown to
112 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
113 PARAM_CLOSEDHEIGHT = 80;
115 private static final int OPTSET_HEIGHT = 30;
117 JPanel SetNamePanel = new JPanel();
119 JPanel setDetails = new JPanel();
121 JSplitPane settingsPanel = new JSplitPane();
123 JSplitPane jobPanel = new JSplitPane();
125 JPanel jobOptions = new JPanel();
127 JScrollPane jobOptionsPane = new JScrollPane();
129 JPanel jobParameters = new JPanel();
131 JButton createpref = new JButton();
133 JButton deletepref = new JButton();
135 JButton revertpref = new JButton();
137 JButton updatepref = new JButton();
139 JButton startjob = new JButton();
141 JButton canceljob = new JButton();
143 JComboBox setName = new JComboBox();
145 JTextArea setDescr = new JTextArea();
147 JScrollPane paramPane = new JScrollPane();
149 JPanel paramList = new JPanel();
151 RunnerConfig serviceOptions;
153 ParamDatastoreI paramStore;
155 WsJobParameters(Jws2Instance service)
160 public WsJobParameters(Jws2Instance service, WsParamSetI preset)
162 this(null, service, preset, null);
168 * - if null, create new JFrame outside of desktop
172 public WsJobParameters(JFrame parent, Jws2Instance service, WsParamSetI preset,
173 List<Argument> jobArgset)
175 this(parent, null, service, preset, jobArgset);
186 public WsJobParameters(JFrame parent, ParamDatastoreI paramStorei,
187 Jws2Instance service, WsParamSetI preset, List<Argument> jobArgset)
191 this.paramStore = paramStorei;
192 if (paramStore == null)
194 paramStore = service.getParamStore();
196 this.service = service;
197 // argSetModified(false);
198 // populate parameter table
199 initForService(service, preset, jobArgset);
200 // display in new JFrame attached to parent.
206 JDialog frame = null;
209 * shows a modal dialog containing the parameters.
213 public boolean showRunDialog()
216 frame = new JDialog(Desktop.instance, true);
218 frame.setTitle("Edit parameters for " + service.getActionText());
219 Rectangle deskr = Desktop.instance.getBounds();
220 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
221 (int) (deskr.getCenterY() - 250), 480, 500));
222 frame.setContentPane(this);
223 // should recover defaults from user prefs.
225 settingsPanel.setDividerLocation(0.4);
226 jobPanel.setDividerLocation(0.3);
227 frame.setVisible(true);
236 protected JButton makeButton(String label, String tooltip,
237 ActionListener action)
239 JButton button = new JButton();
240 button.setText(label);
241 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
242 button.setForeground(Color.black);
243 button.setHorizontalAlignment(SwingConstants.CENTER);
244 button.setToolTipText(tooltip);
245 button.addActionListener(action);
249 private void jbInit()
251 updatepref = makeButton("Update", "Update the stored user preference",
255 public void actionPerformed(ActionEvent e)
257 update_actionPerformed(e);
260 deletepref = makeButton("Delete", "Delete the user preference",
264 public void actionPerformed(ActionEvent e)
266 delete_actionPerformed(e);
269 createpref = makeButton("Create", "Create a new preference",
273 public void actionPerformed(ActionEvent e)
275 create_actionPerformed(e);
278 revertpref = makeButton("Revert",
279 "Undo all changes to the current set", new ActionListener()
282 public void actionPerformed(ActionEvent e)
284 revert_actionPerformed(e);
287 startjob.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
288 startjob.setText("Start");
289 startjob.setToolTipText("Start Job");
290 startjob.addActionListener(new ActionListener()
292 public void actionPerformed(ActionEvent e)
294 startjob_actionPerformed(e);
297 canceljob.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
298 canceljob.setText("Cancel");
299 canceljob.setToolTipText("Cancel Job");
300 canceljob.addActionListener(new ActionListener()
302 public void actionPerformed(ActionEvent e)
304 canceljob_actionPerformed(e);
308 setDetails.setBorder(new TitledBorder("Details"));
309 setDetails.setLayout(new BorderLayout());
310 setDescr.setColumns(40);
311 setDescr.setWrapStyleWord(true);
312 setDescr.setLineWrap(true);
313 setDescr.setBackground(getBackground());
314 setDescr.setEditable(true);
315 setDescr.getDocument().addDocumentListener(this);
316 JScrollPane setDescrView = new JScrollPane();
317 // setDescrView.setPreferredSize(new Dimension(350, 200));
318 setDescrView.getViewport().setView(setDescr);
319 setName.setEditable(true);
320 setName.addItemListener(this);
321 setName.getEditor().addActionListener(this);
322 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
323 SetNamePanel.setLayout(new GridLayout(2, 1));
324 SetNamePanel.setMinimumSize(new Dimension(300, 40));
325 JLabel setNameLabel = new JLabel("Parameter set: ");
326 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
327 // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
328 setNameInfo.add(setNameLabel);
329 setNameInfo.add(setName);
330 // initial button visibility
331 updatepref.setVisible(false);
332 deletepref.setVisible(false);
333 revertpref.setVisible(false);
334 createpref.setVisible(false);
335 JPanel setsavebuts = new JPanel();
336 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
337 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
338 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
339 setsavebuts.add(deletepref);
340 setsavebuts.add(revertpref);
341 setsavebuts.add(createpref);
342 setsavebuts.add(updatepref);
343 setsavebuts.setSize(new Dimension(150, 20));
344 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
345 buttonArea.add(setsavebuts);
346 SetNamePanel.add(setNameInfo);
347 SetNamePanel.add(buttonArea);
348 setDetails.add(setDescrView, BorderLayout.CENTER);
349 // setDetails.setPreferredSize(new Dimension(360, 100));
350 jobParameters.setBorder(new TitledBorder("Parameters"));
351 jobParameters.setLayout(new BorderLayout());
352 paramPane.setPreferredSize(new Dimension(360, 300));
353 paramPane.getVerticalScrollBar().setUnitIncrement(20);
354 // paramPanel.setPreferredSize(new Dimension(360, 300));
355 // TODO: relayout buttons nicely
356 paramPane.getViewport().setView(paramList);
357 jobParameters.add(paramPane, BorderLayout.CENTER);
358 JPanel jobOptionsPanel = new JPanel();
359 jobOptionsPanel.setLayout(new BorderLayout());
360 jobOptionsPanel.setBorder(new TitledBorder("Options"));
361 jobOptionsPane.getViewport().setView(jobOptions);
362 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
363 settingsPanel.setLeftComponent(jobOptionsPanel);
364 settingsPanel.setRightComponent(jobParameters);
365 settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
367 setLayout(new BorderLayout());
368 // setPreferredSize(new Dimension(400, 600));
369 // setSize(new Dimension(400, 600));
370 jobPanel.setLeftComponent(setDetails);
371 jobPanel.setRightComponent(settingsPanel);
372 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
373 add(SetNamePanel, BorderLayout.NORTH);
374 add(jobPanel, BorderLayout.CENTER);
375 JPanel dialogpanel = new JPanel();
376 dialogpanel.add(startjob);
377 dialogpanel.add(canceljob);
378 add(dialogpanel, BorderLayout.SOUTH);
381 protected void revert_actionPerformed(ActionEvent e)
383 reInitDialog(lastParmSet);
387 protected void update_actionPerformed(ActionEvent e)
391 String curname = ((String) setName.getSelectedItem()).trim();
392 _updatePreset(lastParmSet, curname);
393 lastParmSet = curname;
395 initArgSetModified();
396 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()) : null;
474 // TODO: Recover window geometry prefs for this service
475 // jobPanel.setDividerLocation(proportionalLocation)
476 // settingsPanel.setDividerLocation(proportionalLocation)
477 Hashtable exnames = new Hashtable();
478 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
480 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
482 servicePresets = new Hashtable();
483 // Add the default entry - if not present already.
484 if (!exnames.contains(SVC_DEF))
486 setName.addItem(SVC_DEF);
487 exnames.put(SVC_DEF, SVC_DEF);
488 servicePresets.put(SVC_DEF, SVC_DEF);
490 String curname = (p == null ? "" : p.getName());
491 for (WsParamSetI pr : paramStore.getPresets())
493 if (!pr.isModifiable())
495 servicePresets.put(pr.getName(), "preset");
500 if (!exnames.contains(pr.getName()))
502 setName.addItem(pr.getName());
505 // TODO: if initial jobArgset matches a given user setting or preset then
506 // should recover setting accordingly
507 // updateTable(p, jobArgset);
510 reInitDialog(p.getName());
511 initArgSetModified();
515 if (jobArgset != null && jobArgset.size() > 0)
517 curSetName = "Supplied Settings";
518 updateTable(p, jobArgset);
526 settingDialog = false;
530 @SuppressWarnings("unchecked")
531 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
533 // populate table from default parameter set.
534 List<ArgumentI> args = paramStore.getServiceParameters();
536 // split to params and required arguments
538 for (ArgumentI myarg : args)
540 // Ideally, Argument would implement isRequired !
541 if (myarg instanceof ParameterI)
543 ParameterI parm = (ParameterI) myarg;
548 if (myarg instanceof OptionI)
550 OptionI opt = (OptionI) myarg;
551 addOption(opt).resetToDefault();
555 System.err.println("Ignoring unknown service argument type "
556 + myarg.getClass().getName());
560 args = null; // no more args to process.
564 isUserPreset = false;
565 // initialise setname
566 setName.setSelectedItem(lastSetName = p.getName());
567 setDescr.setText(lastDescrText = p.getDescription());
571 args = p.getArguments();
572 } catch (Exception e)
576 // TODO: check if args should be unselected prior to resetting using the
581 if (lastParmSet == null)
583 isUserPreset = false;
584 // first call - so create a dummy name
586 setName.setSelectedItem(lastSetName = SVC_DEF);
590 if (jobArgset != null)
592 argSetModified(jobArgset, true);
595 // get setargs from current object
598 for (ArgumentI arg : args)
600 if (arg instanceof ParameterI)
602 setParameter((ParameterI) arg);
606 if (arg instanceof OptionI)
608 // System.out.println("Setting option "
609 // + System.identityHashCode(arg) + ":" + arg.getName()
610 // + " with " + arg.getDefaultValue());
611 selectOption((OptionI) arg, arg.getDefaultValue());
618 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
620 jobOptions.setLayout(new GridLayout(optSet.size(), 1));
621 refreshParamLayout();
622 paramPane.validate();
626 private boolean isModified()
628 return modifiedElements.size() > 0;
631 private Hashtable modifiedElements = new Hashtable();
634 * reset gui and modification state settings
636 private void initArgSetModified()
639 modifiedElements.clear();
640 updateButtonDisplay();
643 private void updateButtonDisplay()
645 boolean _update = false, _create = false, _delete = false, _revert = false;
646 if (modifiedElements.size() > 0)
650 _update = isUserPreset; // can only update user presets
651 if (!isUserPreset || modifiedElements.containsKey(setName))
653 // name modified - can create new preset
661 // can still delete a user preset
662 _delete = isUserPreset;
664 createpref.setVisible(_create);
665 updatepref.setVisible(_update);
666 deletepref.setVisible(_delete);
667 revertpref.setVisible(_revert);
671 private void argSetModified(Object modifiedElement, boolean b)
679 modifiedElements.remove(modifiedElement);
683 if (b && modifiedElement == setName
684 && modifiedElements.contains(modifiedElement))
686 // HACK! prevents iteration on makeSetNameValid
689 modifiedElements.put(modifiedElement, modifiedElement);
691 // set mod status based on presence of elements in table
692 if (b && modifiedElements.size() > 0)
694 makeSetNameValid(!isUserPreset);
695 SetNamePanel.revalidate();
697 updateButtonDisplay();
700 private boolean isServicePreset(String selectedItem)
702 return selectedItem.equals(SVC_DEF)
703 || servicePresets.containsKey(selectedItem);
707 * check if the current set name is a valid set name for saving, if not, then
710 private void makeSetNameValid(boolean newuserset)
712 boolean stn = settingDialog;
713 boolean renamed = false;
714 settingDialog = true;
715 String nm = (String) setName.getSelectedItem();
716 // check if the name is reserved - if it is, rename it.
717 if (isServicePreset(nm))
726 while (paramStore.getPreset(tnm) != null)
728 tnm = nm + " (" + (++i) + ")";
737 boolean makeupdate = false;
738 // sync the gui with the preset database
739 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
741 String snm = (String) setName.getItemAt(i);
745 // setName.setSelectedIndex(i);
750 setName.addItem(curSetName = nm);
751 setName.setSelectedItem(curSetName);
755 settingDialog = false; // we need this name change to be registered.
756 argSetModified(setName, renamed);
761 private void addParameter(ParameterI arg)
763 ParamBox pb = paramSet.get(arg.getName());
766 pb = new ParamBox(this, arg);
767 paramSet.put(arg.getName(), pb);
771 // take the defaults from the parameter
772 pb.updateControls(arg);
775 private void setParameter(ParameterI arg)
777 ParamBox pb = paramSet.get(arg.getName());
784 pb.updateControls(arg);
789 private void selectOption(OptionI option, String string)
791 OptionBox cb = optSet.get(option.getName());
794 cb = addOption(option);
796 cb.enabled.setSelected(string != null); // initial state for an option.
799 if (option.getPossibleValues().contains(string))
801 cb.val.setSelectedItem(string);
805 throw new Error("Invalid value " + string + " for option " + option);
809 if (option.isRequired() && !cb.enabled.isSelected())
811 // TODO: indicate paramset is not valid.. option needs to be selected!
813 cb.setInitialValue();
816 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
818 public class ParamBox extends JPanel implements ChangeListener,
819 ActionListener, MouseListener
821 JButton showDesc = new JButton();
823 JTextArea string = new JTextArea();
825 JScrollPane descPanel = new JScrollPane();
827 JSlider slider = null;
829 JTextField valueField = null;
831 ValueConstrainI validator = null;
833 JPanel settingPanel = new JPanel();
835 JPanel controlPanel = new JPanel();
837 boolean integ = false;
839 boolean choice = false;
841 boolean descisvisible = false;
843 final WsJobParameters pmdialogbox;
847 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
849 pmdialogbox = pmlayout;
850 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
851 setBorder(new TitledBorder(parm.getName()));
853 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
854 showDesc.setText("+");
855 string.setFont(new Font("Verdana", Font.PLAIN, 11));
856 string.setBackground(getBackground());
857 // string.setSize(new Dimension(PARAM_WIDTH, 80));
858 string.setEditable(false);
859 descPanel.getViewport().setView(string);
860 // descPanel.setLocation(2,17);
861 descPanel.setVisible(false);
862 // string.setMinimumSize(new Dimension(140,80));
863 // string.setMaximumSize(new Dimension(280,80));
864 final ParamBox me = this;
865 finfo = parm.getFurtherDetails();
868 showDesc.setToolTipText("<html><p>Click to show brief description, and right click to open link for further information.</p></html>");
869 showDesc.addMouseListener(this);
873 showDesc.setToolTipText("<html><p>Click to show brief description.</p></html>");
875 showDesc.addActionListener(new ActionListener()
878 public void actionPerformed(ActionEvent e)
880 descisvisible = !descisvisible;
881 descPanel.setVisible(descisvisible);
882 me.setPreferredSize(new Dimension(PARAM_WIDTH,
883 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
885 pmlayout.refreshParamLayout();
888 string.setWrapStyleWord(true);
889 string.setLineWrap(true);
890 string.setColumns(32);
891 string.setText(parm.getDescription());
892 JPanel firstrow = new JPanel();
893 firstrow.setLayout(null);
894 controlPanel.setLayout(new BorderLayout());
895 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
896 PARAM_CLOSEDHEIGHT - 50));
897 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
898 firstrow.add(showDesc);
899 firstrow.add(controlPanel);
900 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
901 PARAM_CLOSEDHEIGHT - 30));
903 validator = parm.getValidValue();
905 if (validator != null)
907 integ = validator.getType() == Type.Integer;
911 if (parameter.getPossibleValues() != null)
916 updateControls(parm);
917 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
918 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
925 // reset the widget's initial value.
929 boolean adjusting = false;
931 ParameterI parameter;
935 public int getBoxHeight()
937 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
940 public void updateControls(ParameterI parm)
943 boolean init = (choicebox == null && valueField == null);
950 choicebox = new JComboBox();
951 choicebox.addActionListener(this);
952 controlPanel.add(choicebox, BorderLayout.CENTER);
956 slider = new JSlider();
957 slider.addChangeListener(this);
958 valueField = new JTextField();
959 valueField.addActionListener(this);
960 valueField.setPreferredSize(new Dimension(60, 25));
961 controlPanel.add(slider, BorderLayout.WEST);
962 controlPanel.add(valueField, BorderLayout.EAST);
973 List vals = parm.getPossibleValues();
974 for (Object val : vals)
976 choicebox.addItem(val);
980 if (parm.getDefaultValue() != null)
982 choicebox.setSelectedItem(parm.getDefaultValue());
987 valueField.setText(parm.getDefaultValue());
990 lastVal = updateSliderFromValueField();
996 public ParameterI getParameter()
998 ParameterI prm = parameter.copy();
1001 prm.setDefaultValue((String) choicebox.getSelectedItem());
1005 prm.setDefaultValue(valueField.getText());
1010 public Object updateSliderFromValueField()
1014 if (validator != null)
1021 valueField.setText(valueField.getText().trim());
1022 iVal = Integer.valueOf(valueField.getText());
1023 if (validator.getMin()!=null && validator.getMin().intValue()>iVal)
1025 iVal = validator.getMin().intValue();
1026 // TODO: provide visual indication that hard limit was reached for this parameter
1028 if (validator.getMax()!=null && validator.getMax().intValue()<iVal)
1030 iVal = validator.getMax().intValue();
1031 // TODO: provide visual indication that hard limit was reached for this parameter
1033 } catch (Exception e)
1037 if (validator.getMin() != null && validator.getMax() != null)
1039 slider.getModel().setRangeProperties(iVal, 1,
1040 validator.getMin().intValue(),
1041 validator.getMax().intValue(), true);
1045 slider.setVisible(false);
1055 fVal = Float.valueOf(valueField.getText());
1056 if (validator.getMin()!=null && validator.getMin().floatValue()>fVal)
1058 fVal = validator.getMin().floatValue();
1059 // TODO: provide visual indication that hard limit was reached for this parameter
1061 if (validator.getMax()!=null && validator.getMax().floatValue()<fVal)
1063 fVal = validator.getMax().floatValue();
1064 // TODO: provide visual indication that hard limit was reached for this parameter
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 Desktop.showUrl(finfo.toString());
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();
1193 URL linkImageURL = getClass().getResource("/images/link.gif");
1197 boolean hasLink = false;
1201 public OptionBox(OptionI opt)
1204 setLayout(new BorderLayout());
1205 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1206 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1207 enabled.setText("");
1208 enabled.setText(opt.getName());
1209 enabled.addActionListener(this);
1210 finfo = option.getFurtherDetails();
1214 // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
1215 enabled.setToolTipText("<html><p>" + opt.getDescription()
1216 + "</p><img src=\"" + linkImageURL + "\"/></html>");
1217 // optlabel.addMouseListener(this);
1218 enabled.addMouseListener(this);
1222 // optlabel.setToolTipText(opt.getDescription());
1223 enabled.setToolTipText(opt.getDescription());
1225 add(enabled, BorderLayout.NORTH);
1226 if (opt.getPossibleValues().size() > 1)
1228 setLayout(new GridLayout(1, 2));
1229 for (Object str : opt.getPossibleValues())
1231 val.addItem((String) str);
1233 val.setSelectedItem((String) opt.getDefaultValue());
1234 val.addActionListener(this);
1235 add(val, BorderLayout.SOUTH);
1237 // TODO: add actionListeners for popup (to open further info),
1238 // and to update list of parameters if an option is enabled
1239 // that takes a value.
1243 public void resetToDefault()
1245 enabled.setSelected(false);
1246 if (option.isRequired())
1248 // Apply default value
1249 selectOption(option, option.getDefaultValue());
1253 boolean initEnabled = false;
1255 String initVal = null;
1257 public void setInitialValue()
1259 initEnabled = enabled.isSelected();
1260 if (option.getPossibleValues() != null
1261 && option.getPossibleValues().size() > 1)
1263 initVal = (String) val.getSelectedItem();
1267 initVal = (initEnabled) ? option.getDefaultValue() : null;
1271 public OptionI getOptionIfEnabled()
1273 if (!enabled.isSelected())
1277 OptionI opt = option.copy();
1279 if (val.getSelectedItem() != null)
1281 opt.setDefaultValue((String) val.getSelectedItem());
1286 public void actionPerformed(ActionEvent e)
1288 if (e.getSource() != enabled)
1290 enabled.setSelected(true);
1295 private void checkIfModified()
1297 boolean notmod = (initEnabled == enabled.isSelected());
1298 if (enabled.isSelected())
1300 if (initVal != null)
1302 notmod &= initVal.equals(val.getSelectedItem());
1306 // compare against default service setting
1307 notmod &= option.getDefaultValue() == null
1308 || option.getDefaultValue().equals(val.getSelectedItem());
1313 notmod &= initVal == null;
1315 argSetModified(this, !notmod);
1318 public void mouseClicked(MouseEvent e)
1320 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1322 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1326 public void mousePressed(MouseEvent e)
1328 // TODO Auto-generated method stub
1332 public void mouseReleased(MouseEvent e)
1334 // TODO Auto-generated method stub
1338 public void mouseEntered(MouseEvent e)
1340 // TODO Auto-generated method stub
1344 public void mouseExited(MouseEvent e)
1346 // TODO Auto-generated method stub
1352 private OptionBox addOption(OptionI opt)
1354 OptionBox cb = optSet.get(opt.getName());
1357 cb = new OptionBox(opt);
1358 optSet.put(opt.getName(), cb);
1364 public static void showUrlPopUp(JComponent invoker, final String finfo,
1368 JPopupMenu mnu = new JPopupMenu();
1369 JMenuItem mitem = new JMenuItem("View " + finfo);
1370 mitem.addActionListener(new ActionListener()
1374 public void actionPerformed(ActionEvent e)
1376 Desktop.showUrl(finfo);
1381 mnu.show(invoker, x, y);
1384 protected void refreshParamLayout()
1386 FlowLayout fl = new FlowLayout();
1387 paramList.setLayout(fl);
1388 int s = 2 * fl.getVgap();
1389 for (ParamBox pbox : paramSet.values())
1391 s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
1393 paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
1398 * testing method - grab a service and parameter set and show the window
1402 public static void main(String[] args)
1404 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1407 if (args.length > 3)
1409 Vector<String> services = new Vector<String>();
1410 services.addElement(args[p++]);
1411 Jws2Discoverer.setServiceUrls(services);
1416 } catch (Exception e)
1418 System.err.println("Aborting. Problem discovering services.");
1419 e.printStackTrace();
1422 Jws2Discoverer.Jws2Instance lastserv = null;
1423 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1426 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1428 if (lastserv != null)
1430 List<Preset> prl = null;
1432 if (++p < args.length)
1434 PresetManager prman = lastserv.getPresets();
1437 pr = prman.getPresetByName(args[p]);
1440 // just grab the last preset.
1441 prl = prman.getPresets();
1447 PresetManager prman = lastserv.getPresets();
1450 prl = prman.getPresets();
1453 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1454 while (en != null && en.hasNext())
1460 en = prl.iterator();
1465 System.out.println("Testing opts dupes for "
1466 + lastserv.getUri() + " : "
1467 + lastserv.getActionText() + ":" + pr.getName());
1468 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1473 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1474 } catch (Exception e)
1476 System.err.println("Failed to copy " + o.getName());
1477 e.printStackTrace();
1480 System.err.println("Failed to copy " + o.getName());
1481 e.printStackTrace();
1486 System.out.println("Testing param dupes:");
1487 List<Parameter> rg = lastserv.getRunnerConfig()
1489 for (Parameter o : rg)
1493 Parameter cpy = jalview.ws.jws2.ParameterUtils
1495 } catch (Exception e)
1497 System.err.println("Failed to copy " + o.getName());
1498 e.printStackTrace();
1501 System.err.println("Failed to copy " + o.getName());
1502 e.printStackTrace();
1507 System.out.println("Testing param write:");
1508 List<String> writeparam = null, readparam = null;
1511 writeparam = jalview.ws.jws2.ParameterUtils
1513 pr.getArguments(lastserv.getRunnerConfig()),
1515 System.out.println("Testing param read :");
1516 List<Option> pset = jalview.ws.jws2.ParameterUtils
1517 .processParameters(writeparam,
1518 lastserv.getRunnerConfig(), " ");
1519 readparam = jalview.ws.jws2.ParameterUtils
1520 .writeParameterSet(pset, " ");
1521 Iterator<String> o = pr.getOptions().iterator(), s = writeparam
1522 .iterator(), t = readparam.iterator();
1523 boolean failed = false;
1524 while (s.hasNext() && t.hasNext())
1526 String on = o.next(), sn = s.next(), st = t.next();
1529 System.out.println("Original was " + on
1530 + " Phase 1 wrote " + sn + "\tPhase 2 wrote "
1537 System.out.println("Original parameters:\n"
1539 System.out.println("Wrote parameters in first set:\n"
1541 System.out.println("Wrote parameters in second set:\n"
1545 } catch (Exception e)
1547 e.printStackTrace();
1550 WsJobParameters pgui = new WsJobParameters(lastserv, new JabaPreset(lastserv, pr));
1551 JFrame jf = new JFrame("Parameters for "
1552 + lastserv.getActionText());
1553 JPanel cont = new JPanel();
1554 // jf.setPreferredSize(new Dimension(600, 800));
1557 final Thread thr = Thread.currentThread();
1558 jf.addWindowListener(new WindowListener()
1561 public void windowActivated(WindowEvent e)
1563 // TODO Auto-generated method stub
1567 public void windowClosed(WindowEvent e)
1571 public void windowClosing(WindowEvent e)
1577 public void windowDeactivated(WindowEvent e)
1579 // TODO Auto-generated method stub
1583 public void windowDeiconified(WindowEvent e)
1585 // TODO Auto-generated method stub
1589 public void windowIconified(WindowEvent e)
1591 // TODO Auto-generated method stub
1595 public void windowOpened(WindowEvent e)
1597 // TODO Auto-generated method stub
1602 jf.setVisible(true);
1603 boolean inter = false;
1608 Thread.sleep(10000);
1609 } catch (Exception e)
1622 public List<ArgumentI> getJobParams()
1624 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
1625 // recover options and parameters from GUI
1626 for (OptionBox opts : optSet.values())
1628 OptionI opt = opts.getOptionIfEnabled();
1634 for (ParamBox parambox : paramSet.values())
1636 ParameterI parm = parambox.getParameter();
1646 String lastParmSet = null;
1649 * Hashtable<String, Object[]> editedParams = new Hashtable<String,
1652 * store the given parameters in the user parameter set database.
1654 * @param storeSetName - lastParmSet
1656 * @param descr - setDescr.getText()
1658 * @param jobParams - getJobParams()
1660 * private void _storeUserPreset(String storeSetName, String descr,
1661 * List<ArgumentI> jobParams) { // this is a simple hash store. Object[] pset;
1662 * editedParams.put(storeSetName, pset = new Object[3]); pset[0] =
1663 * storeSetName; pset[1] = descr; pset[2] = jobParams; // writeParam("Saving "
1664 * + storeSetName + ": ", jobParams); }
1666 * private void writeParam(String nm, List<ArgumentI> params) { for (ArgumentI
1667 * p : params) { System.out.println(nm + ":" + System.identityHashCode(p) +
1668 * " Name: " + p.getName() + " Value: " + p.getDefaultValue()); } }
1670 * private Object[] _getUserPreset(String setName) { Object[] pset =
1671 * editedParams.get(setName); // if (pset != null) // writeParam("Retrieving "
1672 * + setName + ": ", (List<Argument>) pset[2]); return pset; }
1674 * * remove the given user preset from the preset stash
1678 * private void _deleteUserPreset(String setName) {
1679 * editedParams.remove(setName); }
1682 private void syncSetNamesWithStore()
1685 // remove any set names in the drop down menu that aren't either a reserved
1686 // setting, or a user defined or service preset.
1687 Vector items = new Vector();
1688 while (n < setName.getItemCount())
1690 String item = (String) setName.getItemAt(n);
1691 if (!item.equals(SVC_DEF) && !paramStore.presetExists(item))
1693 setName.removeItemAt(n);
1697 items.addElement(item);
1701 if (!items.contains(SVC_DEF))
1703 setName.addItem(SVC_DEF);
1705 for (WsParamSetI upn : paramStore.getPresets())
1707 if (!items.contains(upn.getName()))
1709 setName.addItem(upn.getName());
1715 * true if lastParmSet is a user preset
1717 boolean isUserPreset = false;
1719 private void reInitDialog(String nextPreset)
1721 settingDialog = true;
1722 // updateTable(null,null); // first reset to defaults
1723 WsParamSetI pset = null;
1724 if (nextPreset != null && nextPreset.length() > 0)
1726 pset = paramStore.getPreset(nextPreset);
1730 if (pset.isModifiable())
1732 isUserPreset = true;
1733 setDescr.setText(pset.getDescription());
1734 updateTable(null, pset.getArguments());
1735 lastParmSet = nextPreset;
1739 isUserPreset = false;
1740 setDescr.setText("");
1741 // must be a default preset from service
1742 updateTable(pset, null);
1743 lastParmSet = nextPreset;
1748 isUserPreset = false;
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 String setname = (String) setName.getSelectedItem();
1769 System.out.println("Item state changed for " + setname
1770 + " (handling ? " + !settingDialog + ")");
1776 if (setname == null)
1780 // user has selected a different item from combo-box
1783 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1784 if (lsetname.equals(setname))
1786 // setname was just edited - so ignore this event.
1789 settingDialog = true;
1790 System.out.println("Prompting to save " + lsetname);
1791 if (javax.swing.JOptionPane.showConfirmDialog(this,
1792 "Parameter set '" + lsetname + "' is modifed\nsave ?",
1793 "Save changes ?", javax.swing.JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
1795 System.out.println("Saving for " + lsetname);
1796 _storeCurrentPreset(lsetname);
1800 settingDialog = true;
1801 reInitDialog(setname);
1802 settingDialog = false;
1806 private void _renameExistingPreset(String oldName, String curSetName2)
1808 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(), getJobParams());
1812 * store current settings as given name. You should then reset gui.
1814 * @param curSetName2
1816 private void _storeCurrentPreset(String curSetName2)
1818 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1820 private void _updatePreset(String lastParmSet2, String curname)
1822 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(), getJobParams());
1827 * last saved name for this user preset
1829 String lastSetName = null;
1832 * last saved value of the description text for this user preset
1834 String lastDescrText = null;
1836 public void actionPerformed(ActionEvent e)
1838 if (e.getSource() instanceof Component)
1840 Component src = (Component) e.getSource();
1841 if (src.getParent() == setName)
1843 // rename any existing records we know about for this set.
1844 String newname = (String) e.getActionCommand().trim();
1846 if (isServicePreset(newname))
1848 JOptionPane.showConfirmDialog(this,
1849 "Invalid name - preset already exists.", "Invalid name",
1850 JOptionPane.OK_OPTION);
1852 setName.setSelectedItem(curSetName);
1855 curSetName = newname;
1856 System.err.println("New name for user setting " + curSetName
1857 + " (was " + setName.getSelectedItem() + ")");
1858 if (curSetName.equals(setName.getSelectedItem()))
1862 if (curSetName != null)
1864 argSetModified(setName, true);
1872 private void checkDescrModified()
1879 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1880 : !setDescr.getText().equals(lastDescrText)));
1885 public void insertUpdate(DocumentEvent e)
1887 checkDescrModified();
1890 public void removeUpdate(DocumentEvent e)
1892 checkDescrModified();
1895 public void changedUpdate(DocumentEvent e)
1897 checkDescrModified();
1902 * @return null or the service preset selected by the user
1904 public WsParamSetI getPreset()
1906 if (isUserPreset || isModified()
1907 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
1913 return paramStore.getPreset(lastParmSet);