KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.NullProgressMonitor;
17 import org.eclipse.core.runtime.SubProgressMonitor;
18
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20
21 import org.eclipse.ltk.internal.core.refactoring.NotCancelableProgressMonitor;
22 import org.eclipse.ltk.internal.core.refactoring.history.UnknownRefactoringDescriptor;
23
24 /**
25  * Operation that, when performed, creates a {@link Change} object for a given
26  * refactoring. If created with a refactoring object directly, no precondition
27  * checking is performed. If created with a {@link CheckConditionsOperation} the
28  * requested precondition checking is performed before creating the change.
29  * <p>
30  * If the precondition checking returns a fatal error or the status's severity
31  * exceeds a certain threshold then no change will be created.
32  * </p>
33  * <p>
34  * If a change has been created the operation calls {@link Change#initializeValidationData(IProgressMonitor)}
35  * to initialize the change's validation data.
36  * </p>
37  * <p>
38  * The operation should be executed via the run method offered by
39  * <code>IWorkspace</code> to achieve proper delta batching.
40  * </p>
41  * <p>
42  * Note: this class is not intended to be extended by clients.
43  * </p>
44  *
45  * @since 3.0
46  */

47 public class CreateChangeOperation implements IWorkspaceRunnable {
48
49     private Refactoring fRefactoring;
50     
51     private CheckConditionsOperation fCheckConditionOperation;
52     private int fConditionCheckingFailedSeverity;
53     
54     private Change fChange;
55     
56     /**
57      * Creates a new operation with the given refactoring. No condition checking
58      * is performed before creating the change object. It is assumed that the
59      * condition checking has already been performed outside of this operation.
60      * The operation might fail if the precondition checking has not been performed
61      * yet.
62      *
63      * @param refactoring the refactoring for which the change is to be created
64      */

65     public CreateChangeOperation(Refactoring refactoring) {
66         Assert.isNotNull(refactoring);
67         fRefactoring= refactoring;
68     }
69     
70     /**
71      * Creates a new operation with the given {@link CheckConditionsOperation}. When
72      * performed the operation first checks the conditions as specified by the <code>
73      * CheckConditionsOperation</code>. Depending on the result of the condition
74      * checking a change object is created or not.
75      *
76      * @param operation the condition checking operation
77      * @param checkFailedSeverity the severity from which on the condition checking is
78      * interpreted as failed. The passed value must be greater than {@link RefactoringStatus#OK}
79      * and less than or equal {@link RefactoringStatus#FATAL}.
80      * The standard value from which on a condition check should is to be interpreted as
81      * failed can be accessed via {@link RefactoringCore#getConditionCheckingFailedSeverity()}.
82      *
83      */

84     public CreateChangeOperation(CheckConditionsOperation operation, int checkFailedSeverity) {
85         Assert.isNotNull(operation);
86         fCheckConditionOperation= operation;
87         fRefactoring= operation.getRefactoring();
88         Assert.isTrue (checkFailedSeverity > RefactoringStatus.OK && checkFailedSeverity <= RefactoringStatus.FATAL);
89         fConditionCheckingFailedSeverity= checkFailedSeverity;
90     }
91     
92     /**
93      * Returns the condition checking failed severity used by this operation.
94      *
95      * @return the condition checking failed severity
96      *
97      * @see RefactoringStatus
98      */

99     public int getConditionCheckingFailedSeverity() {
100         return fConditionCheckingFailedSeverity;
101     }
102
103     /**
104      * {@inheritDoc}
105      */

106     public void run(IProgressMonitor pm) throws CoreException {
107         if (pm == null)
108             pm= new NullProgressMonitor();
109         fChange= null;
110         try {
111             fChange= null;
112             RefactoringTickProvider rtp= fRefactoring.getRefactoringTickProvider();
113             if (fCheckConditionOperation != null) {
114                 int conditionTicks= fCheckConditionOperation.getTicks(rtp);
115                 int allTicks= conditionTicks + rtp.getCreateChangeTicks() + rtp.getInitializeChangeTicks();
116                 pm.beginTask("", allTicks); //$NON-NLS-1$
117
pm.subTask(""); //$NON-NLS-1$
118
fCheckConditionOperation.run(new SubProgressMonitor(pm, conditionTicks));
119                 RefactoringStatus status= fCheckConditionOperation.getStatus();
120                 if (status != null && status.getSeverity() < fConditionCheckingFailedSeverity) {
121                     fChange= fRefactoring.createChange(new SubProgressMonitor(pm, rtp.getCreateChangeTicks()));
122                     fChange.initializeValidationData(new NotCancelableProgressMonitor(
123                             new SubProgressMonitor(pm, rtp.getInitializeChangeTicks())));
124                 } else {
125                     pm.worked(rtp.getCreateChangeTicks() + rtp.getInitializeChangeTicks());
126                 }
127             } else {
128                 pm.beginTask("", rtp.getCreateChangeTicks() + rtp.getInitializeChangeTicks()); //$NON-NLS-1$
129
fChange= fRefactoring.createChange(new SubProgressMonitor(pm, rtp.getCreateChangeTicks()));
130                 fChange.initializeValidationData(new NotCancelableProgressMonitor(
131                     new SubProgressMonitor(pm, rtp.getInitializeChangeTicks())));
132             }
133         } finally {
134             pm.done();
135         }
136     }
137
138     /**
139      * Returns the outcome of the operation or <code>null</code> if an exception
140      * occurred when performing the operation or the operation hasn't been
141      * performed yet.
142      *
143      * @return the created change or <code>null</code>
144      */

145     public Change getChange() {
146         if (fChange != null) {
147             final ChangeDescriptor descriptor= fChange.getDescriptor();
148             if (descriptor == null) {
149                 final CompositeChange composite= new CompositeChange(fChange.getName()) {
150
151                     public final ChangeDescriptor getDescriptor() {
152                         return new RefactoringChangeDescriptor(new UnknownRefactoringDescriptor(fChange.getName()));
153                     }
154                 };
155                 composite.markAsSynthetic();
156                 composite.add(fChange);
157                 fChange= composite;
158             }
159         }
160         return fChange;
161     }
162     
163     /**
164      * Returns the status of the condition checking. Returns <code>null</code> if
165      * no condition checking has been requested.
166      *
167      * @return the status of the condition checking
168      */

169     public RefactoringStatus getConditionCheckingStatus() {
170         if (fCheckConditionOperation != null)
171             return fCheckConditionOperation.getStatus();
172         return null;
173     }
174     
175     /**
176      * Returns the condition checking style as set to the {@link CheckConditionsOperation}.
177      * If no condition checking operation is provided (e.g. the change is created directly
178      * by calling {@link Refactoring#createChange(IProgressMonitor)} then {@link
179      * CheckConditionsOperation#NONE} is returned.
180      *
181      * @return the condition checking style
182      */

183     public int getConditionCheckingStyle() {
184         if (fCheckConditionOperation != null)
185             return fCheckConditionOperation.getStyle();
186         return CheckConditionsOperation.NONE;
187     }
188 }
189
Popular Tags