Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd19f883ec08da14a61612275bdf08ea8cc1a908 (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 QnX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Qnx Software Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.core.model;

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IContainerEntry;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.cdt.core.model.IPathEntryContainer;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class SetPathEntryContainerOperation extends CModelOperation {

	IPathEntryContainer newContainer;
	ICProject[] affectedProjects;
	PathEntryManager fPathEntryManager;
	IPath containerPath;

	public SetPathEntryContainerOperation(ICProject[] affectedProjects, IPathEntryContainer newContainer) {
		super(affectedProjects);
		this.affectedProjects = affectedProjects;
		this.newContainer = newContainer;
		this.containerPath = (newContainer == null) ? new Path("") : newContainer.getPath(); //$NON-NLS-1$
		fPathEntryManager = PathEntryManager.getDefault();
	}

	public SetPathEntryContainerOperation(ICProject[] affectedProjects, IPath containerPath) {
		super(affectedProjects);
		this.affectedProjects = affectedProjects;
		this.containerPath = containerPath;
		fPathEntryManager = PathEntryManager.getDefault();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.internal.core.model.CModelOperation#isReadOnly()
	 */
	@Override
	public boolean isReadOnly() {
		return true;
	}

	@Override
	protected void executeOperation() throws CModelException {
		if (isCanceled()) {
			return;
		}
		
//		IPath containerPath = (newContainer == null) ? new Path("") : newContainer.getPath(); //$NON-NLS-1$
		final int projectLength = affectedProjects.length;
		final ICProject[] modifiedProjects = new ICProject[projectLength];
		System.arraycopy(affectedProjects, 0, modifiedProjects, 0, projectLength);
		final IPathEntry[][] oldResolvedEntries = new IPathEntry[projectLength][];
		// filter out unmodified project containers
		int remaining = 0;
		for (int i = 0; i < projectLength; i++) {
			if (isCanceled()) {
				return;
			}
			ICProject affectedProject = affectedProjects[i];
			boolean found = false;
			IPathEntry[] rawPath = fPathEntryManager.getRawPathEntries(affectedProject);
			for (int j = 0, cpLength = rawPath.length; j < cpLength; j++) {
				IPathEntry entry = rawPath[j];
				if (entry.getEntryKind() == IPathEntry.CDT_CONTAINER) {
					IContainerEntry cont = (IContainerEntry)entry;
					if (cont.getPath().equals(containerPath)) {
						found = true;
						break;
					}
				}
			}
			if (!found || newContainer == null) {
				// filter out this project - does not reference the container
				// path
				modifiedProjects[i] = null;
				// Still add it to the cache
				fPathEntryManager.containerPut(affectedProject, containerPath, newContainer);
				continue;
			}
			
			IPathEntryContainer oldContainer = fPathEntryManager.containerGet(affectedProject, containerPath, true);
			if (oldContainer != null && newContainer != null && oldContainer.equals(newContainer)) {
				modifiedProjects[i] = null; // filter out this project -
				// container did not change
				continue;
			}
			remaining++;
			oldResolvedEntries[i] = fPathEntryManager.removeCachedResolvedPathEntries(affectedProject);
			fPathEntryManager.containerPut(affectedProject, containerPath, newContainer);
		}

		// Nothing change.
		if (remaining == 0) {
			return;
		}

		// trigger model refresh
		for (int i = 0; i < projectLength; i++) {
			if (isCanceled()) {
				return;
			}
			ICProject affectedProject = modifiedProjects[i];
			if (affectedProject == null) {
				continue; // was filtered out
			}
			// Only fire deltas if we had previous cache
			if (oldResolvedEntries[i] != null) {
				IPathEntry[] newEntries = fPathEntryManager.getResolvedPathEntries(affectedProject);
				ICElementDelta[] deltas = fPathEntryManager.generatePathEntryDeltas(affectedProject, oldResolvedEntries[i], newEntries);
				if (deltas.length > 0) {
					affectedProject.close();
					//shouldFire = true;
					for (int j = 0; j < deltas.length; j++) {
						addDelta(deltas[j]);
					}
				}
			}
		}

	}

}

Back to the top