KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
15 import java.util.HashMap 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.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.viewers.ViewerSorter;
23 import org.eclipse.team.core.synchronize.*;
24 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
25 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
26
27 /**
28  * This class is reponsible for creating and maintaining a presentation model of
29  * {@link SynchronizeModelElement} elements that can be shown in a viewer. The model
30  * is based on the synchronization information contained in the provided {@link SyncInfoSet}.
31  * <p>
32  * label updates (property propagation to parent nodes)
33  * sync change listener (changes, additions, removals, reset)
34  * batching busy updates
35  * </p>
36  *
37  * @see HierarchicalModelProvider
38  * @see CompressedFoldersModelProvider
39  * @since 3.0
40  */

41 public abstract class SynchronizeModelProvider extends AbstractSynchronizeModelProvider implements ISyncInfoSetChangeListener {
42     
43     protected final Map JavaDoc resourceMap = Collections.synchronizedMap(new HashMap JavaDoc());
44
45     protected static final boolean DEBUG = false;
46     
47     public SynchronizeModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) {
48         super(configuration, set);
49     }
50
51     public SynchronizeModelProvider(AbstractSynchronizeModelProvider parentProvider, ISynchronizeModelElement modelRoot, ISynchronizePageConfiguration configuration, SyncInfoSet set) {
52         super(parentProvider, modelRoot, configuration, set);
53         associateRoot(modelRoot);
54     }
55     
56     private void associateRoot(ISynchronizeModelElement modelRoot) {
57         // associate the root resource with the provider's root element
58
resourceMap.put(ResourcesPlugin.getWorkspace().getRoot(), modelRoot);
59     }
60     
61     /**
62      * Dispose of the builder
63      */

64     public void dispose() {
65         resourceMap.clear();
66         super.dispose();
67     }
68
69     /**
70      * Returns the sorter for this model provider.
71      *
72      * @return the sorter for this model provider.
73      */

74     public abstract ViewerSorter getViewerSorter();
75
76     /**
77      * Return the model object (i.e. an instance of <code>SyncInfoModelElement</code>
78      * or one of its subclasses) for the given IResource.
79      * @param resource
80      * the resource
81      * @return the <code>SyncInfoModelElement</code> for the given resource
82      */

83     protected ISynchronizeModelElement getModelObject(IResource resource) {
84         return (ISynchronizeModelElement) resourceMap.get(resource);
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#getModelObjects(org.eclipse.core.resources.IResource)
89      */

90     protected ISynchronizeModelElement[] getModelObjects(IResource resource) {
91         ISynchronizeModelElement element = getModelObject(resource);
92         if (element == null) {
93             return new ISynchronizeModelElement[0];
94         }
95         return new ISynchronizeModelElement[] { element };
96     }
97     
98     protected void associateDiffNode(ISynchronizeModelElement node) {
99         IResource resource = node.getResource();
100         if(resource != null) {
101             resourceMap.put(resource, node);
102         }
103     }
104
105     protected void unassociateDiffNode(IResource resource) {
106         resourceMap.remove(resource);
107     }
108     
109     /**
110      * Helper method to remove a resource from the viewer. If the resource
111      * is not mapped to a model element, this is a no-op.
112      * @param resource the resource to remove
113      */

114     protected void removeFromViewer(IResource resource) {
115         ISynchronizeModelElement element = getModelObject(resource);
116         if(element != null) {
117             removeFromViewer(new ISynchronizeModelElement[] { element });
118         }
119     }
120     
121     /**
122      * Helper method to remove a set of resources from the viewer. If a resource
123      * is not mapped to a model element, it is ignored.
124      * @param resources the resources to remove
125      */

126     protected void removeFromViewer(IResource[] resources) {
127         List JavaDoc elements = new ArrayList JavaDoc();
128         for (int i = 0; i < resources.length; i++) {
129             IResource resource = resources[i];
130             ISynchronizeModelElement element = getModelObject(resource);
131             if(element != null) {
132                 elements.add(element);
133             }
134         }
135         if (!elements.isEmpty()) {
136             removeFromViewer((ISynchronizeModelElement[]) elements.toArray(new ISynchronizeModelElement[elements.size()]));
137         }
138     }
139     
140     /* (non-Javadoc)
141      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#clearModelObjects(org.eclipse.team.ui.synchronize.ISynchronizeModelElement)
142      */

143     protected void recursiveClearModelObjects(ISynchronizeModelElement node) {
144         super.recursiveClearModelObjects(node);
145         if (node == getModelRoot()) {
146             // If we are clearing everything under the root
147
// than just purge the resource map
148
resourceMap.clear();
149             // Reassociate the root node to allow the children to be readded
150
associateRoot(getModelRoot());
151         } else {
152             IResource resource = node.getResource();
153             if (resource != null) {
154                 unassociateDiffNode(resource);
155             }
156         }
157     }
158     
159     /* (non-Javadoc)
160      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#addToViewer(org.eclipse.team.ui.synchronize.ISynchronizeModelElement)
161      */

162     protected void addToViewer(ISynchronizeModelElement node) {
163         associateDiffNode(node);
164         super.addToViewer(node);
165     }
166
167     /* (non-Javadoc)
168      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#hasViewerState()
169      */

170     protected boolean hasViewerState() {
171         return ! resourceMap.isEmpty();
172     }
173
174     public ISynchronizeModelElement[] getClosestExistingParents(IResource resource) {
175         ISynchronizeModelElement element = getModelObject(resource);
176         if(element == null) {
177             do {
178                 resource = resource.getParent();
179                 element = getModelObject(resource);
180             } while(element == null && resource != null);
181         }
182         if (element == null) {
183             return new ISynchronizeModelElement[0];
184         }
185         return new ISynchronizeModelElement[] { element };
186     }
187     
188     /* (non-Javadoc)
189      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#handleChanges(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
190      */

191     protected final void handleChanges(ISyncInfoTreeChangeEvent event, IProgressMonitor monitor) {
192         super.handleChanges(event, monitor);
193     }
194     
195     /* (non-Javadoc)
196      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#handleResourceChanges(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
197      */

198     protected void handleResourceChanges(ISyncInfoTreeChangeEvent event) {
199         // Refresh the viewer for each changed resource
200
SyncInfo[] infos = event.getChangedResources();
201         for (int i = 0; i < infos.length; i++) {
202             SyncInfo info = infos[i];
203             IResource local = info.getLocal();
204             ISynchronizeModelElement diffNode = getModelObject(local);
205             if (diffNode != null) {
206                 handleChange(diffNode, info);
207             }
208         }
209     }
210
211     /**
212      * The sync state for the existing diff node has changed and the new state
213      * is provided by the given sync info.
214      * @param diffNode the changed diff node
215      * @param info the new sync state
216      */

217     protected void handleChange(ISynchronizeModelElement diffNode, SyncInfo info) {
218         IResource local = info.getLocal();
219
220         if(diffNode instanceof SyncInfoModelElement) {
221             ((SyncInfoModelElement)diffNode).update(info);
222             propogateConflictState(diffNode, false);
223             queueForLabelUpdate(diffNode);
224         } else {
225             removeFromViewer(local);
226             addResource(info);
227             ISynchronizeModelElement node = getModelObject(info.getLocal());
228             buildModelObjects(node);
229             
230         }
231     }
232
233     /**
234      * Add the give sync infos to the provider, creating
235      * any intermediate nodes a required.
236      * @param added the added infos
237      */

238     protected void addResources(SyncInfo[] added) {
239         for (int i = 0; i < added.length; i++) {
240             SyncInfo info = added[i];
241             addResource(info);
242         }
243     }
244
245     /**
246      * Add the give sync info to the provider, creating
247      * any intermediate nodes a required and adding any children as well
248      * @param info the added infos
249      */

250     protected abstract void addResource(SyncInfo info);
251
252     /**
253      * Create the model object for the given sync info as a child of the given parent node.
254      * @param parent the parent
255      * @param info the info to be used for the new node
256      * @return the created node
257      */

258     protected abstract ISynchronizeModelElement createModelObject(ISynchronizeModelElement parent, SyncInfo info);
259 }
260
Popular Tags