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");
129 private static final String SVC_DEF = "Defaults"; // this is the null
130 // parameter set as shown to
133 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
134 PARAM_CLOSEDHEIGHT = 80;
136 private static final int OPTSET_HEIGHT = 30;
138 JPanel SetNamePanel = new JPanel();
140 JPanel setDetails = new JPanel();
142 JSplitPane settingsPanel = new JSplitPane();
144 JSplitPane jobPanel = new JSplitPane();
146 boolean split = true;
148 JTabbedPane tabpanels = new JTabbedPane();
150 JPanel jobOptions = new JPanel();
152 JScrollPane jobOptionsPane = new JScrollPane();
154 JPanel jobParameters = new JPanel();
156 JButton createpref = new JButton();
158 JButton deletepref = new JButton();
160 JButton revertpref = new JButton();
162 JButton updatepref = new JButton();
164 JButton startjob = new JButton();
166 JButton canceljob = new JButton();
168 JComboBox setName = new JComboBox();
170 JTextArea setDescr = new JTextArea();
172 JScrollPane paramPane = new JScrollPane();
174 JPanel paramList = new JPanel();
176 RunnerConfig serviceOptions;
178 ParamDatastoreI paramStore;
180 WsJobParameters(Jws2Instance service)
185 public WsJobParameters(Jws2Instance service, WsParamSetI preset)
187 this(null, service, preset, null);
193 * - if null, create new JFrame outside of desktop
197 public WsJobParameters(JFrame parent, Jws2Instance service,
198 WsParamSetI preset, List<Argument> jobArgset)
200 this(parent, null, service, preset, jobArgset);
211 public WsJobParameters(JFrame parent, ParamDatastoreI paramStorei,
212 Jws2Instance service, WsParamSetI preset, List<Argument> jobArgset)
216 this.paramStore = paramStorei;
217 if (paramStore == null)
219 paramStore = service.getParamStore();
221 this.service = service;
222 // argSetModified(false);
223 // populate parameter table
224 initForService(service, preset, jobArgset);
225 // display in new JFrame attached to parent.
231 JDialog frame = null;
234 * shows a modal dialog containing the parameters.
238 public boolean showRunDialog()
241 frame = new JDialog(Desktop.instance, true);
243 frame.setTitle("Edit parameters for " + service.getActionText());
244 Rectangle deskr = Desktop.instance.getBounds();
245 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
246 (int) (deskr.getCenterY() - 250), 480, 500));
247 frame.setContentPane(this);
248 // should recover defaults from user prefs.
249 // settingsPanel.setDividerLocation(0.4);
250 // jobPanel.setDividerLocation(0.5);
254 javax.swing.SwingUtilities.invokeLater(new Runnable()
258 settingsPanel.setDividerLocation(0.5);
259 jobPanel.setDividerLocation(setDescr.getLineCount() > 3 ? 0.35
266 tabpanels.setSelectedComponent(jobOptionsPane);
268 frame.setVisible(true);
277 private void jbInit()
279 updatepref = JvSwingUtils.makeButton("Update",
280 "Update this existing user parameter set.",
284 public void actionPerformed(ActionEvent e)
286 update_actionPerformed(e);
289 deletepref = JvSwingUtils.makeButton("Delete",
290 "Delete the currently selected user parameter set.",
294 public void actionPerformed(ActionEvent e)
296 delete_actionPerformed(e);
299 createpref = JvSwingUtils.makeButton("Create",
300 "Create a new parameter set with the current settings.",
304 public void actionPerformed(ActionEvent e)
306 create_actionPerformed(e);
309 revertpref = JvSwingUtils.makeButton("Revert",
310 "Undo all changes to the current parameter set",
314 public void actionPerformed(ActionEvent e)
316 revert_actionPerformed(e);
319 startjob = JvSwingUtils.makeButton("Start Job",
320 "Start Job with current settings.", new ActionListener()
322 public void actionPerformed(ActionEvent e)
324 startjob_actionPerformed(e);
327 canceljob = JvSwingUtils.makeButton("Cancel Job",
328 "Close this dialog and cancel job.", new ActionListener()
330 public void actionPerformed(ActionEvent e)
332 canceljob_actionPerformed(e);
336 setDetails.setBorder(new TitledBorder("Details"));
337 setDetails.setLayout(new BorderLayout());
338 setDescr.setColumns(40);
339 setDescr.setWrapStyleWord(true);
340 setDescr.setLineWrap(true);
341 setDescr.setBackground(getBackground());
342 setDescr.setEditable(true);
343 setDescr.getDocument().addDocumentListener(this);
344 setDescr.setToolTipText("Click to edit the notes for this parameter set.");
345 JScrollPane setDescrView = new JScrollPane();
346 // setDescrView.setPreferredSize(new Dimension(350, 200));
347 setDescrView.getViewport().setView(setDescr);
348 setName.setEditable(true);
349 setName.addItemListener(this);
350 setName.getEditor().addActionListener(this);
351 JPanel setNameInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
352 SetNamePanel.setLayout(new GridLayout(2, 1));
353 SetNamePanel.setMinimumSize(new Dimension(300, 40));
354 JLabel setNameLabel = new JLabel("Current parameter set name :");
355 setNameLabel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
356 // setNameLabel.setHorizontalAlignment(FlowLayout.LEFT);
357 setNameInfo.add(setNameLabel);
358 setNameInfo.add(setName);
359 // initial button visibility
360 updatepref.setVisible(false);
361 deletepref.setVisible(false);
362 revertpref.setVisible(false);
363 createpref.setVisible(false);
364 JPanel setsavebuts = new JPanel();
365 setsavebuts.setLayout(new FlowLayout(FlowLayout.LEFT)); // GridLayout(1,2));
366 ((FlowLayout) setsavebuts.getLayout()).setHgap(10);
367 ((FlowLayout) setsavebuts.getLayout()).setVgap(0);
368 setsavebuts.add(deletepref);
369 setsavebuts.add(revertpref);
370 setsavebuts.add(createpref);
371 setsavebuts.add(updatepref);
372 setsavebuts.setSize(new Dimension(150, 20));
373 JPanel buttonArea = new JPanel(new GridLayout(1, 1));
374 buttonArea.add(setsavebuts);
375 SetNamePanel.add(setNameInfo);
376 SetNamePanel.add(buttonArea);
377 setDetails.add(setDescrView, BorderLayout.CENTER);
378 // setDetails.setPreferredSize(new Dimension(360, 100));
379 jobParameters.setBorder(new TitledBorder("Parameters"));
380 jobParameters.setLayout(new BorderLayout());
381 paramPane.setPreferredSize(new Dimension(360, 300));
382 paramPane.getVerticalScrollBar().setUnitIncrement(20);
383 // paramPanel.setPreferredSize(new Dimension(360, 300));
384 // TODO: relayout buttons nicely
385 paramPane.getViewport().setView(paramList);
386 jobParameters.add(paramPane, BorderLayout.CENTER);
387 JPanel jobOptionsPanel = new JPanel();
388 jobOptionsPanel.setLayout(new BorderLayout());
389 jobOptionsPanel.setBorder(new TitledBorder("Options"));
390 jobOptionsPane.getViewport().setView(jobOptions);
391 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
393 setLayout(new BorderLayout());
394 jobPanel.setLeftComponent(setDetails);
397 settingsPanel.setLeftComponent(jobOptionsPanel);
398 settingsPanel.setRightComponent(jobParameters);
399 settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
400 jobPanel.setRightComponent(settingsPanel);
404 jobPanel.setRightComponent(tabpanels);
405 jobOptionsPane.setName("Options");
406 tabpanels.add(jobOptionsPane);
407 paramPane.setName("Parameters");
408 tabpanels.add(paramPane);
410 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
411 add(SetNamePanel, BorderLayout.NORTH);
412 add(jobPanel, BorderLayout.CENTER);
413 JPanel dialogpanel = new JPanel();
414 dialogpanel.add(startjob);
415 dialogpanel.add(canceljob);
416 add(dialogpanel, BorderLayout.SOUTH);
419 protected void revert_actionPerformed(ActionEvent e)
421 reInitDialog(lastParmSet);
425 protected void update_actionPerformed(ActionEvent e)
429 String curname = ((String) setName.getSelectedItem()).trim();
430 _updatePreset(lastParmSet, curname);
431 lastParmSet = curname;
433 initArgSetModified();
434 syncSetNamesWithStore();
438 private void _deleteUserPreset(String lastParmSet2)
440 paramStore.deletePreset(lastParmSet2);
443 protected void delete_actionPerformed(ActionEvent e)
447 // delete current preset's saved entry
448 _deleteUserPreset(lastParmSet);
450 reInitDialog(null); // service default
453 protected void create_actionPerformed(ActionEvent e)
455 String curname = ((String) setName.getSelectedItem()).trim();
456 if (curname.length() > 0)
458 _storeCurrentPreset(curname);
459 lastParmSet = curname;
461 initArgSetModified();
465 // TODO: show warning
466 System.err.println("Invalid name. Not saved.");
470 protected void canceljob_actionPerformed(ActionEvent e)
475 frame.setVisible(false);
479 protected void startjob_actionPerformed(ActionEvent e)
484 frame.setVisible(false);
488 Jws2Instance service;
491 * list of service presets in the gui
493 Hashtable servicePresets = null;
496 * set if dialog is being set - so handlers will avoid spurious events
498 boolean settingDialog = false;
500 void initForService(Jws2Instance service, WsParamSetI jabap,
501 List<Argument> jabajobArgset)
503 WsParamSetI p = null;
504 List<ArgumentI> jobArgset = null;
505 settingDialog = true;
506 { // instantiate the abstract proxy for Jaba objects
507 jobArgset = jabajobArgset == null ? null : JabaParamStore
508 .getJwsArgsfromJaba(jabajobArgset);
509 p = jabap; // (jabap != null) ? paramStore.getPreset(jabap.getName()) :
512 // TODO: Recover window geometry prefs for this service
513 // jobPanel.setDividerLocation(proportionalLocation)
514 // settingsPanel.setDividerLocation(proportionalLocation)
515 Hashtable exnames = new Hashtable();
516 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
518 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
520 servicePresets = new Hashtable();
521 // Add the default entry - if not present already.
522 if (!exnames.contains(SVC_DEF))
524 setName.addItem(SVC_DEF);
525 exnames.put(SVC_DEF, SVC_DEF);
526 servicePresets.put(SVC_DEF, SVC_DEF);
528 String curname = (p == null ? "" : p.getName());
529 for (WsParamSetI pr : paramStore.getPresets())
531 if (!pr.isModifiable())
533 servicePresets.put(pr.getName(), "preset");
538 if (!exnames.contains(pr.getName()))
540 setName.addItem(pr.getName());
543 // TODO: if initial jobArgset matches a given user setting or preset then
544 // should recover setting accordingly
545 // updateTable(p, jobArgset);
548 reInitDialog(p.getName());
549 initArgSetModified();
553 if (jobArgset != null && jobArgset.size() > 0)
555 curSetName = "Supplied Settings";
556 updateTable(p, jobArgset);
564 settingDialog = false;
568 @SuppressWarnings("unchecked")
569 private void updateTable(WsParamSetI p, List<ArgumentI> jobArgset)
571 // populate table from default parameter set.
572 List<ArgumentI> args = paramStore.getServiceParameters();
574 // split to params and required arguments
576 for (ArgumentI myarg : args)
578 // Ideally, Argument would implement isRequired !
579 if (myarg instanceof ParameterI)
581 ParameterI parm = (ParameterI) myarg;
586 if (myarg instanceof OptionI)
588 OptionI opt = (OptionI) myarg;
589 addOption(opt).resetToDefault();
593 System.err.println("Ignoring unknown service argument type "
594 + myarg.getClass().getName());
598 args = null; // no more args to process.
602 isUserPreset = false;
603 // initialise setname
604 setName.setSelectedItem(lastSetName = p.getName());
605 setDescr.setText(lastDescrText = p.getDescription());
609 args = p.getArguments();
610 } catch (Exception e)
614 // TODO: check if args should be unselected prior to resetting using the
619 if (lastParmSet == null)
621 isUserPreset = false;
622 // first call - so create a dummy name
624 setName.setSelectedItem(lastSetName = SVC_DEF);
628 if (jobArgset != null)
630 argSetModified(jobArgset, true);
633 // get setargs from current object
636 for (ArgumentI arg : args)
638 if (arg instanceof ParameterI)
640 setParameter((ParameterI) arg);
644 if (arg instanceof OptionI)
646 // System.out.println("Setting option "
647 // + System.identityHashCode(arg) + ":" + arg.getName()
648 // + " with " + arg.getDefaultValue());
649 selectOption((OptionI) arg, arg.getDefaultValue());
656 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
658 jobOptions.setLayout(new GridLayout(optSet.size(), 1));
659 refreshParamLayout();
660 paramPane.validate();
664 private boolean isModified()
666 return modifiedElements.size() > 0;
669 private Hashtable modifiedElements = new Hashtable();
672 * reset gui and modification state settings
674 private void initArgSetModified()
677 modifiedElements.clear();
678 updateButtonDisplay();
681 private void updateButtonDisplay()
683 boolean _update = false, _create = false, _delete = false, _revert = false;
684 if (modifiedElements.size() > 0)
688 _update = isUserPreset; // can only update user presets
689 if (!isUserPreset || modifiedElements.containsKey(setName))
691 // name modified - can create new preset
699 // can still delete a user preset
700 _delete = isUserPreset;
702 createpref.setVisible(_create);
703 updatepref.setVisible(_update);
704 deletepref.setVisible(_delete);
705 revertpref.setVisible(_revert);
709 private void argSetModified(Object modifiedElement, boolean b)
717 modifiedElements.remove(modifiedElement);
721 if (b && modifiedElement == setName
722 && modifiedElements.contains(modifiedElement))
724 // HACK! prevents iteration on makeSetNameValid
727 modifiedElements.put(modifiedElement, modifiedElement);
729 // set mod status based on presence of elements in table
730 if (b && modifiedElements.size() > 0)
732 makeSetNameValid(!isUserPreset);
733 SetNamePanel.revalidate();
735 updateButtonDisplay();
738 private boolean isServicePreset(String selectedItem)
740 return selectedItem.equals(SVC_DEF)
741 || servicePresets.containsKey(selectedItem);
745 * check if the current set name is a valid set name for saving, if not, then
748 private void makeSetNameValid(boolean newuserset)
750 boolean stn = settingDialog;
751 boolean renamed = false;
752 settingDialog = true;
753 String nm = (curSetName != null ? curSetName : (String) setName
755 // check if the name is reserved - if it is, rename it.
756 if (isServicePreset(nm))
765 while (paramStore.getPreset(tnm) != null)
767 tnm = nm + " (" + (++i) + ")";
776 boolean makeupdate = false;
777 // sync the gui with the preset database
778 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
780 String snm = (String) setName.getItemAt(i);
784 // setName.setSelectedIndex(i);
789 setName.addItem(curSetName = nm);
790 setName.setSelectedItem(curSetName);
794 settingDialog = false; // we need this name change to be registered.
795 argSetModified(setName, renamed);
800 private void addParameter(ParameterI arg)
802 ParamBox pb = paramSet.get(arg.getName());
805 pb = new ParamBox(this, arg);
806 paramSet.put(arg.getName(), pb);
810 // take the defaults from the parameter
811 pb.updateControls(arg);
814 private void setParameter(ParameterI arg)
816 ParamBox pb = paramSet.get(arg.getName());
823 pb.updateControls(arg);
828 private void selectOption(OptionI option, String string)
830 OptionBox cb = optSet.get(option.getName());
833 cb = addOption(option);
835 cb.enabled.setSelected(string != null); // initial state for an option.
838 if (option.getPossibleValues().contains(string))
840 cb.val.setSelectedItem(string);
844 throw new Error("Invalid value " + string + " for option " + option);
848 if (option.isRequired() && !cb.enabled.isSelected())
850 // TODO: indicate paramset is not valid.. option needs to be selected!
852 cb.setInitialValue();
855 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
857 public class ParamBox extends JPanel implements ChangeListener,
858 ActionListener, MouseListener
860 JButton showDesc = new JButton();
862 JTextArea string = new JTextArea();
864 JScrollPane descPanel = new JScrollPane();
866 JSlider slider = null;
868 JTextField valueField = null;
870 ValueConstrainI validator = null;
872 JPanel settingPanel = new JPanel();
874 JPanel controlPanel = new JPanel();
876 boolean integ = false;
878 boolean choice = false;
880 boolean descisvisible = false;
882 final WsJobParameters pmdialogbox;
886 public ParamBox(final WsJobParameters pmlayout, ParameterI parm)
888 pmdialogbox = pmlayout;
889 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
890 setBorder(new TitledBorder(parm.getName()));
892 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
893 showDesc.setText("+");
894 string.setFont(new Font("Verdana", Font.PLAIN, 11));
895 string.setBackground(getBackground());
896 // string.setSize(new Dimension(PARAM_WIDTH, 80));
897 string.setEditable(false);
898 descPanel.getViewport().setView(string);
899 // descPanel.setLocation(2,17);
900 descPanel.setVisible(false);
901 // string.setMinimumSize(new Dimension(140,80));
902 // string.setMaximumSize(new Dimension(280,80));
903 final ParamBox me = this;
904 finfo = parm.getFurtherDetails();
907 showDesc.setToolTipText("<html>"
909 .wrapTooltip("Click to show brief description<br><img src=\""
911 + "\"/> Right click for further information.")
913 showDesc.addMouseListener(this);
917 showDesc.setToolTipText("<html>"
919 .wrapTooltip("Click to show brief description.")
922 showDesc.addActionListener(new ActionListener()
925 public void actionPerformed(ActionEvent e)
927 descisvisible = !descisvisible;
928 descPanel.setVisible(descisvisible);
929 me.setPreferredSize(new Dimension(PARAM_WIDTH,
930 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
932 pmlayout.refreshParamLayout();
935 string.setWrapStyleWord(true);
936 string.setLineWrap(true);
937 string.setColumns(32);
938 string.setText(parm.getDescription());
939 JPanel firstrow = new JPanel();
940 firstrow.setLayout(null);
941 controlPanel.setLayout(new BorderLayout());
942 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
943 PARAM_CLOSEDHEIGHT - 50));
944 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
945 firstrow.add(showDesc);
946 firstrow.add(controlPanel);
947 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
948 PARAM_CLOSEDHEIGHT - 30));
950 validator = parm.getValidValue();
952 if (validator != null)
954 integ = validator.getType() == Type.Integer;
958 if (parameter.getPossibleValues() != null)
963 updateControls(parm);
964 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
965 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
972 // reset the widget's initial value.
976 boolean adjusting = false;
978 ParameterI parameter;
982 public int getBoxHeight()
984 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
987 public void updateControls(ParameterI parm)
990 boolean init = (choicebox == null && valueField == null);
997 choicebox = new JComboBox();
998 choicebox.addActionListener(this);
999 controlPanel.add(choicebox, BorderLayout.CENTER);
1003 slider = new JSlider();
1004 slider.addChangeListener(this);
1005 valueField = new JTextField();
1006 valueField.addActionListener(this);
1007 valueField.setPreferredSize(new Dimension(60, 25));
1008 controlPanel.add(slider, BorderLayout.WEST);
1009 controlPanel.add(valueField, BorderLayout.EAST);
1020 List vals = parm.getPossibleValues();
1021 for (Object val : vals)
1023 choicebox.addItem(val);
1027 if (parm.getDefaultValue() != null)
1029 choicebox.setSelectedItem(parm.getDefaultValue());
1034 valueField.setText(parm.getDefaultValue());
1037 lastVal = updateSliderFromValueField();
1043 public ParameterI getParameter()
1045 ParameterI prm = parameter.copy();
1048 prm.setDefaultValue((String) choicebox.getSelectedItem());
1052 prm.setDefaultValue(valueField.getText());
1057 public Object updateSliderFromValueField()
1061 if (validator != null)
1068 valueField.setText(valueField.getText().trim());
1069 iVal = Integer.valueOf(valueField.getText());
1070 if (validator.getMin() != null
1071 && validator.getMin().intValue() > iVal)
1073 iVal = validator.getMin().intValue();
1074 // TODO: provide visual indication that hard limit was reached for
1077 if (validator.getMax() != null
1078 && validator.getMax().intValue() < iVal)
1080 iVal = validator.getMax().intValue();
1081 // TODO: provide visual indication that hard limit was reached for
1084 } catch (Exception e)
1088 if (validator.getMin() != null && validator.getMax() != null)
1090 slider.getModel().setRangeProperties(iVal, 1,
1091 validator.getMin().intValue(),
1092 validator.getMax().intValue(), true);
1096 slider.setVisible(false);
1106 fVal = Float.valueOf(valueField.getText());
1107 if (validator.getMin() != null
1108 && validator.getMin().floatValue() > fVal)
1110 fVal = validator.getMin().floatValue();
1111 // TODO: provide visual indication that hard limit was reached for
1114 if (validator.getMax() != null
1115 && validator.getMax().floatValue() < fVal)
1117 fVal = validator.getMax().floatValue();
1118 // TODO: provide visual indication that hard limit was reached for
1121 } catch (Exception e)
1125 if (validator.getMin() != null && validator.getMax() != null)
1127 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1128 (int) validator.getMin().floatValue() * 1000,
1129 (int) validator.getMax().floatValue() * 1000, true);
1133 slider.setVisible(false);
1143 slider.setVisible(false);
1145 { valueField.getText().trim() };
1150 { (String) choicebox.getSelectedItem() };
1156 public void stateChanged(ChangeEvent e)
1160 valueField.setText(""
1161 + ((integ) ? ("" + (int) slider.getValue())
1162 : ("" + (float) (slider.getValue() / 1000f))));
1168 public void actionPerformed(ActionEvent e)
1176 updateSliderFromValueField();
1181 private void checkIfModified()
1183 Object cstate = updateSliderFromValueField();
1184 boolean notmod = false;
1185 if (cstate.getClass() == lastVal.getClass())
1187 if (cstate instanceof int[])
1189 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1191 else if (cstate instanceof float[])
1193 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1195 else if (cstate instanceof String[])
1197 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1200 pmdialogbox.argSetModified(this, !notmod);
1203 public void mouseClicked(MouseEvent e)
1205 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1207 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1211 public void mousePressed(MouseEvent e)
1213 // TODO Auto-generated method stub
1217 public void mouseReleased(MouseEvent e)
1219 // TODO Auto-generated method stub
1223 public void mouseEntered(MouseEvent e)
1225 // TODO Auto-generated method stub
1229 public void mouseExited(MouseEvent e)
1231 // TODO Auto-generated method stub
1237 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1239 public class OptionBox extends JPanel implements MouseListener,
1242 JComboBox val = new JComboBox();
1244 JCheckBox enabled = new JCheckBox();
1246 JLabel optlabel = new JLabel();
1250 boolean hasLink = false;
1254 public OptionBox(OptionI opt)
1257 setLayout(new BorderLayout());
1258 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1259 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1260 enabled.setText("");
1261 enabled.setText(opt.getName());
1262 enabled.addActionListener(this);
1263 finfo = option.getFurtherDetails();
1267 // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
1268 enabled.setToolTipText("<html>"
1269 + JvSwingUtils.wrapTooltip(opt.getDescription()
1270 + "<br><img src=\"" + linkImageURL + "\"/>")
1272 // optlabel.addMouseListener(this);
1273 enabled.addMouseListener(this);
1277 // optlabel.setToolTipText(opt.getDescription());
1278 enabled.setToolTipText("<html>"
1279 + JvSwingUtils.wrapTooltip(opt.getDescription())
1282 add(enabled, BorderLayout.NORTH);
1283 if (opt.getPossibleValues().size() > 1)
1285 setLayout(new GridLayout(1, 2));
1286 for (Object str : opt.getPossibleValues())
1288 val.addItem((String) str);
1290 val.setSelectedItem((String) opt.getDefaultValue());
1291 val.addActionListener(this);
1292 add(val, BorderLayout.SOUTH);
1294 // TODO: add actionListeners for popup (to open further info),
1295 // and to update list of parameters if an option is enabled
1296 // that takes a value.
1300 public void resetToDefault()
1302 enabled.setSelected(false);
1303 if (option.isRequired())
1305 // Apply default value
1306 selectOption(option, option.getDefaultValue());
1310 boolean initEnabled = false;
1312 String initVal = null;
1314 public void setInitialValue()
1316 initEnabled = enabled.isSelected();
1317 if (option.getPossibleValues() != null
1318 && option.getPossibleValues().size() > 1)
1320 initVal = (String) val.getSelectedItem();
1324 initVal = (initEnabled) ? option.getDefaultValue() : null;
1328 public OptionI getOptionIfEnabled()
1330 if (!enabled.isSelected())
1334 OptionI opt = option.copy();
1336 if (val.getSelectedItem() != null)
1338 opt.setDefaultValue((String) val.getSelectedItem());
1343 public void actionPerformed(ActionEvent e)
1345 if (e.getSource() != enabled)
1347 enabled.setSelected(true);
1352 private void checkIfModified()
1354 boolean notmod = (initEnabled == enabled.isSelected());
1355 if (enabled.isSelected())
1357 if (initVal != null)
1359 notmod &= initVal.equals(val.getSelectedItem());
1363 // compare against default service setting
1364 notmod &= option.getDefaultValue() == null
1365 || option.getDefaultValue().equals(val.getSelectedItem());
1370 notmod &= initVal == null;
1372 argSetModified(this, !notmod);
1375 public void mouseClicked(MouseEvent e)
1377 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1379 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1383 public void mousePressed(MouseEvent e)
1385 // TODO Auto-generated method stub
1389 public void mouseReleased(MouseEvent e)
1391 // TODO Auto-generated method stub
1395 public void mouseEntered(MouseEvent e)
1397 // TODO Auto-generated method stub
1401 public void mouseExited(MouseEvent e)
1403 // TODO Auto-generated method stub
1409 private OptionBox addOption(OptionI opt)
1411 OptionBox cb = optSet.get(opt.getName());
1414 cb = new OptionBox(opt);
1415 optSet.put(opt.getName(), cb);
1421 public static void showUrlPopUp(JComponent invoker, final String finfo,
1425 JPopupMenu mnu = new JPopupMenu();
1426 JMenuItem mitem = new JMenuItem("View " + finfo);
1427 mitem.addActionListener(new ActionListener()
1431 public void actionPerformed(ActionEvent e)
1433 Desktop.showUrl(finfo);
1438 mnu.show(invoker, x, y);
1441 protected void refreshParamLayout()
1443 FlowLayout fl = new FlowLayout();
1444 paramList.setLayout(fl);
1445 int s = 2 * fl.getVgap();
1446 for (ParamBox pbox : paramSet.values())
1448 s += fl.getVgap() + pbox.getBoxHeight(); // getBoxHeight();
1450 paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
1455 * testing method - grab a service and parameter set and show the window
1459 public static void main(String[] args)
1461 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1464 if (args.length > 3)
1466 Vector<String> services = new Vector<String>();
1467 services.addElement(args[p++]);
1468 Jws2Discoverer.setServiceUrls(services);
1473 } catch (Exception e)
1475 System.err.println("Aborting. Problem discovering services.");
1476 e.printStackTrace();
1479 Jws2Discoverer.Jws2Instance lastserv = null;
1480 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1483 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1485 if (lastserv != null)
1487 List<Preset> prl = null;
1489 if (++p < args.length)
1491 PresetManager prman = lastserv.getPresets();
1494 pr = prman.getPresetByName(args[p]);
1497 // just grab the last preset.
1498 prl = prman.getPresets();
1504 PresetManager prman = lastserv.getPresets();
1507 prl = prman.getPresets();
1510 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1511 while (en != null && en.hasNext())
1517 en = prl.iterator();
1522 System.out.println("Testing opts dupes for "
1523 + lastserv.getUri() + " : "
1524 + lastserv.getActionText() + ":" + pr.getName());
1525 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1530 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1531 } catch (Exception e)
1533 System.err.println("Failed to copy " + o.getName());
1534 e.printStackTrace();
1537 System.err.println("Failed to copy " + o.getName());
1538 e.printStackTrace();
1543 System.out.println("Testing param dupes:");
1544 List<Parameter> rg = lastserv.getRunnerConfig()
1546 for (Parameter o : rg)
1550 Parameter cpy = jalview.ws.jws2.ParameterUtils
1552 } catch (Exception e)
1554 System.err.println("Failed to copy " + o.getName());
1555 e.printStackTrace();
1558 System.err.println("Failed to copy " + o.getName());
1559 e.printStackTrace();
1564 System.out.println("Testing param write:");
1565 List<String> writeparam = null, readparam = null;
1568 writeparam = jalview.ws.jws2.ParameterUtils
1570 pr.getArguments(lastserv.getRunnerConfig()),
1572 System.out.println("Testing param read :");
1573 List<Option> pset = jalview.ws.jws2.ParameterUtils
1574 .processParameters(writeparam,
1575 lastserv.getRunnerConfig(), " ");
1576 readparam = jalview.ws.jws2.ParameterUtils
1577 .writeParameterSet(pset, " ");
1578 Iterator<String> o = pr.getOptions().iterator(), s = writeparam
1579 .iterator(), t = readparam.iterator();
1580 boolean failed = false;
1581 while (s.hasNext() && t.hasNext())
1583 String on = o.next(), sn = s.next(), st = t.next();
1586 System.out.println("Original was " + on
1587 + " Phase 1 wrote " + sn + "\tPhase 2 wrote "
1594 System.out.println("Original parameters:\n"
1596 System.out.println("Wrote parameters in first set:\n"
1598 System.out.println("Wrote parameters in second set:\n"
1602 } catch (Exception e)
1604 e.printStackTrace();
1607 WsJobParameters pgui = new WsJobParameters(lastserv,
1608 new JabaPreset(lastserv, pr));
1609 JFrame jf = new JFrame("Parameters for "
1610 + lastserv.getActionText());
1611 JPanel cont = new JPanel();
1612 // jf.setPreferredSize(new Dimension(600, 800));
1615 final Thread thr = Thread.currentThread();
1616 jf.addWindowListener(new WindowListener()
1619 public void windowActivated(WindowEvent e)
1621 // TODO Auto-generated method stub
1625 public void windowClosed(WindowEvent e)
1629 public void windowClosing(WindowEvent e)
1635 public void windowDeactivated(WindowEvent e)
1637 // TODO Auto-generated method stub
1641 public void windowDeiconified(WindowEvent e)
1643 // TODO Auto-generated method stub
1647 public void windowIconified(WindowEvent e)
1649 // TODO Auto-generated method stub
1653 public void windowOpened(WindowEvent e)
1655 // TODO Auto-generated method stub
1660 jf.setVisible(true);
1661 boolean inter = false;
1666 Thread.sleep(10000);
1667 } catch (Exception e)
1680 public List<ArgumentI> getJobParams()
1682 List<ArgumentI> argSet = new ArrayList<ArgumentI>();
1683 // recover options and parameters from GUI
1684 for (OptionBox opts : optSet.values())
1686 OptionI opt = opts.getOptionIfEnabled();
1692 for (ParamBox parambox : paramSet.values())
1694 ParameterI parm = parambox.getParameter();
1704 String lastParmSet = null;
1707 * Hashtable<String, Object[]> editedParams = new Hashtable<String,
1710 * store the given parameters in the user parameter set database.
1712 * @param storeSetName - lastParmSet
1714 * @param descr - setDescr.getText()
1716 * @param jobParams - getJobParams()
1718 * private void _storeUserPreset(String storeSetName, String descr,
1719 * List<ArgumentI> jobParams) { // this is a simple hash store. Object[] pset;
1720 * editedParams.put(storeSetName, pset = new Object[3]); pset[0] =
1721 * storeSetName; pset[1] = descr; pset[2] = jobParams; // writeParam("Saving "
1722 * + storeSetName + ": ", jobParams); }
1724 * private void writeParam(String nm, List<ArgumentI> params) { for (ArgumentI
1725 * p : params) { System.out.println(nm + ":" + System.identityHashCode(p) +
1726 * " Name: " + p.getName() + " Value: " + p.getDefaultValue()); } }
1728 * private Object[] _getUserPreset(String setName) { Object[] pset =
1729 * editedParams.get(setName); // if (pset != null) // writeParam("Retrieving "
1730 * + setName + ": ", (List<Argument>) pset[2]); return pset; }
1732 * * remove the given user preset from the preset stash
1736 * private void _deleteUserPreset(String setName) {
1737 * editedParams.remove(setName); }
1740 private void syncSetNamesWithStore()
1743 // remove any set names in the drop down menu that aren't either a reserved
1744 // setting, or a user defined or service preset.
1745 Vector items = new Vector();
1746 while (n < setName.getItemCount())
1748 String item = (String) setName.getItemAt(n);
1749 if (!item.equals(SVC_DEF) && !paramStore.presetExists(item))
1751 setName.removeItemAt(n);
1755 items.addElement(item);
1759 if (!items.contains(SVC_DEF))
1761 setName.addItem(SVC_DEF);
1763 for (WsParamSetI upn : paramStore.getPresets())
1765 if (!items.contains(upn.getName()))
1767 setName.addItem(upn.getName());
1773 * true if lastParmSet is a user preset
1775 boolean isUserPreset = false;
1777 private void reInitDialog(String nextPreset)
1779 settingDialog = true;
1780 // updateTable(null,null); // first reset to defaults
1781 WsParamSetI pset = null;
1782 if (nextPreset != null && nextPreset.length() > 0)
1784 pset = paramStore.getPreset(nextPreset);
1788 if (pset.isModifiable())
1790 isUserPreset = true;
1791 setDescr.setText(pset.getDescription());
1792 updateTable(null, pset.getArguments());
1793 lastParmSet = nextPreset;
1797 isUserPreset = false;
1798 setDescr.setText("");
1799 // must be a default preset from service
1800 updateTable(pset, null);
1801 lastParmSet = nextPreset;
1806 isUserPreset = false;
1808 setDescr.setText("");
1809 updateTable(null, null);
1810 lastParmSet = SVC_DEF;
1813 initArgSetModified();
1814 syncSetNamesWithStore();
1815 setName.setSelectedItem(lastParmSet);
1817 settingDialog = false;
1821 String curSetName = null;
1823 public void itemStateChanged(ItemEvent e)
1825 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1827 final String setname = (String) setName.getSelectedItem();
1828 System.out.println("Item state changed for " + setname
1829 + " (handling ? " + !settingDialog + ")");
1835 if (setname == null)
1839 javax.swing.SwingUtilities.invokeLater(new Runnable()
1843 doPreferenceComboStateChange(setname);
1849 private void doPreferenceComboStateChange(String setname)
1851 // user has selected a different item from combo-box
1854 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1855 if (lsetname.equals(setname))
1857 // setname was just edited - so ignore this event.
1860 settingDialog = true;
1861 System.out.println("Prompting to save " + lsetname);
1862 if (javax.swing.JOptionPane
1867 + "' is modifed, and your changes will be lost.\nReally change preset ?",
1868 "Warning: Unsaved Changes",
1869 javax.swing.JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
1871 // revert the combobox to the current item
1872 settingDialog = true;
1873 setName.setSelectedItem(lsetname);
1874 settingDialog = false;
1877 // System.out.println("Saving for " + lsetname);
1878 // _storeCurrentPreset(lsetname);
1882 settingDialog = true;
1883 reInitDialog(setname);
1884 settingDialog = false;
1888 private void _renameExistingPreset(String oldName, String curSetName2)
1890 paramStore.updatePreset(oldName, curSetName2, setDescr.getText(),
1895 * store current settings as given name. You should then reset gui.
1897 * @param curSetName2
1899 private void _storeCurrentPreset(String curSetName2)
1901 paramStore.storePreset(curSetName2, setDescr.getText(), getJobParams());
1904 private void _updatePreset(String lastParmSet2, String curname)
1906 paramStore.updatePreset(lastParmSet2, curname, setDescr.getText(),
1912 * last saved name for this user preset
1914 String lastSetName = null;
1917 * last saved value of the description text for this user preset
1919 String lastDescrText = null;
1921 public void actionPerformed(ActionEvent e)
1923 if (e.getSource() instanceof Component)
1925 Component src = (Component) e.getSource();
1926 if (src.getParent() == setName)
1928 // rename any existing records we know about for this set.
1929 String newname = (String) e.getActionCommand().trim();
1931 if (isServicePreset(newname))
1933 final String oldname = curSetName != null ? curSetName
1935 final Component ourframe = this;
1936 settingDialog = true;
1937 setName.getEditor().setItem(oldname);
1938 settingDialog = false;
1939 javax.swing.SwingUtilities.invokeLater(new Runnable()
1943 JOptionPane.showMessageDialog(ourframe,
1944 "Invalid name - preset already exists.",
1945 "Invalid name", JOptionPane.WARNING_MESSAGE);
1951 curSetName = newname;
1952 System.err.println("New name for user setting " + curSetName
1953 + " (was " + setName.getSelectedItem() + ")");
1954 if (curSetName.equals(setName.getSelectedItem()))
1958 if (curSetName != null)
1960 argSetModified(setName, true);
1968 private void checkDescrModified()
1975 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1976 : !setDescr.getText().equals(lastDescrText)));
1981 public void insertUpdate(DocumentEvent e)
1983 checkDescrModified();
1986 public void removeUpdate(DocumentEvent e)
1988 checkDescrModified();
1991 public void changedUpdate(DocumentEvent e)
1993 checkDescrModified();
1998 * @return null or the service preset selected by the user
2000 public WsParamSetI getPreset()
2002 if (isUserPreset || isModified()
2003 || (lastParmSet != null && lastParmSet.equals(SVC_DEF)))
2009 return paramStore.getPreset(lastParmSet);