KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.compare.structuremergeviewer.IDiffContainer;
21 import org.eclipse.compare.structuremergeviewer.IDiffElement;
22 import org.eclipse.core.resources.*;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.viewers.ViewerSorter;
25 import org.eclipse.team.core.synchronize.*;
26 import org.eclipse.team.internal.ui.*;
27 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
28 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
29
30 public class CompressedFoldersModelProvider extends HierarchicalModelProvider {
31
32     protected class UnchangedCompressedDiffNode extends UnchangedResourceModelElement {
33         public UnchangedCompressedDiffNode(IDiffContainer parent, IResource resource) {
34             super(parent, resource);
35         }
36         
37         /* (non-Javadoc)
38          * @see org.eclipse.compare.structuremergeviewer.DiffNode#getName()
39          */

40         public String JavaDoc getName() {
41             IResource resource = getResource();
42             return resource.getProjectRelativePath().toString();
43         }
44         
45         /* (non-Javadoc)
46          * @see org.eclipse.team.ui.synchronize.SyncInfoModelElement#getImageDescriptor(java.lang.Object)
47          */

48         public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
49             return TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_COMPRESSED_FOLDER);
50         }
51     }
52     
53     /**
54      * A compressed folder appears under a project and contains out-of-sync resources
55      */

56     public class CompressedFolderDiffNode extends SyncInfoModelElement {
57
58         public CompressedFolderDiffNode(IDiffContainer parent, SyncInfo info) {
59             super(parent, info);
60         }
61
62         /* (non-Javadoc)
63          * @see org.eclipse.compare.structuremergeviewer.DiffNode#getName()
64          */

65         public String JavaDoc getName() {
66             IResource resource = getResource();
67             return resource.getProjectRelativePath().toString();
68         }
69         
70         /* (non-Javadoc)
71          * @see org.eclipse.team.ui.synchronize.SyncInfoModelElement#getImageDescriptor(java.lang.Object)
72          */

73         public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
74             return TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_COMPRESSED_FOLDER);
75         }
76     }
77     
78     public static class CompressedFolderModelProviderDescriptor implements ISynchronizeModelProviderDescriptor {
79         public static final String JavaDoc ID = TeamUIPlugin.ID + ".modelprovider_compressedfolders"; //$NON-NLS-1$
80
public String JavaDoc getId() {
81             return ID;
82         }
83         public String JavaDoc getName() {
84             return TeamUIMessages.CompressedFoldersModelProvider_0;
85         }
86         public ImageDescriptor getImageDescriptor() {
87             return TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_COMPRESSED_FOLDER);
88         }
89     }
90     private static final CompressedFolderModelProviderDescriptor compressedDescriptor = new CompressedFolderModelProviderDescriptor();
91     
92     public CompressedFoldersModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) {
93         super(configuration, set);
94     }
95     
96     public CompressedFoldersModelProvider(
97             AbstractSynchronizeModelProvider parentProvider,
98             ISynchronizeModelElement modelRoot,
99             ISynchronizePageConfiguration configuration, SyncInfoSet set) {
100         super(parentProvider, modelRoot, configuration, set);
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.team.internal.ui.synchronize.HierarchicalModelProvider#getDescriptor()
105      */

106     public ISynchronizeModelProviderDescriptor getDescriptor() {
107         return compressedDescriptor;
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.team.ui.synchronize.SyncInfoDiffNodeRoot#getSorter()
112      */

113     public ViewerSorter getViewerSorter() {
114         return new SynchronizeModelElementSorter() {
115             protected int compareNames(IResource resource1, IResource resource2) {
116                 if (resource1.getType() == IResource.FOLDER && resource2.getType() == IResource.FOLDER) {
117                     return collator.compare(resource1.getProjectRelativePath().toString(), resource2.getProjectRelativePath().toString());
118                 }
119                 return super.compareNames(resource1, resource2);
120             }
121         };
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.team.ui.synchronize.viewers.HierarchicalModelProvider#createModelObjects(org.eclipse.compare.structuremergeviewer.DiffNode)
126      */

127     protected IDiffElement[] createModelObjects(ISynchronizeModelElement container) {
128         IResource resource = null;
129         if (container == getModelRoot()) {
130             resource = ResourcesPlugin.getWorkspace().getRoot();
131         } else {
132             resource = container.getResource();
133         }
134         if(resource != null) {
135             if (resource.getType() == IResource.PROJECT) {
136                 return getProjectChildren(container, (IProject)resource);
137             }
138             if (resource.getType() == IResource.FOLDER) {
139                 return getFolderChildren(container, resource);
140             }
141         }
142         return super.createModelObjects(container);
143     }
144     
145     private IDiffElement[] getFolderChildren(ISynchronizeModelElement parent, IResource resource) {
146         // Folders will only contain out-of-sync children
147
IResource[] children = getSyncInfoTree().members(resource);
148         List JavaDoc result = new ArrayList JavaDoc();
149         for (int i = 0; i < children.length; i++) {
150             IResource child = children[i];
151             if (child.getType() == IResource.FILE) {
152                 result.add(createModelObject(parent, child));
153             }
154         }
155         return (IDiffElement[])result.toArray(new IDiffElement[result.size()]);
156     }
157
158     private IDiffElement[] getProjectChildren(ISynchronizeModelElement parent, IProject project) {
159         // The out-of-sync elements could possibly include the project so the code
160
// below is written to ignore the project
161
SyncInfo[] outOfSync = getSyncInfoTree().getSyncInfos(project, IResource.DEPTH_INFINITE);
162         Set JavaDoc result = new HashSet JavaDoc();
163         Set JavaDoc resourcesToShow = new HashSet JavaDoc();
164         for (int i = 0; i < outOfSync.length; i++) {
165             SyncInfo info = outOfSync[i];
166             IResource local = info.getLocal();
167             if (local.getProjectRelativePath().segmentCount() == 1 && local.getType() == IResource.FILE) {
168                 resourcesToShow.add(local);
169             } else {
170                 if (local.getType() == IResource.FILE) {
171                     resourcesToShow.add(local.getParent());
172                 } else if (local.getType() == IResource.FOLDER){
173                     resourcesToShow.add(local);
174                 }
175             }
176         }
177         for (Iterator JavaDoc iter = resourcesToShow.iterator(); iter.hasNext();) {
178             IResource resource = (IResource) iter.next();
179             result.add(createModelObject(parent, resource));
180         }
181         
182         return (IDiffElement[])result.toArray(new IDiffElement[result.size()]);
183     }
184     
185     /* (non-Javadoc)
186      * @see org.eclipse.team.ui.synchronize.views.HierarchicalModelProvider#createChildNode(org.eclipse.compare.structuremergeviewer.DiffNode, org.eclipse.core.resources.IResource)
187      */

188     
189     /* (non-Javadoc)
190      * @see org.eclipse.team.ui.synchronize.viewers.HierarchicalModelProvider#createModelObject(org.eclipse.compare.structuremergeviewer.DiffNode, org.eclipse.core.resources.IResource)
191      */

192     protected ISynchronizeModelElement createModelObject(ISynchronizeModelElement parent, IResource resource) {
193         if (resource.getType() == IResource.FOLDER) {
194             SyncInfo info = getSyncInfoTree().getSyncInfo(resource);
195             ISynchronizeModelElement newNode;
196             if(info != null) {
197                 newNode = new CompressedFolderDiffNode(parent, info);
198             } else {
199                 newNode = new UnchangedCompressedDiffNode(parent, resource);
200             }
201             addToViewer(newNode);
202             return newNode;
203         }
204         return super.createModelObject(parent, resource);
205     }
206     
207     /**
208      * Update the viewer for the sync set additions in the provided event.
209      * This method is invoked by <code>handleChanges(ISyncInfoSetChangeEvent)</code>.
210      * Subclasses may override.
211      * @param event
212      */

213     protected void handleResourceAdditions(ISyncInfoTreeChangeEvent event) {
214         SyncInfo[] infos = event.getAddedResources();
215         for (int i = 0; i < infos.length; i++) {
216             SyncInfo info = infos[i];
217             addResource(info);
218         }
219     }
220     
221     protected void addResource(SyncInfo info) {
222         IResource local = info.getLocal();
223         ISynchronizeModelElement existingNode = getModelObject(local);
224         if (existingNode == null) {
225             if (local.getType() == IResource.FILE) {
226                 ISynchronizeModelElement parentNode = getModelObject(local.getParent());
227                 if (parentNode == null) {
228                     ISynchronizeModelElement projectNode = getModelObject(local.getProject());
229                     if (projectNode == null) {
230                         projectNode = createModelObject(getModelRoot(), local.getProject());
231                     }
232                     if (local.getParent().getType() == IResource.PROJECT) {
233                         parentNode = projectNode;
234                     } else {
235                         parentNode = createModelObject(projectNode, local.getParent());
236                     }
237                 }
238                 createModelObject(parentNode, local);
239             } else {
240                 ISynchronizeModelElement projectNode = getModelObject(local.getProject());
241                 if (projectNode == null) {
242                     projectNode = createModelObject(getModelRoot(), local.getProject());
243                 }
244                 if (local.getProject() != local) {
245                     createModelObject(projectNode, local);
246                 }
247             }
248         } else {
249             // Either The folder node was added as the parent of a newly added out-of-sync file
250
// or the file was somehow already there so just refresh
251
handleChange(existingNode, info);
252         }
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.team.internal.ui.sync.views.SyncSetContentProvider#handleResourceRemovals(org.eclipse.team.internal.ui.sync.views.SyncSetChangedEvent)
257      */

258     protected void handleResourceRemovals(ISyncInfoTreeChangeEvent event) {
259         IResource[] roots = event.getRemovedSubtreeRoots();
260         
261         // First, deal with any projects that have been removed
262
List JavaDoc removedProjects = new ArrayList JavaDoc();
263         for (int i = 0; i < roots.length; i++) {
264             IResource resource = roots[i];
265             if (resource.getType() == IResource.PROJECT) {
266                 removeFromViewer(resource);
267                 removedProjects.add(resource);
268             }
269         }
270
271         IResource[] resources = event.getRemovedResources();
272         List JavaDoc resourcesToRemove = new ArrayList JavaDoc();
273         List JavaDoc resourcesToAdd = new ArrayList JavaDoc();
274         for (int i = 0; i < resources.length; i++) {
275             IResource resource = resources[i];
276             if (!removedProjects.contains(resource.getProject())) {
277                 if (resource.getType() == IResource.FILE) {
278                     if (isCompressedParentEmpty(resource) && !isOutOfSync(resource.getParent())) {
279                         // The parent compressed folder is also empty so remove it
280
resourcesToRemove.add(resource.getParent());
281                     } else {
282                         resourcesToRemove.add(resource);
283                     }
284                 } else {
285                     // A folder has been removed (i.e. is in-sync)
286
// but may still contain children
287
resourcesToRemove.add(resource);
288                     if (hasFileMembers((IContainer)resource)) {
289                         resourcesToAdd.addAll(Arrays.asList(getSyncInfosForFileMembers((IContainer)resource)));
290                     }
291                 }
292             }
293         }
294         if (!resourcesToRemove.isEmpty()) {
295             removeFromViewer((IResource[]) resourcesToRemove.toArray(new IResource[resourcesToRemove.size()]));
296         }
297         if (!resourcesToAdd.isEmpty()) {
298             addResources((SyncInfo[]) resourcesToAdd.toArray(new SyncInfo[resourcesToAdd.size()]));
299         }
300     }
301
302     protected int getLogicalModelDepth(IResource resource) {
303         if(resource.getType() == IResource.PROJECT) {
304             return IResource.DEPTH_INFINITE;
305         } else {
306             return IResource.DEPTH_ONE;
307         }
308     }
309     
310     private boolean isCompressedParentEmpty(IResource resource) {
311         IContainer parent = resource.getParent();
312         if (parent == null
313                 || parent.getType() == IResource.ROOT
314                 || parent.getType() == IResource.PROJECT) {
315             return false;
316         }
317         return !hasFileMembers(parent);
318     }
319
320     private boolean hasFileMembers(IContainer parent) {
321         // Check if the sync set has any file children of the parent
322
IResource[] members = getSyncInfoTree().members(parent);
323         for (int i = 0; i < members.length; i++) {
324             IResource member = members[i];
325             if (member.getType() == IResource.FILE) {
326                 return true;
327             }
328         }
329         // The parent does not contain any files
330
return false;
331     }
332     
333     private SyncInfo[] getSyncInfosForFileMembers(IContainer parent) {
334         // Check if the sync set has any file children of the parent
335
List JavaDoc result = new ArrayList JavaDoc();
336         IResource[] members = getSyncInfoTree().members(parent);
337         for (int i = 0; i < members.length; i++) {
338             IResource member = members[i];
339             if (member.getType() == IResource.FILE) {
340                 result.add(getSyncInfoTree().getSyncInfo(member));
341             }
342         }
343         return (SyncInfo[]) result.toArray(new SyncInfo[result.size()]);
344     }
345 }
346
Popular Tags