1 11 package org.eclipse.core.resources.mapping; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import org.eclipse.core.internal.resources.mapping.ChangeDescription; 16 import org.eclipse.core.internal.resources.mapping.ResourceChangeDescriptionFactory; 17 import org.eclipse.core.internal.utils.Messages; 18 import org.eclipse.core.internal.utils.Policy; 19 import org.eclipse.core.resources.*; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.osgi.util.NLS; 22 23 47 public final class ResourceChangeValidator { 48 49 private static ResourceChangeValidator instance; 50 51 55 public static ResourceChangeValidator getValidator() { 56 if (instance == null) 57 instance = new ResourceChangeValidator(); 58 return instance; 59 } 60 61 65 private ResourceChangeValidator() { 66 super(); 67 } 68 69 private IStatus combineResults(IStatus[] result) { 70 List notOK = new ArrayList (); 71 for (int i = 0; i < result.length; i++) { 72 IStatus status = result[i]; 73 if (!status.isOK()) { 74 notOK.add(status); 75 } 76 } 77 if (notOK.isEmpty()) { 78 return Status.OK_STATUS; 79 } 80 if (notOK.size() == 1) { 81 return (IStatus) notOK.get(0); 82 } 83 return new MultiStatus(ResourcesPlugin.PI_RESOURCES, 0, (IStatus[]) notOK.toArray(new IStatus[notOK.size()]), Messages.mapping_multiProblems, null); 84 } 85 86 92 public IResourceChangeDescriptionFactory createDeltaFactory() { 93 return new ResourceChangeDescriptionFactory(); 94 } 95 96 private ModelProvider[] getProviders(IResource[] resources) { 97 IModelProviderDescriptor[] descriptors = ModelProvider.getModelProviderDescriptors(); 98 List result = new ArrayList (); 99 for (int i = 0; i < descriptors.length; i++) { 100 IModelProviderDescriptor descriptor = descriptors[i]; 101 try { 102 IResource[] matchingResources = descriptor.getMatchingResources(resources); 103 if (matchingResources.length > 0) { 104 result.add(descriptor.getModelProvider()); 105 } 106 } catch (CoreException e) { 107 Policy.log(e.getStatus().getSeverity(), NLS.bind("Could not instantiate provider {0}", descriptor.getId()), e); } 109 } 110 return (ModelProvider[]) result.toArray(new ModelProvider[result.size()]); 111 } 112 113 116 private IResource[] getRootResources(IResourceDelta root) { 117 final ChangeDescription changeDescription = new ChangeDescription(); 118 try { 119 root.accept(new IResourceDeltaVisitor() { 120 public boolean visit(IResourceDelta delta) { 121 return changeDescription.recordChange(delta); 122 } 123 }); 124 } catch (CoreException e) { 125 Policy.log(IStatus.ERROR, "Internal error", e); } 129 return changeDescription.getRootResources(); 130 } 131 132 151 public IStatus validateChange(IResourceDelta delta, IProgressMonitor monitor) { 152 monitor = Policy.monitorFor(monitor); 153 try { 154 IResource[] resources = getRootResources(delta); 155 ModelProvider[] providers = getProviders(resources); 156 if (providers.length == 0) 157 return Status.OK_STATUS; 158 monitor.beginTask(Messages.mapping_validate, providers.length); 159 IStatus[] result = new IStatus[providers.length]; 160 for (int i = 0; i < providers.length; i++) 161 result[i] = providers[i].validateChange(delta, Policy.subMonitorFor(monitor, 1)); 162 return combineResults(result); 163 } finally { 164 monitor.done(); 165 } 166 } 167 } 168 | Popular Tags |