KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > UserInputWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ltk.ui.refactoring;
13
14 import org.eclipse.core.runtime.IStatus;
15
16 import org.eclipse.jface.wizard.IWizardPage;
17
18 import org.eclipse.ltk.core.refactoring.CheckConditionsOperation;
19 import org.eclipse.ltk.core.refactoring.CreateChangeOperation;
20 import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
21 import org.eclipse.ltk.core.refactoring.Refactoring;
22 import org.eclipse.ltk.core.refactoring.RefactoringCore;
23 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
24
25 import org.eclipse.ltk.internal.ui.refactoring.FinishResult;
26 import org.eclipse.ltk.internal.ui.refactoring.IErrorWizardPage;
27 import org.eclipse.ltk.internal.ui.refactoring.InternalAPI;
28 import org.eclipse.ltk.internal.ui.refactoring.UIPerformChangeOperation;
29
30 /**
31  * An abstract wizard page that is to be used to implement user input pages presented
32  * inside a {@link org.eclipse.ltk.ui.refactoring.RefactoringWizard refactoring wizard}.
33  * User input pages are shown at the beginning of a wizard. As soon as the last input
34  * page is left the refactoring's condition checking is performed. Depending on the
35  * outcome an error page or the preview page is shown.
36  * <p>
37  * Clients may extend this class.
38  * </p>
39  * @since 3.0
40  */

41 public abstract class UserInputWizardPage extends RefactoringWizardPage {
42
43     private boolean fIsLastUserInputPage;
44     
45     /**
46      * Creates a new user input page.
47      * @param name the page's name.
48      */

49     public UserInputWizardPage(String JavaDoc name) {
50         super(name);
51     }
52     
53     /**
54      * Returns <code>true</code> if this is the last user input page in the stack
55      * of input pages; <code>false</code> otherwise. The last user input page is not
56      * necessarily the page after which the refactoring's precondition has to be
57      * triggered. For wizards implementing a dynamic work flow, this may happen for
58      * other pages as well.
59      *
60      * @return whether this is the last user input page or not.
61      */

62     public boolean isLastUserInputPage() {
63         return fIsLastUserInputPage;
64     }
65     
66     /**
67      * Triggers the refactoring's condition checking and returns either the
68      * error wizard page or a preview page, depending on the outcome of the
69      * precondition checking.
70      *
71      * @return either the error or the preview page, depending on the refactoring's
72      * precondition checking
73      */

74     protected final IWizardPage computeSuccessorPage() {
75         return getRefactoringWizard().computeUserInputSuccessorPage(this, getContainer());
76     }
77
78     /**
79      * Sets the page's complete status depending on the given <tt>
80      * ReactoringStatus</tt>.
81      *
82      * @param status the <tt>RefactoringStatus</tt>
83      */

84     public void setPageComplete(RefactoringStatus status) {
85         getRefactoringWizard().setConditionCheckingStatus(status);
86
87         int severity= status.getSeverity();
88         if (severity == RefactoringStatus.FATAL){
89             setPageComplete(false);
90             setErrorMessage(status.getMessageMatchingSeverity(severity));
91         } else {
92             setPageComplete(true);
93             setErrorMessage(null);
94             if (severity == RefactoringStatus.OK)
95                 setMessage(null, NONE);
96             else
97                 setMessage(status.getMessageMatchingSeverity(severity), getCorrespondingIStatusSeverity(severity));
98         }
99     }
100     
101     /**
102      * {@inheritDoc}
103      */

104     public void setVisible(boolean visible) {
105         if (visible)
106             getRefactoringWizard().internalSetChange(InternalAPI.INSTANCE, null);
107         super.setVisible(visible);
108     }
109     
110     /**
111      * {@inheritDoc}
112      */

113     public IWizardPage getNextPage() {
114         if (fIsLastUserInputPage)
115             return computeSuccessorPage();
116         else
117             return super.getNextPage();
118     }
119     
120     /**
121      * {@inheritDoc}
122      */

123     public boolean canFlipToNextPage() {
124         if (fIsLastUserInputPage) {
125             // we can't call getNextPage to determine if flipping is allowed since computing
126
// the next page is quite expensive (checking preconditions and creating a
127
// change). So we say yes if the page is complete.
128
return isPageComplete();
129         } else {
130             return super.canFlipToNextPage();
131         }
132     }
133
134     /**
135      * {@inheritDoc}
136      */

137     protected boolean performFinish() {
138         RefactoringWizard wizard= getRefactoringWizard();
139         int threshold= RefactoringCore.getConditionCheckingFailedSeverity();
140         RefactoringStatus activationStatus= wizard.getInitialConditionCheckingStatus();
141         RefactoringStatus inputStatus= null;
142         RefactoringStatus status= new RefactoringStatus();
143         Refactoring refactoring= getRefactoring();
144         
145         if (activationStatus != null && activationStatus.getSeverity() >= threshold) {
146             if (!activationStatus.hasFatalError())
147                 inputStatus= wizard.checkFinalConditions();
148         } else {
149             CreateChangeOperation create= new CreateChangeOperation(
150                 new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS),
151                 threshold);
152             PerformChangeOperation perform= new UIPerformChangeOperation(getShell().getDisplay(), create, getContainer());
153             
154             FinishResult result= wizard.internalPerformFinish(InternalAPI.INSTANCE, perform);
155             wizard.internalSetChange(InternalAPI.INSTANCE, create.getChange());
156             if (result.isException())
157                 return true;
158             if (result.isInterrupted())
159                 return false;
160             inputStatus= new RefactoringStatus();
161             inputStatus.merge(create.getConditionCheckingStatus());
162             RefactoringStatus validationStatus= perform.getValidationStatus();
163             // only merge this in if we have a fatal error. In all other cases
164
// the change got executed
165
if (validationStatus != null && validationStatus.hasFatalError())
166                 inputStatus.merge(perform.getValidationStatus());
167         }
168         
169         status.merge(activationStatus);
170         status.merge(inputStatus);
171         
172         if (status.getSeverity() >= threshold) {
173             wizard.setConditionCheckingStatus(status);
174             IWizardPage nextPage= wizard.getPage(IErrorWizardPage.PAGE_NAME);
175             wizard.getContainer().showPage(nextPage);
176             return false;
177         }
178         
179         return true;
180     }
181     
182     /* package */ void markAsLastUserInputPage() {
183         fIsLastUserInputPage= true;
184     }
185     
186     private static int getCorrespondingIStatusSeverity(int severity) {
187         if (severity == RefactoringStatus.FATAL)
188             return IStatus.ERROR;
189         if (severity == RefactoringStatus.ERROR)
190             return IStatus.WARNING;
191         if (severity == RefactoringStatus.WARNING)
192             return IStatus.WARNING;
193         if (severity == RefactoringStatus.INFO)
194             return IStatus.INFO;
195         return IStatus.OK;
196     }
197 }
198
Popular Tags