KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > subscribers > SubscriberScopeManager


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
22  * A {@link ISynchronizationScopeManager} that uses a {@link Subscriber} to provide
23  * a {@link RemoteResourceMappingContext} and to notify participants when the
24  * remote state of resources change.
25  * @since 3.2
26  */

27 public class SubscriberScopeManager extends SynchronizationScopeManager implements ISubscriberChangeListener {
28
29     private final Subscriber subscriber;
30     private Map participants = new HashMap();
31
32     /**
33      * Create a manager for the given subscriber and input.
34      * @param name a human readable name for the scope
35      * @param inputMappings the input mappings
36      * @param subscriber the subscriber
37      * @param consultModels whether models should be consulted when calculating the scope
38      */

39     public SubscriberScopeManager(String JavaDoc name, ResourceMapping[] inputMappings, Subscriber subscriber, boolean consultModels) {
40         this(name, inputMappings, subscriber, SubscriberResourceMappingContext.createContext(subscriber), consultModels);
41     }
42
43     /**
44      * Create a manager for the given subscriber and input.
45      * @param name a human readable name for the scope
46      * @param inputMappings the input mappings
47      * @param subscriber the subscriber
48      * @param context a remote resource mapping context for the subscriber
49      * @param consultModels whether models should be consulted when calculating the scope
50      */

51     public SubscriberScopeManager(String JavaDoc name, ResourceMapping[] inputMappings, Subscriber subscriber, RemoteResourceMappingContext context, boolean consultModels) {
52         super(name, inputMappings, context, consultModels);
53         this.subscriber = subscriber;
54     }
55
56     /**
57      * Return the subscriber for this manager.
58      * @return the subscriber for this manager
59      */

60     protected Subscriber getSubscriber() {
61         return subscriber;
62     }
63     
64     /* (non-Javadoc)
65      * @see org.eclipse.team.core.mapping.IResourceMappingScopeManager#dispose()
66      */

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     /* (non-Javadoc)
76      * @see org.eclipse.team.core.mapping.provider.ResourceMappingScopeManager#initialize(org.eclipse.core.runtime.IProgressMonitor)
77      */

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     /* (non-Javadoc)
89      * @see org.eclipse.team.core.mapping.provider.ResourceMappingScopeManager#refresh(org.eclipse.core.resources.mapping.ResourceMapping[], org.eclipse.core.runtime.IProgressMonitor)
90      */

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     /*
105      * Hook up the participants for the participating models.
106      * This is done to ensure that future local and remote changes to
107      * resources will update the resources contained in the scope
108      * appropriately
109      */

110     /* private */ 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     /*
124      * Obtain a participant through the factory which is obtained using IAdaptable
125      */

126     private ISynchronizationScopeParticipant createParticipant(ModelProvider provider) {
127         Object JavaDoc 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     /* (non-Javadoc)
136      * @see org.eclipse.team.core.subscribers.ISubscriberChangeListener#subscriberResourceChanged(org.eclipse.team.core.subscribers.ISubscriberChangeEvent[])
137      */

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 JavaDoc {
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 JavaDoc exception) {
167                     // Handled by platform
168
}
169             });
170         }
171         if (!result.isEmpty()) {
172             refresh((ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]));
173         }
174     }
175
176 }
177
Popular Tags