KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Jun 13, 2005
3  */

4 package com.nightlabs.rcp.wizard;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.eclipse.jface.wizard.IWizard;
11 import org.eclipse.jface.wizard.IWizardPage;
12
13 /**
14  * This class enables a wizard to support multiple local hops. A local hop means a
15  * locally controlled list of pages where the current page can add/remove other pages.
16  * The wizard will show all pages of one hop before going to the next hop. You can use
17  * basically every {@link org.eclipse.jface.wizard.Wizard} (and probably even every
18  * class implementing {@link org.eclipse.jface.wizard.IWizard} correctly), but it's
19  * recommended to use {@link com.nightlabs.rcp.wizard.IDynamicPathWizard} (or its
20  * default implementation {@link com.nightlabs.rcp.wizard.DynamicPathWizard}).
21  * <p>
22  * It is possible to combine hops with normal pages. The entry page of each hop
23  * must be registered in the <tt>Wizard</tt> either as static page (or a dynamic
24  * page, if using an {@link com.nightlabs.rcp.wizard.IDynamicPathWizard}).
25  *
26  * @author Marco Schulze - marco at nightlabs dot de
27  */

28 public class WizardHop implements IWizardHop
29 {
30     private IWizardHop parentHop;
31
32     private List JavaDoc hopPages = new ArrayList JavaDoc();
33
34     private IWizardHopPage entryPage = null;
35     private IWizardHopPage exitPage = null;
36
37     /**
38      * If you use this constructor, you must call {@link #setEntryPage(IWizardHopPage)}
39      * before it is usable. It's recommended to create an instance of this class
40      * in the constructor of the entry page of this <tt>WizardHop</tt> with
41      * the constructor {@link #WizardHop(IWizardHopPage)}.
42      */

43     protected WizardHop()
44     {
45     }
46
47     /**
48      * This creates a new instance of <tt>WizardHop</tt> and registers it in the
49      * <tt>entryPage</tt>. This means, all you need to do to use the hop mechanism
50      * is to call <tt>new WizardHop(this);</tt> in your entry page's constructor.
51      *
52      * @param entryPage The entry page of this hop.
53      */

54     public WizardHop(IWizardHopPage entryPage)
55     {
56         setEntryPage(entryPage);
57     }
58     
59     /**
60      * @see com.nightlabs.rcp.wizard.IWizardHop#getParentHop()
61      */

62     public IWizardHop getParentHop()
63     {
64         return parentHop;
65     }
66     /**
67      * @see com.nightlabs.rcp.wizard.IWizardHop#setParentHop(com.nightlabs.rcp.wizard.IWizardHop)
68      */

69     public void setParentHop(IWizardHop parentHop)
70     {
71         this.parentHop = parentHop;
72     }
73     
74     /**
75      * @return Returns the wizard.
76      */

77     public IWizard getWizard()
78     {
79         if (entryPage == null)
80             return null;
81
82         return entryPage.getWizard();
83     }
84
85     /**
86      * @param entryPage The entryPage to set.
87      */

88     public void setEntryPage(IWizardHopPage entryPage)
89     {
90         entryPage.setWizardHop(this);
91         this.entryPage = entryPage;
92     }
93     
94     /**
95      * @see com.nightlabs.rcp.wizard.IWizardHop#getExitPage()
96      */

97     public IWizardHopPage getExitPage()
98     {
99         return exitPage;
100     }
101
102     /**
103      * @see com.nightlabs.rcp.wizard.IWizardHop#setExitPage(com.nightlabs.rcp.wizard.IWizardHopPage)
104      */

105     public void setExitPage(IWizardHopPage exitPage)
106     {
107         this.exitPage = exitPage;
108         if (exitPage != null) {
109             exitPage.setWizard(getWizard());
110
111             if (exitPage.getWizardHop() == null)
112                 throw new NullPointerException JavaDoc("exitPage.getWizardHop() returns null!");
113
114             exitPage.getWizardHop().setParentHop(this);
115         }
116     }
117
118     /**
119      * @return Returns the entryPage.
120      */

121     public IWizardHopPage getEntryPage()
122     {
123         return entryPage;
124     }
125
126     public void addHopPage(IWizardHopPage page)
127     {
128         page.setWizard(getWizard());
129         page.setWizardHop(this);
130         hopPages.add(page);
131     }
132
133     public void removeAllHopPages()
134     {
135         hopPages.clear();
136     }
137
138     public IWizardPage getNextPage(IWizardPage currentPage)
139     {
140         IWizard wizard = getWizard();
141         if (wizard == null)
142             return null;
143
144         if (currentPage == entryPage) {
145             if (hopPages.isEmpty())
146                 return wizard.getNextPage(entryPage);
147
148             return (IWizardPage) hopPages.get(0);
149         }
150
151         int currIdx = hopPages.indexOf(currentPage);
152         int nextIdx = -1;
153
154         if (currIdx >= 0)
155             nextIdx = currIdx + 1;
156
157         if (nextIdx >= 0) {
158             if (nextIdx < hopPages.size())
159                 return (IWizardPage) hopPages.get(nextIdx);
160
161             if (exitPage != null)
162                 return exitPage;
163         }
164
165         // find out the first entry page of the current branch of hops, which is
166
// known to the wizard.
167
IWizardHopPage firstEntryPage = entryPage;
168         while (firstEntryPage.getWizardHop().getParentHop() != null) {
169             firstEntryPage = firstEntryPage.getWizardHop().getParentHop().getEntryPage();
170         }
171
172         return wizard.getNextPage(firstEntryPage);
173     }
174
175 // public IWizardPage getPreviousPage(IWizardPage currentPage)
176
// {
177
// IWizard wizard = getWizard();
178
// if (wizard == null)
179
// return null;
180
//
181
//// return wizard.getPreviousPage(entryPage);
182
//
183
// if (currentPage != entryPage) {
184
// int currIdx = hopPages.indexOf(currentPage);
185
// int previousIdx = -1;
186
//
187
// if (currIdx == 0)
188
// return entryPage;
189
//
190
// if (currIdx > 0)
191
// previousIdx = currIdx - 1;
192
//
193
// if (previousIdx >= 0)
194
// return (IWizardPage) hopPages.get(previousIdx);
195
// }
196
//
197
// if (parentHop != null)
198
// return parentHop.getLastHopPage();
199
//
200
// IWizardPage previous = wizard.getPreviousPage(entryPage);
201
// if (previous instanceof IWizardHopPage) {
202
// IWizardHop previousHop = ((IWizardHopPage)previous).getWizardHop();
203
// while (previousHop.getExitPage() != null) {
204
// // The exitPage of the previousHop is always the entryPage of the following
205
// // hop and therefore exitPage.getWizardHop() is always the following hop.
206
// previousHop = previousHop.getExitPage().getWizardHop();
207
// }
208
// IWizardPage p = previousHop.getLastHopPage();
209
//
210
// if (p != null)
211
// previous = p;
212
// }
213
//
214
// return previous;
215
// }
216

217     /**
218      * @see com.nightlabs.rcp.wizard.IWizardHop#getFirstHopPage()
219      */

220     public IWizardHopPage getFirstHopPage()
221     {
222         if (hopPages.isEmpty())
223             return null;
224
225         return (IWizardHopPage) hopPages.get(0);
226     }
227
228     /**
229      * @see com.nightlabs.rcp.wizard.IWizardHop#getLastHopPage()
230      */

231     public IWizardHopPage getLastHopPage()
232     {
233         if (hopPages.isEmpty())
234             return null;
235
236         return (IWizardHopPage) hopPages.get(hopPages.size() - 1);
237     }
238
239     /**
240      * @see com.nightlabs.rcp.wizard.IWizardHop#removeHopPage(com.nightlabs.rcp.wizard.IWizardHopPage)
241      */

242     public boolean removeHopPage(IWizardHopPage page)
243     {
244         return hopPages.remove(page);
245     }
246
247     /**
248      * @see com.nightlabs.rcp.wizard.IWizardHop#getHopPages()
249      */

250     public List JavaDoc getHopPages()
251     {
252         return Collections.unmodifiableList(hopPages);
253     }
254
255 }
256
Popular Tags