1 11 package org.eclipse.core.internal.resources; 12 13 import java.util.*; 14 import org.eclipse.core.internal.events.ILifecycleListener; 15 import org.eclipse.core.internal.events.LifecycleEvent; 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.resources.team.ResourceRuleFactory; 18 import org.eclipse.core.resources.team.TeamHook; 19 import org.eclipse.core.runtime.jobs.ISchedulingRule; 20 import org.eclipse.core.runtime.jobs.MultiRule; 21 22 27 class Rules implements IResourceRuleFactory, ILifecycleListener { 28 private final ResourceRuleFactory defaultFactory = new ResourceRuleFactory() {}; 29 32 private final Map projectsToRules = Collections.synchronizedMap(new HashMap()); 33 private final TeamHook teamHook; 34 private final IWorkspaceRoot root; 35 36 40 Rules(Workspace workspace) { 41 this.root = workspace.getRoot(); 42 this.teamHook = workspace.getTeamHook(); 43 workspace.addLifecycleListener(this); 44 } 45 46 49 public ISchedulingRule buildRule() { 50 return root; 52 } 53 54 57 public ISchedulingRule copyRule(IResource source, IResource destination) { 58 if (source.getType() == IResource.ROOT || destination.getType() == IResource.ROOT) 59 return root; 60 return factoryFor(destination).copyRule(source, destination); 62 } 63 64 67 public ISchedulingRule createRule(IResource resource) { 68 if (resource.getType() == IResource.ROOT) 69 return root; 70 return factoryFor(resource).createRule(resource); 71 } 72 73 76 public ISchedulingRule deleteRule(IResource resource) { 77 if (resource.getType() == IResource.ROOT) 78 return root; 79 return factoryFor(resource).deleteRule(resource); 80 } 81 82 85 private IResourceRuleFactory factoryFor(IResource destination) { 86 IResourceRuleFactory fac = (IResourceRuleFactory) projectsToRules.get(destination.getFullPath().segment(0)); 87 if (fac == null) { 88 if (!destination.getProject().isAccessible()) 90 return defaultFactory; 91 fac = teamHook.getRuleFactory(destination.getProject()); 93 projectsToRules.put(destination.getFullPath().segment(0), fac); 94 } 95 return fac; 96 } 97 98 101 public void handleEvent(LifecycleEvent event) { 102 switch (event.kind) { 106 case LifecycleEvent.PRE_PROJECT_CLOSE : 107 case LifecycleEvent.PRE_PROJECT_DELETE : 108 case LifecycleEvent.PRE_PROJECT_MOVE : 109 setRuleFactory((IProject) event.resource, null); 110 } 111 } 112 113 116 public ISchedulingRule charsetRule(IResource resource) { 117 if (resource.getType() == IResource.ROOT) 118 return null; 119 return factoryFor(resource).charsetRule(resource); 120 } 121 122 125 public ISchedulingRule markerRule(IResource resource) { 126 return null; 128 } 129 130 133 public ISchedulingRule modifyRule(IResource resource) { 134 if (resource.getType() == IResource.ROOT) 135 return root; 136 return factoryFor(resource).modifyRule(resource); 137 } 138 139 142 public ISchedulingRule moveRule(IResource source, IResource destination) { 143 if (source.getType() == IResource.ROOT || destination.getType() == IResource.ROOT) 144 return root; 145 if (!source.getFullPath().segment(0).equals(destination.getFullPath().segment(0))) 147 return MultiRule.combine(deleteRule(source), createRule(destination)); 148 return factoryFor(source).moveRule(source, destination); 149 } 150 151 154 public ISchedulingRule refreshRule(IResource resource) { 155 if (resource.getType() == IResource.ROOT) 156 return root; 157 return factoryFor(resource).refreshRule(resource); 158 } 159 160 163 void setRuleFactory(IProject project, IResourceRuleFactory factory) { 164 if (factory == null) 165 projectsToRules.remove(project.getName()); 166 else 167 projectsToRules.put(project.getName(), factory); 168 } 169 170 174 public ISchedulingRule validateEditRule(IResource[] resources) { 175 if (resources.length == 0) 176 return null; 177 if (resources.length == 1) { 179 if (resources[0].getType() == IResource.ROOT) 180 return root; 181 return factoryFor(resources[0]).validateEditRule(resources); 182 } 183 HashSet rules = new HashSet(); 185 IResource[] oneResource = new IResource[1]; 186 for (int i = 0; i < resources.length; i++) { 187 if (resources[i].getType() == IResource.ROOT) 188 return root; 189 oneResource[0] = resources[i]; 190 ISchedulingRule rule = factoryFor(resources[i]).validateEditRule(oneResource); 191 if (rule != null) 192 rules.add(rule); 193 } 194 if (rules.isEmpty()) 195 return null; 196 if (rules.size() == 1) 197 return (ISchedulingRule) rules.iterator().next(); 198 ISchedulingRule[] ruleArray = (ISchedulingRule[]) rules.toArray(new ISchedulingRule[rules.size()]); 199 return new MultiRule(ruleArray); 200 } 201 } 202 | Popular Tags |