2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.awt.BorderLayout;
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28 import java.awt.GridLayout;
29 import java.awt.Rectangle;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.ComponentEvent;
33 import java.awt.event.ComponentListener;
34 import java.awt.event.ContainerEvent;
35 import java.awt.event.ContainerListener;
36 import java.awt.event.ItemEvent;
37 import java.awt.event.ItemListener;
38 import java.awt.event.KeyEvent;
39 import java.awt.event.KeyListener;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
42 import java.awt.event.WindowEvent;
43 import java.awt.event.WindowListener;
44 import java.awt.event.WindowStateListener;
46 import java.util.ArrayList;
47 import java.util.EventObject;
48 import java.util.HashSet;
49 import java.util.Hashtable;
50 import java.util.Iterator;
51 import java.util.List;
53 import java.util.Vector;
55 import javax.swing.InputVerifier;
56 import javax.swing.JButton;
57 import javax.swing.JCheckBox;
58 import javax.swing.JComboBox;
59 import javax.swing.JComponent;
60 import javax.swing.JDialog;
61 import javax.swing.JFrame;
62 import javax.swing.JLabel;
63 import javax.swing.JList;
64 import javax.swing.JMenuItem;
65 import javax.swing.JOptionPane;
66 import javax.swing.JPanel;
67 import javax.swing.JPopupMenu;
68 import javax.swing.JScrollPane;
69 import javax.swing.JSlider;
70 import javax.swing.JSplitPane;
71 import javax.swing.JTabbedPane;
72 import javax.swing.JTable;
73 import javax.swing.JTextArea;
74 import javax.swing.JTextField;
75 import javax.swing.ListSelectionModel;
76 import javax.swing.SwingConstants;
77 import javax.swing.SwingUtilities;
78 import javax.swing.border.TitledBorder;
79 import javax.swing.event.CellEditorListener;
80 import javax.swing.event.ChangeEvent;
81 import javax.swing.event.ChangeListener;
82 import javax.swing.event.DocumentEvent;
83 import javax.swing.event.DocumentListener;
84 import javax.swing.table.*;
86 import compbio.metadata.Argument;
87 import compbio.metadata.Option;
88 import compbio.metadata.Parameter;
89 import compbio.metadata.Preset;
90 import compbio.metadata.PresetManager;
91 import compbio.metadata.RunnerConfig;
92 import compbio.metadata.ValueConstrain;
93 import compbio.metadata.WrongParameterException;
94 import compbio.metadata.ValueConstrain.Type;
96 import jalview.util.jarInputStreamProvider;
97 import jalview.ws.jws2.JabaParamStore;
98 import jalview.ws.jws2.JabaPreset;
99 import jalview.ws.jws2.Jws2Discoverer;
100 import jalview.ws.jws2.ParameterUtils;
101 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
102 import jalview.ws.jws2.dm.JabaOption;
103 import jalview.ws.jws2.dm.JabaParameter;
104 import jalview.ws.params.ArgumentI;
105 import jalview.ws.params.OptionI;
106 import jalview.ws.params.ParamDatastoreI;
107 import jalview.ws.params.ParameterI;
108 import jalview.ws.params.ValueConstrainI;
109 import jalview.ws.params.WsParamSetI;
112 * job parameter editing/browsing dialog box. User can browse existing settings
113 * (user + presets + Defaults), and any changes to parameters creates a modified
114 * user parameter set. LOGIC: If the parameter set is modified, and its name is
115 * a valid, non-existant user parameter set, then a save button is shown. If the
116 * parameter set is modified and its name is a valid, extant user parameter set,
117 * then an update button is shown. If user parameter set's name is edited, and
118 * old name exists as a writable user parameter set, then rename button is
119 * shown. If current parameter set is associated with a user defined parameter
120 * set, then : if set is modifed, a 'revert' button is shown. if set is not
121 * modified, a 'delete' button is shown.
126 public class WsJobParameters extends JPanel implements ItemListener,
127 ActionListener, DocumentListener
129 URL linkImageURL = getClass().getResource("/images/link.gif");
131 private static final String SVC_DEF = "Defaults"; // this is the null
132 // parameter set as shown to
135 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
136 PARAM_CLOSEDHEIGHT = 80;
138 private static final int OPTSET_HEIGHT = 30;
140 JPanel SetNamePanel = new JPanel();
142 JPanel setDetails = new JPanel();
144 JSplitPane settingsPanel = new JSplitPane();
146 JSplitPane jobPanel = new JSplitPane();
148 boolean split = true;
150 JTabbedPane tabpanels = new JTabbedPane();
152 JPanel jobOptions = new JPanel();
154 JScrollPane jobOptionsPane = new JScrollPane();
156 JPanel jobParameters = new JPanel();
158 JButton createpref = new JButton();
160 JButton deletepref = new JButton();
162 JButton revertpref = new JButton();
164 JButton updatepref = new JButton();
166 JButton startjob = new JButton();
168 JButton canceljob = new JButton();
170 JComboBox setName = new JComboBox();
172 JTextArea setDescr = new JTextArea();
174 JScrollPane paramPane = new JScrollPane();
176 JPanel paramList = new JPanel();
178 RunnerConfig serviceOptions;
180 ParamDatastoreI paramStore;
182 WsJobParameters(Jws2Instance service)
187 public WsJobParameters(Jws2Instance service, WsParamSetI preset)
189 this(null, service, preset, null);
195 * - if null, create new JFrame outside of desktop
199 public WsJobParameters(JFrame parent, Jws2Instance service,
200 WsParamSetI preset, List<Argument> jobArgset)
202 this(parent, null, service, preset, jobArgset);
213 public WsJobParameters(JFrame parent, ParamDatastoreI paramStorei,
214 Jws2Instance service, WsParamSetI preset, List<Argument> jobArgset)
218 this.paramStore = paramStorei;
219 if (paramStore == null)
221 paramStore = service.getParamStore();
223 this.service = service;
224 // argSetModified(false);
225 // populate parameter table
226 initForService(service, preset, jobArgset);
227 // display in new JFrame attached to parent.
233 JDialog frame = null;
236 * shows a modal dialog containing the parameters.
240 public boolean showRunDialog()
243 frame = new JDialog(Desktop.instance, true);
245 frame.setTitle("Edit parameters for " + service.getActionText());
246 Rectangle deskr = Desktop.instance.getBounds();
247 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
248 (int) (deskr.getCenterY() - 250), 480, 500));
249 frame.setContentPane(this);
250 // should recover defaults from user prefs.
251 // settingsPanel.setDividerLocation(0.4);
252 // jobPanel.setDividerLocation(0.5);
256 javax.swing.SwingUtilities.invokeLater(new Runnable()
260 settingsPanel.setDividerLocation(0.5);
261 jobPanel.setDividerLocation(setDescr.getLineCount() > 3 ? 0.35
268 tabpanels.setSelectedComponent(jobOptionsPane);
270 frame.setVisible(true);
279 private void jbInit()
281 updatepref = JvSwingUtils.makeButton("Update",
282 "Update this existing user parameter set.",
286 public void actionPerformed(ActionEvent e)
288 update_actionPerformed(e);
291 deletepref = JvSwingUtils.makeButton("Delete",
292 "Delete the currently selected user parameter set.",
296 public void actionPerformed(ActionEvent e)
298 delete_actionPerformed(e);
301 createpref = JvSwingUtils.makeButton("Create",
302 "Create a new parameter set with the current settings.",
306 public void actionPerformed(ActionEvent e)
308 create_actionPerformed(e);
311 revertpref = JvSwingUtils.makeButton("Revert",
312 "Undo all changes to the current parameter set",
316 public void actionPerformed(ActionEvent e)
318 revert_actionPerformed(e);
321 startjob = JvSwingUtils.makeButton("Start Job",
322 "Start Job with current settings.", new ActionListener()
324 public void actionPerformed(ActionEvent e)
326 startjob_actionPerformed(e);
329 canceljob = JvSwingUtils.makeButton("Cancel Job",
330 "Close this dialog and cancel job.", new ActionListener()
332 public void actionPerformed(ActionEvent e)
334 canceljob_actionPerformed(e);
338 setDetails.setBorder(new TitledBorder("Details"));
339 setDetails.setLayout(new BorderLayout());
340 setDescr.setColumns(40);
341 setDescr.setWrapStyleWord(true);
342 setDescr.setLineWrap(true);
343 setDescr.setBackground(getBackground());
344 setDescr.setEditable(true);
345 setDescr.getDocument().addDocumentListener(this);
346 setDescr.setToolTipText("Click to edit the notes for this parameter set.");
347 JScrollPane setDescrView = new JScrollPane();
348 // setDescrView.setPreferredSize(new Dimension(350, 200));
349 setDescrView.getViewport().setView(setDescr);
350 setName.setEditable(true);
351 setName.addItemListener(this);
352 setName.getEditor().addActionListener(this);
353 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
354 GridBagLayout gbl = new GridBagLayout();
355 SetNamePanel.setLayout(gbl);
356 SetNamePanel.setMinimumSize(new Dimension(300, 60));
357 JLabel setNameLabel = new JLabel("Current parameter set name :");
358 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
359 // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
360 setNameInfo.add(setNameLabel);
361 setNameInfo.add(setName);
362 // initial button visibility
363 updatepref.setVisible(false);
364 deletepref.setVisible(false);
365 revertpref.setVisible(false);
366 createpref.setVisible(false);
367 JPanel setsavebuts = new JPanel();
368 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
369 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
370 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
371 setsavebuts.add(deletepref);
372 setsavebuts.add(revertpref);
373 setsavebuts.add(createpref);
374 setsavebuts.add(updatepref);
375 setsavebuts.setSize(new Dimension(150, 20));
376 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
377 buttonArea.add(setsavebuts);
378 SetNamePanel.add(setNameInfo);
379 GridBagConstraints gbc = new GridBagConstraints();
381 gbl.setConstraints(setNameInfo, gbc);
382 SetNamePanel.add(buttonArea);
383 gbc = new GridBagConstraints();
387 gbl.setConstraints(buttonArea, gbc);
388 setDetails.add(setDescrView, BorderLayout.CENTER);
389 // setDetails.setPreferredSize(new Dimension(360, 100));
390 jobParameters.setBorder(new TitledBorder("Parameters"));
391 jobParameters.setLayout(new BorderLayout());
392 paramPane.setPreferredSize(new Dimension(360, 300));
393 paramPane.getVerticalScrollBar().setUnitIncrement(20);
394 // paramPanel.setPreferredSize(new Dimension(360, 300));
395 // TODO: relayout buttons nicely
396 paramPane.getViewport().setView(paramList);
397 jobParameters.add(paramPane, BorderLayout.CENTER);
398 JPanel jobOptionsPanel = new JPanel();
399 jobOptionsPanel.setLayout(new BorderLayout());
400 jobOptionsPanel.setBorder(new TitledBorder("Options"));
401 jobOptionsPane.getViewport().setView(jobOptions);
402 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
404 setLayout(new BorderLayout());
405 jobPanel.setLeftComponent(setDetails);
408 settingsPanel.setLeftComponent(jobOptionsPanel);
409 settingsPanel.setRightComponent(jobParameters);
410 settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
411 jobPanel.setRightComponent(settingsPanel);
415 jobPanel.setRightComponent(tabpanels);
416 jobOptionsPane.setName("Options");
417 tabpanels.add(jobOptionsPane);
418 paramPane.setName("Parameters");
419 tabpanels.add(paramPane);
421 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
422 add(SetNamePanel, BorderLayout.NORTH);
423 add(jobPanel, BorderLayout.CENTER);
424 JPanel dialogpanel = new JPanel();
425 dialogpanel.add(startjob);
426 dialogpanel.add(canceljob);
427 add(dialogpanel, BorderLayout.SOUTH);
430 protected void revert_actionPerformed(ActionEvent e)
432 reInitDialog(lastParmSet);
436 protected void update_actionPerformed(ActionEvent e)
440 String curname = ((String) setName.getSelectedItem()).trim();
441 _updatePreset(lastParmSet, curname);
442 lastParmSet = curname;
444 initArgSetModified();
445 syncSetNamesWithStore();
449 private void _deleteUserPreset(String lastParmSet2)
451 paramStore.deletePreset(lastParmSet2);
454 protected void delete_actionPerformed(ActionEvent e)
458 // delete current preset's saved entry
459 _deleteUserPreset(lastParmSet);
461 reInitDialog(null); // service default
464 protected void create_actionPerformed(ActionEvent e)
466 String curname = ((String) setName.getSelectedItem()).trim();
467 if (curname.length() > 0)
469 _storeCurrentPreset(curname);
470 lastParmSet = curname;
472 initArgSetModified();
476 // TODO: show warning
477 System.err.println("Invalid name. Not saved.");
481 protected void canceljob_actionPerformed(ActionEvent e)
486 frame.setVisible(false);
490 protected void startjob_actionPerformed(ActionEvent e)
495 frame.setVisible(false);
499 Jws2Instance service;
502 * list of service presets in the gui
504 Hashtable servicePresets = null;
507 * set if dialog is being set - so handlers will avoid spurious events
509 boolean settingDialog = false;
511 void initForService(Jws2Instance service, WsParamSetI jabap,
512 List<Argument> jabajobArgset)
514 WsParamSetI p = null;
515 List<ArgumentI> jobArgset = null;
516 settingDialog = true;
517 { // instantiate the abstract proxy for Jaba objects
518 jobArgset = jabajobArgset == null ? null : JabaParamStore
519 .getJwsArgsfromJaba(jabajobArgset);
520 p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
523 // TODO: Recover window geometry prefs for this service
524 // jobPanel.setDividerLocation(proportionalLocation)
525 // settingsPanel.setDividerLocation(proportionalLocation)
526 Hashtable exnames = new Hashtable();
527 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
529 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
531 servicePresets = new Hashtable();
532 // Add the default entry - if not present already.
533 if (!exnames.contains(SVC_DEF))
535 setName.addItem(SVC_DEF);
536 exnames.put(SVC_DEF, SVC_DEF);
537 servicePresets.put(SVC_DEF, SVC_DEF);
539 String curname = (p == null ? "" : p.getName());
540 for (WsParamSetI pr : paramStore.getPresets())
542 if (!pr.isModifiable())
544 servicePresets.put(pr.getName(), "preset");
549 if (!exnames.contains(pr.getName()))
551 setName.addItem(pr.getName());
554 // TODO: if initial jobArgset matches a given user setting or preset then
555 // should recover setting accordingly
556 // updateTable(p, jobArgset);
559 reInitDialog(p.getName());
560 initArgSetModified();
564 if (jobArgset != null && jobArgset.size() > 0)
566 curSetName = "Supplied Settings";
567 updateTable(p, jobArgset);
575 settingDialog = false;
579 @SuppressWarnings("unchecked")
580 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
582 // populate table from default parameter set.
583 List<ArgumentI> args = paramStore.getServiceParameters();
585 // split to params and required arguments
587 for (ArgumentI myarg : args)
589 // Ideally, Argument would implement isRequired !
590 if (myarg instanceof ParameterI)
592 ParameterI parm = (ParameterI) myarg;
597 if (myarg instanceof OptionI)
599 OptionI opt = (OptionI) myarg;
600 addOption(opt).resetToDefault();
604 System.err.println("Ignoring unknown service argument type "
605 + myarg.getClass().getName());
609 args = null; // no more args to process.
613 isUserPreset = false;
614 // initialise setname
615 setName.setSelectedItem(lastSetName = p.getName());
616 setDescr.setText(lastDescrText = p.getDescription());
620 args = p.getArguments();
621 } catch (Exception e)
625 // TODO: check if args should be unselected prior to resetting using the
630 if (lastParmSet == null)
632 isUserPreset = false;
633 // first call - so create a dummy name
635 setName.setSelectedItem(lastSetName = SVC_DEF);
639 if (jobArgset != null)
641 argSetModified(jobArgset, true);
644 // get setargs from current object
647 for (ArgumentI arg : args)
649 if (arg instanceof ParameterI)
651 setParameter((ParameterI) arg);
655 if (arg instanceof OptionI)
657 // System.out.println("Setting option "
658 // + System.identityHashCode(arg) + ":" + arg.getName()
659 // + " with " + arg.getDefaultValue());
660 selectOption((OptionI) arg, arg.getDefaultValue());
667 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
670 jobOptions.setLayout(fl=new FlowLayout(FlowLayout.LEFT));
672 refreshParamLayout();
673 paramPane.validate();
677 private boolean isModified()
679 return modifiedElements.size() > 0;
682 private Hashtable modifiedElements = new Hashtable();
685 * reset gui and modification state settings
687 private void initArgSetModified()
690 modifiedElements.clear();
691 updateButtonDisplay();
694 private void updateButtonDisplay()
696 boolean _update = false, _create = false, _delete = false, _revert = false;
697 if (modifiedElements.size() > 0)
701 _update = isUserPreset; // can only update user presets
702 if (!isUserPreset || modifiedElements.containsKey(setName))
704 // name modified - can create new preset
712 // can still delete a user preset
713 _delete = isUserPreset;
715 createpref.setVisible(_create);
716 updatepref.setVisible(_update);
717 deletepref.setVisible(_delete);
718 revertpref.setVisible(_revert);
722 private void argSetModified(Object modifiedElement, boolean b)
730 modifiedElements.remove(modifiedElement);
734 if (b && modifiedElement == setName
735 && modifiedElements.contains(modifiedElement))
737 // HACK! prevents iteration on makeSetNameValid
740 modifiedElements.put(modifiedElement, modifiedElement);
742 // set mod status based on presence of elements in table
743 if (b && modifiedElements.size() > 0)
745 makeSetNameValid(!isUserPreset);
746 SetNamePanel.revalidate();
748 updateButtonDisplay();
751 private boolean isServicePreset(String selectedItem)
753 return selectedItem.equals(SVC_DEF)
754 || servicePresets.containsKey(selectedItem);
758 * check if the current set name is a valid set name for saving, if not, then
761 private void makeSetNameValid(boolean newuserset)
763 boolean stn = settingDialog;
764 boolean renamed = false;
765 settingDialog = true;
766 String nm = (curSetName != null ? curSetName : (String) setName
768 // check if the name is reserved - if it is, rename it.
769 if (isServicePreset(nm))
778 while (paramStore.getPreset(tnm) != null)
780 tnm = nm + " (" + (++i) + ")";
789 boolean makeupdate = false;
790 // sync the gui with the preset database
791 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
793 String snm = (String) setName.getItemAt(i);
797 // setName.setSelectedIndex(i);
802 setName.addItem(curSetName = nm);
803 setName.setSelectedItem(curSetName);
807 settingDialog = false; // we need this name change to be registered.
808 argSetModified(setName, renamed);
813 private void addParameter(ParameterI arg)
815 ParamBox pb = paramSet.get(arg.getName());
818 pb = new ParamBox(this, arg);
819 paramSet.put(arg.getName(), pb);
823 // take the defaults from the parameter
824 pb.updateControls(arg);
827 private void setParameter(ParameterI arg)
829 ParamBox pb = paramSet.get(arg.getName());
836 pb.updateControls(arg);
841 private void selectOption(OptionI option, String string)
843 OptionBox cb = optSet.get(option.getName());
846 cb = addOption(option);
848 cb.enabled.setSelected(string != null); // initial state for an option.
851 if (option.getPossibleValues().contains(string))
853 cb.val.setSelectedItem(string);
857 throw new Error("Invalid value " + string + " for option " + option);
861 if (option.isRequired() && !cb.enabled.isSelected())
863 // TODO: indicate paramset is not valid.. option needs to be selected!
865 cb.setInitialValue();
868 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
870 public class ParamBox extends JPanel implements ChangeListener,
871 ActionListener, MouseListener
873 JButton showDesc = new JButton();
875 JTextArea string = new JTextArea();
877 JScrollPane descPanel = new JScrollPane();
879 JSlider slider = null;
881 JTextField valueField = null;
883 ValueConstrainI validator = null;
885 JPanel settingPanel = new JPanel();
887 JPanel controlPanel = new JPanel();
889 boolean integ = false;
891 boolean choice = false;
893 boolean descisvisible = false;
895 final WsJobParameters pmdialogbox;
899 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
901 pmdialogbox = pmlayout;
902 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
903 setBorder(new TitledBorder(parm.getName()));
905 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
906 showDesc.setText("+");
907 string.setFont(new Font("Verdana", Font.PLAIN, 11));
908 string.setBackground(getBackground());
909 // string.setSize(new Dimension(PARAM_WIDTH, 80));
910 string.setEditable(false);
911 descPanel.getViewport().setView(string);
912 // descPanel.setLocation(2,17);
913 descPanel.setVisible(false);
914 // string.setMinimumSize(new Dimension(140,80));
915 // string.setMaximumSize(new Dimension(280,80));
916 final ParamBox me = this;
917 finfo = parm.getFurtherDetails();
920 showDesc.setToolTipText("<html>"
922 .wrapTooltip("Click to show brief description<br><img src=\""
924 + "\"/> Right click for further information.")
926 showDesc.addMouseListener(this);
930 showDesc.setToolTipText("<html>"
932 .wrapTooltip("Click to show brief description.")
935 showDesc.addActionListener(new ActionListener()
938 public void actionPerformed(ActionEvent e)
940 descisvisible = !descisvisible;
941 descPanel.setVisible(descisvisible);
942 me.setPreferredSize(new Dimension(PARAM_WIDTH,
943 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
945 pmlayout.refreshParamLayout();
948 string.setWrapStyleWord(true);
949 string.setLineWrap(true);
950 string.setColumns(32);
951 string.setText(parm.getDescription());
952 JPanel firstrow = new JPanel();
953 firstrow.setLayout(null);
954 controlPanel.setLayout(new BorderLayout());
955 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
956 PARAM_CLOSEDHEIGHT - 50));
957 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
958 firstrow.add(showDesc);
959 firstrow.add(controlPanel);
960 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
961 PARAM_CLOSEDHEIGHT - 30));
963 validator = parm.getValidValue();
965 if (validator != null)
967 integ = validator.getType() == Type.Integer;
971 if (parameter.getPossibleValues() != null)
976 updateControls(parm);
977 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
978 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
985 // reset the widget's initial value.
989 boolean adjusting = false;
991 ParameterI parameter;
995 public int getBoxHeight()
997 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
1000 public void updateControls(ParameterI parm)
1003 boolean init = (choicebox == null && valueField == null);
1010 choicebox = new JComboBox();
1011 choicebox.addActionListener(this);
1012 controlPanel.add(choicebox, BorderLayout.CENTER);
1016 slider = new JSlider();
1017 slider.addChangeListener(this);
1018 valueField = new JTextField();
1019 valueField.addActionListener(this);
1020 valueField.setPreferredSize(new Dimension(60, 25));
1021 controlPanel.add(slider, BorderLayout.WEST);
1022 controlPanel.add(valueField, BorderLayout.EAST);
1033 List vals = parm.getPossibleValues();
1034 for (Object val : vals)
1036 choicebox.addItem(val);
1040 if (parm.getDefaultValue() != null)
1042 choicebox.setSelectedItem(parm.getDefaultValue());
1047 valueField.setText(parm.getDefaultValue());
1050 lastVal = updateSliderFromValueField();
1056 public ParameterI getParameter()
1058 ParameterI prm = parameter.copy();
1061 prm.setDefaultValue((String) choicebox.getSelectedItem());
1065 prm.setDefaultValue(valueField.getText());
1070 public Object updateSliderFromValueField()
1074 if (validator != null)
1081 valueField.setText(valueField.getText().trim());
1082 iVal = Integer.valueOf(valueField.getText());
1083 if (validator.getMin() != null
1084 && validator.getMin().intValue() > iVal)
1086 iVal = validator.getMin().intValue();
1087 // TODO: provide visual indication that hard limit was reached for
1090 if (validator.getMax() != null
1091 && validator.getMax().intValue() < iVal)
1093 iVal = validator.getMax().intValue();
1094 // TODO: provide visual indication that hard limit was reached for
1097 } catch (Exception e)
1101 if (validator.getMin() != null && validator.getMax() != null)
1103 slider.getModel().setRangeProperties(iVal, 1,
1104 validator.getMin().intValue(),
1105 validator.getMax().intValue(), true);
1109 slider.setVisible(false);
1119 fVal = Float.valueOf(valueField.getText());
1120 if (validator.getMin() != null
1121 && validator.getMin().floatValue() > fVal)
1123 fVal = validator.getMin().floatValue();
1124 // TODO: provide visual indication that hard limit was reached for
1127 if (validator.getMax() != null
1128 && validator.getMax().floatValue() < fVal)
1130 fVal = validator.getMax().floatValue();
1131 // TODO: provide visual indication that hard limit was reached for
1134 } catch (Exception e)
1138 if (validator.getMin() != null && validator.getMax() != null)
1140 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1141 (int) validator.getMin().floatValue() * 1000,
1142 (int) validator.getMax().floatValue() * 1000, true);
1146 slider.setVisible(false);
1156 slider.setVisible(false);
1158 { valueField.getText().trim() };
1163 { (String) choicebox.getSelectedItem() };
1169 public void stateChanged(ChangeEvent e)
1173 valueField.setText(""
1174 + ((integ) ? ("" + (int) slider.getValue())
1175 : ("" + (float) (slider.getValue() / 1000f))));
1181 public void actionPerformed(ActionEvent e)
1189 updateSliderFromValueField();
1194 private void checkIfModified()
1196 Object cstate = updateSliderFromValueField();
1197 boolean notmod = false;
1198 if (cstate.getClass() == lastVal.getClass())
1200 if (cstate instanceof int[])
1202 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1204 else if (cstate instanceof float[])
1206 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1208 else if (cstate instanceof String[])
1210 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1213 pmdialogbox.argSetModified(this, !notmod);
1216 public void mouseClicked(MouseEvent e)
1218 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1220 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1224 public void mousePressed(MouseEvent e)
1226 // TODO Auto-generated method stub
1230 public void mouseReleased(MouseEvent e)
1232 // TODO Auto-generated method stub
1236 public void mouseEntered(MouseEvent e)
1238 // TODO Auto-generated method stub
1242 public void mouseExited(MouseEvent e)
1244 // TODO Auto-generated method stub
1250 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1252 public class OptionBox extends JPanel implements MouseListener,
1255 JComboBox val = new JComboBox();
1257 JCheckBox enabled = new JCheckBox();
1259 JLabel optlabel = new JLabel();
1263 boolean hasLink = false;
1267 public OptionBox(OptionI opt)
1270 setLayout(new BorderLayout());
1271 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1272 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1273 enabled.setText("");
1274 enabled.setText(opt.getName());
1275 enabled.addActionListener(this);
1276 finfo = option.getFurtherDetails();
1280 // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
1281 enabled.setToolTipText("<html>"
1282 + JvSwingUtils.wrapTooltip(opt.getDescription()
1283 + "<br><img src=\"" + linkImageURL + "\"/>")
1285 // optlabel.addMouseListener(this);
1286 enabled.addMouseListener(this);
1290 // optlabel.setToolTipText(opt.getDescription());
1291 enabled.setToolTipText("<html>"
1292 + JvSwingUtils.wrapTooltip(opt.getDescription())
1295 add(enabled, BorderLayout.NORTH);
1296 if (opt.getPossibleValues().size() > 1)
1298 setLayout(new GridLayout(1, 2));
1299 for (Object str : opt.getPossibleValues())
1301 val.addItem((String) str);
1303 val.setSelectedItem((String) opt.getDefaultValue());
1304 val.addActionListener(this);
1305 add(val, BorderLayout.SOUTH);
1307 // TODO: add actionListeners for popup (to open further info),
1308 // and to update list of parameters if an option is enabled
1309 // that takes a value.
1313 public void resetToDefault()
1315 enabled.setSelected(false);
1316 if (option.isRequired())
1318 // Apply default value
1319 selectOption(option, option.getDefaultValue());
1323 boolean initEnabled = false;
1325 String initVal = null;
1327 public void setInitialValue()
1329 initEnabled = enabled.isSelected();
1330 if (option.getPossibleValues() != null
1331 && option.getPossibleValues().size() > 1)
1333 initVal = (String) val.getSelectedItem();
1337 initVal = (initEnabled) ? option.getDefaultValue() : null;
1341 public OptionI getOptionIfEnabled()
1343 if (!enabled.isSelected())
1347 OptionI opt = option.copy();
1349 if (val.getSelectedItem() != null)
1351 opt.setDefaultValue((String) val.getSelectedItem());
1356 public void actionPerformed(ActionEvent e)
1358 if (e.getSource() != enabled)
1360 enabled.setSelected(true);
1365 private void checkIfModified()
1367 boolean notmod = (initEnabled == enabled.isSelected());
1368 if (enabled.isSelected())
1370 if (initVal != null)
1372 notmod &= initVal.equals(val.getSelectedItem());
1376 // compare against default service setting
1377 notmod &= option.getDefaultValue() == null
1378 || option.getDefaultValue().equals(val.getSelectedItem());
1383 notmod &= initVal == null;
1385 argSetModified(this, !notmod);
1388 public void mouseClicked(MouseEvent e)
1390 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1392 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1396 public void mousePressed(MouseEvent e)
1398 // TODO Auto-generated method stub
1402 public void mouseReleased(MouseEvent e)
1404 // TODO Auto-generated method stub
1408 public void mouseEntered(MouseEvent e)
1410 // TODO Auto-generated method stub
1414 public void mouseExited(MouseEvent e)
1416 // TODO Auto-generated method stub
1422 private OptionBox addOption(OptionI opt)
1424 OptionBox cb = optSet.get(opt.getName());
1427 cb = new OptionBox(opt);
1428 optSet.put(opt.getName(), cb);
1434 public static void showUrlPopUp(JComponent invoker, final String finfo,
1438 JPopupMenu mnu = new JPopupMenu();
1439 JMenuItem mitem = new JMenuItem("View " + finfo);
1440 mitem.addActionListener(new ActionListener()
1444 public void actionPerformed(ActionEvent e)
1446 Desktop.showUrl(finfo);
1451 mnu.show(invoker, x, y);
1454 protected void refreshParamLayout()
1456 FlowLayout fl = new FlowLayout();
1457 paramList.setLayout(fl);
1458 int s = 2 * fl.getVgap();
1459 for (ParamBox pbox : paramSet.values())
1461 s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
1463 paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
1468 * testing method - grab a service and parameter set and show the window
1472 public static void main(String[] args)
1474 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1477 if (args.length > 3)
1479 Vector<String> services = new Vector<String>();
1480 services.addElement(args[p++]);
1481 Jws2Discoverer.setServiceUrls(services);
1486 } catch (Exception e)
1488 System.err.println("Aborting. Problem discovering services.");
1489 e.printStackTrace();
1492 Jws2Discoverer.Jws2Instance lastserv = null;
1493 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1496 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1498 if (lastserv != null)
1500 List<Preset> prl = null;
1502 if (++p < args.length)
1504 PresetManager prman = lastserv.getPresets();
1507 pr = prman.getPresetByName(args[p]);
1510 // just grab the last preset.
1511 prl = prman.getPresets();
1517 PresetManager prman = lastserv.getPresets();
1520 prl = prman.getPresets();
1523 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1524 while (en != null && en.hasNext())
1530 en = prl.iterator();
1535 System.out.println("Testing opts dupes for "
1536 + lastserv.getUri() + " : "
1537 + lastserv.getActionText() + ":" + pr.getName());
1538 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1543 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1544 } catch (Exception e)
1546 System.err.println("Failed to copy " + o.getName());
1547 e.printStackTrace();
1550 System.err.println("Failed to copy " + o.getName());
1551 e.printStackTrace();
1556 System.out.println("Testing param dupes:");
1557 List<Parameter> rg = lastserv.getRunnerConfig()
1559 for (Parameter o : rg)
1563 Parameter cpy = jalview.ws.jws2.ParameterUtils
1565 } catch (Exception e)
1567 System.err.println("Failed to copy " + o.getName());
1568 e.printStackTrace();
1571 System.err.println("Failed to copy " + o.getName());
1572 e.printStackTrace();
1577 System.out.println("Testing param write:");
1578 List<String> writeparam = null, readparam = null;
1581 writeparam = jalview.ws.jws2.ParameterUtils
1583 pr.getArguments(lastserv.getRunnerConfig()),
1585 System.out.println("Testing param read :");
1586 List<Option> pset = jalview.ws.jws2.ParameterUtils
1587 .processParameters(writeparam,
1588 lastserv.getRunnerConfig(), " ");
1589 readparam = jalview.ws.jws2.ParameterUtils
1590 .writeParameterSet(pset, " ");
1591 Iterator<String> o = pr.getOptions().iterator(), s = writeparam
1592 .iterator(), t = readparam.iterator();
1593 boolean failed = false;
1594 while (s.hasNext() && t.hasNext())
1596 String on = o.next(), sn = s.next(), st = t.next();
1599 System.out.println("Original was " + on
1600 + " Phase 1 wrote " + sn + "\tPhase 2 wrote "
1607 System.out.println("Original parameters:\n"
1609 System.out.println("Wrote parameters in first set:\n"
1611 System.out.println("Wrote parameters in second set:\n"
1615 } catch (Exception e)
1617 e.printStackTrace();
1620 WsJobParameters pgui = new WsJobParameters(lastserv,
1621 new JabaPreset(lastserv, pr));
1622 JFrame jf = new JFrame("Parameters for "
1623 + lastserv.getActionText());
1624 JPanel cont = new JPanel();
1625 // jf.setPreferredSize(new Dimension(600, 800));
1628 final Thread thr = Thread.currentThread();
1629 jf.addWindowListener(new WindowListener()
1632 public void windowActivated(WindowEvent e)
1634 // TODO Auto-generated method stub
1638 public void windowClosed(WindowEvent e)
1642 public void windowClosing(WindowEvent e)
1648 public void windowDeactivated(WindowEvent e)
1650 // TODO Auto-generated method stub
1654 public void windowDeiconified(WindowEvent e)
1656 // TODO Auto-generated method stub
1660 public void windowIconified(WindowEvent e)
1662 // TODO Auto-generated method stub
1666 public void windowOpened(WindowEvent e)
1668 // TODO Auto-generated method stub
1673 jf.setVisible(true);
1674 boolean inter = false;
1679 Thread.sleep(10000);
1680 } catch (Exception e)
1693 public List<ArgumentI> getJobParams()
1695 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
1696 // recover options and parameters from GUI
1697 for (OptionBox opts : optSet.values())
1699 OptionI opt = opts.getOptionIfEnabled();
1705 for (ParamBox parambox : paramSet.values())
1707 ParameterI parm = parambox.getParameter();
1717 String lastParmSet = null;
1720 * Hashtable<String, Object[]> editedParams = new Hashtable<String,
1723 * store the given parameters in the user parameter set database.
1725 * @param storeSetName - lastParmSet
1727 * @param descr - setDescr.getText()
1729 * @param jobParams - getJobParams()
1731 * private void _storeUserPreset(String storeSetName, String descr,
1732 * List<ArgumentI> jobParams) { // this is a simple hash store. Object[] pset;
1733 * editedParams.put(storeSetName, pset = new Object[3]); pset[0] =
1734 * storeSetName; pset[1] = descr; pset[2] = jobParams; // writeParam("Saving "
1735 * + storeSetName + ": ", jobParams); }
1737 * private void writeParam(String nm, List<ArgumentI> params) { for (ArgumentI
1738 * p : params) { System.out.println(nm + ":" + System.identityHashCode(p) +
1739 * " Name: " + p.getName() + " Value: " + p.getDefaultValue()); } }
1741 * private Object[] _getUserPreset(String setName) { Object[] pset =
1742 * editedParams.get(setName); // if (pset != null) // writeParam("Retrieving "
1743 * + setName + ": ", (List<Argument>) pset[2]); return pset; }
1745 * * remove the given user preset from the preset stash
1749 * private void _deleteUserPreset(String setName) {
1750 * editedParams.remove(setName); }
1753 private void syncSetNamesWithStore()
1756 // remove any set names in the drop down menu that aren't either a reserved
1757 // setting, or a user defined or service preset.
1758 Vector items = new Vector();
1759 while (n < setName.getItemCount())
1761 String item = (String) setName.getItemAt(n);
1762 if (!item.equals(SVC_DEF) && !paramStore.presetExists(item))
1764 setName.removeItemAt(n);
1768 items.addElement(item);
1772 if (!items.contains(SVC_DEF))
1774 setName.addItem(SVC_DEF);
1776 for (WsParamSetI upn : paramStore.getPresets())
1778 if (!items.contains(upn.getName()))
1780 setName.addItem(upn.getName());
1786 * true if lastParmSet is a user preset
1788 boolean isUserPreset = false;
1790 private void reInitDialog(String nextPreset)
1792 settingDialog = true;
1793 // updateTable(null,null); // first reset to defaults
1794 WsParamSetI pset = null;
1795 if (nextPreset != null && nextPreset.length() > 0)
1797 pset = paramStore.getPreset(nextPreset);
1801 if (pset.isModifiable())
1803 isUserPreset = true;
1804 setDescr.setText(pset.getDescription());
1805 updateTable(null, pset.getArguments());
1806 lastParmSet = nextPreset;
1810 isUserPreset = false;
1811 setDescr.setText("");
1812 // must be a default preset from service
1813 updateTable(pset, null);
1814 lastParmSet = nextPreset;
1819 isUserPreset = false;
1821 setDescr.setText("");
1822 updateTable(null, null);
1823 lastParmSet = SVC_DEF;
1826 initArgSetModified();
1827 syncSetNamesWithStore();
1828 setName.setSelectedItem(lastParmSet);
1830 settingDialog = false;
1834 String curSetName = null;
1836 public void itemStateChanged(ItemEvent e)
1838 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1840 final String setname = (String) setName.getSelectedItem();
1841 System.out.println("Item state changed for " + setname
1842 + " (handling ? " + !settingDialog + ")");
1848 if (setname == null)
1852 javax.swing.SwingUtilities.invokeLater(new Runnable()
1856 doPreferenceComboStateChange(setname);
1862 private void doPreferenceComboStateChange(String setname)
1864 // user has selected a different item from combo-box
1867 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1868 if (lsetname.equals(setname))
1870 // setname was just edited - so ignore this event.
1873 settingDialog = true;
1874 System.out.println("Prompting to save " + lsetname);
1875 if (javax.swing.JOptionPane
1880 + "' is modifed, and your changes will be lost.\nReally change preset ?",
1881 "Warning: Unsaved Changes",
1882 javax.swing.JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
1884 // revert the combobox to the current item
1885 settingDialog = true;
1886 setName.setSelectedItem(lsetname);
1887 settingDialog = false;
1890 // System.out.println("Saving for " + lsetname);
1891 // _storeCurrentPreset(lsetname);
1895 settingDialog = true;
1896 reInitDialog(setname);
1897 settingDialog = false;
1901 private void _renameExistingPreset(String oldName, String curSetName2)
1903 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(),
1908 * store current settings as given name. You should then reset gui.
1910 * @param curSetName2
1912 private void _storeCurrentPreset(String curSetName2)
1914 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1917 private void _updatePreset(String lastParmSet2, String curname)
1919 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(),
1925 * last saved name for this user preset
1927 String lastSetName = null;
1930 * last saved value of the description text for this user preset
1932 String lastDescrText = null;
1934 public void actionPerformed(ActionEvent e)
1936 if (e.getSource() instanceof Component)
1938 Component src = (Component) e.getSource();
1939 if (src.getParent() == setName)
1941 // rename any existing records we know about for this set.
1942 String newname = (String) e.getActionCommand().trim();
1944 if (isServicePreset(newname))
1946 final String oldname = curSetName != null ? curSetName
1948 final Component ourframe = this;
1949 settingDialog = true;
1950 setName.getEditor().setItem(oldname);
1951 settingDialog = false;
1952 javax.swing.SwingUtilities.invokeLater(new Runnable()
1956 JOptionPane.showMessageDialog(ourframe,
1957 "Invalid name - preset already exists.",
1958 "Invalid name", JOptionPane.WARNING_MESSAGE);
1964 curSetName = newname;
1965 System.err.println("New name for user setting " + curSetName
1966 + " (was " + setName.getSelectedItem() + ")");
1967 if (curSetName.equals(setName.getSelectedItem()))
1971 if (curSetName != null)
1973 argSetModified(setName, true);
1981 private void checkDescrModified()
1988 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1989 : !setDescr.getText().equals(lastDescrText)));
1994 public void insertUpdate(DocumentEvent e)
1996 checkDescrModified();
1999 public void removeUpdate(DocumentEvent e)
2001 checkDescrModified();
2004 public void changedUpdate(DocumentEvent e)
2006 checkDescrModified();
2011 * @return null or the service preset selected by the user
2013 public WsParamSetI getPreset()
2015 if (isUserPreset || isModified()
2016 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
2022 return paramStore.getPreset(lastParmSet);