KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > WorkspaceTeamStateProvider


1 /*******************************************************************************
2  * Copyright (c) 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.mapping;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.resources.mapping.ResourceMapping;
18 import org.eclipse.core.resources.mapping.ResourceMappingContext;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.team.core.RepositoryProvider;
22 import org.eclipse.team.core.RepositoryProviderType;
23 import org.eclipse.team.internal.core.*;
24 import org.eclipse.team.internal.ui.Utils;
25 import org.eclipse.team.ui.mapping.*;
26 import org.eclipse.team.ui.synchronize.TeamStateProvider;
27
28 /**
29  * A decorated state provider that delegates to the provider for the repository
30  * provider type that is associated with the projects that an element maps to
31  * using the ResourceMapping API.
32  *
33  */

34 public class WorkspaceTeamStateProvider extends TeamStateProvider
35         implements ITeamStateChangeListener, IRepositoryProviderListener,
36         IResourceChangeListener {
37
38     private Map JavaDoc providers = new HashMap JavaDoc();
39
40     public WorkspaceTeamStateProvider() {
41         RepositoryProviderManager.getInstance().addListener(this);
42         ResourcesPlugin.getWorkspace().addResourceChangeListener(this,
43                 IResourceChangeEvent.POST_CHANGE);
44         IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot()
45                 .getProjects();
46         for (int i = 0; i < allProjects.length; i++) {
47             IProject project = allProjects[i];
48             handleProject(project);
49         }
50     }
51     
52     public void dispose() {
53         RepositoryProviderManager.getInstance().removeListener(this);
54         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.team.ui.mapping.DecoratedStateProvider#isDecorationEnabled(java.lang.Object)
59      */

60     public final boolean isDecorationEnabled(Object JavaDoc element) {
61         ITeamStateProvider provider = getDecoratedStateProvider(element);
62         if (provider != null)
63             return provider.isDecorationEnabled(element);
64         return false;
65     }
66
67     /* (non-Javadoc)
68      * @see org.eclipse.team.ui.mapping.DecoratedStateProvider#isDecorated(java.lang.Object)
69      */

70     public boolean hasDecoratedState(Object JavaDoc element) throws CoreException {
71         ITeamStateProvider provider = getDecoratedStateProvider(element);
72         if (provider != null)
73             provider.hasDecoratedState(element);
74         return false;
75     }
76
77     /* (non-Javadoc)
78      * @see org.eclipse.team.ui.mapping.DecoratedStateProvider#getDecoratedStateMask(java.lang.Object)
79      */

80     public final int getDecoratedStateMask(Object JavaDoc element) {
81         ITeamStateProvider provider = getDecoratedStateProvider(element);
82         if (provider != null)
83             return provider.getDecoratedStateMask(element);
84         return 0;
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#getDecoratedProperties(java.lang.Object)
89      */

90     public String JavaDoc[] getDecoratedProperties(Object JavaDoc element) {
91         ITeamStateProvider provider = getDecoratedStateProvider(element);
92         if (provider != null)
93             return provider.getDecoratedProperties(element);
94         return new String JavaDoc[0];
95     }
96
97     
98     /* (non-Javadoc)
99      * @see org.eclipse.team.ui.mapping.DecoratedStateProvider#getState(java.lang.Object, int, org.eclipse.core.runtime.IProgressMonitor)
100      */

101     public ITeamStateDescription getStateDescription(Object JavaDoc element, int stateMask,
102             String JavaDoc[] properties, IProgressMonitor monitor) throws CoreException {
103         ITeamStateProvider provider = getDecoratedStateProvider(element);
104         if (provider != null)
105             return provider.getStateDescription(element, stateMask, properties, monitor);
106         return null;
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#getResourceMappingContext(java.lang.Object)
111      */

112     public ResourceMappingContext getResourceMappingContext(Object JavaDoc element) {
113         ITeamStateProvider provider = getDecoratedStateProvider(element);
114         if (provider != null)
115             return provider.getResourceMappingContext(element);
116         return ResourceMappingContext.LOCAL_CONTEXT;
117     }
118     
119     private ITeamStateProvider getDecoratedStateProvider(Object JavaDoc element) {
120         RepositoryProviderType type = getProviderType(element);
121         if (type != null)
122             return (ITeamStateProvider) Utils.getAdapter(type,
123                     ITeamStateProvider.class);
124         return null;
125     }
126
127     private ITeamStateProvider getDecoratedStateProviderForId(String JavaDoc id) {
128         RepositoryProviderType type = getProviderTypeForId(id);
129         if (type != null)
130             return (ITeamStateProvider) Utils.getAdapter(type,
131                     ITeamStateProvider.class, true);
132         return null;
133     }
134
135     private RepositoryProviderType getProviderType(Object JavaDoc element) {
136         ResourceMapping mapping = Utils.getResourceMapping(element);
137         if (mapping != null) {
138             String JavaDoc providerId = getProviderId(mapping.getProjects());
139             if (providerId != null)
140                 return getProviderTypeForId(providerId);
141         }
142         return null;
143     }
144
145     private String JavaDoc getProviderId(IProject[] projects) {
146         String JavaDoc id = null;
147         for (int i = 0; i < projects.length; i++) {
148             IProject project = projects[i];
149             String JavaDoc nextId = getProviderId(project);
150             if (id == null)
151                 id = nextId;
152             else if (nextId != null && !id.equals(nextId))
153                 return null;
154         }
155         return id;
156     }
157
158     private String JavaDoc getProviderId(IProject project) {
159         RepositoryProvider provider = RepositoryProvider.getProvider(project);
160         if (provider != null)
161             return provider.getID();
162         return null;
163     }
164
165     private RepositoryProviderType getProviderTypeForId(String JavaDoc providerId) {
166         return RepositoryProviderType.getProviderType(providerId);
167     }
168
169     private void handleProject(IProject project) {
170         if (RepositoryProvider.isShared(project)) {
171             try {
172                 String JavaDoc currentId = project
173                         .getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY);
174                 if (currentId != null) {
175                     listenerForStateChangesForId(currentId);
176                 }
177             } catch (CoreException e) {
178                 TeamPlugin.log(e);
179             }
180         }
181     }
182     
183     private void listenerForStateChangesForId(String JavaDoc id) {
184         if (!providers.containsKey(id)) {
185             ITeamStateProvider provider = getDecoratedStateProviderForId(id);
186             if (provider != null) {
187                 providers.put(id, provider);
188                 provider.addDecoratedStateChangeListener(this);
189             }
190         }
191     }
192
193     /* (non-Javadoc)
194      * @see org.eclipse.team.ui.mapping.IDecoratedStateChangeListener#decoratedStateChanged(org.eclipse.team.ui.mapping.IDecoratedStateChangeEvent)
195      */

196     public void teamStateChanged(ITeamStateChangeEvent event) {
197         fireStateChangeEvent(event);
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.team.internal.core.IRepositoryProviderListener#providerUnmapped(org.eclipse.core.resources.IProject)
202      */

203     public void providerUnmapped(IProject project) {
204         // We don't need to worry about this
205
}
206
207     /* (non-Javadoc)
208      * @see org.eclipse.team.internal.core.IRepositoryProviderListener#providerMapped(org.eclipse.team.core.RepositoryProvider)
209      */

210     public void providerMapped(RepositoryProvider provider) {
211         String JavaDoc id = provider.getID();
212         listenerForStateChangesForId(id);
213     }
214
215     /* (non-Javadoc)
216      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
217      */

218     public void resourceChanged(IResourceChangeEvent event) {
219         IResourceDelta delta = event.getDelta();
220         IResourceDelta[] projectDeltas = delta
221                 .getAffectedChildren(IResourceDelta.ADDED
222                         | IResourceDelta.CHANGED);
223         for (int i = 0; i < projectDeltas.length; i++) {
224             IResourceDelta projectDelta = projectDeltas[i];
225             IResource resource = projectDelta.getResource();
226             if ((projectDelta.getFlags() & IResourceDelta.OPEN) != 0
227                     && resource.getType() == IResource.PROJECT) {
228                 IProject project = (IProject) resource;
229                 if (project.isAccessible()) {
230                     handleProject(project);
231                 }
232             }
233         }
234     }
235 }
236
Popular Tags