KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > wizard > DynamicPathWizard


1 /*
2  * Created on Jan 13, 2005
3  * by alex
4  *
5  */

6 package com.nightlabs.rcp.wizard;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.eclipse.jface.wizard.IWizardPage;
13 import org.eclipse.jface.wizard.Wizard;
14
15 /**
16  * Used for Wizards with dynamic WizardPages.
17  * Inteded to be used as follows.<br/>
18  * Subclass DynamicPathWizardPage and implement
19  * <ul>
20  * <li>{@link org.eclipse.jface.wizard.Wizard#performFinish()}, where you perform your
21  * wizards action as you are used with normal
22  * Wizards.</li>
23  * <li>{@link #getWizardEntryPage()}, which should return
24  * the first page the user sees</li>
25  * </ul>
26  * From now on you have two possibilities.
27  * <p>
28  * You can use the add/removeDynamicWizardPage methods to add the
29  * pages as you need them. If you don't override the getNextPage-behavior
30  * of the added pages they will appear in the same order as added.
31  * </p>
32  * <p>
33  * The second possibility is to set a DynamicWizardPopulator
34  * ({@link #setPopulator(DynamicWizardPopulator)}). After removing
35  * the old dynamic pages the populator will be asked to add its pages by
36  * {@link com.nightlabs.rcp.wizard.DynamicWizardPopulator#addDynamicWizardPages(DynamicPathWizard)}.
37  * This is done every time Next is pressed and the wizards first page
38  * is showing (and your WizardDialog is a subclass of DynamicPathWizardDialog).
39  * This lets you easily decide on user input wich path to go
40  * by simply switching populators and the user can go back and
41  * even take an other way.
42  * </p>
43  *
44  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
45  *
46  */

47 public abstract class DynamicPathWizard extends Wizard implements IDynamicPathWizard {
48     
49     private List JavaDoc dynamicWizardPages = new ArrayList JavaDoc();
50     private DynamicWizardPopulator populator;
51     private DynamicPathWizardDialog dynamicWizardDialog;
52
53     /**
54      * Calls {@link #DynamicPathWizard(boolean)} with <tt>init = true</tt>.
55      */

56     public DynamicPathWizard() {
57         this(true);
58     }
59
60     /**
61      * @param init Whether or not to call the method {@link #init()}. If you are not ready
62      * to implicitely call {@link #init()} in the super constructor, you can call
63      * <tt>super(false)</tt>, but you must call <tt>init()</tt> manually then!
64      *
65      * @deprecated Because {@link #init()} is not necessary anymore, this constructor
66      * isn't either.
67      */

68     public DynamicPathWizard(boolean init) {
69         setForcePreviousAndNextButtons(true);
70         if (init)
71             init();
72     }
73
74     /**
75      * Does some initialization. This method is called automatically by the constructor
76      * if <tt>init = true</tt> is passed to {@link #DynamicPathWizard(boolean)} or
77      * if {@link #DynamicPathWizard()} is used. If you decide to use {@link #DynamicPathWizard(boolean)}
78      * with <tt>init = false</tt>, you must call this method manually!
79      *
80      * @deprecated
81      */

82     protected void init() {
83 // addPage(getWizardEntryPage());
84
}
85
86     /**
87      * @see org.eclipse.jface.wizard.Wizard#addPages()
88      */

89     public void addPages()
90     {
91 // super.addPages(); // empty super method.
92
addPage(getWizardEntryPage());
93     }
94
95     /**
96      * <strong>Important API change:</strong> Since this method exists, you
97      * MUST NOT overwrite {@link #getWizardEntryPage()} anymore!!!
98      * <p>
99      * This method is called exactly once and you should create the first page of
100      * your wizard here. Note, that it is already called in the constructor
101      * if <tt>init</tt> is <tt>true</tt>.
102      * <p>
103      * Note: You might want to extend the {@link DynamicPathWizardPage} instead of manually
104      * implementing an {@link IDynamicPathWizardPage}.
105      *
106      * @return Returns the first page of the wizard.
107      *
108      * @see DynamicPathWizardPage
109      * @see IDynamicPathWizardPage
110      */

111     public abstract IDynamicPathWizardPage createWizardEntryPage();
112
113     private IDynamicPathWizardPage wizardEntryPage = null;
114
115     /**
116      * <strong>Important API change: Do not overwrite this method anymore!!!</strong>
117      * Overwrite {@link #createWizardEntryPage()} instead!
118      *
119      * @return Returns the first page of the wizard. If this page does not yet exist
120      * (means it's the first call to this method), {@link #createWizardEntryPage()} is
121      * called.
122      */

123     public IDynamicPathWizardPage getWizardEntryPage()
124     {
125         if (wizardEntryPage == null)
126             wizardEntryPage = createWizardEntryPage();
127         
128         if (wizardEntryPage == null)
129             throw new NullPointerException JavaDoc("createWizardEntryPage() must not return null!");
130
131         return wizardEntryPage;
132     }
133
134     public List JavaDoc getDynamicWizardPages()
135     {
136         return Collections.unmodifiableList(dynamicWizardPages);
137     }
138
139     /**
140      * @see com.nightlabs.rcp.wizard.IDynamicPathWizard#getDynamicWizardPageCount()
141      */

142     public int getDynamicWizardPageCount()
143     {
144         return dynamicWizardPages.size();
145     }
146
147     protected void assertCanInsertDynamicWizardPage(int index)
148     {
149         int lastReadOnlyIndex = -1;
150         IWizardPage currentPage = dynamicWizardDialog.getCurrentPage();
151         if (currentPage == null)
152             return;
153
154         IWizardPage page = getWizardEntryPage();
155         while (page != null && page != currentPage) {
156             if (page instanceof IDynamicPathWizardPage) {
157                 int i = getDynamicWizardPageIndex((IDynamicPathWizardPage)page);
158                 if (i > lastReadOnlyIndex)
159                     lastReadOnlyIndex = i;
160             }
161
162             page = page.getNextPage();
163         }
164
165         if (index <= lastReadOnlyIndex)
166             throw new IllegalStateException JavaDoc("Cannot add a wizard page at index " + index + ", because the current page forces all indices <=" + lastReadOnlyIndex + " to be unchangeable!");
167     }
168
169     public void addDynamicWizardPage(int index, IDynamicPathWizardPage page) {
170         assertCanInsertDynamicWizardPage(index);
171         dynamicWizardPages.add(index, page);
172         page.setWizard(this);
173     }
174
175     public void addDynamicWizardPage(IDynamicPathWizardPage page) {
176         dynamicWizardPages.add(page);
177         page.setWizard(this);
178     }
179
180     public int getDynamicWizardPageIndex(IDynamicPathWizardPage page) {
181         return dynamicWizardPages.indexOf(page);
182     }
183
184     /**
185      * @see com.nightlabs.rcp.wizard.IDynamicPathWizard#getDynamicWizardPage(int)
186      */

187     public IDynamicPathWizardPage getDynamicWizardPage(int index)
188     {
189         return (IDynamicPathWizardPage) dynamicWizardPages.get(index);
190     }
191
192     protected void assertCanRemoveDynamicWizardPage(IDynamicPathWizardPage page)
193     {
194         if (dynamicWizardDialog == null)
195             return;
196
197         IWizardPage currentPage = dynamicWizardDialog.getCurrentPage();
198         if (currentPage == null)
199             return;
200
201         IWizardPage pageToCheck = getWizardEntryPage();
202         while (pageToCheck != null && pageToCheck != currentPage) {
203             if (pageToCheck == page)
204                 throw new IllegalStateException JavaDoc("Cannot remove page \"" + page.getName() + "\", because it is part of the path BEFORE the current page! Can only remove pages that are following the current page!");
205
206             pageToCheck = pageToCheck.getNextPage();
207         }
208     }
209
210     /**
211      * @see com.nightlabs.rcp.wizard.IDynamicPathWizard#removeDynamicWizardPage(int)
212      */

213     public void removeDynamicWizardPage(int index) {
214         if (index < 0 || index >= dynamicWizardPages.size())
215             return;
216
217         assertCanRemoveDynamicWizardPage(
218                 (IDynamicPathWizardPage) dynamicWizardPages.get(index));
219         dynamicWizardPages.remove(index);
220     }
221
222     /**
223      * @see com.nightlabs.rcp.wizard.IDynamicPathWizard#removeDynamicWizardPage(com.nightlabs.rcp.wizard.IDynamicPathWizardPage)
224      */

225     public void removeDynamicWizardPage(IDynamicPathWizardPage page) {
226         assertCanRemoveDynamicWizardPage(page);
227         dynamicWizardPages.remove(page);
228     }
229     
230     public void removeAllDynamicWizardPages() {
231         dynamicWizardPages.clear();
232     }
233     
234     public boolean canFinish() {
235         IDynamicPathWizardPage page = getWizardEntryPage();
236         IDynamicPathWizardPage lastPage = null;
237         while (page != null) {
238             if (!page.isPageComplete())
239                 return false;
240             
241             lastPage = page;
242             page = (IDynamicPathWizardPage)page.getNextPage();
243         }
244         if (lastPage.canBeLastPage())
245             return true;
246         else
247             return false;
248
249 // if (!getWizardEntryPage().isPageComplete())
250
// return false;
251
//
252
// if (dynamicWizardPages.size() > 0) {
253
// for (int i = 0; i < dynamicWizardPages.size(); i++) {
254
// if (!((IWizardPage) dynamicWizardPages.get(i)).isPageComplete())
255
// return false;
256
// }
257
// return true; // no page is incomplete
258
// }
259
// else {
260
// return true; // only wizardEntryPage matters
261
// }
262
}
263     
264     public DynamicWizardPopulator getPopulator() {
265         return populator;
266     }
267     public void setPopulator(DynamicWizardPopulator populator) {
268         this.populator = populator;
269     }
270     public DynamicPathWizardDialog getDynamicWizardDialog() {
271         return dynamicWizardDialog;
272     }
273     public void setDynamicWizardDialog(DynamicPathWizardDialog dynamicWizardDialog) {
274         this.dynamicWizardDialog = dynamicWizardDialog;
275     }
276
277     /**
278      *
279      * @return Either
280      */

281     protected IWizardPage getFirstDynamicPage() {
282         if (populator != null)
283             return populator.getDynamicPathEntryPage();
284         else {
285             if (dynamicWizardPages.size() > 0)
286                 return (IWizardPage)dynamicWizardPages.get(0);
287             return null;
288         }
289     }
290
291     /**
292      * @see org.eclipse.jface.wizard.Wizard#getPreviousPage(org.eclipse.jface.wizard.IWizardPage)
293      */

294     public IWizardPage getPreviousPage(IWizardPage page)
295     {
296         return super.getPreviousPage(page);
297     }
298
299     public IWizardPage getNextPage(IWizardPage page) {
300
301         IWizardPage[] pages = getPages();
302         boolean isStaticPage = false;
303         for (int i = 0; i < pages.length; ++i) {
304             if (pages[i] == page) {
305                 isStaticPage = true;
306                 break;
307             }
308         }
309         
310         if (isStaticPage && page != pages[pages.length - 1])
311             return super.getNextPage(page);
312
313 // if (page == getWizardEntryPage()) {
314
if (page == pages[pages.length - 1]) {
315             IWizardPage populatorPage = getFirstDynamicPage();
316             if (populatorPage != null)
317                 return populatorPage;
318 // return null;
319
}
320         int index = dynamicWizardPages.indexOf(page);
321         if (index == dynamicWizardPages.size() - 1) {
322             return null;
323 // if (page == getFirstDynamicPage() && dynamicWizardPages.size() > 0)
324
// return (IWizardPage)dynamicWizardPages.get(0);
325
// last page or page not found
326
// return null;
327
}
328         else if (index >= 0)
329             return (IWizardPage)dynamicWizardPages.get(index + 1);
330         else
331             return null;
332     }
333
334     public void updateDialog() {
335         if (dynamicWizardDialog != null)
336             dynamicWizardDialog.update();
337     }
338
339 }
340
Popular Tags