1 11 package org.eclipse.ltk.core.refactoring.participants; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 44 public class ResourceChangeChecker implements IConditionChecker { 45 46 private IResourceChangeDescriptionFactory fDeltaFactory; 47 48 public ResourceChangeChecker() { 49 fDeltaFactory= ResourceChangeValidator.getValidator().createDeltaFactory(); 50 } 51 52 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 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 IFile[] getChangedFiles() throws CoreException { 88 IResourceDelta root= fDeltaFactory.getDelta(); 89 final List result= new ArrayList (); 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 |