1 11 package org.eclipse.team.core.subscribers; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.resources.mapping.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.team.core.mapping.*; 19 import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager; 20 21 27 public class SubscriberScopeManager extends SynchronizationScopeManager implements ISubscriberChangeListener { 28 29 private final Subscriber subscriber; 30 private Map participants = new HashMap(); 31 32 39 public SubscriberScopeManager(String name, ResourceMapping[] inputMappings, Subscriber subscriber, boolean consultModels) { 40 this(name, inputMappings, subscriber, SubscriberResourceMappingContext.createContext(subscriber), consultModels); 41 } 42 43 51 public SubscriberScopeManager(String name, ResourceMapping[] inputMappings, Subscriber subscriber, RemoteResourceMappingContext context, boolean consultModels) { 52 super(name, inputMappings, context, consultModels); 53 this.subscriber = subscriber; 54 } 55 56 60 protected Subscriber getSubscriber() { 61 return subscriber; 62 } 63 64 67 public void dispose() { 68 for (Iterator iter = participants.values().iterator(); iter.hasNext();) { 69 ISynchronizationScopeParticipant p = (ISynchronizationScopeParticipant) iter.next(); 70 p.dispose(); 71 } 72 super.dispose(); 73 } 74 75 78 public void initialize(IProgressMonitor monitor) throws CoreException { 79 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { 80 public void run(IProgressMonitor monitor) throws CoreException { 81 SubscriberScopeManager.super.initialize(monitor); 82 hookupParticipants(); 83 getSubscriber().addListener(SubscriberScopeManager.this); 84 } 85 }, getSchedulingRule(), IResource.NONE, monitor); 86 } 87 88 91 public ResourceTraversal[] refresh(final ResourceMapping[] mappings, IProgressMonitor monitor) throws CoreException { 92 final List result = new ArrayList(1); 93 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { 94 public void run(IProgressMonitor monitor) throws CoreException { 95 result.add(SubscriberScopeManager.super.refresh(mappings, monitor)); 96 hookupParticipants(); 97 } 98 }, getSchedulingRule(), IResource.NONE, monitor); 99 if (result.isEmpty()) 100 return new ResourceTraversal[0]; 101 return (ResourceTraversal[])result.get(0); 102 } 103 104 110 void hookupParticipants() { 111 ModelProvider[] providers = getScope().getModelProviders(); 112 for (int i = 0; i < providers.length; i++) { 113 ModelProvider provider = providers[i]; 114 if (!participants.containsKey(provider)) { 115 ISynchronizationScopeParticipant p = createParticipant(provider); 116 if (p != null) { 117 participants.put(provider, p); 118 } 119 } 120 } 121 } 122 123 126 private ISynchronizationScopeParticipant createParticipant(ModelProvider provider) { 127 Object factoryObject = provider.getAdapter(ISynchronizationScopeParticipantFactory.class); 128 if (factoryObject instanceof ISynchronizationScopeParticipantFactory) { 129 ISynchronizationScopeParticipantFactory factory = (ISynchronizationScopeParticipantFactory) factoryObject; 130 return factory.createParticipant(provider, this.getScope()); 131 } 132 return null; 133 } 134 135 138 public void subscriberResourceChanged(ISubscriberChangeEvent[] deltas) { 139 List changedResources = new ArrayList(); 140 List changedProjects = new ArrayList(); 141 for (int i = 0; i < deltas.length; i++) { 142 ISubscriberChangeEvent event = deltas[i]; 143 if ((event.getFlags() & (ISubscriberChangeEvent.ROOT_ADDED | ISubscriberChangeEvent.ROOT_REMOVED)) != 0) { 144 changedProjects.add(event.getResource().getProject()); 145 } 146 if ((event.getFlags() & ISubscriberChangeEvent.SYNC_CHANGED) != 0) { 147 changedResources.add(event.getResource()); 148 } 149 } 150 fireChange((IResource[]) changedResources.toArray(new IResource[changedResources.size()]), (IProject[]) changedProjects.toArray(new IProject[changedProjects.size()])); 151 } 152 153 private void fireChange(final IResource[] resources, final IProject[] projects) { 154 final Set result = new HashSet(); 155 ISynchronizationScopeParticipant[] handlers = (ISynchronizationScopeParticipant[]) participants.values().toArray(new ISynchronizationScopeParticipant[participants.size()]); 156 for (int i = 0; i < handlers.length; i++) { 157 final ISynchronizationScopeParticipant participant = handlers[i]; 158 SafeRunner.run(new ISafeRunnable() { 159 public void run() throws Exception { 160 ResourceMapping[] mappings = participant.handleContextChange(SubscriberScopeManager.this.getScope(), resources, projects); 161 for (int j = 0; j < mappings.length; j++) { 162 ResourceMapping mapping = mappings[j]; 163 result.add(mapping); 164 } 165 } 166 public void handleException(Throwable exception) { 167 } 169 }); 170 } 171 if (!result.isEmpty()) { 172 refresh((ResourceMapping[]) result.toArray(new ResourceMapping[result.size()])); 173 } 174 } 175 176 } 177 | Popular Tags |