KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > TeamCapabilityHelper


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.team.core.RepositoryProvider;
21 import org.eclipse.team.internal.core.TeamPlugin;
22 import org.eclipse.ui.IPluginContribution;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.activities.*;
25
26 /**
27  * Utility class that manages promotion of team capabilities in response to workspace changes
28  * and existing repository providers.
29  *
30  * @since 3.0
31  */

32 public class TeamCapabilityHelper {
33     
34     /**
35      * Mapping from repository provider id to IPluginContribution. Used for proper
36      * activity mapping of natures.
37      */

38     private Map JavaDoc providerIdToPluginId;
39
40     /**
41      * Singleton instance.
42      */

43     private static TeamCapabilityHelper singleton;
44     
45     /**
46      * Get the singleton instance of this class.
47      * @return the singleton instance of this class.
48      * @since 3.0
49      */

50     public static TeamCapabilityHelper getInstance() {
51         if (singleton == null) {
52             singleton = new TeamCapabilityHelper();
53         }
54         return singleton;
55     }
56     
57     /**
58      * Create a new <code>IDEWorkbenchActivityHelper</code> which will listen
59      * for workspace changes and promote activities accordingly.
60      */

61     private TeamCapabilityHelper() {
62         providerIdToPluginId = new HashMap JavaDoc();
63         loadRepositoryProviderIds();
64  
65         // crawl the initial projects
66
IProject [] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
67         IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI.getWorkbench().getActivitySupport();
68         for (int i = 0; i < projects.length; i++) {
69             try {
70                 processProject(projects[i], workbenchActivitySupport);
71             } catch (CoreException e) {
72                 // do nothing
73
}
74         }
75    }
76     
77    /**
78     * Loads the list of registered provider types
79     */

80    public void loadRepositoryProviderIds() {
81         providerIdToPluginId.clear();
82         IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint("org.eclipse.team.core.repository"); //$NON-NLS-1$
83
if (point != null) {
84             IExtension[] extensions = point.getExtensions();
85             for (int i = 0; i < extensions.length; i++) {
86                 IExtension extension = extensions[i];
87                 IConfigurationElement[] elements = extension.getConfigurationElements();
88                 for (int j = 0; j < elements.length; j++) {
89                     IConfigurationElement element = elements[j];
90                     final String JavaDoc pluginId = extension.getNamespace();
91                     if (element.getName().equals(TeamPlugin.REPOSITORY_EXTENSION)) {
92                         final String JavaDoc id = element.getAttribute("id"); //$NON-NLS-1$
93
if (id == null) {
94                             // bad extension point
95
continue;
96                         }
97                         providerIdToPluginId.put(id, new IPluginContribution() {
98                             public String JavaDoc getLocalId() {
99                                 return id;
100                             }
101                             public String JavaDoc getPluginId() {
102                                 return pluginId;
103                             }
104                         });
105                     }
106                 }
107             }
108         }
109     }
110
111     /**
112      * Handle natures for the given project.
113      *
114      * @param project the project
115      * @param workbenchActivitySupport the activity support
116      */

117     protected void processProject(IProject project, IWorkbenchActivitySupport workbenchActivitySupport) throws CoreException {
118         if (!project.isOpen())
119             return;
120         String JavaDoc id = getProviderIdFor(project);
121         processRepositoryId(id, workbenchActivitySupport);
122     }
123
124     /**
125      * Helper method that enables the activities for the given repository provider.
126      *
127      * @param id the repository provider id
128      * @param workbenchActivitySupport the activity support
129      */

130     public void processRepositoryId(String JavaDoc id, IWorkbenchActivitySupport workbenchActivitySupport) {
131         if (id == null)
132             return;
133         IActivityManager activityManager = workbenchActivitySupport
134         .getActivityManager();
135         Set JavaDoc activities = new HashSet JavaDoc(activityManager.getEnabledActivityIds());
136         boolean changed = false;
137
138         IPluginContribution contribution = (IPluginContribution) providerIdToPluginId.get(id);
139         if (contribution == null)
140             return; //bad provider ID.
141
IIdentifier identifier = activityManager.getIdentifier(WorkbenchActivityHelper.createUnifiedId(contribution));
142         if (activities.addAll(identifier.getActivityIds())) {
143             changed = true;
144         }
145
146         if (changed)
147             workbenchActivitySupport.setEnabledActivityIds(activities);
148     }
149
150     /**
151      * Returns the provider id for this project or <code>null</code> if no providers are mapped
152      * to this project. Note that this won't instantiate the provider, but instead will simply query
153      * the persistent property
154      *
155      * @param project the project to query.
156      * @return the provider id for this project or <code>null</code> if no providers are mapped
157      * to this project
158      * @throws CoreException
159      */

160     public String JavaDoc getProviderIdFor(IProject project) throws CoreException {
161         if(project.isAccessible()) {
162             //First, look for the session property
163
Object JavaDoc prop = project.getSessionProperty(TeamPlugin.PROVIDER_PROP_KEY);
164             if(prop != null && prop instanceof RepositoryProvider) {
165                 RepositoryProvider provider = (RepositoryProvider) prop;
166                 return provider.getID();
167             }
168             //Next, check if it has the ID as a persistent property
169
return project.getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY);
170         }
171         return null;
172     }
173 }
174
Popular Tags