Index: src/org/openide/WizardDescriptor.java =================================================================== RCS file: /cvs/openide/src/org/openide/WizardDescriptor.java,v retrieving revision 1.89 diff -u -r1.89 WizardDescriptor.java --- src/org/openide/WizardDescriptor.java 21 Aug 2003 08:57:36 -0000 1.89 +++ src/org/openide/WizardDescriptor.java 11 Nov 2003 18:27:56 -0000 @@ -1002,6 +1002,20 @@ public interface FinishPanel extends Panel { } + /** A special interface for panels that need to do additional + * validation when Next or Finish button is clicked. + */ + public interface ValidatingPanel extends Panel { + /** Is called when Next of Finish buttons are clicked and + * allows deeper check to find out that panel is in valid + * state and it is ok to leave it. + * + * @return null if everything is ok or a localized message to show to + * user + */ + public String validate (); + } + /** Special iterator that works on an array of Panels. */ public static class ArrayIterator extends Object implements Iterator { @@ -1111,6 +1125,20 @@ public void actionPerformed (ActionEvent ev) { if (ev.getSource () == nextButton) { Dimension previousSize = panels.current().getComponent().getSize(); + + if (panels.current() instanceof ValidatingPanel) { + ValidatingPanel v = (ValidatingPanel)panels.current (); + String msg = v.validate(); + if ("xtest-fail-without-msg".equals (msg)) { + return; + } + + if (msg != null) { + DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg)); + return; + } + } + panels.nextPanel (); try { // change UI to show next step, show wait cursor during @@ -1140,6 +1168,19 @@ } if (ev.getSource () == finishButton) { + if (panels.current() instanceof ValidatingPanel) { + ValidatingPanel v = (ValidatingPanel)panels.current (); + String msg = v.validate(); + if ("xtest-fail-without-msg".equals (msg)) { + return; + } + + if (msg != null) { + DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg)); + return; + } + } + Object oldValue = getValue (); setValueWithoutPCH (OK_OPTION); if (Arrays.asList(getClosingOptions()).contains(finishButton)) { Index: test/unit/src/org/openide/WizardDescTest.java =================================================================== RCS file: /cvs/openide/test/unit/src/org/openide/WizardDescTest.java,v retrieving revision 1.2 diff -u -r1.2 WizardDescTest.java --- test/unit/src/org/openide/WizardDescTest.java 21 Aug 2003 12:45:49 -0000 1.2 +++ test/unit/src/org/openide/WizardDescTest.java 11 Nov 2003 18:28:02 -0000 @@ -102,16 +102,66 @@ assertEquals ("Closed with cancel option.", WizardDescriptor.CANCEL_OPTION, wd.getValue ()); } + public void testNextOptionWhenLazyValidationFails () throws Exception { + Panel panels[] = new Panel[3]; + + class MyPanel extends Panel implements WizardDescriptor.ValidatingPanel { + public String validateMsg; + + public MyPanel () { + super ("enhanced panel"); + } + + public String validate () { + return validateMsg; + } + } + + class MyFinishPanel extends MyPanel implements WizardDescriptor.FinishPanel { + } + + MyPanel mp = new MyPanel (); + MyFinishPanel mfp = new MyFinishPanel (); + panels[0] = mp; + panels[1] = mfp; + panels[2] = new Panel ("Last one"); + wd = new WizardDescriptor(panels); + + assertNull ("Component has not been yet initialized", panels[1].component); + mp.validateMsg = "xtest-fail-without-msg"; + wd.doNextClick (); + assertNull ("The lazy validation failed, still no initialiaation", panels[1].component); + assertNull ("The lazy validation failed, still no initialiaation", panels[2].component); + mp.validateMsg = null; + wd.doNextClick (); + assertNotNull ("Now we switched to another panel", panels[1].component); + assertNull ("The lazy validation failed, still no initialiaation", panels[2].component); + + // remember previous state + Object state = wd.getValue(); + mfp.validateMsg = "xtest-fail-without-msg"; + wd.doFinishClick(); + assertNull ("The validation failed, still no initialiaation", panels[2].component); + assertEquals ("State has not changed", state, wd.getValue ()); + + mfp.validateMsg = null; + wd.doFinishClick (); + assertNull ("Finish was clicked, no initialization either", panels[2].component); + assertEquals ("The state is finish", WizardDescriptor.FINISH_OPTION, wd.getValue ()); + } public class Panel implements WizardDescriptor.Panel, WizardDescriptor.FinishPanel { - + private JLabel component; private String text; public Panel(String text) { this.text = text; } public Component getComponent() { - return new JLabel(text); + if (component == null) { + component = new JLabel (text); + } + return component; } public void addChangeListener(ChangeListener l) {