1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.jface.viewers.ViewerSorter; 23 import org.eclipse.team.core.synchronize.*; 24 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement; 25 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 26 27 41 public abstract class SynchronizeModelProvider extends AbstractSynchronizeModelProvider implements ISyncInfoSetChangeListener { 42 43 protected final Map resourceMap = Collections.synchronizedMap(new HashMap ()); 44 45 protected static final boolean DEBUG = false; 46 47 public SynchronizeModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) { 48 super(configuration, set); 49 } 50 51 public SynchronizeModelProvider(AbstractSynchronizeModelProvider parentProvider, ISynchronizeModelElement modelRoot, ISynchronizePageConfiguration configuration, SyncInfoSet set) { 52 super(parentProvider, modelRoot, configuration, set); 53 associateRoot(modelRoot); 54 } 55 56 private void associateRoot(ISynchronizeModelElement modelRoot) { 57 resourceMap.put(ResourcesPlugin.getWorkspace().getRoot(), modelRoot); 59 } 60 61 64 public void dispose() { 65 resourceMap.clear(); 66 super.dispose(); 67 } 68 69 74 public abstract ViewerSorter getViewerSorter(); 75 76 83 protected ISynchronizeModelElement getModelObject(IResource resource) { 84 return (ISynchronizeModelElement) resourceMap.get(resource); 85 } 86 87 90 protected ISynchronizeModelElement[] getModelObjects(IResource resource) { 91 ISynchronizeModelElement element = getModelObject(resource); 92 if (element == null) { 93 return new ISynchronizeModelElement[0]; 94 } 95 return new ISynchronizeModelElement[] { element }; 96 } 97 98 protected void associateDiffNode(ISynchronizeModelElement node) { 99 IResource resource = node.getResource(); 100 if(resource != null) { 101 resourceMap.put(resource, node); 102 } 103 } 104 105 protected void unassociateDiffNode(IResource resource) { 106 resourceMap.remove(resource); 107 } 108 109 114 protected void removeFromViewer(IResource resource) { 115 ISynchronizeModelElement element = getModelObject(resource); 116 if(element != null) { 117 removeFromViewer(new ISynchronizeModelElement[] { element }); 118 } 119 } 120 121 126 protected void removeFromViewer(IResource[] resources) { 127 List elements = new ArrayList (); 128 for (int i = 0; i < resources.length; i++) { 129 IResource resource = resources[i]; 130 ISynchronizeModelElement element = getModelObject(resource); 131 if(element != null) { 132 elements.add(element); 133 } 134 } 135 if (!elements.isEmpty()) { 136 removeFromViewer((ISynchronizeModelElement[]) elements.toArray(new ISynchronizeModelElement[elements.size()])); 137 } 138 } 139 140 143 protected void recursiveClearModelObjects(ISynchronizeModelElement node) { 144 super.recursiveClearModelObjects(node); 145 if (node == getModelRoot()) { 146 resourceMap.clear(); 149 associateRoot(getModelRoot()); 151 } else { 152 IResource resource = node.getResource(); 153 if (resource != null) { 154 unassociateDiffNode(resource); 155 } 156 } 157 } 158 159 162 protected void addToViewer(ISynchronizeModelElement node) { 163 associateDiffNode(node); 164 super.addToViewer(node); 165 } 166 167 170 protected boolean hasViewerState() { 171 return ! resourceMap.isEmpty(); 172 } 173 174 public ISynchronizeModelElement[] getClosestExistingParents(IResource resource) { 175 ISynchronizeModelElement element = getModelObject(resource); 176 if(element == null) { 177 do { 178 resource = resource.getParent(); 179 element = getModelObject(resource); 180 } while(element == null && resource != null); 181 } 182 if (element == null) { 183 return new ISynchronizeModelElement[0]; 184 } 185 return new ISynchronizeModelElement[] { element }; 186 } 187 188 191 protected final void handleChanges(ISyncInfoTreeChangeEvent event, IProgressMonitor monitor) { 192 super.handleChanges(event, monitor); 193 } 194 195 198 protected void handleResourceChanges(ISyncInfoTreeChangeEvent event) { 199 SyncInfo[] infos = event.getChangedResources(); 201 for (int i = 0; i < infos.length; i++) { 202 SyncInfo info = infos[i]; 203 IResource local = info.getLocal(); 204 ISynchronizeModelElement diffNode = getModelObject(local); 205 if (diffNode != null) { 206 handleChange(diffNode, info); 207 } 208 } 209 } 210 211 217 protected void handleChange(ISynchronizeModelElement diffNode, SyncInfo info) { 218 IResource local = info.getLocal(); 219 220 if(diffNode instanceof SyncInfoModelElement) { 221 ((SyncInfoModelElement)diffNode).update(info); 222 propogateConflictState(diffNode, false); 223 queueForLabelUpdate(diffNode); 224 } else { 225 removeFromViewer(local); 226 addResource(info); 227 ISynchronizeModelElement node = getModelObject(info.getLocal()); 228 buildModelObjects(node); 229 230 } 231 } 232 233 238 protected void addResources(SyncInfo[] added) { 239 for (int i = 0; i < added.length; i++) { 240 SyncInfo info = added[i]; 241 addResource(info); 242 } 243 } 244 245 250 protected abstract void addResource(SyncInfo info); 251 252 258 protected abstract ISynchronizeModelElement createModelObject(ISynchronizeModelElement parent, SyncInfo info); 259 } 260 | Popular Tags |