1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 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 40 public String getName() { 41 IResource resource = getResource(); 42 return resource.getProjectRelativePath().toString(); 43 } 44 45 48 public ImageDescriptor getImageDescriptor(Object object) { 49 return TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_COMPRESSED_FOLDER); 50 } 51 } 52 53 56 public class CompressedFolderDiffNode extends SyncInfoModelElement { 57 58 public CompressedFolderDiffNode(IDiffContainer parent, SyncInfo info) { 59 super(parent, info); 60 } 61 62 65 public String getName() { 66 IResource resource = getResource(); 67 return resource.getProjectRelativePath().toString(); 68 } 69 70 73 public ImageDescriptor getImageDescriptor(Object object) { 74 return TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_COMPRESSED_FOLDER); 75 } 76 } 77 78 public static class CompressedFolderModelProviderDescriptor implements ISynchronizeModelProviderDescriptor { 79 public static final String ID = TeamUIPlugin.ID + ".modelprovider_compressedfolders"; public String getId() { 81 return ID; 82 } 83 public String 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 106 public ISynchronizeModelProviderDescriptor getDescriptor() { 107 return compressedDescriptor; 108 } 109 110 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 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 IResource[] children = getSyncInfoTree().members(resource); 148 List result = new ArrayList (); 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 SyncInfo[] outOfSync = getSyncInfoTree().getSyncInfos(project, IResource.DEPTH_INFINITE); 162 Set result = new HashSet (); 163 Set resourcesToShow = new HashSet (); 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 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 188 189 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 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 handleChange(existingNode, info); 252 } 253 } 254 255 258 protected void handleResourceRemovals(ISyncInfoTreeChangeEvent event) { 259 IResource[] roots = event.getRemovedSubtreeRoots(); 260 261 List removedProjects = new ArrayList (); 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 resourcesToRemove = new ArrayList (); 273 List resourcesToAdd = new ArrayList (); 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 resourcesToRemove.add(resource.getParent()); 281 } else { 282 resourcesToRemove.add(resource); 283 } 284 } else { 285 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 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 return false; 331 } 332 333 private SyncInfo[] getSyncInfosForFileMembers(IContainer parent) { 334 List result = new ArrayList (); 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 |