1 13 package org.eclipse.ui.internal.ide.dialogs; 14 15 import java.lang.reflect.InvocationTargetException ; 16 17 import org.eclipse.jface.operation.IRunnableWithProgress; 18 import org.eclipse.jface.wizard.IWizard; 19 import org.eclipse.jface.wizard.IWizardContainer; 20 import org.eclipse.jface.wizard.IWizardPage; 21 import org.eclipse.jface.wizard.WizardPage; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.custom.BusyIndicator; 24 import org.eclipse.swt.graphics.Point; 25 import org.eclipse.swt.graphics.Rectangle; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Shell; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 33 import org.eclipse.ui.internal.ide.misc.WizardStepGroup; 34 35 48 public class MultiStepConfigureWizardPage extends WizardPage { 49 private MultiStepWizardDialog wizardDialog; 50 51 private Composite pageSite; 52 53 private WizardStepGroup stepGroup; 54 55 private WizardStepContainer stepContainer = new WizardStepContainer(); 56 57 62 public MultiStepConfigureWizardPage(String pageName) { 63 super(pageName); 64 } 65 66 69 public boolean canFlipToNextPage() { 70 return stepContainer.canFlipToNextPage(); 71 } 72 73 76 public void createControl(Composite parent) { 77 Composite composite = new Composite(parent, SWT.NULL); 78 GridLayout layout = new GridLayout(); 79 layout.numColumns = 2; 80 composite.setLayout(layout); 81 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 82 composite.setFont(parent.getFont()); 83 84 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, 85 IIDEHelpContextIds.NEW_PROJECT_CONFIGURE_WIZARD_PAGE); 86 87 createStepGroup(composite); 88 createEmbeddedPageSite(composite); 89 90 setControl(composite); 91 } 92 93 97 private void createEmbeddedPageSite(Composite parent) { 98 pageSite = new Composite(parent, SWT.NONE); 99 pageSite.setLayout(new GridLayout()); 100 pageSite.setLayoutData(new GridData(GridData.FILL_BOTH)); 101 } 102 103 106 private void createStepGroup(Composite parent) { 107 stepGroup = new WizardStepGroup(); 108 stepGroup.createContents(parent); 109 } 110 111 115 WizardStepContainer getStepContainer() { 116 return stepContainer; 117 } 118 119 122 public String getMessage() { 123 String msg = stepContainer.getMessage(); 124 if (msg == null || msg.length() == 0) { 125 msg = super.getMessage(); 126 } 127 return msg; 128 } 129 130 133 public IWizardPage getPreviousPage() { 134 return stepContainer.getPreviousPage(); 135 } 136 137 140 public void setPreviousPage(IWizardPage page) { 141 super.setPreviousPage(null); 143 } 144 145 151 void setSteps(WizardStep[] steps) { 152 if (stepGroup != null) { 153 stepGroup.setSteps(steps); 154 } 155 } 156 157 161 void setWizardDialog(MultiStepWizardDialog dialog) { 162 wizardDialog = dialog; 163 } 164 165 168 public void setVisible(boolean visible) { 169 super.setVisible(visible); 170 WizardStep[] steps = stepGroup.getSteps(); 171 MultiStepWizard stepWizard = wizardDialog.getMultiStepWizard(); 172 wizardDialog.setFinishLabel(stepWizard.getFinishStepLabel(steps)); 173 174 getControl().getDisplay().asyncExec(new Runnable () { 175 public void run() { 176 stepContainer.processCurrentStep(); 177 } 178 }); 179 } 180 181 184 class WizardStepContainer implements IWizardContainer { 185 private int stepIndex = 0; 186 187 private IWizard wizard; 188 189 private IWizardPage currentPage; 190 191 194 public void run(boolean fork, boolean cancelable, 195 IRunnableWithProgress runnable) 196 throws InvocationTargetException , InterruptedException { 197 getContainer().run(fork, cancelable, runnable); 198 } 199 200 203 public IWizardPage getCurrentPage() { 204 return currentPage; 205 } 206 207 210 public Shell getShell() { 211 return getContainer().getShell(); 212 } 213 214 217 public void showPage(IWizardPage page) { 218 showPage(page, true); 219 } 220 221 224 public void updateButtons() { 225 getContainer().updateButtons(); 226 } 227 228 231 public void updateMessage() { 232 getContainer().updateMessage(); 233 } 234 235 238 public void updateTitleBar() { 239 getContainer().updateTitleBar(); 240 } 241 242 245 public void updateWindowTitle() { 246 getContainer().updateWindowTitle(); 247 } 248 249 252 public void backPressed() { 253 showPage(currentPage.getPreviousPage(), false); 254 } 255 256 259 public void nextPressed() { 260 showPage(currentPage.getNextPage(), true); 261 } 262 263 266 public void helpPressed() { 267 if (currentPage != null) { 268 currentPage.performHelp(); 269 } 270 } 271 272 277 public final boolean performCancel() { 278 if (wizard != null) { 279 return wizard.performCancel(); 280 } 281 282 return true; 283 } 284 285 290 public final boolean performFinish() { 291 if (wizard != null) { 292 if (wizard.performFinish()) { 293 wizard.dispose(); 294 wizard.setContainer(null); 295 stepGroup.markStepAsDone(); 296 stepIndex++; 297 return true; 298 } 299 return false; 300 } 301 return true; 302 303 } 304 305 314 private Point calculatePageSizeDelta(IWizardPage page) { 315 Control pageControl = page.getControl(); 316 317 if (pageControl == null) { 318 return new Point(0, 0); 320 } 321 322 Point contentSize = pageControl.computeSize(SWT.DEFAULT, 323 SWT.DEFAULT, true); 324 Rectangle rect = pageSite.getClientArea(); 325 Point containerSize = new Point(rect.width, rect.height); 326 327 return new Point(Math.max(0, contentSize.x - containerSize.x), Math 328 .max(0, contentSize.y - containerSize.y)); 329 } 330 331 337 private void updateSizeForPage(IWizardPage page) { 338 Point delta = calculatePageSizeDelta(page); 340 341 if (delta.x > 0 || delta.y > 0) { 342 Point siteSize = pageSite.getSize(); 343 GridData data = (GridData) pageSite.getLayoutData(); 344 data.heightHint = siteSize.y + delta.y; 345 data.widthHint = siteSize.x + delta.x; 346 } 347 } 348 349 355 private void updateSizeForWizard(IWizard wizard) { 356 Point delta = new Point(0, 0); 357 IWizardPage[] pages = wizard.getPages(); 358 for (int i = 0; i < pages.length; i++) { 359 Point pageDelta = calculatePageSizeDelta(pages[i]); 361 362 delta.x = Math.max(delta.x, pageDelta.x); 363 delta.y = Math.max(delta.y, pageDelta.y); 364 } 365 366 if (delta.x > 0 || delta.y > 0) { 367 Point siteSize = pageSite.getSize(); 368 GridData data = (GridData) pageSite.getLayoutData(); 369 data.heightHint = siteSize.y + delta.y; 370 data.widthHint = siteSize.x + delta.x; 371 } 372 } 373 374 377 public void processCurrentStep() { 378 WizardStep[] steps = stepGroup.getSteps(); 379 while (stepIndex < steps.length) { 380 if (stepIndex == (steps.length - 1)) { 382 wizardDialog.setFinishLabel(null); 383 } 384 385 final WizardStep step = steps[stepIndex]; 386 stepGroup.setCurrentStep(step); 387 388 final IWizard[] stepWizard = new IWizard[1]; 389 BusyIndicator.showWhile(getShell().getDisplay(), 390 new Runnable () { 391 public void run() { 392 stepWizard[0] = step.getWizard(); 393 int tries = 0; 394 while (stepWizard[0] == null && tries++ < 3) { 395 boolean tryAgain = wizardDialog 396 .getMultiStepWizard() 397 .handleMissingStepWizard(step); 398 if (!tryAgain) { 399 break; 400 } 401 402 stepWizard[0] = step.getWizard(); 403 } 404 } 405 }); 406 407 if (stepWizard[0] == null) { 408 break; 409 } 410 setWizard(stepWizard[0]); 411 if (stepWizard[0].getPageCount() > 0) { 412 return; 413 } 414 415 performFinish(); 416 } 417 418 wizardDialog.forceClose(); 419 } 420 421 426 public void setWizard(IWizard newWizard) { 427 wizard = newWizard; 428 429 wizard.createPageControls(pageSite); 432 433 IWizardPage[] pages = wizard.getPages(); 435 for (int i = 0; i < pages.length; i++) { 436 IWizardPage page = pages[i]; 437 if (page.getControl() != null) { 438 page.getControl().setVisible(false); 439 } 440 } 441 442 updateSizeForWizard(wizard); 444 wizardDialog.updateLayout(); 445 446 wizard.setContainer(this); 447 showPage(wizard.getStartingPage(), false); 448 } 449 450 456 public void showPage(IWizardPage page, boolean rememberPrevious) { 457 if (page == null || page == currentPage) { 458 return; 459 } 460 461 if (rememberPrevious && currentPage != null) { 462 page.setPreviousPage(currentPage); 463 } 464 465 if (wizard != page.getWizard()) { 466 throw new IllegalStateException (); 467 } 468 469 if (page.getControl() == null) { 472 page.createControl(pageSite); 473 if (page.getControl() == null) { 476 throw new IllegalArgumentException (); 477 } 478 updateSizeForPage(page); 480 wizardDialog.updateLayout(); 481 } 482 483 IWizardPage oldPage = currentPage; 485 currentPage = page; 486 currentPage.setVisible(true); 487 if (oldPage != null) { 488 oldPage.setVisible(false); 489 } 490 page.getControl().setBounds(pageSite.getClientArea()); 491 492 wizardDialog.updateAll(); 494 } 495 496 501 public boolean canWizardFinish() { 502 if (wizard != null) { 503 return wizard.canFinish(); 504 } 505 506 return false; 507 } 508 509 515 public boolean canFlipToNextPage() { 516 if (currentPage != null) { 517 return currentPage.canFlipToNextPage(); 518 } 519 return false; 520 } 521 522 527 public String getMessage() { 528 if (currentPage != null) { 529 return currentPage.getMessage(); 530 } 531 532 return null; 533 } 534 535 540 public IWizardPage getPreviousPage() { 541 if (currentPage != null) { 542 return currentPage.getPreviousPage(); 543 } 544 545 return null; 546 } 547 } 548 } 549 | Popular Tags |