KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 import org.eclipse.core.resources.IWorkspaceRunnable;
19
20 /**
21  * Operation that, when run, checks the preconditions of the {@link Refactoring}
22  * passed on creation.
23  * <p>
24  * The operation should be executed via the run method offered by
25  * <code>IWorkspace</code> to achieve proper delta batching.
26  * </p>
27  * <p>
28  * Note: this class is not intended to be extended by clients.
29  * </p>
30  *
31  * @see org.eclipse.ltk.core.refactoring.Refactoring#checkInitialConditions(IProgressMonitor)
32  * @see org.eclipse.ltk.core.refactoring.Refactoring#checkFinalConditions(IProgressMonitor)
33  * @see org.eclipse.ltk.core.refactoring.Refactoring#checkAllConditions(IProgressMonitor)
34  *
35  * @since 3.0
36  */

37 public class CheckConditionsOperation implements IWorkspaceRunnable {
38     
39     private Refactoring fRefactoring;
40     private int fStyle;
41     private RefactoringStatus fStatus;
42     
43     /** Flag indicating that no conditions will be checked */
44     public final static int NONE= 0;
45     /** Flag indicating that only initial conditions will be checked*/
46     public final static int INITIAL_CONDITONS= 1 << 1;
47     /** Flag indicating that only final conditions will be checked */
48     public final static int FINAL_CONDITIONS= 1 << 2;
49     /** Flag indicating that all conditions will be checked */
50     public final static int ALL_CONDITIONS= INITIAL_CONDITONS | FINAL_CONDITIONS;
51     
52     private final static int LAST= 1 << 3;
53     
54     /**
55      * Creates a new <code>CheckConditionsOperation</code>.
56      *
57      * @param refactoring the refactoring for which the preconditions are to
58      * be checked.
59      * @param style style to define which conditions to check. Must be one of
60      * <code>INITIAL_CONDITONS</code>, <code>FINAL_CONDITIONS</code> or
61      * <code>ALL_CONDITIONS</code>
62      */

63     public CheckConditionsOperation(Refactoring refactoring, int style) {
64         Assert.isNotNull(refactoring);
65         fRefactoring= refactoring;
66         fStyle= style;
67         Assert.isTrue(checkStyle(fStyle));
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public void run(IProgressMonitor pm) throws CoreException {
74         if (pm == null)
75             pm= new NullProgressMonitor();
76         try {
77             fStatus= null;
78             if ((fStyle & ALL_CONDITIONS) == ALL_CONDITIONS)
79                 fStatus= fRefactoring.checkAllConditions(pm);
80             else if ((fStyle & INITIAL_CONDITONS) == INITIAL_CONDITONS)
81                 fStatus= fRefactoring.checkInitialConditions(pm);
82             else if ((fStyle & FINAL_CONDITIONS) == FINAL_CONDITIONS)
83                 fStatus= fRefactoring.checkFinalConditions(pm);
84         } finally {
85             pm.done();
86         }
87     }
88
89     /**
90      * Returns the outcome of the operation or <code>null</code> if an exception
91      * has occurred while performing the operation or if the operation hasn't
92      * been performed yet.
93      *
94      * @return the {@link RefactoringStatus} of the condition checking
95      */

96     public RefactoringStatus getStatus() {
97         return fStatus;
98     }
99     
100     /**
101      * Returns the operation's refactoring
102      *
103      * @return the operation's refactoring
104      */

105     public Refactoring getRefactoring() {
106         return fRefactoring;
107     }
108     
109     /**
110      * Returns the condition checking style.
111      *
112      * @return the condition checking style
113      */

114     public int getStyle() {
115         return fStyle;
116     }
117     
118     private boolean checkStyle(int style) {
119         return style > NONE && style < LAST;
120     }
121     
122     /* package */ int getTicks(RefactoringTickProvider provider) {
123         if ((fStyle & ALL_CONDITIONS) == ALL_CONDITIONS)
124             return provider.getCheckAllConditionsTicks();
125         else if ((fStyle & INITIAL_CONDITONS) == INITIAL_CONDITONS)
126             return provider.getCheckInitialConditionsTicks();
127         else if ((fStyle & FINAL_CONDITIONS) == FINAL_CONDITIONS)
128             return provider.getCheckFinalConditionsTicks();
129         return 0;
130     }
131 }
132
Popular Tags