From: jprocter Date: Fri, 13 Aug 2010 13:07:53 +0000 (+0000) Subject: improved parameter set store/save/rename logic and debugged parameter/option display X-Git-Tag: Release_2_6~131 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=c6016c2fdffb1f7f84e44fbc0a1c0630d89ca17e;p=jalview.git improved parameter set store/save/rename logic and debugged parameter/option display --- diff --git a/src/jalview/gui/WsJobParameters.java b/src/jalview/gui/WsJobParameters.java index e639b41..b651833 100644 --- a/src/jalview/gui/WsJobParameters.java +++ b/src/jalview/gui/WsJobParameters.java @@ -54,6 +54,8 @@ import javax.swing.border.TitledBorder; import javax.swing.event.CellEditorListener; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import javax.swing.table.*; import compbio.metadata.Argument; @@ -69,14 +71,35 @@ import compbio.metadata.ValueConstrain.Type; import jalview.ws.jws2.Jws2Discoverer; import jalview.ws.jws2.Jws2Discoverer.Jws2Instance; +/** + * job parameter editing/browsing dialog box. User can browse existing settings + * (user + presets + Defaults), and any changes to parameters creates a modified + * user parameter set. LOGIC: If the parameter set is modified, and its name is + * a valid, non-existant user parameter set, then a save button is shown. If the + * parameter set is modified and its name is a valid, extant user parameter set, + * then an update button is shown. If user parameter set's name is edited, and + * old name exists as a writable user parameter set, then rename button is + * shown. If current parameter set is associated with a user defined parameter + * set, then : if set is modifed, a 'revert' button is shown. if set is not + * modified, a 'delete' button is shown. + * + * @author JimP + * + */ public class WsJobParameters extends JPanel implements ItemListener, - ActionListener + ActionListener, DocumentListener { + private static final String SVC_DEF = "Defaults"; // this is the null + // parameter set as shown to + // user + private static final int PARAM_WIDTH = 340, PARAM_HEIGHT = 150, PARAM_CLOSEDHEIGHT = 80; private static final int OPTSET_HEIGHT = 30; + JPanel SetNamePanel = new JPanel(); + JPanel setDetails = new JPanel(); JSplitPane settingsPanel = new JSplitPane(); @@ -93,6 +116,10 @@ public class WsJobParameters extends JPanel implements ItemListener, JButton renmodified = new JButton(); + JButton deletesetting = new JButton(); + + JButton revertsetting = new JButton(); + JButton startjob = new JButton(); JButton canceljob = new JButton(); @@ -163,35 +190,58 @@ public class WsJobParameters extends JPanel implements ItemListener, return false; } + protected JButton makeButton(String label, String tooltip, + ActionListener action) + { + JButton button = new JButton(); + button.setText(label); + button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10)); + button.setForeground(Color.black); + button.setHorizontalAlignment(SwingConstants.CENTER); + button.setToolTipText(tooltip); + button.addActionListener(action); + return button; + } + private void jbInit() { - savmodified.setText("Save"); - savmodified.setToolTipText("Not implemented yet :) "); - savmodified.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10)); - savmodified.setForeground(Color.red); - savmodified.setHorizontalAlignment(SwingConstants.RIGHT); - savmodified.addActionListener(new ActionListener() - { + savmodified = makeButton("Save", "Not implemented yet :) ", + new ActionListener() + { - public void actionPerformed(ActionEvent e) - { - savModified_actionPerformed(e); - } - }); - renmodified.setText("Rename"); - renmodified.setToolTipText("Not implemented yet :) "); - renmodified.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10)); - renmodified.setForeground(Color.red); - renmodified.setHorizontalAlignment(SwingConstants.CENTER); - renmodified.addActionListener(new ActionListener() - { + public void actionPerformed(ActionEvent e) + { + savModified_actionPerformed(e); + } + }); + renmodified = makeButton("Rename", "Not implemented yet :) ", + new ActionListener() + { + + public void actionPerformed(ActionEvent e) + { + renModified_actionPerformed(e); + } + }); + deletesetting = makeButton("Delete", "Not implemented yet :) ", + new ActionListener() + { + + public void actionPerformed(ActionEvent e) + { + deleteSetting_actionPerformed(e); + } + }); + revertsetting = makeButton("Revert", "Undo changes to parameters.", + new ActionListener() + { + + public void actionPerformed(ActionEvent e) + { + revertSetting_actionPerformed(e); + } + }); - public void actionPerformed(ActionEvent e) - { - renModified_actionPerformed(e); - } - }); - renmodified.setVisible(false); startjob.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10)); startjob.setText("Start"); startjob.setToolTipText("Start Job"); @@ -219,21 +269,28 @@ public class WsJobParameters extends JPanel implements ItemListener, setDescr.setWrapStyleWord(true); setDescr.setLineWrap(true); setDescr.setBackground(getBackground()); - setDescr.setEditable(false); + setDescr.setEditable(true); + setDescr.getDocument().addDocumentListener(this); JScrollPane setDescrView = new JScrollPane(); // setDescrView.setPreferredSize(new Dimension(350, 200)); setDescrView.getViewport().setView(setDescr); setName.setEditable(true); setName.addItemListener(this); setName.getEditor().addActionListener(this); - savmodified.setEnabled(false); // initially unmodified parameters - // modified.setPreferredSize(new Dimension(15, 15)); - JPanel SetNamePanel = new JPanel(); SetNamePanel.setLayout(new BorderLayout()); SetNamePanel.add(setName, BorderLayout.WEST); - SetNamePanel.add(renmodified, BorderLayout.CENTER); - SetNamePanel.add(savmodified, BorderLayout.EAST); - setDetails.add(SetNamePanel, BorderLayout.NORTH); + // initial button visibility + deletesetting.setVisible(false); + revertsetting.setVisible(false); + renmodified.setVisible(false); + savmodified.setVisible(false); + JPanel setsavebuts = new JPanel(); + setsavebuts.setLayout(new FlowLayout()); + setsavebuts.add(revertsetting, BorderLayout.CENTER); + setsavebuts.add(renmodified, BorderLayout.CENTER); + setsavebuts.add(deletesetting, BorderLayout.CENTER); + setsavebuts.add(savmodified, BorderLayout.EAST); + SetNamePanel.add(setsavebuts,BorderLayout.EAST); setDetails.add(setDescrView, BorderLayout.CENTER); // setDetails.setPreferredSize(new Dimension(360, 100)); jobParameters.setBorder(new TitledBorder("Parameters")); @@ -261,6 +318,7 @@ public class WsJobParameters extends JPanel implements ItemListener, jobPanel.setRightComponent(settingsPanel); jobPanel.setOrientation(JSplitPane.VERTICAL_SPLIT); jobPanel.setDividerLocation(0.8); + add(SetNamePanel, BorderLayout.NORTH); add(jobPanel, BorderLayout.CENTER); JPanel dialogpanel = new JPanel(); dialogpanel.add(startjob); @@ -268,22 +326,50 @@ public class WsJobParameters extends JPanel implements ItemListener, add(dialogpanel, BorderLayout.SOUTH); } - protected void renModified_actionPerformed(ActionEvent e) + protected void revertSetting_actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub - // save current prefs with new name and delete user pref with old name } - protected void savModified_actionPerformed(ActionEvent e) + protected void deleteSetting_actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub - // Store the current settings - // write out param file and name. - // update gui - initArgSetModified(); // reset the modification state + String setname = (String) setName.getSelectedItem(); + int p=setName.getSelectedIndex(); + if (_getUserPreset(setname)!=null) + { + _deleteUserPreset(setname); + + } + if (p>0 && p+1==setName.getItemCount()) + { + p--; + } + setName.setSelectedIndex(p); + + } + protected void renModified_actionPerformed(ActionEvent e) + { + if (curSetName==null || _getUserPreset(lastSetName)==null) { + System.err.println("can't rename - names unchanged or original name not a preset."); + return; + } + _deleteUserPreset(lastSetName); + lastSetName=curSetName; + savModified_actionPerformed(e); + curSetName=null; + boolean setd = settingDialog; + settingDialog=true; + syncSetNamesWithStore(); + settingDialog=setd; + } + + protected void savModified_actionPerformed(ActionEvent e) + { + _storeUserPreset(lastSetName=(String) setName.getSelectedItem(), setDescr.getText(), getJobParams()); + curSetName=null; + initArgSetModified(); // reset the modification state } protected void canceljob_actionPerformed(ActionEvent e) @@ -307,6 +393,11 @@ public class WsJobParameters extends JPanel implements ItemListener, Jws2Instance service; /** + * list of service presets in the gui + */ + Hashtable servicePresets = null; + + /** * set if dialog is being set - so handlers will avoid spurious events */ boolean settingDialog = false; @@ -320,20 +411,21 @@ public class WsJobParameters extends JPanel implements ItemListener, // jobPanel.setDividerLocation(proportionalLocation) // settingsPanel.setDividerLocation(proportionalLocation) Hashtable exnames = new Hashtable(); - for (int i=0,iSize=setName.getItemCount(); i 0; } private Hashtable modifiedElements = new Hashtable(); + /** * reset gui and modification state settings */ - private void initArgSetModified() { + private void initArgSetModified() + { curSetName = null; modifiedElements.clear(); renmodified.setVisible(false); - savmodified.setEnabled(false); - + savmodified.setVisible(false); + } + private void argSetModified(Object modifiedElement, boolean b) { + if (settingDialog) + { + return; + } + if (!b) { modifiedElements.remove(modifiedElement); @@ -482,24 +577,93 @@ public class WsJobParameters extends JPanel implements ItemListener, modifiedElements.put(modifiedElement, modifiedElement); } // set mod status based on presence of elements in table - savmodified.setEnabled(modifiedElements.size() > 0); + if (modifiedElements.size() > 0) + { + makeSetNameValid(); + savmodified.setVisible(true); + revertsetting.setVisible(false); + } + else + { + revertsetting.setVisible(false); + deletesetting + .setVisible(!isServicePreset((String) setName + .getSelectedItem()) + && _getUserPreset((String) setName.getSelectedItem()) != null); + savmodified.setVisible(false); + } // special reveal if setName has been modified if (modifiedElements.get(setName) != null) { - if (curSetName != null && lastSetName!=null && !lastSetName.equals(curSetName)) + if (curSetName != null && lastSetName != null + && !lastSetName.equals(curSetName)) { - // also test to see if lastSetName exists in preset store - renmodified.setVisible(true); + renmodified.setVisible(!isServicePreset(lastSetName)); } } else { // setname isn't in modlist - so don't rename - renmodified.setEnabled(false); + renmodified.setVisible(false); } validate(); } + private boolean isServicePreset(String selectedItem) + { + return selectedItem.equals(SVC_DEF) + || servicePresets.containsKey(selectedItem); + } + + /** + * check if the current set name is a valid set name for saving, if not, then + * fix it. + */ + private void makeSetNameValid() + { + boolean stn = settingDialog; + boolean renamed = false; + settingDialog = true; + String nm = (String) setName.getSelectedItem(); + // check if the name is reserved - if it is, rename it. + if (isServicePreset(nm)) + { + nm = "User " + nm; + renamed = true; + } + // if () + // if nm exists in user's preset store then savmodified will update an + // existing user defined preset + // if nm doesn't exist, then the button will create a new preset. + + boolean makeupdate = false; + // sync the gui with the preset database + for (int i = 0, iS = setName.getItemCount(); i < iS; i++) + { + String snm = (String) setName.getItemAt(i); + if (snm.equals(nm)) + { + makeupdate = true; + setName.setSelectedIndex(i); + } + } + + if (_getUserPreset(nm) != null) + { + savmodified.setText("Update"); + } + else + { + if (renamed) + { + setName.addItem(nm); + setName.setSelectedIndex(setName.getItemCount() - 1); + } + savmodified.setText("Save"); + } + settingDialog = stn; + } + private void addParameter(Parameter parm) { ParamBox pb = paramSet.get(parm.getName()); @@ -514,6 +678,20 @@ public class WsJobParameters extends JPanel implements ItemListener, pb.updateControls(parm); } + private void setParameter(Parameter arg) + { + ParamBox pb = paramSet.get(arg.getName()); + if (pb == null) + { + addParameter(arg); + } + else + { + pb.updateControls(arg); + } + + } + private void selectOption(Option opt, String string) { OptionBox cb = optSet.get(opt.getName()); @@ -521,29 +699,9 @@ public class WsJobParameters extends JPanel implements ItemListener, { cb = addOption(opt); } - if (string == null) - { - // no value specified. Either a .. -// if (opt.getPossibleValues()==null || opt.getPossibleValues().size()<=1) { - // switch -// cb.enabled.setSelected(true); -// } else { -// // or unselected option -// cb.enabled.setSelected(false); -// } - if (opt.isRequired()) - { - // indicate option needs to be selected! - } - else - { - cb.enabled.setSelected(false); - } - } - else + cb.enabled.setSelected(true); // initial state for an option. + if (string != null) { - cb.enabled.setSelected(true); - if (opt.getPossibleValues().contains(string)) { cb.val.setSelectedItem(string); @@ -677,7 +835,7 @@ public class WsJobParameters extends JPanel implements ItemListener, public void updateControls(Parameter parm) { adjusting = true; - boolean init = (choicebox==null && valueField==null); + boolean init = (choicebox == null && valueField == null); float fVal = 0f; int iVal = 0; if (init) @@ -705,14 +863,15 @@ public class WsJobParameters extends JPanel implements ItemListener, { if (choice) { - if (init) { + if (init) + { List vals = parm.getPossibleValues(); for (Object val : vals) { choicebox.addItem(val); } } - + if (parm.getDefaultValue() != null) { choicebox.setSelectedItem(parm.getDefaultValue()); @@ -907,6 +1066,16 @@ public class WsJobParameters extends JPanel implements ItemListener, setInitialValue(); } + public void resetToDefault() + { + enabled.setSelected(false); + if (option.isRequired()) + { + // Apply default value + selectOption(option, option.getDefaultValue()); + } + } + boolean initEnabled = false; String initVal = null; @@ -919,6 +1088,10 @@ public class WsJobParameters extends JPanel implements ItemListener, { initVal = (String) val.getSelectedItem(); } + else + { + initVal = null; + } } public Option getOptionIfEnabled() @@ -954,9 +1127,9 @@ public class WsJobParameters extends JPanel implements ItemListener, private void checkIfModified() { boolean notmod = (initEnabled == enabled.isSelected()); - if (initVal != null) + if (enabled.isSelected() && initVal != null) { - notmod = initVal.equals(val.getSelectedItem()); + notmod |= initVal.equals(val.getSelectedItem()); } argSetModified(this, !notmod); } @@ -1178,9 +1351,40 @@ public class WsJobParameters extends JPanel implements ItemListener, return editedParams.get(setName); } + /** + * remove the given user preset from the preset stash + * + * @param setName + */ + private void _deleteUserPreset(String setName) + { + editedParams.remove(setName); + } + + private void syncSetNamesWithStore() + { + int n = 0; + // remove any set names in the drop down menu that aren't either a reserved + // setting, or a user defined or service preset. + while (n < setName.getItemCount()) + { + String item = (String) setName.getItemAt(n); + if (!isServicePreset(item) && _getUserPreset(item) == null) + { + setName.removeItemAt(n); + } + else + { + n++; + } + + } + } + private void reInitDialog(String nextPreset) { settingDialog = true; + syncSetNamesWithStore(); // updateTable(null,null); Object[] pset = _getUserPreset(nextPreset); if (pset != null) @@ -1192,6 +1396,7 @@ public class WsJobParameters extends JPanel implements ItemListener, } else { + setDescr.setText(""); // must be a default preset from service Preset p = null; try @@ -1210,6 +1415,10 @@ public class WsJobParameters extends JPanel implements ItemListener, updateTable(p, null); lastParmSet = nextPreset; } + else + { + updateTable(null, null); + } } initArgSetModified(); validate(); @@ -1236,9 +1445,13 @@ public class WsJobParameters extends JPanel implements ItemListener, } if (curSetName == null || !setname.equals(curSetName)) { - if (isModified()) + if (isModified() + && javax.swing.JOptionPane.showConfirmDialog(this, + "Parameter set is modifed - save ?", + "Save changes ?", + javax.swing.JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) { - raiseQueryAndSave(); + savModified_actionPerformed(null); } reInitDialog(setname); } @@ -1246,14 +1459,6 @@ public class WsJobParameters extends JPanel implements ItemListener, } /** - * ask the user if they wan't to save the current settings before switching - */ - private void raiseQueryAndSave() - { - - } - - /** * last saved name for this user preset */ String lastSetName = null; @@ -1272,27 +1477,60 @@ public class WsJobParameters extends JPanel implements ItemListener, if (src.getParent() == setName) { // rename any existing records we know about for this set. - curSetName = (String) e.getActionCommand(); + String newname = (String) e.getActionCommand(); + String msg=null; + if (isServicePreset(newname)) { + JOptionPane.showConfirmDialog(this, "Invalid name - preset already exists.", "Invalid name", JOptionPane.OK_OPTION); + return; + } + curSetName = newname; System.err.println("Command " + curSetName + " : " + setName.getSelectedItem()); if (curSetName.trim().equals(setName.getSelectedItem())) { - curSetName=null; + curSetName = null; } - if (curSetName!=null) + if (curSetName != null) { - argSetModified(setName, lastSetName!=null || !curSetName.equals(lastSetName)); + setName.addItem(curSetName); + setName.setSelectedItem(curSetName); + argSetModified(setName, + lastSetName != null && !curSetName.equals(lastSetName)); return; } } } - if (e.getSource() == setDescr) + } + + private void checkDescrModified() + { + if (!settingDialog) { - if (lastDescrText != null) - { - argSetModified(setDescr, !setDescr.getText().equals(lastDescrText)); - } + + argSetModified( + setDescr, + (lastDescrText == null ? setDescr.getText().trim().length() > 0 + : !setDescr.getText().equals(lastDescrText))); + } } + + @Override + public void insertUpdate(DocumentEvent e) + { + checkDescrModified(); + } + + @Override + public void removeUpdate(DocumentEvent e) + { + checkDescrModified(); + } + + @Override + public void changedUpdate(DocumentEvent e) + { + checkDescrModified(); + } } \ No newline at end of file