1 11 12 package org.eclipse.ltk.ui.refactoring; 13 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 20 import org.eclipse.core.resources.ResourcesPlugin; 21 22 import org.eclipse.swt.widgets.Shell; 23 24 import org.eclipse.jface.operation.IRunnableContext; 25 import org.eclipse.jface.wizard.IWizardContainer; 26 import org.eclipse.jface.wizard.IWizardPage; 27 import org.eclipse.jface.wizard.Wizard; 28 29 import org.eclipse.ui.PlatformUI; 30 31 import org.eclipse.ltk.core.refactoring.Change; 32 import org.eclipse.ltk.core.refactoring.CheckConditionsOperation; 33 import org.eclipse.ltk.core.refactoring.CreateChangeOperation; 34 import org.eclipse.ltk.core.refactoring.PerformChangeOperation; 35 import org.eclipse.ltk.core.refactoring.Refactoring; 36 import org.eclipse.ltk.core.refactoring.RefactoringCore; 37 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 38 import org.eclipse.ltk.internal.ui.refactoring.ChangeExceptionHandler; 39 import org.eclipse.ltk.internal.ui.refactoring.ErrorWizardPage; 40 import org.eclipse.ltk.internal.ui.refactoring.ExceptionHandler; 41 import org.eclipse.ltk.internal.ui.refactoring.FinishResult; 42 import org.eclipse.ltk.internal.ui.refactoring.IErrorWizardPage; 43 import org.eclipse.ltk.internal.ui.refactoring.IPreviewWizardPage; 44 import org.eclipse.ltk.internal.ui.refactoring.InternalAPI; 45 import org.eclipse.ltk.internal.ui.refactoring.Messages; 46 import org.eclipse.ltk.internal.ui.refactoring.PreviewWizardPage; 47 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages; 48 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages; 49 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin; 50 import org.eclipse.ltk.internal.ui.refactoring.WorkbenchRunnableAdapter; 51 52 74 public abstract class RefactoringWizard extends Wizard { 75 76 79 public static final int NONE= 0; 80 81 86 public static final int CHECK_INITIAL_CONDITIONS_ON_OPEN= 1 << 0; 87 88 94 public static final int WIZARD_BASED_USER_INTERFACE= 1 << 1; 95 96 108 public static final int DIALOG_BASED_USER_INTERFACE= 1 << 2; 109 110 113 public static final int DIALOG_BASED_UESR_INTERFACE= DIALOG_BASED_USER_INTERFACE; 114 115 120 public static final int YES_NO_BUTTON_STYLE= 1 << 3; 121 122 127 public static final int NO_PREVIEW_PAGE= 1 << 4; 128 129 133 public static final int PREVIEW_EXPAND_FIRST_NODE= 1 << 5; 134 135 141 public static final int NO_BACK_BUTTON_ON_STATUS_DIALOG= 1 << 6; 142 143 private static final int LAST= 1 << 7; 144 145 private int fFlags; 146 private Refactoring fRefactoring; 147 private String fDefaultPageTitle; 148 149 private Change fChange; 150 private RefactoringStatus fInitialConditionCheckingStatus= new RefactoringStatus(); 151 private RefactoringStatus fConditionCheckingStatus; 152 153 private int fUserInputPages; 154 private boolean fInAddPages; 155 156 private boolean fIsChangeCreationCancelable; 157 private boolean fForcePreviewReview; 158 private boolean fPreviewShown; 159 160 169 public RefactoringWizard(Refactoring refactoring, int flags) { 170 Assert.isNotNull(refactoring); 171 Assert.isTrue(flags < LAST); 172 if ((flags & DIALOG_BASED_USER_INTERFACE) == 0) 173 flags |= WIZARD_BASED_USER_INTERFACE; 174 Assert.isTrue((flags & DIALOG_BASED_USER_INTERFACE) != 0 || (flags & WIZARD_BASED_USER_INTERFACE) != 0); 175 fRefactoring= refactoring; 176 fFlags= flags; 177 setNeedsProgressMonitor(true); 178 setChangeCreationCancelable(true); 179 setWindowTitle(RefactoringUIMessages.RefactoringWizard_title); 180 setDefaultPageImageDescriptor(RefactoringPluginImages.DESC_WIZBAN_REFACTOR); 181 } 182 183 185 190 public final Refactoring getRefactoring(){ 191 return fRefactoring; 192 } 193 194 203 public final void setDefaultPageTitle(String defaultPageTitle) { 204 Assert.isNotNull(defaultPageTitle); 205 fDefaultPageTitle= defaultPageTitle; 206 } 207 208 216 public final String getDefaultPageTitle() { 217 return fDefaultPageTitle; 218 } 219 220 229 public final void setForcePreviewReview(boolean forcePreviewReview) { 230 fForcePreviewReview= forcePreviewReview; 231 IWizardContainer container= getContainer(); 232 if (container != null) 233 container.updateButtons(); 234 } 235 236 245 public int getMessageLineWidthInChars() { 246 return 80; 247 } 248 249 259 public final void setChangeCreationCancelable(boolean isChangeCreationCancelable){ 260 fIsChangeCreationCancelable= isChangeCreationCancelable; 261 } 262 263 273 public final void setInitialConditionCheckingStatus(RefactoringStatus status) { 274 Assert.isNotNull(status); 275 fInitialConditionCheckingStatus= status; 276 setConditionCheckingStatus(status); 277 } 278 279 287 public final Change getChange() { 288 return fChange; 289 } 290 291 299 final RefactoringStatus getInitialConditionCheckingStatus() { 300 return fInitialConditionCheckingStatus; 301 } 302 303 309 boolean needsWizardBasedUserInterface() { 310 return (fFlags & WIZARD_BASED_USER_INTERFACE) != 0; 311 } 312 313 315 321 public final void addPages() { 322 Assert.isNotNull(fRefactoring); 323 try { 324 fInAddPages= true; 325 if (checkActivationOnOpen()) { 326 internalCheckCondition(CheckConditionsOperation.INITIAL_CONDITONS); 327 } 328 if (fInitialConditionCheckingStatus.hasFatalError()) { 329 addErrorPage(); 330 setConditionCheckingStatus(getConditionCheckingStatus()); 332 } else { 333 Assert.isTrue(getPageCount() == 0); 334 addUserInputPages(); 335 fUserInputPages= getPageCount(); 336 if (fUserInputPages > 0) { 337 IWizardPage[] pages= getPages(); 338 ((UserInputWizardPage)pages[fUserInputPages - 1]).markAsLastUserInputPage(); 339 } 340 addErrorPage(); 341 addPreviewPage(); 342 } 343 initializeDefaultPageTitles(); 344 } finally { 345 fInAddPages= false; 346 } 347 } 348 349 355 public final void addPage(IWizardPage page) { 356 Assert.isTrue(page instanceof RefactoringWizardPage && fInAddPages); 357 super.addPage(page); 358 } 359 360 366 protected abstract void addUserInputPages(); 367 368 private void addErrorPage(){ 369 addPage(new ErrorWizardPage()); 370 } 371 372 private void addPreviewPage(){ 373 addPage(new PreviewWizardPage()); 374 } 375 376 private boolean hasUserInput() { 377 return fUserInputPages > 0; 378 } 379 380 private void initializeDefaultPageTitles() { 381 if (fDefaultPageTitle == null) 382 return; 383 384 IWizardPage[] pages= getPages(); 385 for (int i= 0; i < pages.length; i++) { 386 IWizardPage page= pages[i]; 387 if (page.getTitle() == null) 388 page.setTitle(fDefaultPageTitle); 389 } 390 } 391 392 394 397 public IWizardPage getStartingPage() { 398 if (hasUserInput()) 399 return super.getStartingPage(); 400 401 405 return computeUserInputSuccessorPage(null, PlatformUI.getWorkbench().getActiveWorkbenchWindow()); 406 } 407 408 411 public IWizardPage getPreviousPage(IWizardPage page) { 412 if (hasUserInput()) 413 return super.getPreviousPage(page); 414 if (! page.getName().equals(IErrorWizardPage.PAGE_NAME)){ 415 if (fConditionCheckingStatus.isOK()) 416 return null; 417 } 418 return super.getPreviousPage(page); 419 } 420 421 IWizardPage computeUserInputSuccessorPage(IWizardPage caller, IRunnableContext context) { 422 Change change= createChange(new CreateChangeOperation( 423 new CheckConditionsOperation(fRefactoring, CheckConditionsOperation.FINAL_CONDITIONS), 424 RefactoringStatus.FATAL), true, context); 425 RefactoringStatus status= getConditionCheckingStatus(); 427 428 if (change == null && status == null) { 430 internalSetChange(InternalAPI.INSTANCE, change); 431 return caller; 432 } 433 434 if (!status.hasFatalError()) 436 internalSetChange(InternalAPI.INSTANCE, change); 437 438 if (status.isOK()) { 439 return getPage(IPreviewWizardPage.PAGE_NAME); 440 } else { 441 return getPage(IErrorWizardPage.PAGE_NAME); 442 } 443 } 444 445 448 public boolean canFinish() { 449 if (fForcePreviewReview && !fPreviewShown) 450 return false; 451 return super.canFinish(); 452 } 453 454 456 final RefactoringStatus checkFinalConditions() { 457 return internalCheckCondition(CheckConditionsOperation.FINAL_CONDITIONS); 458 } 459 460 private RefactoringStatus internalCheckCondition(int style) { 461 462 CheckConditionsOperation op= new CheckConditionsOperation(fRefactoring, style); 463 464 Exception exception= null; 465 try { 466 PlatformUI.getWorkbench().getProgressService().busyCursorWhile( 467 new WorkbenchRunnableAdapter(op, ResourcesPlugin.getWorkspace().getRoot())); 468 } catch (InterruptedException e) { 469 exception= e; 470 } catch (InvocationTargetException e) { 471 exception= e; 472 } 473 RefactoringStatus status= null; 474 if (exception != null) { 475 RefactoringUIPlugin.log(exception); 476 status= new RefactoringStatus(); 477 status.addFatalError(RefactoringUIMessages.RefactoringWizard_internal_error_1); 478 } else { 479 status= op.getStatus(); 480 } 481 setConditionCheckingStatus(status, style); 482 return status; 483 } 484 485 private void setConditionCheckingStatus(RefactoringStatus status, int style) { 486 if ((style & CheckConditionsOperation.ALL_CONDITIONS) == CheckConditionsOperation.ALL_CONDITIONS) 487 setConditionCheckingStatus(status); 488 else if ((style & CheckConditionsOperation.INITIAL_CONDITONS) == CheckConditionsOperation.INITIAL_CONDITONS) 489 setInitialConditionCheckingStatus(status); 490 else if ((style & CheckConditionsOperation.FINAL_CONDITIONS) == CheckConditionsOperation.FINAL_CONDITIONS) 491 setFinalConditionCheckingStatus(status); 492 } 493 494 private RefactoringStatus getConditionCheckingStatus() { 495 return fConditionCheckingStatus; 496 } 497 498 503 final void setConditionCheckingStatus(RefactoringStatus status) { 504 ErrorWizardPage page= (ErrorWizardPage)getPage(IErrorWizardPage.PAGE_NAME); 505 if (page != null) 506 page.setStatus(status); 507 fConditionCheckingStatus= status; 508 } 509 510 517 private void setFinalConditionCheckingStatus(RefactoringStatus status) { 518 RefactoringStatus newStatus= new RefactoringStatus(); 519 if (fInitialConditionCheckingStatus != null) 520 newStatus.merge(fInitialConditionCheckingStatus); 521 newStatus.merge(status); 522 setConditionCheckingStatus(newStatus); 523 } 524 525 527 536 public final Change internalCreateChange(InternalAPI api, CreateChangeOperation operation, boolean updateStatus) { 537 Assert.isNotNull(api); 538 return createChange(operation, updateStatus, getContainer()); 539 } 540 541 549 public final FinishResult internalPerformFinish(InternalAPI api, PerformChangeOperation op) { 550 op.setUndoManager(RefactoringCore.getUndoManager(), fRefactoring.getName()); 551 Shell parent= getContainer().getShell(); 552 try{ 553 getContainer().run(true, true, new WorkbenchRunnableAdapter(op, ResourcesPlugin.getWorkspace().getRoot())); 554 } catch (InvocationTargetException e) { 555 Throwable inner= e.getTargetException(); 556 if (op.changeExecutionFailed()) { 557 ChangeExceptionHandler handler= new ChangeExceptionHandler(parent, fRefactoring); 558 if (inner instanceof RuntimeException ) { 559 handler.handle(op.getChange(), (RuntimeException )inner); 560 return FinishResult.createException(); 561 } else if (inner instanceof CoreException) { 562 handler.handle(op.getChange(), (CoreException)inner); 563 return FinishResult.createException(); 564 } 565 } 566 ExceptionHandler.handle(e, parent, 567 RefactoringUIMessages.RefactoringWizard_refactoring, 568 RefactoringUIMessages.RefactoringWizard_unexpected_exception_1); 569 return FinishResult.createException(); 570 } catch (InterruptedException e) { 571 return FinishResult.createInterrupted(); 572 } 573 return FinishResult.createOK(); 574 } 575 576 private Change createChange(CreateChangeOperation operation, boolean updateStatus, IRunnableContext context){ 577 InvocationTargetException exception= null; 578 try { 579 context.run(true, fIsChangeCreationCancelable, new WorkbenchRunnableAdapter( 580 operation, ResourcesPlugin.getWorkspace().getRoot())); 581 } catch (InterruptedException e) { 582 setConditionCheckingStatus(null); 583 return null; 584 } catch (InvocationTargetException e) { 585 exception= e; 586 } 587 588 if (updateStatus) { 589 RefactoringStatus status= null; 590 if (exception != null) { 591 status= new RefactoringStatus(); 592 String msg= exception.getMessage(); 593 if (msg != null) { 594 status.addFatalError(Messages.format(RefactoringUIMessages.RefactoringWizard_see_log, msg)); 595 } else { 596 status.addFatalError(RefactoringUIMessages.RefactoringWizard_Internal_error); 597 } 598 RefactoringUIPlugin.log(exception); 599 } else { 600 status= operation.getConditionCheckingStatus(); 601 } 602 setConditionCheckingStatus(status, operation.getConditionCheckingStyle()); 603 } else { 604 if (exception != null) 605 ExceptionHandler.handle(exception, getContainer().getShell(), 606 RefactoringUIMessages.RefactoringWizard_refactoring, 607 RefactoringUIMessages.RefactoringWizard_unexpected_exception); 608 } 609 Change change= operation.getChange(); 610 return change; 611 } 612 613 615 public boolean performFinish() { 616 Assert.isNotNull(fRefactoring); 617 618 RefactoringWizardPage page= (RefactoringWizardPage)getContainer().getCurrentPage(); 619 return page.performFinish(); 620 } 621 622 public boolean performCancel() { 623 if (fChange != null) 624 fChange.dispose(); 625 return super.performCancel(); 626 } 627 628 630 637 public final boolean internalHasPreviewPage(InternalAPI api) { 638 Assert.isNotNull(api); 639 return (fFlags & NO_PREVIEW_PAGE) == 0; 640 } 641 642 649 public final boolean internalIsYesNoStyle(InternalAPI api) { 650 Assert.isNotNull(api); 651 return (fFlags & YES_NO_BUTTON_STYLE) != 0; 652 } 653 654 661 public final boolean internalGetExpandFirstNode(InternalAPI api) { 662 Assert.isNotNull(api); 663 return (fFlags & PREVIEW_EXPAND_FIRST_NODE) != 0; 664 } 665 666 672 public final void internalSetChange(InternalAPI api, Change change){ 673 Assert.isNotNull(api); 674 IPreviewWizardPage page= (IPreviewWizardPage)getPage(IPreviewWizardPage.PAGE_NAME); 675 if (page != null) 676 page.setChange(change); 677 fChange= change; 678 } 679 680 686 public final void internalSetPreviewShown(InternalAPI api, boolean shown) { 687 Assert.isNotNull(api); 688 fPreviewShown= shown; 689 getContainer().updateButtons(); 690 } 691 692 698 public final boolean internalShowBackButtonOnStatusDialog(InternalAPI api) { 699 Assert.isNotNull(api); 700 return (fFlags & NO_BACK_BUTTON_ON_STATUS_DIALOG) == 0; 701 } 702 703 705 private boolean checkActivationOnOpen() { 706 return (fFlags & CHECK_INITIAL_CONDITIONS_ON_OPEN) != 0; 707 } 708 } 709 | Popular Tags |