KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
22  * Operation that, when run, executes a refactoring. This includes
23  * condition checking, change creation, change execution and remembering
24  * of the undo change on the refactoring's undo stack.
25  * <p>
26  * The operation should be executed via the run method offered by
27  * <code>IWorkspace</code> to achieve proper delta batching.
28  * </p>
29  * <p>
30  * Note: this class is not intended to be extended by clients.
31  * </p>
32  *
33  * @see org.eclipse.core.resources.IWorkspace
34  *
35  * @since 3.0
36  */

37 public class PerformRefactoringOperation implements IWorkspaceRunnable {
38     
39     private int fStyle;
40     private Refactoring fRefactoring;
41     
42     private RefactoringStatus fPreconditionStatus;
43     private RefactoringStatus fValidationStatus;
44     private Change fUndo;
45     
46     /**
47      * Create a new perform refactoring operation. The operation will not
48      * perform the refactoring if the refactoring's condition checking returns
49      * an error of severity {@link RefactoringStatus#FATAL}.
50      *
51      * @param refactoring the refactoring to perform
52      * @param style the condition checking style as defined by
53      * {@link CheckConditionsOperation}
54      */

55     public PerformRefactoringOperation(Refactoring refactoring, int style) {
56         Assert.isNotNull(refactoring);
57         fRefactoring= refactoring;
58         fStyle= style;
59     }
60     
61     /**
62      * Return the refactoring status of the condition checking.
63      *
64      * @return the refactoring status of the condition checking or <code>null</code>
65      * if the operation hasn't been performed yet
66      */

67     public RefactoringStatus getConditionStatus() {
68         return fPreconditionStatus;
69     }
70     
71     /**
72      * Returns the refactoring status of the change's validation checking
73      * or <code>null</code> if a change couldn't be created or the operation
74      * hasn't been performed yet.
75      *
76      * @return the refactoring status of the change's validation checking
77      */

78     public RefactoringStatus getValidationStatus() {
79         return fValidationStatus;
80     }
81     
82     /**
83      * The undo object or <code>null</code> if no undo exists. The undo
84      * object is initialize via the call {@link Change#initializeValidationData(IProgressMonitor)}
85      *
86      * @return the undo object or <code>null</code>
87      */

88     public Change getUndoChange() {
89         return fUndo;
90     }
91
92     /**
93      * {@inheritDoc}
94      */

95     public void run(IProgressMonitor monitor) throws CoreException {
96         if (monitor == null)
97             monitor= new NullProgressMonitor();
98         monitor.beginTask("", 10); //$NON-NLS-1$
99
final CreateChangeOperation create= new CreateChangeOperation(new CheckConditionsOperation(fRefactoring, fStyle), RefactoringStatus.FATAL);
100         create.run(new SubProgressMonitor(monitor, 6));
101         fPreconditionStatus= create.getConditionCheckingStatus();
102         if (fPreconditionStatus.hasFatalError()) {
103             monitor.done();
104             return;
105         }
106         final Change change= create.getChange();
107         if (change != null) {
108             final PerformChangeOperation perform= new PerformChangeOperation(change);
109             perform.setUndoManager(RefactoringCore.getUndoManager(), fRefactoring.getName());
110             perform.run(new SubProgressMonitor(monitor, 2));
111             fValidationStatus= perform.getValidationStatus();
112             fUndo= perform.getUndoChange();
113         }
114     }
115 }
116
Popular Tags