KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IResourceDelta;
23 import org.eclipse.core.resources.IResourceDeltaVisitor;
24 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
25 import org.eclipse.core.resources.mapping.ResourceChangeValidator;
26
27 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
28
29 /**
30  * A resource operation checker is a shared checker to collect all
31  * changes done by the refactoring and the participants to resources
32  * so that they can be validated as one change. A resource operation
33  * checker supersedes the {@link ValidateEditChecker}. So if clients
34  * add their content changes to this checker there is no need to add
35  * them to the {@link ValidateEditChecker} as well.
36  * <p>
37  * Note: this class is not intended to be extended by clients.
38  * </p>
39  *
40  * @see ResourceChangeValidator
41  *
42  * @since 3.2
43  */

44 public class ResourceChangeChecker implements IConditionChecker {
45
46     private IResourceChangeDescriptionFactory fDeltaFactory;
47     
48     public ResourceChangeChecker() {
49         fDeltaFactory= ResourceChangeValidator.getValidator().createDeltaFactory();
50     }
51     
52     /**
53      * A helper method to check a set of changed files.
54      *
55      * @param files the array of files that change
56      * @param monitor a progress monitor to report progress or <code>null</code>
57      * if progress reporting is not desired
58      *
59      * @return a refactoring status containing the detect problems
60      * @throws CoreException a {@link CoreException} if an error occurs
61      *
62      * @see ResourceChangeValidator#validateChange(IResourceDelta, IProgressMonitor)
63      */

64     public static RefactoringStatus checkFilesToBeChanged(IFile[] files, IProgressMonitor monitor) throws CoreException {
65         ResourceChangeChecker checker= new ResourceChangeChecker();
66         for (int i= 0; i < files.length; i++) {
67             checker.getDeltaFactory().change(files[i]);
68         }
69         return checker.check(monitor);
70     }
71     
72     /**
73      * Returns the delta factory to be used to record resource
74      * operations.
75      *
76      * @return the delta factory
77      */

78     public IResourceChangeDescriptionFactory getDeltaFactory() {
79         return fDeltaFactory;
80     }
81     
82     public RefactoringStatus check(IProgressMonitor monitor) throws CoreException {
83         IStatus status= ResourceChangeValidator.getValidator().validateChange(fDeltaFactory.getDelta(), monitor);
84         return createFrom(status);
85     }
86
87     /* package */ IFile[] getChangedFiles() throws CoreException {
88         IResourceDelta root= fDeltaFactory.getDelta();
89         final List JavaDoc result= new ArrayList JavaDoc();
90         root.accept(new IResourceDeltaVisitor() {
91             public boolean visit(IResourceDelta delta) throws CoreException {
92                 final IResource resource= delta.getResource();
93                 if (resource.getType() == IResource.FILE) {
94                     final int kind= delta.getKind();
95                     if (isSet(kind, IResourceDelta.CHANGED)) {
96                         result.add(resource);
97                     } else if (isSet(kind, IResourceDelta.ADDED) && isSet(delta.getFlags(), IResourceDelta.CONTENT | IResourceDelta.MOVED_FROM)) {
98                         final IFile movedFrom= resource.getWorkspace().getRoot().getFile(delta.getMovedFromPath());
99                         result.add(movedFrom);
100                     }
101                 }
102                 return true;
103             }
104         });
105         return (IFile[]) result.toArray(new IFile[result.size()]);
106     }
107         
108     private static final boolean isSet(int flags, int flag) {
109         return (flags & flag) == flag;
110     }
111     
112     private static RefactoringStatus createFrom(IStatus status) {
113         if (status.isOK())
114             return new RefactoringStatus();
115
116         if (!status.isMultiStatus()) {
117             switch (status.getSeverity()) {
118                 case IStatus.OK :
119                     return new RefactoringStatus();
120                 case IStatus.INFO :
121                     return RefactoringStatus.createInfoStatus(status.getMessage());
122                 case IStatus.WARNING :
123                     return RefactoringStatus.createWarningStatus(status.getMessage());
124                 case IStatus.ERROR :
125                     return RefactoringStatus.createErrorStatus(status.getMessage());
126                 case IStatus.CANCEL :
127                     return RefactoringStatus.createFatalErrorStatus(status.getMessage());
128                 default :
129                     return RefactoringStatus.createFatalErrorStatus(status.getMessage());
130             }
131         } else {
132             IStatus[] children= status.getChildren();
133             RefactoringStatus result= new RefactoringStatus();
134             for (int i= 0; i < children.length; i++) {
135                 result.merge(createFrom(children[i]));
136             }
137             return result;
138         }
139     }
140 }
141
Popular Tags