1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.team.core.synchronize.*; 21 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement; 22 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 23 24 31 public abstract class CompositeModelProvider extends AbstractSynchronizeModelProvider { 32 33 private final List providers = new ArrayList (); 34 private final Map resourceToElements = new HashMap (); private final Map elementToProvider = new HashMap (); 37 protected CompositeModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) { 38 super(configuration, set); 39 } 40 41 45 protected void addProvider(ISynchronizeModelProvider provider) { 46 providers.add(provider); 47 } 48 49 53 protected void removeProvider(ISynchronizeModelProvider provider) { 54 providers.remove(provider); 55 provider.dispose(); 56 } 57 58 61 protected ISynchronizeModelProvider getProvider(ISynchronizeModelElement element) { 62 return (ISynchronizeModelProvider)elementToProvider.get(element); 63 } 64 65 68 public ISynchronizeModelElement[] getClosestExistingParents(IResource resource) { 69 ISynchronizeModelProvider[] providers = getProviders(); 70 if (providers.length == 0) { 71 return new ISynchronizeModelElement[0]; 72 } 73 if (providers.length == 1 && providers[0] instanceof AbstractSynchronizeModelProvider) { 74 return ((AbstractSynchronizeModelProvider)providers[0]).getClosestExistingParents(resource); 75 } 76 List result = new ArrayList (); 77 for (int i = 0; i < providers.length; i++) { 78 ISynchronizeModelProvider provider = providers[i]; 79 if (provider instanceof AbstractSynchronizeModelProvider) { 80 ISynchronizeModelElement[] elements = ((AbstractSynchronizeModelProvider)provider).getClosestExistingParents(resource); 81 for (int j = 0; j < elements.length; j++) { 82 ISynchronizeModelElement element = elements[j]; 83 result.add(element); 84 } 85 } 86 } 87 return (ISynchronizeModelElement[]) result.toArray(new ISynchronizeModelElement[result.size()]); 88 } 89 90 94 protected ISynchronizeModelProvider[] getProviders() { 95 return (ISynchronizeModelProvider[]) providers.toArray(new ISynchronizeModelProvider[providers.size()]); 96 } 97 98 103 protected ISynchronizeModelProvider[] getProvidersContaining(IResource resource) { 104 List elements = (List )resourceToElements.get(resource); 105 if (elements == null || elements.isEmpty()) { 106 return new ISynchronizeModelProvider[0]; 107 } 108 List result = new ArrayList (); 109 for (Iterator iter = elements.iterator(); iter.hasNext();) { 110 ISynchronizeModelElement element = (ISynchronizeModelElement)iter.next(); 111 result.add(getProvider(element)); 112 } 113 return (ISynchronizeModelProvider[]) result.toArray(new ISynchronizeModelProvider[result.size()]); 114 } 115 116 119 protected final void handleResourceAdditions(ISyncInfoTreeChangeEvent event) { 120 handleAdditions(event.getAddedResources()); 121 } 122 123 128 protected void handleAdditions(SyncInfo[] resources) { 129 for (int i = 0; i < resources.length; i++) { 130 SyncInfo info = resources[i]; 131 handleAddition(info); 132 } 133 } 134 135 139 protected abstract void handleAddition(SyncInfo info); 140 141 144 protected final void handleResourceChanges(ISyncInfoTreeChangeEvent event) { 145 SyncInfo[] infos = event.getChangedResources(); 146 for (int i = 0; i < infos.length; i++) { 147 SyncInfo info = infos[i]; 148 handleChange(info); 149 } 150 } 151 152 157 protected void handleChange(SyncInfo info) { 158 handleRemoval(info.getLocal()); 159 handleAddition(info); 160 } 161 162 165 protected final void handleResourceRemovals(ISyncInfoTreeChangeEvent event) { 166 IResource[] resources = event.getRemovedResources(); 167 for (int i = 0; i < resources.length; i++) { 168 IResource resource = resources[i]; 169 handleRemoval(resource); 170 } 171 } 172 173 177 protected void handleRemoval(IResource resource) { 178 ISynchronizeModelProvider[] providers = getProvidersContaining(resource); 179 for (int i = 0; i < providers.length; i++) { 180 ISynchronizeModelProvider provider = providers[i]; 181 removeFromProvider(resource, provider); 182 } 183 } 184 185 192 protected void removeFromProvider(IResource resource, ISynchronizeModelProvider provider) { 193 if (provider != this) { 194 provider.getSyncInfoSet().remove(resource); 195 } 196 } 197 198 201 protected void nodeAdded(ISynchronizeModelElement node, AbstractSynchronizeModelProvider provider) { 202 IResource r = node.getResource(); 204 if(r != null) { 205 List elements = (List )resourceToElements.get(r); 206 if(elements == null) { 207 elements = new ArrayList (2); 208 resourceToElements.put(r, elements); 209 } 210 elements.add(node); 211 } 212 elementToProvider.put(node, provider); 213 super.nodeAdded(node, provider); 214 } 215 216 219 public void modelObjectCleared(ISynchronizeModelElement node) { 220 super.modelObjectCleared(node); 221 IResource r = node.getResource(); 222 if(r != null) { 223 List elements = (List )resourceToElements.get(r); 224 if(elements != null) { 225 elements.remove(node); 226 if (elements.isEmpty()) { 227 resourceToElements.remove(r); 228 } 229 } 230 } 231 elementToProvider.remove(node); 232 } 233 234 237 protected void recursiveClearModelObjects(ISynchronizeModelElement node) { 238 super.recursiveClearModelObjects(node); 239 if (node == getModelRoot()) { 240 clearProviders(); 241 } 242 } 243 244 private void clearProviders() { 245 for (Iterator iter = providers.iterator(); iter.hasNext();) { 246 ISynchronizeModelProvider provider = (ISynchronizeModelProvider) iter.next(); 247 provider.dispose(); 248 } 249 providers.clear(); 250 resourceToElements.clear(); 251 elementToProvider.clear(); 252 } 253 254 260 protected ISynchronizeModelProvider createModelProvider(ISynchronizeModelElement parent, String id, SyncInfoTree syncInfoTree) { 261 if (id != null && id.endsWith(FlatModelProvider.FlatModelProviderDescriptor.ID)) { 262 return new FlatModelProvider(this, parent, getConfiguration(), syncInfoTree); 263 } else if (id != null && id.endsWith(CompressedFoldersModelProvider.CompressedFolderModelProviderDescriptor.ID)) { 264 return new CompressedFoldersModelProvider(this, parent, getConfiguration(), syncInfoTree); 265 } else { 266 return new HierarchicalModelProvider(this, parent, getConfiguration(), syncInfoTree); 267 } 268 } 269 270 273 public void dispose() { 274 clearProviders(); 275 super.dispose(); 276 } 277 278 281 protected boolean hasViewerState() { 282 return resourceToElements != null && !resourceToElements.isEmpty(); 283 } 284 285 288 protected ISynchronizeModelElement[] getModelObjects(IResource resource) { 289 List elements = (List )resourceToElements.get(resource); 290 if (elements == null) { 291 return new ISynchronizeModelElement[0]; 292 } 293 return (ISynchronizeModelElement[]) elements.toArray(new ISynchronizeModelElement[elements.size()]); 294 } 295 } 296 | Popular Tags |