Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a1589142382b74258f2841a8e3e54bbe90a6b0d1 (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
/*******************************************************************************
 * Copyright (c) 2003, 2004 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;

import java.util.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
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.*;

/**
 * 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 providerIdToPluginId;

    /**
     * Singleton instance.
     */
    private static TeamCapabilityHelper singleton;
    
    /*
     * This is copied from RepositoryProviderType to provide a quick way to query if
     * a project is mapped to a provider id.
     */
    private final static QualifiedName PROVIDER_PROP_KEY = 
		new QualifiedName("org.eclipse.team.core", "repository");  //$NON-NLS-1$  //$NON-NLS-2$
    
    /**
     * 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 (int i = 0; i < projects.length; i++) {
            try {
                processProject(projects[i], 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 (int i = 0; i < extensions.length; i++) {
				IExtension extension = extensions[i];
				IConfigurationElement[] elements = extension.getConfigurationElements();
				for (int j = 0; j < elements.length; j++) {
					IConfigurationElement element = elements[j];
					final String pluginId = extension.getDeclaringPluginDescriptor().getUniqueIdentifier();
					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() {
							public String getLocalId() {
								return id;
							}
							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;
		IActivityManager activityManager = workbenchActivitySupport
				.getActivityManager();
		String id = getProviderIdFor(project);
		if (id == null)
			return;
		Set activities = new HashSet(activityManager.getEnabledActivityIds());
		boolean changed = false;

		IPluginContribution contribution = (IPluginContribution) 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 {
    	String id = null;
    	if(project.isAccessible()) {	
			//First, look for the session property
			RepositoryProvider provider = (RepositoryProvider)project.getSessionProperty(PROVIDER_PROP_KEY);
			if(provider != null)
				id = provider.getID();
			//Next, check if it has the ID as a persistent property
			id = project.getPersistentProperty(PROVIDER_PROP_KEY);
    	}
    	return id;
    }
}

Back to the top