1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 34 public class WorkspaceTeamStateProvider extends TeamStateProvider 35 implements ITeamStateChangeListener, IRepositoryProviderListener, 36 IResourceChangeListener { 37 38 private Map providers = new HashMap (); 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 60 public final boolean isDecorationEnabled(Object element) { 61 ITeamStateProvider provider = getDecoratedStateProvider(element); 62 if (provider != null) 63 return provider.isDecorationEnabled(element); 64 return false; 65 } 66 67 70 public boolean hasDecoratedState(Object element) throws CoreException { 71 ITeamStateProvider provider = getDecoratedStateProvider(element); 72 if (provider != null) 73 provider.hasDecoratedState(element); 74 return false; 75 } 76 77 80 public final int getDecoratedStateMask(Object element) { 81 ITeamStateProvider provider = getDecoratedStateProvider(element); 82 if (provider != null) 83 return provider.getDecoratedStateMask(element); 84 return 0; 85 } 86 87 90 public String [] getDecoratedProperties(Object element) { 91 ITeamStateProvider provider = getDecoratedStateProvider(element); 92 if (provider != null) 93 return provider.getDecoratedProperties(element); 94 return new String [0]; 95 } 96 97 98 101 public ITeamStateDescription getStateDescription(Object element, int stateMask, 102 String [] 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 112 public ResourceMappingContext getResourceMappingContext(Object 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 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 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 element) { 136 ResourceMapping mapping = Utils.getResourceMapping(element); 137 if (mapping != null) { 138 String providerId = getProviderId(mapping.getProjects()); 139 if (providerId != null) 140 return getProviderTypeForId(providerId); 141 } 142 return null; 143 } 144 145 private String getProviderId(IProject[] projects) { 146 String id = null; 147 for (int i = 0; i < projects.length; i++) { 148 IProject project = projects[i]; 149 String 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 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 providerId) { 166 return RepositoryProviderType.getProviderType(providerId); 167 } 168 169 private void handleProject(IProject project) { 170 if (RepositoryProvider.isShared(project)) { 171 try { 172 String 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 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 196 public void teamStateChanged(ITeamStateChangeEvent event) { 197 fireStateChangeEvent(event); 198 } 199 200 203 public void providerUnmapped(IProject project) { 204 } 206 207 210 public void providerMapped(RepositoryProvider provider) { 211 String id = provider.getID(); 212 listenerForStateChangesForId(id); 213 } 214 215 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 |