KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > PerformRefactoringHistoryOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.ltk.core.refactoring;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.SubProgressMonitor;
17
18 import org.eclipse.core.resources.IWorkspaceRunnable;
19 import org.eclipse.core.resources.ResourcesPlugin;
20
21 import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryService;
22 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
23
24 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
25 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
26
27 /**
28  * Operation that, when run, executes a series of refactoring sequentially.
29  * Refactorings are executed using {@link PerformRefactoringOperation}.
30  * <p>
31  * The operation should be executed via the run method offered by
32  * <code>IWorkspace</code> to achieve proper delta batching.
33  * </p>
34  * <p>
35  * Note: this class is not intended to be instantiated or extended outside of
36  * the refactoring framework.
37  * </p>
38  *
39  * @see org.eclipse.core.resources.IWorkspace
40  * @see PerformRefactoringOperation
41  * @see RefactoringHistory
42  * @see RefactoringHistoryService
43  *
44  * @since 3.2
45  */

46 public class PerformRefactoringHistoryOperation implements IWorkspaceRunnable {
47
48     /** The status of the execution */
49     private RefactoringStatus fExecutionStatus= new RefactoringStatus();
50
51     /** The refactoring history */
52     private final RefactoringHistory fRefactoringHistory;
53
54     /**
55      * Creates a new perform refactoring history operation.
56      *
57      * @param history
58      * the refactoring history
59      */

60     public PerformRefactoringHistoryOperation(final RefactoringHistory history) {
61         Assert.isNotNull(history);
62         fRefactoringHistory= history;
63     }
64
65     /**
66      * Hook method which is called when the specified refactoring is going to be
67      * executed.
68      *
69      * @param refactoring
70      * the refactoring about to be executed
71      * @param descriptor
72      * the refactoring descriptor
73      * @param monitor
74      * the progress monitor to use
75      * @return a status describing the outcome of the initialization
76      */

77     protected RefactoringStatus aboutToPerformRefactoring(final Refactoring refactoring, final RefactoringDescriptor descriptor, final IProgressMonitor monitor) {
78         Assert.isNotNull(refactoring);
79         Assert.isNotNull(descriptor);
80         return new RefactoringStatus();
81     }
82
83     /**
84      * Method which is called to create a refactoring instance from a
85      * refactoring descriptor. The refactoring must be in an initialized state
86      * at the return of the method call. The default implementation delegates
87      * the task to the refactoring descriptor.
88      *
89      * @param descriptor
90      * the refactoring descriptor
91      * @param status
92      * a refactoring status to describe the outcome of the
93      * initialization
94      * @return the refactoring, or <code>null</code> if this refactoring
95      * descriptor represents the unknown refactoring, or if no
96      * refactoring contribution is available for this refactoring
97      * descriptor
98      * @throws CoreException
99      * if an error occurs while creating the refactoring instance
100      */

101     protected Refactoring createRefactoring(final RefactoringDescriptor descriptor, final RefactoringStatus status) throws CoreException {
102         Assert.isNotNull(descriptor);
103         return descriptor.createRefactoring(status);
104     }
105
106     /**
107      * Returns the execution status. Guaranteed not to be <code>null</code>.
108      *
109      * @return the status of the session
110      */

111     public final RefactoringStatus getExecutionStatus() {
112         return fExecutionStatus;
113     }
114
115     /**
116      * Hook method which is called when the specified refactoring has been
117      * performed.
118      *
119      * @param refactoring
120      * the refactoring which has been performed
121      * @param monitor
122      * the progress monitor to use
123      */

124     protected void refactoringPerformed(final Refactoring refactoring, final IProgressMonitor monitor) {
125         Assert.isNotNull(refactoring);
126         Assert.isNotNull(monitor);
127
128         // Do nothing
129
}
130
131     /**
132      * {@inheritDoc}
133      */

134     public void run(final IProgressMonitor monitor) throws CoreException {
135         fExecutionStatus= new RefactoringStatus();
136         final RefactoringDescriptorProxy[] proxies= fRefactoringHistory.getDescriptors();
137         monitor.beginTask(RefactoringCoreMessages.PerformRefactoringHistoryOperation_perform_refactorings, 160 * proxies.length);
138         final IRefactoringHistoryService service= RefactoringHistoryService.getInstance();
139         try {
140             service.connect();
141             for (int index= 0; index < proxies.length; index++) {
142                 final RefactoringDescriptor descriptor= proxies[index].requestDescriptor(new SubProgressMonitor(monitor, 10, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
143                 if (descriptor != null) {
144                     Refactoring refactoring= null;
145                     RefactoringStatus status= new RefactoringStatus();
146                     try {
147                         try {
148                             refactoring= createRefactoring(descriptor, status);
149                         } catch (CoreException exception) {
150                             status.merge(RefactoringStatus.create(exception.getStatus()));
151                         }
152                         if (refactoring != null && !status.hasFatalError()) {
153                             final PerformRefactoringOperation operation= new PerformRefactoringOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS);
154                             try {
155                                 status.merge(aboutToPerformRefactoring(refactoring, descriptor, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
156                                 if (!status.hasFatalError()) {
157                                     ResourcesPlugin.getWorkspace().run(operation, new SubProgressMonitor(monitor, 90, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
158                                     status.merge(operation.getConditionStatus());
159                                     if (!status.hasFatalError())
160                                         status.merge(operation.getValidationStatus());
161                                 }
162                             } finally {
163                                 refactoringPerformed(refactoring, new SubProgressMonitor(monitor, 10, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
164                             }
165                         }
166                     } finally {
167                         fExecutionStatus.merge(status);
168                     }
169                 }
170             }
171         } finally {
172             service.disconnect();
173             monitor.done();
174         }
175     }
176 }
177
Popular Tags