KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > CompositeModelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.internal.ui.synchronize;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.team.core.synchronize.*;
21 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
22 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
23
24 /**
25  * This class provides functionality for defining composite synchronize model
26  * providers. A composite provider is one that breaks up the displayed
27  * {@link SyncInfoSet} into subsets that may be didplayed using one
28  * or more synchronize model providers.
29  *
30  */

31 public abstract class CompositeModelProvider extends AbstractSynchronizeModelProvider {
32     
33     private final List JavaDoc providers = new ArrayList JavaDoc();
34     private final Map JavaDoc resourceToElements = new HashMap JavaDoc(); // Map IResource to List of ISynchronizeModelElement
35
private final Map JavaDoc elementToProvider = new HashMap JavaDoc(); // Map ISynchronizeModelElement -> AbstractSynchronizeModelProvider
36

37     protected CompositeModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) {
38         super(configuration, set);
39     }
40     
41     /**
42      * Add the provider to the list of providers.
43      * @param provider the provider to be added
44      */

45     protected void addProvider(ISynchronizeModelProvider provider) {
46         providers.add(provider);
47     }
48     
49     /**
50      * Remove the provider from the list of providers.
51      * @param provider the provider to be removed
52      */

53     protected void removeProvider(ISynchronizeModelProvider provider) {
54         providers.remove(provider);
55         provider.dispose();
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#getProvider(org.eclipse.team.ui.synchronize.ISynchronizeModelElement)
60      */

61     protected ISynchronizeModelProvider getProvider(ISynchronizeModelElement element) {
62         return (ISynchronizeModelProvider)elementToProvider.get(element);
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#getClosestExistingParents(org.eclipse.core.resources.IResource)
67      */

68     public ISynchronizeModelElement[] getClosestExistingParents(IResource resource) {
69         ISynchronizeModelProvider[] providers = getProviders();
70         if (providers.length == 0) {
71             return new ISynchronizeModelElement[0];
72         }
73         if (providers.length == 1 && providers[0] instanceof AbstractSynchronizeModelProvider) {
74             return ((AbstractSynchronizeModelProvider)providers[0]).getClosestExistingParents(resource);
75         }
76         List JavaDoc result = new ArrayList JavaDoc();
77         for (int i = 0; i < providers.length; i++) {
78             ISynchronizeModelProvider provider = providers[i];
79             if (provider instanceof AbstractSynchronizeModelProvider) {
80                 ISynchronizeModelElement[] elements = ((AbstractSynchronizeModelProvider)provider).getClosestExistingParents(resource);
81                 for (int j = 0; j < elements.length; j++) {
82                     ISynchronizeModelElement element = elements[j];
83                     result.add(element);
84                 }
85             }
86         }
87         return (ISynchronizeModelElement[]) result.toArray(new ISynchronizeModelElement[result.size()]);
88     }
89
90     /**
91      * Return all the sub-providers of this composite.
92      * @return the sub-providers of this composite
93      */

94     protected ISynchronizeModelProvider[] getProviders() {
95         return (ISynchronizeModelProvider[]) providers.toArray(new ISynchronizeModelProvider[providers.size()]);
96     }
97     
98     /**
99      * Return the providers that are displaying the given resource.
100      * @param resource the resource
101      * @return the providers displaying the resource
102      */

103     protected ISynchronizeModelProvider[] getProvidersContaining(IResource resource) {
104         List JavaDoc elements = (List JavaDoc)resourceToElements.get(resource);
105         if (elements == null || elements.isEmpty()) {
106             return new ISynchronizeModelProvider[0];
107         }
108         List JavaDoc result = new ArrayList JavaDoc();
109         for (Iterator JavaDoc iter = elements.iterator(); iter.hasNext();) {
110             ISynchronizeModelElement element = (ISynchronizeModelElement)iter.next();
111             result.add(getProvider(element));
112         }
113         return (ISynchronizeModelProvider[]) result.toArray(new ISynchronizeModelProvider[result.size()]);
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#handleResourceAdditions(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
118      */

119     protected final void handleResourceAdditions(ISyncInfoTreeChangeEvent event) {
120         handleAdditions(event.getAddedResources());
121     }
122     
123     /**
124      * Handle the resource additions by adding them to any existing
125      * sub-providers or by creating addition sub-providers as needed.
126      * @param resources
127      */

128     protected void handleAdditions(SyncInfo[] resources) {
129         for (int i = 0; i < resources.length; i++) {
130             SyncInfo info = resources[i];
131             handleAddition(info);
132         }
133     }
134
135     /**
136      * Handle the addition of the given sync info to this provider
137      * @param info the added sync info
138      */

139     protected abstract void handleAddition(SyncInfo info);
140
141     /* (non-Javadoc)
142      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#handleResourceChanges(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
143      */

144     protected final void handleResourceChanges(ISyncInfoTreeChangeEvent event) {
145         SyncInfo[] infos = event.getChangedResources();
146         for (int i = 0; i < infos.length; i++) {
147             SyncInfo info = infos[i];
148             handleChange(info);
149         }
150     }
151     
152     /**
153      * The state of the sync info for a resource has changed. Propogate the
154      * change to any sub-providers that contain the resource.
155      * @param info the sync info for the resource whpose sync state has changed
156      */

157     protected void handleChange(SyncInfo info) {
158         handleRemoval(info.getLocal());
159         handleAddition(info);
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#handleResourceRemovals(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
164      */

165     protected final void handleResourceRemovals(ISyncInfoTreeChangeEvent event) {
166         IResource[] resources = event.getRemovedResources();
167         for (int i = 0; i < resources.length; i++) {
168             IResource resource = resources[i];
169             handleRemoval(resource);
170         }
171     }
172
173     /**
174      * Remove the resource from all providers that are displaying it
175      * @param resource the resource to be removed
176      */

177     protected void handleRemoval(IResource resource) {
178         ISynchronizeModelProvider[] providers = getProvidersContaining(resource);
179         for (int i = 0; i < providers.length; i++) {
180             ISynchronizeModelProvider provider = providers[i];
181             removeFromProvider(resource, provider);
182         }
183     }
184     
185     /**
186      * Remove the resource from the sync set of the given provider
187      * unless the provider is this composite. Subclasses can
188      * override if they show resources directly.
189      * @param resource the resource to be removed
190      * @param provider the provider from which to remove the resource
191      */

192     protected void removeFromProvider(IResource resource, ISynchronizeModelProvider provider) {
193         if (provider != this) {
194             provider.getSyncInfoSet().remove(resource);
195         }
196     }
197
198     /* (non-Javadoc)
199      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#nodeAdded(org.eclipse.team.ui.synchronize.ISynchronizeModelElement)
200      */

201     protected void nodeAdded(ISynchronizeModelElement node, AbstractSynchronizeModelProvider provider) {
202         // Update the resource-to-element map and the element-to-provider map
203
IResource r = node.getResource();
204         if(r != null) {
205             List JavaDoc elements = (List JavaDoc)resourceToElements.get(r);
206             if(elements == null) {
207                 elements = new ArrayList JavaDoc(2);
208                 resourceToElements.put(r, elements);
209             }
210             elements.add(node);
211         }
212         elementToProvider.put(node, provider);
213         super.nodeAdded(node, provider);
214     }
215     
216     /* (non-Javadoc)
217      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#modelObjectCleared(org.eclipse.team.ui.synchronize.ISynchronizeModelElement)
218      */

219     public void modelObjectCleared(ISynchronizeModelElement node) {
220         super.modelObjectCleared(node);
221         IResource r = node.getResource();
222         if(r != null) {
223             List JavaDoc elements = (List JavaDoc)resourceToElements.get(r);
224             if(elements != null) {
225                 elements.remove(node);
226                 if (elements.isEmpty()) {
227                     resourceToElements.remove(r);
228                 }
229             }
230         }
231         elementToProvider.remove(node);
232     }
233     
234     /* (non-Javadoc)
235      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#clearModelObjects(org.eclipse.team.ui.synchronize.ISynchronizeModelElement)
236      */

237     protected void recursiveClearModelObjects(ISynchronizeModelElement node) {
238         super.recursiveClearModelObjects(node);
239         if (node == getModelRoot()) {
240             clearProviders();
241         }
242     }
243     
244     private void clearProviders() {
245         for (Iterator JavaDoc iter = providers.iterator(); iter.hasNext();) {
246             ISynchronizeModelProvider provider = (ISynchronizeModelProvider) iter.next();
247             provider.dispose();
248         }
249         providers.clear();
250         resourceToElements.clear();
251         elementToProvider.clear();
252     }
253
254     /**
255      * Helper method for creating a provider for the given id.
256      * @param parent the root node for the new provider
257      * @param id the id of the providers descriptor
258      * @return the new provider
259      */

260     protected ISynchronizeModelProvider createModelProvider(ISynchronizeModelElement parent, String JavaDoc id, SyncInfoTree syncInfoTree) {
261         if (id != null && id.endsWith(FlatModelProvider.FlatModelProviderDescriptor.ID)) {
262             return new FlatModelProvider(this, parent, getConfiguration(), syncInfoTree);
263         } else if (id != null && id.endsWith(CompressedFoldersModelProvider.CompressedFolderModelProviderDescriptor.ID)) {
264             return new CompressedFoldersModelProvider(this, parent, getConfiguration(), syncInfoTree);
265         } else {
266             return new HierarchicalModelProvider(this, parent, getConfiguration(), syncInfoTree);
267         }
268     }
269     
270     /* (non-Javadoc)
271      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#dispose()
272      */

273     public void dispose() {
274         clearProviders();
275         super.dispose();
276     }
277     
278     /* (non-Javadoc)
279      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#hasViewerState()
280      */

281     protected boolean hasViewerState() {
282         return resourceToElements != null && !resourceToElements.isEmpty();
283     }
284     
285     /* (non-Javadoc)
286      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#getModelObjects(org.eclipse.core.resources.IResource)
287      */

288     protected ISynchronizeModelElement[] getModelObjects(IResource resource) {
289         List JavaDoc elements = (List JavaDoc)resourceToElements.get(resource);
290         if (elements == null) {
291             return new ISynchronizeModelElement[0];
292         }
293         return (ISynchronizeModelElement[]) elements.toArray(new ISynchronizeModelElement[elements.size()]);
294     }
295 }
296
Popular Tags