Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9772e4e3ffb2c1b33ec046ea2b4a0a3187fe3ac8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize.views;

import java.util.*;

import org.eclipse.core.resources.*;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.team.core.subscribers.SyncInfo;
import org.eclipse.team.internal.core.subscribers.SyncSetChangedEvent;
import org.eclipse.team.ui.synchronize.SyncInfoDiffNode;

/**
 * The contents provider compressed in-sync folder paths
 */
public class CompressedFolderContentProvider extends SyncSetTreeContentProvider {

	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.sync.views.SyncSetContentProvider#handleResourceAdditions(org.eclipse.team.internal.ui.sync.views.SyncSetChangedEvent)
	 */
	protected void handleResourceAdditions(SyncSetChangedEvent event) {
		AbstractTreeViewer tree = getTreeViewer();
		if (tree != null) {
			// TODO: For now, refresh any projects with additions
			IResource[] roots = event.getAddedRoots();
			refreshProjects(tree, roots);
		} else {
			super.handleResourceAdditions(event);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.sync.views.SyncSetContentProvider#handleResourceRemovals(org.eclipse.team.internal.ui.sync.views.SyncSetChangedEvent)
	 */
	protected void handleResourceRemovals(SyncSetChangedEvent event) {
		AbstractTreeViewer tree = getTreeViewer();
		if (tree != null) {
			// TODO: For now, refresh any projects with deletions
			IResource[] roots = event.getRemovedRoots();
			refreshProjects(tree, roots);
		} else {
			super.handleResourceRemovals(event);
		}
	}

	private void refreshProjects(AbstractTreeViewer tree, IResource[] roots) {
		if (roots.length == 0) return;
		Set projects = new HashSet();
		for (int i = 0; i < roots.length; i++) {
			if (roots[i].getType() == IResource.PROJECT) {
				// when a project is involved, refresh the whole tree
				tree.refresh();
				return;
			}
			projects.add(getModelObject(roots[i].getProject()));
		}
		for (Iterator iter = projects.iterator(); iter.hasNext();) {
			Object element = (Object) iter.next();
			tree.refresh(element);
		}
	}
	
	public Object getParent(Object element) {
		if (element instanceof CompressedFolder) {
			// The parent of a compressed folder is always the project
			return getModelObject(getResource(element).getProject());
		}
		Object parent = super.getParent(element);
		if (parent instanceof SyncInfoDiffNode) {
			SyncInfo info = ((SyncInfoDiffNode)parent).getSyncInfo();
			if (info == null) {
				// The resource is in-sync so return a compressed folder
				IResource resource = ((SyncInfoDiffNode)parent).getResource();
				if (resource.getType() == IResource.FOLDER) {					
					return new CompressedFolder(((SyncInfoDiffNode)parent).getSyncInfoSet(), resource);
					
				}
			}
		}
		return parent;
	}

	public Object[] getChildren(Object element) {
		IResource resource = getResource(element);
		if (resource != null) {
			if (resource.getType() == IResource.PROJECT) {
				return getProjectChildren((IProject)resource);
			} else if (resource.getType() == IResource.FOLDER) {
				return getFolderChildren(resource);
			}
		}
		return super.getChildren(element);
	}

	private Object[] getFolderChildren(IResource resource) {
		// Folders will only contain out-of-sync children
		IResource[] children = getSyncSet().members(resource);
		List result = new ArrayList();
		for (int i = 0; i < children.length; i++) {
			IResource child = children[i];
			SyncInfo info = getSyncSet().getSyncInfo(child);
			if (info != null) {
				result.add(getModelObject(info.getLocal()));
			}
		}
		return (Object[]) result.toArray(new Object[result.size()]);
	}

	private Object[] getProjectChildren(IProject project) {
		SyncInfo[] outOfSync = getSyncSet().getOutOfSyncDescendants(project);
		Set result = new HashSet();
		for (int i = 0; i < outOfSync.length; i++) {
			SyncInfo info = outOfSync[i];
			IResource local = info.getLocal();
			if (local.getProjectRelativePath().segmentCount() == 1) {
				// If the resource is a child of the project, include it uncompressed
				result.add(getModelObject(local));
			} else {
				IContainer container = getLowestInSyncParent(local);
				if (container.getType() == IResource.FOLDER) {
					result.add(getModelObject(container));
				}
			}
		}
		return (Object[]) result.toArray(new Object[result.size()]);
	}

	/**
	 * Return a compressed folder if the provided resource is an in-sync folder.
	 * Warning: This method will return a compressed folder for any in-sync
	 * folder, even those that do not contain out-of-sync resources (i.e. those that
	 * are not visible in the view).
	 */
	public Object getModelObject(IResource resource) {
		if (resource.getType() == IResource.FOLDER && getSyncSet().getSyncInfo(resource) == null) {
			return new CompressedFolder(getSyncSet(), resource);
		}
		return super.getModelObject(resource);
	}
	
	private IContainer getLowestInSyncParent(IResource resource) {
		if (resource.getType() == IResource.ROOT) return (IContainer)resource;
		IContainer parent = resource.getParent();
		if (getSyncSet().getSyncInfo(parent) == null) {
			return parent;
		}
		return getLowestInSyncParent(parent);
	}
	
}

Back to the top