Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 430738ee6f4ff71c626bfeb80ee7e054c2e42e41 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.actions;

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.*;
import org.eclipse.core.runtime.*;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager;
import org.eclipse.team.core.subscribers.Subscriber;
import org.eclipse.team.core.subscribers.SubscriberResourceMappingContext;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.mapping.BuildScopeOperation;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

/**
 * A specialized workspace actions that operates on resource traversals
 * instead of resources/
 */
public abstract class WorkspaceTraversalAction extends WorkspaceAction {

	/**
	 * Return the selected mappings that contain resources 
	 * within a CVS managed project.
	 * @return the selected mappings that contain resources 
	 * within a CVS managed project
	 */
	protected ResourceMapping[] getCVSResourceMappings() {
		return getSelectedResourceMappings(CVSProviderPlugin.getTypeId());
	}

	private static ResourceTraversal[] getTraversals(IWorkbenchPart part, ISynchronizationScopeManager manager, IProgressMonitor monitor) throws CoreException {
		try {
			BuildScopeOperation op = new BuildScopeOperation(part, manager);
			op.run(monitor);
			return manager.getScope().getTraversals();
		} catch (InvocationTargetException e) {
			throw TeamException.asTeamException(e);
		} catch (InterruptedException e) {
			throw new OperationCanceledException();
		}
	}
	
	private static IResource[] getRootTraversalResources(ISynchronizationScopeManager manager, IProgressMonitor monitor) throws CoreException {
		Set<IResource> result = new HashSet<>();
		ResourceTraversal[] traversals = getTraversals(null, manager, monitor);
		for (ResourceTraversal traversal : traversals) {
			IResource[] resources = traversal.getResources();
			for (IResource resource : resources) {
				if (RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()) != null) {
					result.add(resource);
				}
			}
		}
		return result.toArray(new IResource[result.size()]);
	}

	protected Subscriber getWorkspaceSubscriber() {
		return CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber();
	}
	
	public static IResource[] getResourcesToCompare(ResourceMapping[] mappings, Subscriber subscriber) throws InvocationTargetException {
		ISynchronizationScopeManager manager = new SynchronizationScopeManager("",  //$NON-NLS-1$
				mappings, SubscriberResourceMappingContext.createContext(subscriber), true);
		try {
			return getResourcesToCompare(manager);
		} finally {
			manager.dispose();
		}
	}
	
	protected IResource[] getResourcesToCompare(final Subscriber subscriber) throws InvocationTargetException {
		return getResourcesToCompare(getCVSResourceMappings(), subscriber);
	}
	
	protected ResourceMappingContext getResourceMappingContext() {
		return SubscriberResourceMappingContext.createContext(CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber());
	}

	public static IResource[] getResourcesToCompare(final ISynchronizationScopeManager manager) throws InvocationTargetException {
		// Determine what resources need to be synchronized.
		// Use a resource mapping context to include any relevant remote resources
		final IResource[][] resources = new IResource[][] { null };
		try {
			PlatformUI.getWorkbench().getProgressService().busyCursorWhile(monitor -> {
				try {
					resources[0] = getRootTraversalResources(manager, monitor);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				}
			});
		} catch (InterruptedException e) {
			// Canceled
			return null;
		}
		return resources[0];
	}
	
	public static IResource[] getProjects(IResource[] resources) {
		Set<IProject> projects = new HashSet<>();
		for (IResource resource : resources) {
			projects.add(resource.getProject());
		}
		return projects.toArray(new IResource[projects.size()]);
	}
	
	/**
	 * 
	 * @param mappings
	 * @return
	 * 
	 * @deprecated need to find a better way to do this
	 */
	@Deprecated
	public static boolean isLogicalModel(ResourceMapping[] mappings) {
		for (ResourceMapping mapping : mappings) {
			if (! (mapping.getModelObject() instanceof IResource) ) {
				return true;
			}
		}
		return false;
	}
	
	protected IFile getSelectedFile() {
		ResourceMapping[] mappings = getCVSResourceMappings();
		if (mappings.length == 1) {
			IResource resource = Utils.getResource(mappings[0].getModelObject());
			if (resource != null && resource.getType() == IResource.FILE)
				return (IFile)resource;
		}
		return null;
	}
	
	protected boolean hasOutgoingChanges(final RepositoryProviderOperation operation) throws InvocationTargetException, InterruptedException {
		final boolean[] hasChange = new boolean[] { false };
		PlatformUI.getWorkbench().getProgressService().run(true, true, monitor -> {
			try {
				monitor.beginTask(CVSUIMessages.WorkspaceTraversalAction_0, 100);
				operation.buildScope(Policy.subMonitorFor(monitor, 50));
				hasChange[0] = CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber()
						.hasLocalChanges(operation.getScope().getTraversals(), Policy.subMonitorFor(monitor, 50));
			} catch (CoreException e) {
				throw new InvocationTargetException(e);
			} finally {
				monitor.done();
			}
		});
		return hasChange[0];
	}
	
	/**
	 * Return the complete set of traversals to be targeted by the action
	 * including those that are included by consulting the models.
	 * 
	 * @param monitor
	 *            a progress monitor
	 * @return the complete set of traversals to be targeted by the action
	 * @throws CoreException
	 */
	protected ResourceTraversal[] getTraversals(IProgressMonitor monitor) throws CoreException {
		SynchronizationScopeManager scopeManager = getScopeManager();
		try {
			return getTraversals(getTargetPart(), scopeManager, monitor);
		} finally {
			scopeManager.dispose();
		}
	}
	
	/**
	 * Return a scope manager that provides the scope for the action.
	 * @return a scope manager that provides the scope for the action
	 */
	protected SynchronizationScopeManager getScopeManager() {
		return new SynchronizationScopeManager(
				"",  //$NON-NLS-1$
				getCVSResourceMappings(), 
				getResourceMappingContext(), true);
	}
}

Back to the top