Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3be899dfe6cf49502867424be13b0714dedee13c (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
/*******************************************************************************
 * Copyright (c) 2003, 2006 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.ui;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.internal.core.TeamPlugin;
import org.eclipse.ui.IPluginContribution;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.activities.IActivityManager;
import org.eclipse.ui.activities.IIdentifier;
import org.eclipse.ui.activities.IWorkbenchActivitySupport;
import org.eclipse.ui.activities.WorkbenchActivityHelper;

/**
 * Utility class that manages promotion of team capabilities in response to workspace changes
 * and existing repository providers.
 *
 * @since 3.0
 */
public class TeamCapabilityHelper {
	/**
	 * Mapping from repository provider id to IPluginContribution.  Used for proper
	 * activity mapping of natures.
	 */
	private Map<String, IPluginContribution> providerIdToPluginId;

	/**
	 * Singleton instance.
	 */
	private static TeamCapabilityHelper singleton;

	/**
	 * Get the singleton instance of this class.
	 * @return the singleton instance of this class.
	 * @since 3.0
	 */
	public static TeamCapabilityHelper getInstance() {
		if (singleton == null) {
			singleton = new TeamCapabilityHelper();
		}
		return singleton;
	}

	/**
	 * Create a new <code>IDEWorkbenchActivityHelper</code> which will listen
	 * for workspace changes and promote activities accordingly.
	 */
	private TeamCapabilityHelper() {
		providerIdToPluginId = new HashMap<>();
		loadRepositoryProviderIds();

		// crawl the initial projects
		IProject [] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI.getWorkbench().getActivitySupport();
		for (IProject project : projects) {
			try {
				processProject(project, workbenchActivitySupport);
			} catch (CoreException e) {
				// do nothing
			}
		}
	}

	/**
	 * Loads the list of registered provider types
	 */
	public void loadRepositoryProviderIds() {
		providerIdToPluginId.clear();
		IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint("org.eclipse.team.core.repository"); //$NON-NLS-1$
		if (point != null) {
			IExtension[] extensions = point.getExtensions();
			for (IExtension extension : extensions) {
				IConfigurationElement[] elements = extension.getConfigurationElements();
				for (IConfigurationElement element : elements) {
					final String pluginId = extension.getNamespace();
					if (element.getName().equals(TeamPlugin.REPOSITORY_EXTENSION)) {
						final String id = element.getAttribute("id"); //$NON-NLS-1$
						if (id == null) {
							// bad extension point
							continue;
						}
						providerIdToPluginId.put(id, new IPluginContribution() {
							@Override
							public String getLocalId() {
								return id;
							}
							@Override
							public String getPluginId() {
								return pluginId;
							}
						});
					}
				}
			}
		}
	}

	/**
	 * Handle natures for the given project.
	 *
	 * @param project the project
	 * @param workbenchActivitySupport the activity support
	 */
	protected void processProject(IProject project, IWorkbenchActivitySupport workbenchActivitySupport) throws CoreException {
		if (!project.isOpen())
			return;
		String id = getProviderIdFor(project);
		processRepositoryId(id, workbenchActivitySupport);
	}

	/**
	 * Helper method that enables the activities for the given repository provider.
	 *
	 * @param id the repository provider id
	 * @param workbenchActivitySupport the activity support
	 */
	public void processRepositoryId(String id, IWorkbenchActivitySupport workbenchActivitySupport) {
		if (id == null)
			return;
		IActivityManager activityManager = workbenchActivitySupport
		.getActivityManager();
		Set<String> activities = new HashSet<>(activityManager.getEnabledActivityIds());
		boolean changed = false;

		IPluginContribution contribution = providerIdToPluginId.get(id);
		if (contribution == null)
			return; //bad provider ID.
		IIdentifier identifier = activityManager.getIdentifier(WorkbenchActivityHelper.createUnifiedId(contribution));
		if (activities.addAll(identifier.getActivityIds())) {
			changed = true;
		}

		if (changed)
			workbenchActivitySupport.setEnabledActivityIds(activities);
	}

	/**
	 * Returns the provider id for this project or <code>null</code> if no providers are mapped
	 * to this project. Note that this won't instantiate the provider, but instead will simply query
	 * the persistent property
	 *
	 * @param project the project to query.
	 * @return the provider id for this project or <code>null</code> if no providers are mapped
	 * to this project
	 * @throws CoreException
	 */
	public String getProviderIdFor(IProject project) throws CoreException {
		if(project.isAccessible()) {
			//First, look for the session property
			Object prop = project.getSessionProperty(TeamPlugin.PROVIDER_PROP_KEY);
			if(prop != null && prop instanceof RepositoryProvider) {
				RepositoryProvider provider = (RepositoryProvider) prop;
				return provider.getID();
			}
			//Next, check if it has the ID as a persistent property
			return project.getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY);
		}
		return null;
	}
}

Back to the top