3 import java.awt.BorderLayout;
5 import java.awt.Component;
6 import java.awt.Dimension;
7 import java.awt.FlowLayout;
9 import java.awt.GridLayout;
10 import java.awt.Rectangle;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.awt.event.ComponentEvent;
14 import java.awt.event.ComponentListener;
15 import java.awt.event.ContainerEvent;
16 import java.awt.event.ContainerListener;
17 import java.awt.event.ItemEvent;
18 import java.awt.event.ItemListener;
19 import java.awt.event.KeyEvent;
20 import java.awt.event.KeyListener;
21 import java.awt.event.MouseEvent;
22 import java.awt.event.MouseListener;
23 import java.awt.event.WindowEvent;
24 import java.awt.event.WindowListener;
25 import java.awt.event.WindowStateListener;
27 import java.util.ArrayList;
28 import java.util.EventObject;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.Vector;
36 import javax.swing.JButton;
37 import javax.swing.JCheckBox;
38 import javax.swing.JComboBox;
39 import javax.swing.JComponent;
40 import javax.swing.JDialog;
41 import javax.swing.JFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JList;
44 import javax.swing.JMenuItem;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JPopupMenu;
48 import javax.swing.JScrollPane;
49 import javax.swing.JSlider;
50 import javax.swing.JSplitPane;
51 import javax.swing.JTable;
52 import javax.swing.JTextArea;
53 import javax.swing.JTextField;
54 import javax.swing.ListSelectionModel;
55 import javax.swing.SwingConstants;
56 import javax.swing.SwingUtilities;
57 import javax.swing.border.TitledBorder;
58 import javax.swing.event.CellEditorListener;
59 import javax.swing.event.ChangeEvent;
60 import javax.swing.event.ChangeListener;
61 import javax.swing.event.DocumentEvent;
62 import javax.swing.event.DocumentListener;
63 import javax.swing.table.*;
65 import compbio.metadata.Argument;
66 import compbio.metadata.Option;
67 import compbio.metadata.Parameter;
68 import compbio.metadata.Preset;
69 import compbio.metadata.PresetManager;
70 import compbio.metadata.RunnerConfig;
71 import compbio.metadata.ValueConstrain;
72 import compbio.metadata.WrongParameterException;
73 import compbio.metadata.ValueConstrain.Type;
75 import jalview.util.jarInputStreamProvider;
76 import jalview.ws.jws2.Jws2Discoverer;
77 import jalview.ws.jws2.ParameterUtils;
78 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
81 * job parameter editing/browsing dialog box. User can browse existing settings
82 * (user + presets + Defaults), and any changes to parameters creates a modified
83 * user parameter set. LOGIC: If the parameter set is modified, and its name is
84 * a valid, non-existant user parameter set, then a save button is shown. If the
85 * parameter set is modified and its name is a valid, extant user parameter set,
86 * then an update button is shown. If user parameter set's name is edited, and
87 * old name exists as a writable user parameter set, then rename button is
88 * shown. If current parameter set is associated with a user defined parameter
89 * set, then : if set is modifed, a 'revert' button is shown. if set is not
90 * modified, a 'delete' button is shown.
95 public class WsJobParameters extends JPanel implements ItemListener,
96 ActionListener, DocumentListener
98 private static final String SVC_DEF = "Defaults"; // this is the null
99 // parameter set as shown to
102 private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150,
103 PARAM_CLOSEDHEIGHT = 80;
105 private static final int OPTSET_HEIGHT = 30;
107 JPanel SetNamePanel = new JPanel();
109 JPanel setDetails = new JPanel();
111 JSplitPane settingsPanel = new JSplitPane();
113 JSplitPane jobPanel = new JSplitPane();
115 JPanel jobOptions = new JPanel();
117 JScrollPane jobOptionsPane = new JScrollPane();
119 JPanel jobParameters = new JPanel();
121 JButton createpref = new JButton();
123 JButton deletepref = new JButton();
125 JButton revertpref = new JButton();
127 JButton updatepref = new JButton();
129 JButton startjob = new JButton();
131 JButton canceljob = new JButton();
133 JComboBox setName = new JComboBox();
135 JTextArea setDescr = new JTextArea();
137 JScrollPane paramPane = new JScrollPane();
139 // JList paramList = new JList();
140 JPanel paramList = new JPanel();
142 RunnerConfig serviceOptions;
144 private BorderLayout jparamLayout;
146 WsJobParameters(Jws2Instance service)
151 public WsJobParameters(Jws2Instance service, Preset p)
153 this(null, service, p, null);
159 * - if null, create new JFrame outside of desktop
163 public WsJobParameters(JFrame parent, Jws2Instance service, Preset p,
164 List<Argument> jobArgset)
168 // argSetModified(false);
169 // populate parameter table
170 initForService(service, p, jobArgset);
171 // display in new JFrame attached to parent.
177 JDialog frame = null;
180 * shows a modal dialog containing the parameters.
184 public boolean showRunDialog()
187 frame = new JDialog(Desktop.instance, true);
189 frame.setTitle("Edit parameters for " + service.getActionText());
190 Rectangle deskr = Desktop.instance.getBounds();
191 frame.setBounds(new Rectangle((int) (deskr.getCenterX() - 240),
192 (int) (deskr.getCenterY() - 250), 480, 500));
193 frame.setContentPane(this);
194 // should recover defaults from user prefs.
196 settingsPanel.setDividerLocation(0.4);
197 jobPanel.setDividerLocation(0.3);
198 frame.setVisible(true);
207 protected JButton makeButton(String label, String tooltip,
208 ActionListener action)
210 JButton button = new JButton();
211 button.setText(label);
212 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
213 button.setForeground(Color.black);
214 button.setHorizontalAlignment(SwingConstants.CENTER);
215 button.setToolTipText(tooltip);
216 button.addActionListener(action);
220 private void jbInit()
222 updatepref = makeButton("Update", "Update the stored user preference",
226 public void actionPerformed(ActionEvent e)
228 update_actionPerformed(e);
231 deletepref = makeButton("Delete", "Delete the user preference",
235 public void actionPerformed(ActionEvent e)
237 delete_actionPerformed(e);
240 createpref = makeButton("Create", "Create a new preference",
244 public void actionPerformed(ActionEvent e)
246 create_actionPerformed(e);
249 revertpref = makeButton("Revert",
250 "Undo all changes to the current set", new ActionListener()
253 public void actionPerformed(ActionEvent e)
255 revert_actionPerformed(e);
258 startjob.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
259 startjob.setText("Start");
260 startjob.setToolTipText("Start Job");
261 startjob.addActionListener(new ActionListener()
263 public void actionPerformed(ActionEvent e)
265 startjob_actionPerformed(e);
268 canceljob.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
269 canceljob.setText("Cancel");
270 canceljob.setToolTipText("Cancel Job");
271 canceljob.addActionListener(new ActionListener()
273 public void actionPerformed(ActionEvent e)
275 canceljob_actionPerformed(e);
279 setDetails.setBorder(new TitledBorder("Details"));
280 setDetails.setLayout(new BorderLayout());
281 setDescr.setColumns(40);
282 setDescr.setWrapStyleWord(true);
283 setDescr.setLineWrap(true);
284 setDescr.setBackground(getBackground());
285 setDescr.setEditable(true);
286 setDescr.getDocument().addDocumentListener(this);
287 JScrollPane setDescrView = new JScrollPane();
288 // setDescrView.setPreferredSize(new Dimension(350, 200));
289 setDescrView.getViewport().setView(setDescr);
290 setName.setEditable(true);
291 setName.addItemListener(this);
292 setName.getEditor().addActionListener(this);
293 SetNamePanel.setLayout(new BorderLayout());
294 SetNamePanel.setMinimumSize(new Dimension(300, 35));
295 SetNamePanel.add(setName, BorderLayout.WEST);
296 // initial button visibility
297 updatepref.setVisible(false);
298 deletepref.setVisible(false);
299 revertpref.setVisible(false);
300 createpref.setVisible(false);
301 JPanel setsavebuts = new JPanel();
302 setsavebuts.setMinimumSize(new Dimension(150, 35));
303 setsavebuts.setLayout(new FlowLayout());
304 setsavebuts.add(createpref, BorderLayout.CENTER);
305 setsavebuts.add(updatepref, BorderLayout.CENTER);
306 setsavebuts.add(revertpref, BorderLayout.EAST);
307 setsavebuts.add(deletepref, BorderLayout.EAST);
308 SetNamePanel.add(setsavebuts, BorderLayout.EAST);
309 setDetails.add(setDescrView, BorderLayout.CENTER);
310 // setDetails.setPreferredSize(new Dimension(360, 100));
311 jobParameters.setBorder(new TitledBorder("Parameters"));
312 jobParameters.setLayout(jparamLayout = new BorderLayout());
313 paramPane.setPreferredSize(new Dimension(360, 300));
314 paramPane.getVerticalScrollBar().setUnitIncrement(20);
315 // paramPanel.setPreferredSize(new Dimension(360, 300));
316 // TODO: relayout buttons nicely
317 paramPane.getViewport().setView(paramList);
318 jobParameters.add(paramPane, BorderLayout.CENTER);
319 JPanel jobOptionsPanel = new JPanel();
320 jobOptionsPanel.setLayout(new BorderLayout());
321 jobOptionsPanel.setBorder(new TitledBorder("Options"));
322 jobOptionsPane.getViewport().setView(jobOptions);
323 jobOptionsPanel.add(jobOptionsPane, BorderLayout.CENTER);
324 settingsPanel.setLeftComponent(jobOptionsPanel);
325 settingsPanel.setRightComponent(jobParameters);
326 settingsPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
328 setLayout(new BorderLayout());
329 // setPreferredSize(new Dimension(400, 600));
330 // setSize(new Dimension(400, 600));
331 jobPanel.setLeftComponent(setDetails);
332 jobPanel.setRightComponent(settingsPanel);
333 jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
334 add(SetNamePanel, BorderLayout.NORTH);
335 add(jobPanel, BorderLayout.CENTER);
336 JPanel dialogpanel = new JPanel();
337 dialogpanel.add(startjob);
338 dialogpanel.add(canceljob);
339 add(dialogpanel, BorderLayout.SOUTH);
342 protected void revert_actionPerformed(ActionEvent e)
344 reInitDialog(lastParmSet);
348 protected void update_actionPerformed(ActionEvent e)
352 String curname = ((String) setName.getSelectedItem()).trim();
353 if (curname.length() > 0 && !curname.equals(lastParmSet))
355 _deleteUserPreset(lastParmSet);
357 _storeCurrentPreset(curname);
358 lastParmSet = curname;
360 initArgSetModified();
364 protected void delete_actionPerformed(ActionEvent e)
368 // delete current preset's saved entry
369 _deleteUserPreset(lastParmSet);
371 reInitDialog(null); // service default
374 protected void create_actionPerformed(ActionEvent e)
376 String curname = ((String) setName.getSelectedItem()).trim();
377 if (curname.length() > 0)
379 _storeCurrentPreset(curname);
380 lastParmSet = curname;
382 initArgSetModified();
386 // TODO: show warning
387 System.err.println("Invalid name. Not saved.");
391 protected void canceljob_actionPerformed(ActionEvent e)
396 frame.setVisible(false);
400 protected void startjob_actionPerformed(ActionEvent e)
405 frame.setVisible(false);
409 Jws2Instance service;
412 * list of service presets in the gui
414 Hashtable servicePresets = null;
417 * set if dialog is being set - so handlers will avoid spurious events
419 boolean settingDialog = false;
421 void initForService(Jws2Instance service, Preset p,
422 List<Argument> jobArgset)
424 settingDialog = true;
425 this.service = service;
426 // TODO: Recover window geometry prefs for this service
427 // jobPanel.setDividerLocation(proportionalLocation)
428 // settingsPanel.setDividerLocation(proportionalLocation)
429 Hashtable exnames = new Hashtable();
430 for (int i = 0, iSize = setName.getItemCount(); i < iSize; i++)
432 exnames.put((String) setName.getItemAt(i), setName.getItemAt(i));
434 // Add the default entry - if not present already.
435 if (!exnames.contains(SVC_DEF))
437 setName.addItem(SVC_DEF);
438 exnames.put(SVC_DEF, SVC_DEF);
440 serviceOptions = service.getRunnerConfig();
441 // add any presets not already added.
442 String curname = (p == null ? "" : p.getName());
443 PresetManager prman = service.getPresets();
444 servicePresets = new Hashtable();
447 List prList = service.getPresets().getPresets();
450 for (Object pr : prList)
452 servicePresets.put(((Preset) pr).getName(), "preset");
453 if (!exnames.contains(((Preset) pr).getName()))
455 setName.addItem(((Preset) pr).getName());
460 // TODO: if initial jobArgset matches a given user setting or preset then
461 // should recover setting accordingly
462 // updateTable(p, jobArgset);
465 reInitDialog(p.getName());
466 initArgSetModified();
470 if (jobArgset != null && jobArgset.size() > 0)
472 curSetName = "Supplied Settings";
473 updateTable(p, jobArgset);
481 settingDialog = false;
485 @SuppressWarnings("unchecked")
486 private void updateTable(Preset p, List<Argument> jobArgset)
488 List<Parameter> setargs = new ArrayList<Parameter>();
489 // populate table from default parameter set.
490 List<Argument> args = serviceOptions.getArguments();
492 // split to params and required arguments
494 for (Argument arg : args)
496 Argument myarg = (Argument) arg;
497 // Ideally, Argument would implement isRequired !
498 if (myarg instanceof Parameter)
500 Parameter parm = (Parameter) myarg;
505 if (myarg instanceof Option)
507 Option opt = (Option) myarg;
508 addOption(opt).resetToDefault();
512 System.err.println("Ignoring unknown service argument type "
513 + arg.getClass().getName());
517 args = null; // no more args to process.
521 isUserPreset = false;
522 // initialise setname
523 setName.setSelectedItem(lastSetName = p.getName());
524 setDescr.setText(lastDescrText = p.getDescription());
528 args = p.getArguments(serviceOptions);
529 } catch (Exception e)
533 // TODO: check if args should be unselected prior to resetting using the
539 if (lastParmSet == null)
541 isUserPreset = false;
542 // first call - so create a dummy name
543 setName.setSelectedItem(lastSetName = SVC_DEF);
547 if (jobArgset != null)
549 argSetModified(jobArgset, true);
552 // get setargs from current object
555 for (Argument arg : args)
557 if (arg instanceof Parameter)
559 setParameter((Parameter) arg);
563 if (arg instanceof Option)
565 // System.out.println("Setting option "
566 // + System.identityHashCode(arg) + ":" + arg.getName()
567 // + " with " + arg.getDefaultValue());
568 selectOption((Option) arg, arg.getDefaultValue());
575 jobOptions.setPreferredSize(new Dimension(PARAM_WIDTH, optSet.size()
577 jobOptions.setLayout(new GridLayout(optSet.size(), 1));
578 refreshParamLayout();
579 paramPane.validate();
583 private boolean isModified()
585 return modifiedElements.size() > 0;
588 private Hashtable modifiedElements = new Hashtable();
591 * reset gui and modification state settings
593 private void initArgSetModified()
596 modifiedElements.clear();
597 updateButtonDisplay();
600 private void updateButtonDisplay()
602 boolean _update = false, _create = false, _delete = false, _revert = false;
603 if (modifiedElements.size() > 0)
607 _update = isUserPreset; // can only update user presets
608 if (!isUserPreset || modifiedElements.containsKey(setName))
610 // name modified - can create new preset
618 // can still delete a user preset
619 _delete = isUserPreset;
621 createpref.setVisible(_create);
622 updatepref.setVisible(_update);
623 deletepref.setVisible(_delete);
624 revertpref.setVisible(_revert);
628 private void argSetModified(Object modifiedElement, boolean b)
637 modifiedElements.remove(modifiedElement);
641 modifiedElements.put(modifiedElement, modifiedElement);
643 // set mod status based on presence of elements in table
644 if (b && modifiedElements.size() > 0)
646 makeSetNameValid(!isUserPreset);
648 updateButtonDisplay();
651 private boolean isServicePreset(String selectedItem)
653 return selectedItem.equals(SVC_DEF)
654 || servicePresets.containsKey(selectedItem);
658 * check if the current set name is a valid set name for saving, if not, then
661 private void makeSetNameValid(boolean newuserset)
663 boolean stn = settingDialog;
664 boolean renamed = false;
665 settingDialog = true;
666 String nm = (String) setName.getSelectedItem();
667 // check if the name is reserved - if it is, rename it.
668 if (isServicePreset(nm))
677 while (_getUserPreset(tnm) != null)
679 tnm = nm + " (" + (++i) + ")";
687 boolean makeupdate = false;
688 // sync the gui with the preset database
689 for (int i = 0, iS = setName.getItemCount(); i < iS; i++)
691 String snm = (String) setName.getItemAt(i);
695 // setName.setSelectedIndex(i);
700 setName.setSelectedItem(curSetName = nm);
705 private void addParameter(Parameter parm)
707 ParamBox pb = paramSet.get(parm.getName());
710 pb = new ParamBox(this, parm);
711 paramSet.put(parm.getName(), pb);
715 // take the defaults from the parameter
716 pb.updateControls(parm);
719 private void setParameter(Parameter arg)
721 ParamBox pb = paramSet.get(arg.getName());
728 pb.updateControls(arg);
733 private void selectOption(Option opt, String string)
735 OptionBox cb = optSet.get(opt.getName());
740 cb.enabled.setSelected(true); // initial state for an option.
743 if (opt.getPossibleValues().contains(string))
745 cb.val.setSelectedItem(string);
749 throw new Error("Invalid value " + string + " for option " + opt);
753 if (opt.isRequired() && !cb.enabled.isSelected())
755 // TODO: indicate paramset is not valid.. option needs to be selected!
757 cb.setInitialValue();
760 Map<String, ParamBox> paramSet = new Hashtable<String, ParamBox>();
762 public class ParamBox extends JPanel implements ChangeListener,
763 ActionListener, MouseListener
765 JButton showDesc = new JButton();
767 JTextArea string = new JTextArea();
769 JScrollPane descPanel = new JScrollPane();
771 JSlider slider = null;
773 JTextField valueField = null;
775 ValueConstrain validator = null;
777 JPanel settingPanel = new JPanel();
779 JPanel controlPanel = new JPanel();
781 boolean integ = false;
783 boolean choice = false;
785 boolean descisvisible = false;
787 final WsJobParameters pmdialogbox;
791 public ParamBox(final WsJobParameters pmlayout, Parameter parm)
793 pmdialogbox = pmlayout;
794 setPreferredSize(new Dimension(PARAM_WIDTH, PARAM_CLOSEDHEIGHT));
795 setBorder(new TitledBorder(parm.getName()));
797 showDesc.setFont(new Font("Verdana", Font.PLAIN, 6));
798 showDesc.setText("+");
799 string.setFont(new Font("Verdana", Font.PLAIN, 11));
800 string.setBackground(getBackground());
801 // string.setSize(new Dimension(PARAM_WIDTH, 80));
802 string.setEditable(false);
803 descPanel.getViewport().setView(string);
804 // descPanel.setLocation(2,17);
805 descPanel.setVisible(false);
806 // string.setMinimumSize(new Dimension(140,80));
807 // string.setMaximumSize(new Dimension(280,80));
808 final ParamBox me = this;
809 finfo = parm.getFurtherDetails();
812 showDesc.setToolTipText("<html><p>Click to show brief description, and right click to open link for further information.</p></html>");
813 showDesc.addMouseListener(this);
817 showDesc.setToolTipText("<html><p>Click to show brief description.</p></html>");
819 showDesc.addActionListener(new ActionListener()
822 public void actionPerformed(ActionEvent e)
824 descisvisible = !descisvisible;
825 descPanel.setVisible(descisvisible);
826 me.setPreferredSize(new Dimension(PARAM_WIDTH,
827 (descisvisible) ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT));
829 pmlayout.refreshParamLayout();
832 string.setWrapStyleWord(true);
833 string.setLineWrap(true);
834 string.setColumns(32);
835 string.setText(parm.getDescription());
836 JPanel firstrow = new JPanel();
837 firstrow.setLayout(null);
838 controlPanel.setLayout(new BorderLayout());
839 controlPanel.setBounds(new Rectangle(39, 10, PARAM_WIDTH - 70,
840 PARAM_CLOSEDHEIGHT - 50));
841 showDesc.setBounds(new Rectangle(10, 10, 16, 16));
842 firstrow.add(showDesc);
843 firstrow.add(controlPanel);
844 firstrow.setBounds(new Rectangle(10, 20, PARAM_WIDTH - 30,
845 PARAM_CLOSEDHEIGHT - 30));
847 validator = parm.getValidValue();
849 if (validator != null)
851 integ = validator.getType() == Type.Integer;
855 if (parameter.getPossibleValues() != null)
860 updateControls(parm);
861 descPanel.setBounds(new Rectangle(10, PARAM_CLOSEDHEIGHT,
862 PARAM_WIDTH - 20, PARAM_HEIGHT - PARAM_CLOSEDHEIGHT - 5));
869 // reset the widget's initial value.
873 boolean adjusting = false;
879 public int getBoxHeight()
881 return (descisvisible ? PARAM_HEIGHT : PARAM_CLOSEDHEIGHT);
884 public void updateControls(Parameter parm)
887 boolean init = (choicebox == null && valueField == null);
894 choicebox = new JComboBox();
895 choicebox.addActionListener(this);
896 controlPanel.add(choicebox, BorderLayout.CENTER);
900 slider = new JSlider();
901 slider.addChangeListener(this);
902 valueField = new JTextField();
903 valueField.addActionListener(this);
904 valueField.setPreferredSize(new Dimension(60, 25));
905 controlPanel.add(slider, BorderLayout.WEST);
906 controlPanel.add(valueField, BorderLayout.EAST);
917 List vals = parm.getPossibleValues();
918 for (Object val : vals)
920 choicebox.addItem(val);
924 if (parm.getDefaultValue() != null)
926 choicebox.setSelectedItem(parm.getDefaultValue());
931 valueField.setText(parm.getDefaultValue());
934 lastVal = updateSliderFromValueField();
940 public Parameter getParameter()
942 Parameter prm = ParameterUtils.copyParameter(parameter);
947 prm.setDefaultValue((String) choicebox.getSelectedItem());
951 prm.setDefaultValue(valueField.getText());
953 } catch (WrongParameterException e)
961 public Object updateSliderFromValueField()
965 if (validator != null)
972 valueField.setText(valueField.getText().trim());
973 iVal = Integer.valueOf(valueField.getText());
974 } catch (Exception e)
978 if (validator.getMin() != null && validator.getMax() != null)
980 slider.getModel().setRangeProperties(iVal, 1,
981 validator.getMin().intValue(),
982 validator.getMax().intValue(), true);
986 slider.setVisible(false);
996 fVal = Float.valueOf(valueField.getText());
997 } catch (Exception e)
1001 if (validator.getMin() != null && validator.getMax() != null)
1003 slider.getModel().setRangeProperties((int) fVal * 1000, 1,
1004 (int) validator.getMin().floatValue() * 1000,
1005 (int) validator.getMax().floatValue() * 1000, true);
1009 slider.setVisible(false);
1019 slider.setVisible(false);
1021 { valueField.getText().trim() };
1026 { (String) choicebox.getSelectedItem() };
1032 public void stateChanged(ChangeEvent e)
1036 valueField.setText(""
1037 + ((integ) ? ("" + (int) slider.getValue())
1038 : ("" + (float) (slider.getValue() / 1000f))));
1044 public void actionPerformed(ActionEvent e)
1052 updateSliderFromValueField();
1057 private void checkIfModified()
1059 Object cstate = updateSliderFromValueField();
1060 boolean notmod = false;
1061 if (cstate.getClass() == lastVal.getClass())
1063 if (cstate instanceof int[])
1065 notmod = (((int[]) cstate)[0] == ((int[]) lastVal)[0]);
1067 else if (cstate instanceof float[])
1069 notmod = (((float[]) cstate)[0] == ((float[]) lastVal)[0]);
1071 else if (cstate instanceof String[])
1073 notmod = (((String[]) cstate)[0].equals(((String[]) lastVal)[0]));
1076 pmdialogbox.argSetModified(this, !notmod);
1079 public void mouseClicked(MouseEvent e)
1081 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1083 Desktop.showUrl(finfo.toString());
1087 public void mousePressed(MouseEvent e)
1089 // TODO Auto-generated method stub
1093 public void mouseReleased(MouseEvent e)
1095 // TODO Auto-generated method stub
1099 public void mouseEntered(MouseEvent e)
1101 // TODO Auto-generated method stub
1105 public void mouseExited(MouseEvent e)
1107 // TODO Auto-generated method stub
1113 Map<String, OptionBox> optSet = new Hashtable<String, OptionBox>();
1115 public class OptionBox extends JPanel implements MouseListener,
1118 JComboBox val = new JComboBox();
1120 JCheckBox enabled = new JCheckBox();
1122 JLabel optlabel = new JLabel();
1124 URL linkImageURL = getClass().getResource("/images/link.gif");
1128 boolean hasLink = false;
1132 public OptionBox(Option opt)
1135 setLayout(new BorderLayout());
1136 enabled.setSelected(opt.isRequired()); // TODO: lock required options
1137 enabled.setFont(new Font("Verdana", Font.PLAIN, 11));
1138 enabled.setText("");
1139 enabled.setText(opt.getName());
1140 enabled.addActionListener(this);
1141 finfo = option.getFurtherDetails();
1145 // optlabel.setToolTipText("<html><p>"+opt.getDescription()+"</p><img src=\""+linkImageURL+"\"/></html>");
1146 enabled.setToolTipText("<html><p>" + opt.getDescription()
1147 + "</p><img src=\"" + linkImageURL + "\"/></html>");
1148 // optlabel.addMouseListener(this);
1149 enabled.addMouseListener(this);
1153 // optlabel.setToolTipText(opt.getDescription());
1154 enabled.setToolTipText(opt.getDescription());
1156 add(enabled, BorderLayout.NORTH);
1157 if (opt.getPossibleValues().size() > 1)
1159 setLayout(new GridLayout(1, 2));
1160 for (Object str : opt.getPossibleValues())
1162 val.addItem((String) str);
1164 val.setSelectedItem((String) opt.getDefaultValue());
1165 val.addActionListener(this);
1166 add(val, BorderLayout.SOUTH);
1168 // TODO: add actionListeners for popup (to open further info),
1169 // and to update list of parameters if an option is enabled
1170 // that takes a value.
1174 public void resetToDefault()
1176 enabled.setSelected(false);
1177 if (option.isRequired())
1179 // Apply default value
1180 selectOption(option, option.getDefaultValue());
1184 boolean initEnabled = false;
1186 String initVal = null;
1188 public void setInitialValue()
1190 initEnabled = enabled.isSelected();
1191 if (option.getPossibleValues() != null
1192 && option.getPossibleValues().size() > 1)
1194 initVal = (String) val.getSelectedItem();
1198 initVal = (initEnabled) ? option.getDefaultValue() : null;
1202 public Option getOptionIfEnabled()
1204 if (!enabled.isSelected())
1210 Option opt = jalview.ws.jws2.ParameterUtils.copyOption(option);
1212 if (val.getSelectedItem() != null)
1214 opt.setDefaultValue((String) val.getSelectedItem());
1216 } catch (WrongParameterException e)
1218 e.printStackTrace();
1224 public void actionPerformed(ActionEvent e)
1226 if (e.getSource() != enabled)
1228 enabled.setSelected(true);
1233 private void checkIfModified()
1235 boolean notmod = (initEnabled == enabled.isSelected());
1236 if (enabled.isSelected())
1238 if (initVal != null)
1240 notmod |= initVal.equals(val.getSelectedItem());
1247 notmod |= initVal == null;
1249 argSetModified(this, !notmod);
1252 public void mouseClicked(MouseEvent e)
1254 if (javax.swing.SwingUtilities.isRightMouseButton(e))
1256 showUrlPopUp(this, finfo.toString(), e.getX(), e.getY());
1260 public void mousePressed(MouseEvent e)
1262 // TODO Auto-generated method stub
1266 public void mouseReleased(MouseEvent e)
1268 // TODO Auto-generated method stub
1272 public void mouseEntered(MouseEvent e)
1274 // TODO Auto-generated method stub
1278 public void mouseExited(MouseEvent e)
1280 // TODO Auto-generated method stub
1286 private OptionBox addOption(Option opt)
1288 OptionBox cb = optSet.get(opt.getName());
1291 cb = new OptionBox(opt);
1292 optSet.put(opt.getName(), cb);
1298 public static void showUrlPopUp(JComponent invoker, final String finfo,
1302 JPopupMenu mnu = new JPopupMenu();
1303 JMenuItem mitem = new JMenuItem("View " + finfo);
1304 mitem.addActionListener(new ActionListener()
1308 public void actionPerformed(ActionEvent e)
1310 Desktop.showUrl(finfo);
1315 mnu.show(invoker, x, y);
1318 protected void refreshParamLayout()
1321 for (ParamBox pbox : paramSet.values())
1323 s += pbox.getBoxHeight();
1325 paramList.setPreferredSize(new Dimension(PARAM_WIDTH, s));
1326 paramList.setLayout(new FlowLayout());
1331 * testing method - grab a service and parameter set and show the window
1335 public static void main(String[] args)
1337 jalview.ws.jws2.Jws2Discoverer disc = jalview.ws.jws2.Jws2Discoverer
1340 if (args.length > 3)
1342 Vector<String> services = new Vector<String>();
1343 services.addElement(args[p++]);
1344 Jws2Discoverer.setServiceUrls(services);
1349 } catch (Exception e)
1351 System.err.println("Aborting. Problem discovering services.");
1352 e.printStackTrace();
1355 Jws2Discoverer.Jws2Instance lastserv = null;
1356 for (Jws2Discoverer.Jws2Instance service : disc.getServices())
1359 if (p >= args.length || service.serviceType.equalsIgnoreCase(args[p]))
1361 if (lastserv != null)
1363 List<Preset> prl = null;
1365 if (++p < args.length)
1367 PresetManager prman = lastserv.getPresets();
1370 pr = prman.getPresetByName(args[p]);
1373 // just grab the last preset.
1374 prl = prman.getPresets();
1380 PresetManager prman = lastserv.getPresets();
1383 prl = prman.getPresets();
1386 Iterator<Preset> en = (prl == null) ? null : prl.iterator();
1387 while (en != null && en.hasNext())
1393 en = prl.iterator();
1398 System.out.println("Testing opts dupes for "
1399 + lastserv.getHost() + " : "
1400 + lastserv.getActionText() + ":" + pr.getName());
1401 List<Option> rg = lastserv.getRunnerConfig().getOptions();
1406 Option cpy = jalview.ws.jws2.ParameterUtils.copyOption(o);
1407 } catch (Exception e)
1409 System.err.println("Failed to copy " + o.getName());
1410 e.printStackTrace();
1413 System.err.println("Failed to copy " + o.getName());
1414 e.printStackTrace();
1419 System.out.println("Testing param dupes:");
1420 List<Parameter> rg = lastserv.getRunnerConfig()
1422 for (Parameter o : rg)
1426 Parameter cpy = jalview.ws.jws2.ParameterUtils
1428 } catch (Exception e)
1430 System.err.println("Failed to copy " + o.getName());
1431 e.printStackTrace();
1434 System.err.println("Failed to copy " + o.getName());
1435 e.printStackTrace();
1439 WsJobParameters pgui = new WsJobParameters(lastserv, pr);
1440 JFrame jf = new JFrame("Parameters for "
1441 + lastserv.getActionText());
1442 JPanel cont = new JPanel();
1443 // jf.setPreferredSize(new Dimension(600, 800));
1446 final Thread thr = Thread.currentThread();
1447 jf.addWindowListener(new WindowListener()
1450 public void windowActivated(WindowEvent e)
1452 // TODO Auto-generated method stub
1456 public void windowClosed(WindowEvent e)
1460 public void windowClosing(WindowEvent e)
1466 public void windowDeactivated(WindowEvent e)
1468 // TODO Auto-generated method stub
1472 public void windowDeiconified(WindowEvent e)
1474 // TODO Auto-generated method stub
1478 public void windowIconified(WindowEvent e)
1480 // TODO Auto-generated method stub
1484 public void windowOpened(WindowEvent e)
1486 // TODO Auto-generated method stub
1491 jf.setVisible(true);
1492 boolean inter = false;
1497 Thread.sleep(10000);
1498 } catch (Exception e)
1511 public List<Argument> getJobParams()
1513 List<Argument> argSet = new ArrayList<Argument>();
1514 // recover options and parameters from GUI
1515 for (OptionBox opts : optSet.values())
1517 Option opt = opts.getOptionIfEnabled();
1523 for (ParamBox parambox : paramSet.values())
1525 Parameter parm = parambox.getParameter();
1535 String lastParmSet = null;
1537 Hashtable<String, Object[]> editedParams = new Hashtable<String, Object[]>();
1540 * store the given parameters in the user parameter set database.
1542 * @param storeSetName
1545 * - setDescr.getText()
1549 private void _storeUserPreset(String storeSetName, String descr,
1550 List<Argument> jobParams)
1552 // this is a simple hash store.
1554 editedParams.put(storeSetName, pset = new Object[3]);
1555 pset[0] = storeSetName;
1557 pset[2] = jobParams;
1558 // writeParam("Saving " + storeSetName + ": ", jobParams);
1561 private void writeParam(String nm, List<Argument> params)
1563 for (Argument p : params)
1565 System.out.println(nm + ":" + System.identityHashCode(p) + " Name: "
1566 + p.getName() + " Value: " + p.getDefaultValue());
1570 private Object[] _getUserPreset(String setName)
1572 Object[] pset = editedParams.get(setName);
1573 // if (pset != null)
1574 // writeParam("Retrieving " + setName + ": ", (List<Argument>) pset[2]);
1579 * remove the given user preset from the preset stash
1583 private void _deleteUserPreset(String setName)
1585 editedParams.remove(setName);
1588 private void syncSetNamesWithStore()
1591 // remove any set names in the drop down menu that aren't either a reserved
1592 // setting, or a user defined or service preset.
1593 Vector items = new Vector();
1594 while (n < setName.getItemCount())
1596 String item = (String) setName.getItemAt(n);
1597 if (!isServicePreset(item) && _getUserPreset(item) == null)
1599 setName.removeItemAt(n);
1603 items.addElement(item);
1607 for (String upn : editedParams.keySet())
1609 if (!items.contains(upn))
1611 setName.addItem(upn);
1617 * true if lastParmSet is a user preset
1619 boolean isUserPreset = false;
1621 private void reInitDialog(String nextPreset)
1623 settingDialog = true;
1624 // updateTable(null,null); // first reset to defaults
1625 Object[] pset = null;
1626 if (nextPreset != null && nextPreset.length() > 0)
1628 pset = _getUserPreset(nextPreset);
1632 isUserPreset = true;
1633 setDescr.setText((String) pset[1]);
1634 updateTable(null, (List<Argument>) pset[2]);
1635 lastParmSet = nextPreset;
1639 isUserPreset = false;
1640 setDescr.setText("");
1641 // must be a default preset from service
1645 PresetManager prman = service.getPresets();
1648 isUserPreset = false;
1649 p = prman.getPresetByName(nextPreset);
1651 } catch (Exception ex)
1653 ex.printStackTrace();
1657 updateTable(p, null);
1658 lastParmSet = nextPreset;
1662 updateTable(null, null);
1663 lastParmSet = SVC_DEF;
1666 initArgSetModified();
1667 syncSetNamesWithStore();
1668 setName.setSelectedItem(lastParmSet);
1670 settingDialog = false;
1674 String curSetName = null;
1676 public void itemStateChanged(ItemEvent e)
1678 if (e.getSource() == setName && e.getStateChange() == e.SELECTED)
1680 String setname = (String) setName.getSelectedItem();
1681 System.out.println("Item state changed for " + setname
1682 + " (handling ? " + !settingDialog + ")");
1688 if (setname == null)
1692 // user has selected a different item from combo-box
1695 String lsetname = (curSetName != null) ? curSetName : lastParmSet;
1696 System.out.println("Prompting to save " + lsetname);
1697 if (javax.swing.JOptionPane.showConfirmDialog(this,
1698 "Parameter set is modifed - save ?", "Save changes ?",
1699 javax.swing.JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
1701 System.out.println("Saving for " + setname);
1702 _storeCurrentPreset(curSetName);
1706 settingDialog = true;
1707 reInitDialog(setname);
1708 settingDialog = false;
1714 * store current settings as given name. You should then reset gui.
1716 * @param curSetName2
1718 private void _storeCurrentPreset(String curSetName2)
1720 _storeUserPreset(curSetName2, setDescr.getText(), getJobParams());
1724 * last saved name for this user preset
1726 String lastSetName = null;
1729 * last saved value of the description text for this user preset
1731 String lastDescrText = null;
1733 public void actionPerformed(ActionEvent e)
1735 if (e.getSource() instanceof Component)
1737 Component src = (Component) e.getSource();
1738 if (src.getParent() == setName)
1740 // rename any existing records we know about for this set.
1741 String newname = (String) e.getActionCommand().trim();
1743 if (isServicePreset(newname))
1745 JOptionPane.showConfirmDialog(this,
1746 "Invalid name - preset already exists.", "Invalid name",
1747 JOptionPane.OK_OPTION);
1749 setName.setSelectedItem(curSetName);
1752 curSetName = newname;
1753 System.err.println("New name for user setting " + curSetName
1754 + " (was " + setName.getSelectedItem() + ")");
1755 if (curSetName.equals(setName.getSelectedItem()))
1759 if (curSetName != null)
1761 argSetModified(setName, true);
1769 private void checkDescrModified()
1776 (lastDescrText == null ? setDescr.getText().trim().length() > 0
1777 : !setDescr.getText().equals(lastDescrText)));
1782 public void insertUpdate(DocumentEvent e)
1784 checkDescrModified();
1787 public void removeUpdate(DocumentEvent e)
1789 checkDescrModified();
1792 public void changedUpdate(DocumentEvent e)
1794 checkDescrModified();