1 6 package com.nightlabs.rcp.wizard; 7 8 import java.util.ArrayList ; 9 import java.util.Collections ; 10 import java.util.List ; 11 12 import org.eclipse.jface.wizard.IWizardPage; 13 import org.eclipse.jface.wizard.Wizard; 14 15 47 public abstract class DynamicPathWizard extends Wizard implements IDynamicPathWizard { 48 49 private List dynamicWizardPages = new ArrayList (); 50 private DynamicWizardPopulator populator; 51 private DynamicPathWizardDialog dynamicWizardDialog; 52 53 56 public DynamicPathWizard() { 57 this(true); 58 } 59 60 68 public DynamicPathWizard(boolean init) { 69 setForcePreviousAndNextButtons(true); 70 if (init) 71 init(); 72 } 73 74 82 protected void init() { 83 } 85 86 89 public void addPages() 90 { 91 addPage(getWizardEntryPage()); 93 } 94 95 111 public abstract IDynamicPathWizardPage createWizardEntryPage(); 112 113 private IDynamicPathWizardPage wizardEntryPage = null; 114 115 123 public IDynamicPathWizardPage getWizardEntryPage() 124 { 125 if (wizardEntryPage == null) 126 wizardEntryPage = createWizardEntryPage(); 127 128 if (wizardEntryPage == null) 129 throw new NullPointerException ("createWizardEntryPage() must not return null!"); 130 131 return wizardEntryPage; 132 } 133 134 public List getDynamicWizardPages() 135 { 136 return Collections.unmodifiableList(dynamicWizardPages); 137 } 138 139 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 ("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 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 ("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 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 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 } 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 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 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 == pages[pages.length - 1]) { 315 IWizardPage populatorPage = getFirstDynamicPage(); 316 if (populatorPage != null) 317 return populatorPage; 318 } 320 int index = dynamicWizardPages.indexOf(page); 321 if (index == dynamicWizardPages.size() - 1) { 322 return null; 323 } 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 |