1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.mapping.*; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.jface.viewers.*; 22 import org.eclipse.team.core.diff.FastDiffFilter; 23 import org.eclipse.team.core.diff.IDiff; 24 import org.eclipse.team.core.mapping.IResourceDiffTree; 25 import org.eclipse.team.core.mapping.ISynchronizationContext; 26 import org.eclipse.team.internal.core.subscribers.DiffChangeSet; 27 import org.eclipse.team.internal.ui.TeamUIPlugin; 28 import org.eclipse.team.internal.ui.Utils; 29 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration; 30 import org.eclipse.team.ui.mapping.SynchronizationOperation; 31 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 32 33 public abstract class ResourceModelProviderOperation extends SynchronizationOperation { 34 35 private final IStructuredSelection selection; 36 37 protected ResourceModelProviderOperation(ISynchronizePageConfiguration configuration, IStructuredSelection selection) { 38 super(configuration, getElements(selection)); 39 this.selection = selection; 40 } 41 42 private static Object [] getElements(IStructuredSelection selection) { 43 return selection.toArray(); 44 } 45 46 53 private IDiff[] getFileDeltas(Object [] pathOrElements) { 54 Set result = new HashSet (); 55 for (int i = 0; i < pathOrElements.length; i++) { 56 Object pathOrElement = pathOrElements[i]; 57 IDiff[] diffs = getFileDeltas(pathOrElement); 58 for (int j = 0; j < diffs.length; j++) { 59 IDiff node = diffs[j]; 60 result.add(node); 61 } 62 } 63 return (IDiff[]) result.toArray(new IDiff[result.size()]); 64 } 65 66 private IDiff[] getFileDeltas(Object pathOrElement) { 67 List result = new ArrayList (); 68 ResourceTraversal[] traversals = getTraversals(pathOrElement); 69 if (traversals.length > 0) { 70 ISynchronizationContext context = getContext(); 71 final IResourceDiffTree diffTree = context.getDiffTree(); 72 IDiff[] diffs = diffTree.getDiffs(traversals); 73 for (int i = 0; i < diffs.length; i++) { 75 IDiff node = diffs[i]; 76 if (isVisible(node) && getDiffFilter().select(node)) 77 result.add(node); 78 } 79 } 80 return (IDiff[]) result.toArray(new IDiff[result.size()]); 81 } 82 83 89 protected boolean isVisible(IDiff node) { 90 return ((SynchronizePageConfiguration)getConfiguration()).isVisible(node); 91 } 92 93 private ResourceTraversal[] getTraversals(Object pathOrElement) { 94 Object element; 96 if (pathOrElement instanceof TreePath) { 97 TreePath tp = (TreePath) pathOrElement; 98 Object o = tp.getFirstSegment(); 99 if (o instanceof DiffChangeSet) { 100 DiffChangeSet dcs = (DiffChangeSet) o; 102 return getTraversalCalculator().getTraversals(dcs, tp); 103 } 104 element = tp.getLastSegment(); 105 } else { 106 element = pathOrElement; 107 } 108 109 if (isResourcePath(pathOrElement)) { 111 IResource resource = (IResource) element; 112 return getTraversalCalculator().getTraversals(resource, (TreePath)pathOrElement); 113 } 114 115 ResourceMapping mapping = Utils.getResourceMapping(element); 117 if (mapping != null) { 118 ResourceTraversal[] traversals = getContext().getScope().getTraversals(mapping); 120 if (traversals != null) 121 return traversals; 122 try { 126 return mapping.getTraversals(ResourceMappingContext.LOCAL_CONTEXT, null); 127 } catch (CoreException e) { 128 TeamUIPlugin.log(e); 129 } 130 } 131 return new ResourceTraversal[0]; 132 } 133 134 private boolean isResourcePath(Object pathOrElement) { 135 if (pathOrElement instanceof TreePath) { 136 TreePath tp = (TreePath) pathOrElement; 137 return getTraversalCalculator().isResourcePath(tp); 138 } 139 return false; 140 } 141 142 146 protected abstract FastDiffFilter getDiffFilter(); 147 148 151 public boolean shouldRun() { 152 Object [] elements = getElements(); 153 for (int i = 0; i < elements.length; i++) { 154 Object object = elements[i]; 155 if (Utils.getResourceMapping(internalGetElement(object)) != null) { 156 return true; 157 } 158 } 159 return false; 160 } 161 162 protected IDiff[] getTargetDiffs() { 163 return getFileDeltas(getTreePathsOrElements()); 164 } 165 166 private Object [] getTreePathsOrElements() { 167 if (selection instanceof ITreeSelection) { 168 ITreeSelection ts = (ITreeSelection) selection; 169 return ts.getPaths(); 170 } 171 return getElements(); 172 } 173 174 177 protected boolean canRunAsJob() { 178 return true; 179 } 180 181 protected ResourceModelTraversalCalculator getTraversalCalculator() { 182 return ResourceModelTraversalCalculator.getTraversalCalculator(getConfiguration()); 183 } 184 185 private Object internalGetElement(Object elementOrPath) { 186 if (elementOrPath instanceof TreePath) { 187 TreePath tp = (TreePath) elementOrPath; 188 return tp.getLastSegment(); 189 } 190 return elementOrPath; 191 } 192 193 public boolean belongsTo(Object family) { 194 if (family == getContext()) { 195 return true; 196 } 197 return super.belongsTo(family); 198 } 199 200 } 201 | Popular Tags |