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.GridLayout;
27 import java.awt.Rectangle;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.ComponentEvent;
31 import java.awt.event.ComponentListener;
32 import java.awt.event.ContainerEvent;
33 import java.awt.event.ContainerListener;
34 import java.awt.event.ItemEvent;
35 import java.awt.event.ItemListener;
36 import java.awt.event.KeyEvent;
37 import java.awt.event.KeyListener;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
40 import java.awt.event.WindowEvent;
41 import java.awt.event.WindowListener;
42 import java.awt.event.WindowStateListener;
44 import java.util.ArrayList;
45 import java.util.EventObject;
46 import java.util.HashSet;
47 import java.util.Hashtable;
48 import java.util.Iterator;
49 import java.util.List;
51 import java.util.Vector;
53 import javax.swing.InputVerifier;
54 import javax.swing.JButton;
55 import javax.swing.JCheckBox;
56 import javax.swing.JComboBox;
57 import javax.swing.JComponent;
58 import javax.swing.JDialog;
59 import javax.swing.JFrame;
60 import javax.swing.JLabel;
61 import javax.swing.JList;
62 import javax.swing.JMenuItem;
63 import javax.swing.JOptionPane;
64 import javax.swing.JPanel;
65 import javax.swing.JPopupMenu;
66 import javax.swing.JScrollPane;
67 import javax.swing.JSlider;
68 import javax.swing.JSplitPane;
69 import javax.swing.JTabbedPane;
70 import javax.swing.JTable;
71 import javax.swing.JTextArea;
72 import javax.swing.JTextField;
73 import javax.swing.ListSelectionModel;
74 import javax.swing.SwingConstants;
75 import javax.swing.SwingUtilities;
76 import javax.swing.border.TitledBorder;
77 import javax.swing.event.CellEditorListener;
78 import javax.swing.event.ChangeEvent;
79 import javax.swing.event.ChangeListener;
80 import javax.swing.event.DocumentEvent;
81 import javax.swing.event.DocumentListener;
82 import javax.swing.table.*;
84 import compbio.metadata.Argument;
85 import compbio.metadata.Option;
86 import compbio.metadata.Parameter;
87 import compbio.metadata.Preset;
88 import compbio.metadata.PresetManager;
89 import compbio.metadata.RunnerConfig;
90 import compbio.metadata.ValueConstrain;
91 import compbio.metadata.WrongParameterException;
92 import compbio.metadata.ValueConstrain.Type;
94 import jalview.util.jarInputStreamProvider;
95 import jalview.ws.jws2.JabaParamStore;
96 import jalview.ws.jws2.JabaPreset;
97 import jalview.ws.jws2.Jws2Discoverer;
98 import jalview.ws.jws2.ParameterUtils;
99 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
100 import jalview.ws.jws2.dm.JabaOption;
101 import jalview.ws.jws2.dm.JabaParameter;
102 import jalview.ws.params.ArgumentI;
103 import jalview.ws.params.OptionI;
104 import jalview.ws.params.ParamDatastoreI;
105 import jalview.ws.params.ParameterI;
106 import jalview.ws.params.ValueConstrainI;
107 import jalview.ws.params.WsParamSetI;
110 * job parameter editing/browsing dialog box. User can browse existing settings
111 * (user + presets + Defaults), and any changes to parameters creates a modified
112 * user parameter set. LOGIC: If the parameter set is modified, and its name is
113 * a valid, non-existant user parameter set, then a save button is shown. If the
114 * parameter set is modified and its name is a valid, extant user parameter set,
115 * then an update button is shown. If user parameter set's name is edited, and
116 * old name exists as a writable user parameter set, then rename button is
117 * shown. If current parameter set is associated with a user defined parameter
118 * set, then : if set is modifed, a 'revert' button is shown. if set is not
119 * modified, a 'delete' button is shown.
124 public class WsJobParameters extends JPanel implements ItemListener,
125 ActionListener, DocumentListener
127 URL linkImageURL = getClass().getResource("/images/link.gif");
128 private static final String SVC_DEF = "Defaults"; // this is the null
129 // parameter set as shown to
132 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
133 PARAM_CLOSEDHEIGHT = 80;
135 private static final int OPTSET_HEIGHT = 30;
137 JPanel SetNamePanel = new JPanel();
139 JPanel setDetails = new JPanel();
141 JSplitPane settingsPanel = new JSplitPane();
143 JSplitPane jobPanel = new JSplitPane();
145 JTabbedPane tabpanels=new JTabbedPane();
146 JPanel jobOptions = new JPanel();
148 JScrollPane jobOptionsPane = new JScrollPane();
150 JPanel jobParameters = new JPanel();
152 JButton createpref = new JButton();
154 JButton deletepref = new JButton();
156 JButton revertpref = new JButton();
158 JButton updatepref = new JButton();
160 JButton startjob = new JButton();
162 JButton canceljob = new JButton();
164 JComboBox setName = new JComboBox();
166 JTextArea setDescr = new JTextArea();
168 JScrollPane paramPane = new JScrollPane();
170 JPanel paramList = new JPanel();
172 RunnerConfig serviceOptions;
174 ParamDatastoreI paramStore;
176 WsJobParameters(Jws2Instance service)
181 public WsJobParameters(Jws2Instance service, WsParamSetI preset)
183 this(null, service, preset, null);
189 * - if null, create new JFrame outside of desktop
193 public WsJobParameters(JFrame parent, Jws2Instance service,
194 WsParamSetI preset, List<Argument> jobArgset)
196 this(parent, null, service, preset, jobArgset);
207 public WsJobParameters(JFrame parent, ParamDatastoreI paramStorei,
208 Jws2Instance service, WsParamSetI preset, List<Argument> jobArgset)
212 this.paramStore = paramStorei;
213 if (paramStore == null)
215 paramStore = service.getParamStore();
217 this.service = service;
218 // argSetModified(false);
219 // populate parameter table
220 initForService(service, preset, jobArgset);
221 // display in new JFrame attached to parent.
227 JDialog frame = null;
230 * shows a modal dialog containing the parameters.
234 public boolean showRunDialog()
237 frame = new JDialog(Desktop.instance, true);
239 frame.setTitle("Edit parameters for " + service.getActionText());
240 Rectangle deskr = Desktop.instance.getBounds();
241 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
242 (int) (deskr.getCenterY() - 250), 480, 500));
243 frame.setContentPane(this);
244 // should recover defaults from user prefs.
245 //settingsPanel.setDividerLocation(0.4);
246 //jobPanel.setDividerLocation(0.5);
249 javax.swing.SwingUtilities.invokeLater(new Runnable() {
251 settingsPanel.setDividerLocation(0.5);
252 jobPanel.setDividerLocation(setDescr.getLineCount()>3 ? 0.35 : 0.16 );
256 tabpanels.setSelectedComponent(jobOptionsPane);
258 frame.setVisible(true);
268 private void jbInit()
270 updatepref = JvSwingUtils.makeButton("Update", "Update this existing user parameter set.",
274 public void actionPerformed(ActionEvent e)
276 update_actionPerformed(e);
279 deletepref = JvSwingUtils.makeButton("Delete", "Delete the currently selected user parameter set.",
283 public void actionPerformed(ActionEvent e)
285 delete_actionPerformed(e);
288 createpref = JvSwingUtils.makeButton("Create", "Create a new parameter set with the current settings.",
292 public void actionPerformed(ActionEvent e)
294 create_actionPerformed(e);
297 revertpref = JvSwingUtils.makeButton("Revert",
298 "Undo all changes to the current parameter set", new ActionListener()
301 public void actionPerformed(ActionEvent e)
303 revert_actionPerformed(e);
306 startjob = JvSwingUtils.makeButton("Start Job", "Start Job with current settings.",new ActionListener()
308 public void actionPerformed(ActionEvent e)
310 startjob_actionPerformed(e);
313 canceljob = JvSwingUtils.makeButton("Cancel Job", "Close this dialog and cancel job.", new ActionListener()
315 public void actionPerformed(ActionEvent e)
317 canceljob_actionPerformed(e);
321 setDetails.setBorder(new TitledBorder("Details"));
322 setDetails.setLayout(new BorderLayout());
323 setDescr.setColumns(40);
324 setDescr.setWrapStyleWord(true);
325 setDescr.setLineWrap(true);
326 setDescr.setBackground(getBackground());
327 setDescr.setEditable(true);
328 setDescr.getDocument().addDocumentListener(this);
329 setDescr.setToolTipText("Click to edit the notes for this parameter set.");
330 JScrollPane setDescrView = new JScrollPane();
331 // setDescrView.setPreferredSize(new Dimension(350, 200));
332 setDescrView.getViewport().setView(setDescr);
333 setName.setEditable(true);
334 setName.addItemListener(this);
335 setName.getEditor().addActionListener(this);
336 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
337 SetNamePanel.setLayout(new GridLayout(2, 1));
338 SetNamePanel.setMinimumSize(new Dimension(300, 40));
339 JLabel setNameLabel = new JLabel("Current parameter set name :");
340 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
341 // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
342 setNameInfo.add(setNameLabel);
343 setNameInfo.add(setName);
344 // initial button visibility
345 updatepref.setVisible(false);
346 deletepref.setVisible(false);
347 revertpref.setVisible(false);
348 createpref.setVisible(false);
349 JPanel setsavebuts = new JPanel();
350 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
351 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
352 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
353 setsavebuts.add(deletepref);
354 setsavebuts.add(revertpref);
355 setsavebuts.add(createpref);
356 setsavebuts.add(updatepref);
357 setsavebuts.setSize(new Dimension(150, 20));
358 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
359 buttonArea.add(setsavebuts);
360 SetNamePanel.add(setNameInfo);
361 SetNamePanel.add(buttonArea);
362 setDetails.add(setDescrView, BorderLayout.CENTER);
363 //setDetails.setPreferredSize(new Dimension(360, 100));
364 jobParameters.setBorder(new TitledBorder("Parameters"));
365 jobParameters.setLayout(new BorderLayout());
366 paramPane.setPreferredSize(new Dimension(360, 300));
367 paramPane.getVerticalScrollBar().setUnitIncrement(20);
368 // paramPanel.setPreferredSize(new Dimension(360, 300));
369 // TODO: relayout buttons nicely
370 paramPane.getViewport().setView(paramList);
371 jobParameters.add(paramPane, BorderLayout.CENTER);
372 JPanel jobOptionsPanel = new JPanel();
373 jobOptionsPanel.setLayout(new BorderLayout());
374 jobOptionsPanel.setBorder(new TitledBorder("Options"));
375 jobOptionsPane.getViewport().setView(jobOptions);
376 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
378 setLayout(new BorderLayout());
379 jobPanel.setLeftComponent(setDetails);
381 settingsPanel.setLeftComponent(jobOptionsPanel);
382 settingsPanel.setRightComponent(jobParameters);
383 settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
384 jobPanel.setRightComponent(settingsPanel);
386 jobPanel.setRightComponent(tabpanels);
387 jobOptionsPane.setName("Options");
388 tabpanels.add(jobOptionsPane);
389 paramPane.setName("Parameters");
390 tabpanels.add(paramPane);
392 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
393 add(SetNamePanel, BorderLayout.NORTH);
394 add(jobPanel, BorderLayout.CENTER);
395 JPanel dialogpanel = new JPanel();
396 dialogpanel.add(startjob);
397 dialogpanel.add(canceljob);
398 add(dialogpanel, BorderLayout.SOUTH);
401 protected void revert_actionPerformed(ActionEvent e)
403 reInitDialog(lastParmSet);
407 protected void update_actionPerformed(ActionEvent e)
411 String curname = ((String) setName.getSelectedItem()).trim();
412 _updatePreset(lastParmSet, curname);
413 lastParmSet = curname;
415 initArgSetModified();
416 syncSetNamesWithStore();
420 private void _deleteUserPreset(String lastParmSet2)
422 paramStore.deletePreset(lastParmSet2);
425 protected void delete_actionPerformed(ActionEvent e)
429 // delete current preset's saved entry
430 _deleteUserPreset(lastParmSet);
432 reInitDialog(null); // service default
435 protected void create_actionPerformed(ActionEvent e)
437 String curname = ((String) setName.getSelectedItem()).trim();
438 if (curname.length() > 0)
440 _storeCurrentPreset(curname);
441 lastParmSet = curname;
443 initArgSetModified();
447 // TODO: show warning
448 System.err.println("Invalid name. Not saved.");
452 protected void canceljob_actionPerformed(ActionEvent e)
457 frame.setVisible(false);
461 protected void startjob_actionPerformed(ActionEvent e)
466 frame.setVisible(false);
470 Jws2Instance service;
473 * list of service presets in the gui
475 Hashtable servicePresets = null;
478 * set if dialog is being set - so handlers will avoid spurious events
480 boolean settingDialog = false;
482 void initForService(Jws2Instance service, WsParamSetI jabap,
483 List<Argument> jabajobArgset)
485 WsParamSetI p = null;
486 List<ArgumentI> jobArgset = null;
487 settingDialog = true;
488 { // instantiate the abstract proxy for Jaba objects
489 jobArgset = jabajobArgset == null ? null : JabaParamStore
490 .getJwsArgsfromJaba(jabajobArgset);
491 p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
494 // TODO: Recover window geometry prefs for this service
495 // jobPanel.setDividerLocation(proportionalLocation)
496 // settingsPanel.setDividerLocation(proportionalLocation)
497 Hashtable exnames = new Hashtable();
498 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
500 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
502 servicePresets = new Hashtable();
503 // Add the default entry - if not present already.
504 if (!exnames.contains(SVC_DEF))
506 setName.addItem(SVC_DEF);
507 exnames.put(SVC_DEF, SVC_DEF);
508 servicePresets.put(SVC_DEF, SVC_DEF);
510 String curname = (p == null ? "" : p.getName());
511 for (WsParamSetI pr : paramStore.getPresets())
513 if (!pr.isModifiable())
515 servicePresets.put(pr.getName(), "preset");
520 if (!exnames.contains(pr.getName()))
522 setName.addItem(pr.getName());
525 // TODO: if initial jobArgset matches a given user setting or preset then
526 // should recover setting accordingly
527 // updateTable(p, jobArgset);
530 reInitDialog(p.getName());
531 initArgSetModified();
535 if (jobArgset != null && jobArgset.size() > 0)
537 curSetName = "Supplied Settings";
538 updateTable(p, jobArgset);
546 settingDialog = false;
550 @SuppressWarnings("unchecked")
551 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
553 // populate table from default parameter set.
554 List<ArgumentI> args = paramStore.getServiceParameters();
556 // split to params and required arguments
558 for (ArgumentI myarg : args)
560 // Ideally, Argument would implement isRequired !
561 if (myarg instanceof ParameterI)
563 ParameterI parm = (ParameterI) myarg;
568 if (myarg instanceof OptionI)
570 OptionI opt = (OptionI) myarg;
571 addOption(opt).resetToDefault();
575 System.err.println("Ignoring unknown service argument type "
576 + myarg.getClass().getName());
580 args = null; // no more args to process.
584 isUserPreset = false;
585 // initialise setname
586 setName.setSelectedItem(lastSetName = p.getName());
587 setDescr.setText(lastDescrText = p.getDescription());
591 args = p.getArguments();
592 } catch (Exception e)
596 // TODO: check if args should be unselected prior to resetting using the
601 if (lastParmSet == null)
603 isUserPreset = false;
604 // first call - so create a dummy name
606 setName.setSelectedItem(lastSetName = SVC_DEF);
610 if (jobArgset != null)
612 argSetModified(jobArgset, true);
615 // get setargs from current object
618 for (ArgumentI arg : args)
620 if (arg instanceof ParameterI)
622 setParameter((ParameterI) arg);
626 if (arg instanceof OptionI)
628 // System.out.println("Setting option "
629 // + System.identityHashCode(arg) + ":" + arg.getName()
630 // + " with " + arg.getDefaultValue());
631 selectOption((OptionI) arg, arg.getDefaultValue());
638 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
640 jobOptions.setLayout(new GridLayout(optSet.size(), 1));
641 refreshParamLayout();
642 paramPane.validate();
646 private boolean isModified()
648 return modifiedElements.size() > 0;
651 private Hashtable modifiedElements = new Hashtable();
654 * reset gui and modification state settings
656 private void initArgSetModified()
659 modifiedElements.clear();
660 updateButtonDisplay();
663 private void updateButtonDisplay()
665 boolean _update = false, _create = false, _delete = false, _revert = false;
666 if (modifiedElements.size() > 0)
670 _update = isUserPreset; // can only update user presets
671 if (!isUserPreset || modifiedElements.containsKey(setName))
673 // name modified - can create new preset
681 // can still delete a user preset
682 _delete = isUserPreset;
684 createpref.setVisible(_create);
685 updatepref.setVisible(_update);
686 deletepref.setVisible(_delete);
687 revertpref.setVisible(_revert);
691 private void argSetModified(Object modifiedElement, boolean b)
699 modifiedElements.remove(modifiedElement);
703 if (b && modifiedElement == setName
704 && modifiedElements.contains(modifiedElement))
706 // HACK! prevents iteration on makeSetNameValid
709 modifiedElements.put(modifiedElement, modifiedElement);
711 // set mod status based on presence of elements in table
712 if (b && modifiedElements.size() > 0)
714 makeSetNameValid(!isUserPreset);
715 SetNamePanel.revalidate();
717 updateButtonDisplay();
720 private boolean isServicePreset(String selectedItem)
722 return selectedItem.equals(SVC_DEF)
723 || servicePresets.containsKey(selectedItem);
727 * check if the current set name is a valid set name for saving, if not, then
730 private void makeSetNameValid(boolean newuserset)
732 boolean stn = settingDialog;
733 boolean renamed = false;
734 settingDialog = true;
735 String nm = (curSetName != null ? curSetName : (String) setName
737 // check if the name is reserved - if it is, rename it.
738 if (isServicePreset(nm))
747 while (paramStore.getPreset(tnm) != null)
749 tnm = nm + " (" + (++i) + ")";
758 boolean makeupdate = false;
759 // sync the gui with the preset database
760 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
762 String snm = (String) setName.getItemAt(i);
766 // setName.setSelectedIndex(i);
771 setName.addItem(curSetName = nm);
772 setName.setSelectedItem(curSetName);
776 settingDialog = false; // we need this name change to be registered.
777 argSetModified(setName, renamed);
782 private void addParameter(ParameterI arg)
784 ParamBox pb = paramSet.get(arg.getName());
787 pb = new ParamBox(this, arg);
788 paramSet.put(arg.getName(), pb);
792 // take the defaults from the parameter
793 pb.updateControls(arg);
796 private void setParameter(ParameterI arg)
798 ParamBox pb = paramSet.get(arg.getName());
805 pb.updateControls(arg);
810 private void selectOption(OptionI option, String string)
812 OptionBox cb = optSet.get(option.getName());
815 cb = addOption(option);
817 cb.enabled.setSelected(string != null); // initial state for an option.
820 if (option.getPossibleValues().contains(string))
822 cb.val.setSelectedItem(string);
826 throw new Error("Invalid value " + string + " for option " + option);
830 if (option.isRequired() && !cb.enabled.isSelected())
832 // TODO: indicate paramset is not valid.. option needs to be selected!
834 cb.setInitialValue();
837 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
839 public class ParamBox extends JPanel implements ChangeListener,
840 ActionListener, MouseListener
842 JButton showDesc = new JButton();
844 JTextArea string = new JTextArea();
846 JScrollPane descPanel = new JScrollPane();
848 JSlider slider = null;
850 JTextField valueField = null;
852 ValueConstrainI validator = null;
854 JPanel settingPanel = new JPanel();
856 JPanel controlPanel = new JPanel();
858 boolean integ = false;
860 boolean choice = false;
862 boolean descisvisible = false;
864 final WsJobParameters pmdialogbox;
868 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
870 pmdialogbox = pmlayout;
871 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
872 setBorder(new TitledBorder(parm.getName()));
874 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
875 showDesc.setText("+");
876 string.setFont(new Font("Verdana", Font.PLAIN, 11));
877 string.setBackground(getBackground());
878 // string.setSize(new Dimension(PARAM_WIDTH, 80));
879 string.setEditable(false);
880 descPanel.getViewport().setView(string);
881 // descPanel.setLocation(2,17);
882 descPanel.setVisible(false);
883 // string.setMinimumSize(new Dimension(140,80));
884 // string.setMaximumSize(new Dimension(280,80));
885 final ParamBox me = this;
886 finfo = parm.getFurtherDetails();
889 showDesc.setToolTipText("<html>"+JvSwingUtils.wrapTooltip("Click to show brief description<br><img src=\"" + linkImageURL + "\"/> Right click for further information.")+"</html>");
890 showDesc.addMouseListener(this);
894 showDesc.setToolTipText("<html>"+JvSwingUtils.wrapTooltip("Click to show brief description.")+"</html>");
896 showDesc.addActionListener(new ActionListener()
899 public void actionPerformed(ActionEvent e)
901 descisvisible = !descisvisible;
902 descPanel.setVisible(descisvisible);
903 me.setPreferredSize(new Dimension(PARAM_WIDTH,
904 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
906 pmlayout.refreshParamLayout();
909 string.setWrapStyleWord(true);
910 string.setLineWrap(true);
911 string.setColumns(32);
912 string.setText(parm.getDescription());
913 JPanel firstrow = new JPanel();
914 firstrow.setLayout(null);
915 controlPanel.setLayout(new BorderLayout());
916 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
917 PARAM_CLOSEDHEIGHT - 50));
918 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
919 firstrow.add(showDesc);
920 firstrow.add(controlPanel);
921 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
922 PARAM_CLOSEDHEIGHT - 30));
924 validator = parm.getValidValue();
926 if (validator != null)
928 integ = validator.getType() == Type.Integer;
932 if (parameter.getPossibleValues() != null)
937 updateControls(parm);
938 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
939 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
946 // reset the widget's initial value.
950 boolean adjusting = false;
952 ParameterI parameter;
956 public int getBoxHeight()
958 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
961 public void updateControls(ParameterI parm)
964 boolean init = (choicebox == null && valueField == null);
971 choicebox = new JComboBox();
972 choicebox.addActionListener(this);
973 controlPanel.add(choicebox, BorderLayout.CENTER);
977 slider = new JSlider();
978 slider.addChangeListener(this);
979 valueField = new JTextField();
980 valueField.addActionListener(this);
981 valueField.setPreferredSize(new Dimension(60, 25));
982 controlPanel.add(slider, BorderLayout.WEST);
983 controlPanel.add(valueField, BorderLayout.EAST);
994 List vals = parm.getPossibleValues();
995 for (Object val : vals)
997 choicebox.addItem(val);
1001 if (parm.getDefaultValue() != null)
1003 choicebox.setSelectedItem(parm.getDefaultValue());
1008 valueField.setText(parm.getDefaultValue());
1011 lastVal = updateSliderFromValueField();
1017 public ParameterI getParameter()
1019 ParameterI prm = parameter.copy();
1022 prm.setDefaultValue((String) choicebox.getSelectedItem());
1026 prm.setDefaultValue(valueField.getText());
1031 public Object updateSliderFromValueField()
1035 if (validator != null)
1042 valueField.setText(valueField.getText().trim());
1043 iVal = Integer.valueOf(valueField.getText());
1044 if (validator.getMin() != null
1045 && validator.getMin().intValue() > iVal)
1047 iVal = validator.getMin().intValue();
1048 // TODO: provide visual indication that hard limit was reached for
1051 if (validator.getMax() != null
1052 && validator.getMax().intValue() < iVal)
1054 iVal = validator.getMax().intValue();
1055 // TODO: provide visual indication that hard limit was reached for
1058 } catch (Exception e)
1062 if (validator.getMin() != null && validator.getMax() != null)
1064 slider.getModel().setRangeProperties(iVal, 1,
1065 validator.getMin().intValue(),
1066 validator.getMax().intValue(), true);
1070 slider.setVisible(false);
1080 fVal = Float.valueOf(valueField.getText());
1081 if (validator.getMin() != null
1082 && validator.getMin().floatValue() > fVal)
1084 fVal = validator.getMin().floatValue();
1085 // TODO: provide visual indication that hard limit was reached for
1088 if (validator.getMax() != null
1089 && validator.getMax().floatValue() < fVal)
1091 fVal = validator.getMax().floatValue();
1092 // TODO: provide visual indication that hard limit was reached for
1095 } catch (Exception e)
1099 if (validator.getMin() != null && validator.getMax() != null)
1101 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1102 (int) validator.getMin().floatValue() * 1000,
1103 (int) validator.getMax().floatValue() * 1000, true);
1107 slider.setVisible(false);
1117 slider.setVisible(false);
1119 { valueField.getText().trim() };
1124 { (String) choicebox.getSelectedItem() };
1130 public void stateChanged(ChangeEvent e)
1134 valueField.setText(""
1135 + ((integ) ? ("" + (int) slider.getValue())
1136 : ("" + (float) (slider.getValue() / 1000f))));
1142 public void actionPerformed(ActionEvent e)
1150 updateSliderFromValueField();
1155 private void checkIfModified()
1157 Object cstate = updateSliderFromValueField();
1158 boolean notmod = false;
1159 if (cstate.getClass() == lastVal.getClass())
1161 if (cstate instanceof int[])
1163 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1165 else if (cstate instanceof float[])
1167 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1169 else if (cstate instanceof String[])
1171 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1174 pmdialogbox.argSetModified(this, !notmod);
1177 public void mouseClicked(MouseEvent e)
1179 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1181 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1185 public void mousePressed(MouseEvent e)
1187 // TODO Auto-generated method stub
1191 public void mouseReleased(MouseEvent e)
1193 // TODO Auto-generated method stub
1197 public void mouseEntered(MouseEvent e)
1199 // TODO Auto-generated method stub
1203 public void mouseExited(MouseEvent e)
1205 // TODO Auto-generated method stub
1211 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1213 public class OptionBox extends JPanel implements MouseListener,
1216 JComboBox val = new JComboBox();
1218 JCheckBox enabled = new JCheckBox();
1220 JLabel optlabel = new JLabel();
1224 boolean hasLink = false;
1228 public OptionBox(OptionI opt)
1231 setLayout(new BorderLayout());
1232 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1233 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1234 enabled.setText("");
1235 enabled.setText(opt.getName());
1236 enabled.addActionListener(this);
1237 finfo = option.getFurtherDetails();
1241 // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
1242 enabled.setToolTipText("<html>" + JvSwingUtils.wrapTooltip(opt.getDescription()+"<br><img src=\"" + linkImageURL + "\"/>")
1244 // optlabel.addMouseListener(this);
1245 enabled.addMouseListener(this);
1249 // optlabel.setToolTipText(opt.getDescription());
1250 enabled.setToolTipText("<html>"+JvSwingUtils.wrapTooltip(opt.getDescription())+"</html>");
1252 add(enabled, BorderLayout.NORTH);
1253 if (opt.getPossibleValues().size() > 1)
1255 setLayout(new GridLayout(1, 2));
1256 for (Object str : opt.getPossibleValues())
1258 val.addItem((String) str);
1260 val.setSelectedItem((String) opt.getDefaultValue());
1261 val.addActionListener(this);
1262 add(val, BorderLayout.SOUTH);
1264 // TODO: add actionListeners for popup (to open further info),
1265 // and to update list of parameters if an option is enabled
1266 // that takes a value.
1270 public void resetToDefault()
1272 enabled.setSelected(false);
1273 if (option.isRequired())
1275 // Apply default value
1276 selectOption(option, option.getDefaultValue());
1280 boolean initEnabled = false;
1282 String initVal = null;
1284 public void setInitialValue()
1286 initEnabled = enabled.isSelected();
1287 if (option.getPossibleValues() != null
1288 && option.getPossibleValues().size() > 1)
1290 initVal = (String) val.getSelectedItem();
1294 initVal = (initEnabled) ? option.getDefaultValue() : null;
1298 public OptionI getOptionIfEnabled()
1300 if (!enabled.isSelected())
1304 OptionI opt = option.copy();
1306 if (val.getSelectedItem() != null)
1308 opt.setDefaultValue((String) val.getSelectedItem());
1313 public void actionPerformed(ActionEvent e)
1315 if (e.getSource() != enabled)
1317 enabled.setSelected(true);
1322 private void checkIfModified()
1324 boolean notmod = (initEnabled == enabled.isSelected());
1325 if (enabled.isSelected())
1327 if (initVal != null)
1329 notmod &= initVal.equals(val.getSelectedItem());
1333 // compare against default service setting
1334 notmod &= option.getDefaultValue() == null
1335 || option.getDefaultValue().equals(val.getSelectedItem());
1340 notmod &= initVal == null;
1342 argSetModified(this, !notmod);
1345 public void mouseClicked(MouseEvent e)
1347 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1349 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1353 public void mousePressed(MouseEvent e)
1355 // TODO Auto-generated method stub
1359 public void mouseReleased(MouseEvent e)
1361 // TODO Auto-generated method stub
1365 public void mouseEntered(MouseEvent e)
1367 // TODO Auto-generated method stub
1371 public void mouseExited(MouseEvent e)
1373 // TODO Auto-generated method stub
1379 private OptionBox addOption(OptionI opt)
1381 OptionBox cb = optSet.get(opt.getName());
1384 cb = new OptionBox(opt);
1385 optSet.put(opt.getName(), cb);
1391 public static void showUrlPopUp(JComponent invoker, final String finfo,
1395 JPopupMenu mnu = new JPopupMenu();
1396 JMenuItem mitem = new JMenuItem("View " + finfo);
1397 mitem.addActionListener(new ActionListener()
1401 public void actionPerformed(ActionEvent e)
1403 Desktop.showUrl(finfo);
1408 mnu.show(invoker, x, y);
1411 protected void refreshParamLayout()
1413 FlowLayout fl = new FlowLayout();
1414 paramList.setLayout(fl);
1415 int s = 2 * fl.getVgap();
1416 for (ParamBox pbox : paramSet.values())
1418 s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
1420 paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
1425 * testing method - grab a service and parameter set and show the window
1429 public static void main(String[] args)
1431 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1434 if (args.length > 3)
1436 Vector<String> services = new Vector<String>();
1437 services.addElement(args[p++]);
1438 Jws2Discoverer.setServiceUrls(services);
1443 } catch (Exception e)
1445 System.err.println("Aborting. Problem discovering services.");
1446 e.printStackTrace();
1449 Jws2Discoverer.Jws2Instance lastserv = null;
1450 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1453 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1455 if (lastserv != null)
1457 List<Preset> prl = null;
1459 if (++p < args.length)
1461 PresetManager prman = lastserv.getPresets();
1464 pr = prman.getPresetByName(args[p]);
1467 // just grab the last preset.
1468 prl = prman.getPresets();
1474 PresetManager prman = lastserv.getPresets();
1477 prl = prman.getPresets();
1480 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1481 while (en != null && en.hasNext())
1487 en = prl.iterator();
1492 System.out.println("Testing opts dupes for "
1493 + lastserv.getUri() + " : "
1494 + lastserv.getActionText() + ":" + pr.getName());
1495 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1500 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1501 } catch (Exception e)
1503 System.err.println("Failed to copy " + o.getName());
1504 e.printStackTrace();
1507 System.err.println("Failed to copy " + o.getName());
1508 e.printStackTrace();
1513 System.out.println("Testing param dupes:");
1514 List<Parameter> rg = lastserv.getRunnerConfig()
1516 for (Parameter o : rg)
1520 Parameter cpy = jalview.ws.jws2.ParameterUtils
1522 } catch (Exception e)
1524 System.err.println("Failed to copy " + o.getName());
1525 e.printStackTrace();
1528 System.err.println("Failed to copy " + o.getName());
1529 e.printStackTrace();
1534 System.out.println("Testing param write:");
1535 List<String> writeparam = null, readparam = null;
1538 writeparam = jalview.ws.jws2.ParameterUtils
1540 pr.getArguments(lastserv.getRunnerConfig()),
1542 System.out.println("Testing param read :");
1543 List<Option> pset = jalview.ws.jws2.ParameterUtils
1544 .processParameters(writeparam,
1545 lastserv.getRunnerConfig(), " ");
1546 readparam = jalview.ws.jws2.ParameterUtils
1547 .writeParameterSet(pset, " ");
1548 Iterator<String> o = pr.getOptions().iterator(), s = writeparam
1549 .iterator(), t = readparam.iterator();
1550 boolean failed = false;
1551 while (s.hasNext() && t.hasNext())
1553 String on = o.next(), sn = s.next(), st = t.next();
1556 System.out.println("Original was " + on
1557 + " Phase 1 wrote " + sn + "\tPhase 2 wrote "
1564 System.out.println("Original parameters:\n"
1566 System.out.println("Wrote parameters in first set:\n"
1568 System.out.println("Wrote parameters in second set:\n"
1572 } catch (Exception e)
1574 e.printStackTrace();
1577 WsJobParameters pgui = new WsJobParameters(lastserv,
1578 new JabaPreset(lastserv, pr));
1579 JFrame jf = new JFrame("Parameters for "
1580 + lastserv.getActionText());
1581 JPanel cont = new JPanel();
1582 // jf.setPreferredSize(new Dimension(600, 800));
1585 final Thread thr = Thread.currentThread();
1586 jf.addWindowListener(new WindowListener()
1589 public void windowActivated(WindowEvent e)
1591 // TODO Auto-generated method stub
1595 public void windowClosed(WindowEvent e)
1599 public void windowClosing(WindowEvent e)
1605 public void windowDeactivated(WindowEvent e)
1607 // TODO Auto-generated method stub
1611 public void windowDeiconified(WindowEvent e)
1613 // TODO Auto-generated method stub
1617 public void windowIconified(WindowEvent e)
1619 // TODO Auto-generated method stub
1623 public void windowOpened(WindowEvent e)
1625 // TODO Auto-generated method stub
1630 jf.setVisible(true);
1631 boolean inter = false;
1636 Thread.sleep(10000);
1637 } catch (Exception e)
1650 public List<ArgumentI> getJobParams()
1652 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
1653 // recover options and parameters from GUI
1654 for (OptionBox opts : optSet.values())
1656 OptionI opt = opts.getOptionIfEnabled();
1662 for (ParamBox parambox : paramSet.values())
1664 ParameterI parm = parambox.getParameter();
1674 String lastParmSet = null;
1677 * Hashtable<String, Object[]> editedParams = new Hashtable<String,
1680 * store the given parameters in the user parameter set database.
1682 * @param storeSetName - lastParmSet
1684 * @param descr - setDescr.getText()
1686 * @param jobParams - getJobParams()
1688 * private void _storeUserPreset(String storeSetName, String descr,
1689 * List<ArgumentI> jobParams) { // this is a simple hash store. Object[] pset;
1690 * editedParams.put(storeSetName, pset = new Object[3]); pset[0] =
1691 * storeSetName; pset[1] = descr; pset[2] = jobParams; // writeParam("Saving "
1692 * + storeSetName + ": ", jobParams); }
1694 * private void writeParam(String nm, List<ArgumentI> params) { for (ArgumentI
1695 * p : params) { System.out.println(nm + ":" + System.identityHashCode(p) +
1696 * " Name: " + p.getName() + " Value: " + p.getDefaultValue()); } }
1698 * private Object[] _getUserPreset(String setName) { Object[] pset =
1699 * editedParams.get(setName); // if (pset != null) // writeParam("Retrieving "
1700 * + setName + ": ", (List<Argument>) pset[2]); return pset; }
1702 * * remove the given user preset from the preset stash
1706 * private void _deleteUserPreset(String setName) {
1707 * editedParams.remove(setName); }
1710 private void syncSetNamesWithStore()
1713 // remove any set names in the drop down menu that aren't either a reserved
1714 // setting, or a user defined or service preset.
1715 Vector items = new Vector();
1716 while (n < setName.getItemCount())
1718 String item = (String) setName.getItemAt(n);
1719 if (!item.equals(SVC_DEF) && !paramStore.presetExists(item))
1721 setName.removeItemAt(n);
1725 items.addElement(item);
1729 if (!items.contains(SVC_DEF))
1731 setName.addItem(SVC_DEF);
1733 for (WsParamSetI upn : paramStore.getPresets())
1735 if (!items.contains(upn.getName()))
1737 setName.addItem(upn.getName());
1743 * true if lastParmSet is a user preset
1745 boolean isUserPreset = false;
1747 private void reInitDialog(String nextPreset)
1749 settingDialog = true;
1750 // updateTable(null,null); // first reset to defaults
1751 WsParamSetI pset = null;
1752 if (nextPreset != null && nextPreset.length() > 0)
1754 pset = paramStore.getPreset(nextPreset);
1758 if (pset.isModifiable())
1760 isUserPreset = true;
1761 setDescr.setText(pset.getDescription());
1762 updateTable(null, pset.getArguments());
1763 lastParmSet = nextPreset;
1767 isUserPreset = false;
1768 setDescr.setText("");
1769 // must be a default preset from service
1770 updateTable(pset, null);
1771 lastParmSet = nextPreset;
1776 isUserPreset = false;
1778 setDescr.setText("");
1779 updateTable(null, null);
1780 lastParmSet = SVC_DEF;
1783 initArgSetModified();
1784 syncSetNamesWithStore();
1785 setName.setSelectedItem(lastParmSet);
1787 settingDialog = false;
1791 String curSetName = null;
1793 public void itemStateChanged(ItemEvent e)
1795 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1797 final String setname = (String) setName.getSelectedItem();
1798 System.out.println("Item state changed for " + setname
1799 + " (handling ? " + !settingDialog + ")");
1805 if (setname == null)
1809 javax.swing.SwingUtilities.invokeLater(new Runnable()
1813 doPreferenceComboStateChange(setname);
1819 private void doPreferenceComboStateChange(String setname)
1821 // user has selected a different item from combo-box
1824 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1825 if (lsetname.equals(setname))
1827 // setname was just edited - so ignore this event.
1830 settingDialog = true;
1831 System.out.println("Prompting to save " + lsetname);
1832 if (javax.swing.JOptionPane
1837 + "' is modifed, and your changes will be lost.\nReally change preset ?",
1838 "Warning: Unsaved Changes",
1839 javax.swing.JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
1841 // revert the combobox to the current item
1842 settingDialog = true;
1843 setName.setSelectedItem(lsetname);
1844 settingDialog = false;
1847 // System.out.println("Saving for " + lsetname);
1848 // _storeCurrentPreset(lsetname);
1852 settingDialog = true;
1853 reInitDialog(setname);
1854 settingDialog = false;
1858 private void _renameExistingPreset(String oldName, String curSetName2)
1860 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(),
1865 * store current settings as given name. You should then reset gui.
1867 * @param curSetName2
1869 private void _storeCurrentPreset(String curSetName2)
1871 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1874 private void _updatePreset(String lastParmSet2, String curname)
1876 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(),
1882 * last saved name for this user preset
1884 String lastSetName = null;
1887 * last saved value of the description text for this user preset
1889 String lastDescrText = null;
1891 public void actionPerformed(ActionEvent e)
1893 if (e.getSource() instanceof Component)
1895 Component src = (Component) e.getSource();
1896 if (src.getParent() == setName)
1898 // rename any existing records we know about for this set.
1899 String newname = (String) e.getActionCommand().trim();
1901 if (isServicePreset(newname))
1903 final String oldname = curSetName!=null ? curSetName : lastParmSet;
1904 final Component ourframe=this;
1906 setName.getEditor().setItem(oldname);
1907 settingDialog=false;
1908 javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {
1909 JOptionPane.showMessageDialog(ourframe,
1910 "Invalid name - preset already exists.", "Invalid name",
1911 JOptionPane.WARNING_MESSAGE);
1916 curSetName = newname;
1917 System.err.println("New name for user setting " + curSetName
1918 + " (was " + setName.getSelectedItem() + ")");
1919 if (curSetName.equals(setName.getSelectedItem()))
1923 if (curSetName != null)
1925 argSetModified(setName, true);
1933 private void checkDescrModified()
1940 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1941 : !setDescr.getText().equals(lastDescrText)));
1946 public void insertUpdate(DocumentEvent e)
1948 checkDescrModified();
1951 public void removeUpdate(DocumentEvent e)
1953 checkDescrModified();
1956 public void changedUpdate(DocumentEvent e)
1958 checkDescrModified();
1963 * @return null or the service preset selected by the user
1965 public WsParamSetI getPreset()
1967 if (isUserPreset || isModified()
1968 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
1974 return paramStore.getPreset(lastParmSet);