1 11 package org.eclipse.ltk.core.refactoring.participants; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 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 55 public class CheckConditionsContext { 56 57 private Map fCheckers= new HashMap (); 58 59 67 public IConditionChecker getChecker(Class clazz) { 68 return (IConditionChecker)fCheckers.get(clazz); 69 } 70 71 80 public void add(IConditionChecker checker) throws CoreException { 81 Object 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 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 values= new ArrayList (fCheckers.values()); 108 Collections.sort(values, new Comparator () { 109 public int compare(Object o1, Object o2) { 110 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()); for (Iterator 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 |