KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > participants > CheckConditionsContext


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.participants;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.NullProgressMonitor;
25 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28
29 import org.eclipse.core.resources.IFile;
30
31 import org.eclipse.ltk.core.refactoring.IRefactoringCoreStatusCodes;
32 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
33
34 import org.eclipse.ltk.internal.core.refactoring.Messages;
35 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
36 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin;
37
38 /**
39  * A context that is shared between the refactoring processor and all its
40  * associated participants during condition checking.
41  * <p>
42  * The context manages a set of {@link IConditionChecker}objects to collect
43  * condition checks that should be perform across all participants and the
44  * processor. For example validating if a file can be changed (see
45  * {@link org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)}
46  * should only be called once for all files modified by the processor and all
47  * participants.
48  * </p>
49  * <p>
50  * Note: this class is not intended to be extended by clients.
51  * </p>
52  *
53  * @since 3.0
54  */

55 public class CheckConditionsContext {
56     
57     private Map JavaDoc fCheckers= new HashMap JavaDoc();
58     
59     /**
60      * Returns the condition checker of the given type.
61      *
62      * @param clazz the type of the condition checker
63      *
64      * @return the condition checker or <code>null</code> if
65      * no checker is registered for the given type
66      */

67     public IConditionChecker getChecker(Class JavaDoc clazz) {
68         return (IConditionChecker)fCheckers.get(clazz);
69     }
70     
71     /**
72      * Adds the given condition checker. An exception will be
73      * thrown if a checker of the same type already exists in
74      * this context.
75      *
76      * @param checker the checker to add
77      * @throws CoreException if a checker of the same type already
78      * exists
79      */

80     public void add(IConditionChecker checker) throws CoreException {
81         Object JavaDoc old= fCheckers.put(checker.getClass(), checker);
82         if (old != null) {
83             fCheckers.put(checker.getClass(), old);
84             throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(),
85                 IRefactoringCoreStatusCodes.CHECKER_ALREADY_EXISTS_IN_CONTEXT,
86                 Messages.format(RefactoringCoreMessages.CheckConditionContext_error_checker_exists, checker.getClass().toString()),
87                 null));
88         }
89     }
90     
91     /**
92      * Checks the condition of all registered condition checkers and returns a
93      * merge status result.
94      *
95      * @param pm a progress monitor or <code>null</code> if no progress
96      * reporting is desired
97      *
98      * @return the combined status result
99      *
100      * @throws CoreException if an error occurs during condition checking
101      */

102     public RefactoringStatus check(IProgressMonitor pm) throws CoreException {
103         if (pm == null)
104             pm= new NullProgressMonitor();
105         RefactoringStatus result= new RefactoringStatus();
106         mergeResourceOperationAndValidateEdit();
107         List JavaDoc values= new ArrayList JavaDoc(fCheckers.values());
108         Collections.sort(values, new Comparator JavaDoc() {
109             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
110                 // Note there can only be one ResourceOperationChecker. So it
111
// is save to not test the case that both objects are
112
// ResourceOperationChecker
113
if (o1 instanceof ResourceChangeChecker)
114                     return -1;
115                 if (o2 instanceof ResourceChangeChecker)
116                     return 1;
117                 return 0;
118             }
119         });
120         pm.beginTask("", values.size()); //$NON-NLS-1$
121
for (Iterator JavaDoc iter= values.iterator(); iter.hasNext();) {
122             IConditionChecker checker= (IConditionChecker)iter.next();
123             result.merge(checker.check(new SubProgressMonitor(pm, 1)));
124             if (pm.isCanceled())
125                 throw new OperationCanceledException();
126         }
127         return result;
128     }
129     
130     private void mergeResourceOperationAndValidateEdit() throws CoreException {
131         ValidateEditChecker validateEditChecker= (ValidateEditChecker) getChecker(ValidateEditChecker.class);
132         if (validateEditChecker == null)
133             return;
134         ResourceChangeChecker resourceChangeChecker= (ResourceChangeChecker) getChecker(ResourceChangeChecker.class);
135         if (resourceChangeChecker == null)
136             return;
137         
138         IFile[] changedFiles= resourceChangeChecker.getChangedFiles();
139         validateEditChecker.addFiles(changedFiles);
140     }
141 }
142
Popular Tags