1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.mapping.*; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.jface.viewers.*; 20 import org.eclipse.team.core.diff.IDiff; 21 import org.eclipse.team.core.mapping.provider.ResourceDiffTree; 22 import org.eclipse.team.internal.core.mapping.CompoundResourceTraversal; 23 import org.eclipse.team.internal.core.subscribers.DiffChangeSet; 24 import org.eclipse.team.internal.ui.Policy; 25 import org.eclipse.team.internal.ui.Utils; 26 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 27 import org.eclipse.team.ui.synchronize.ModelParticipantAction; 28 29 public abstract class ResourceModelParticipantAction extends ModelParticipantAction { 30 31 public ResourceModelParticipantAction(String text, ISynchronizePageConfiguration configuration) { 32 super(text, configuration); 33 } 34 35 42 protected ResourceTraversal[] getResourceTraversals(IStructuredSelection selection, IProgressMonitor monitor) throws CoreException { 43 try { 44 monitor.beginTask(null, selection.size() * 100); 45 CompoundResourceTraversal traversal = new CompoundResourceTraversal(); 46 if (selection instanceof ITreeSelection) { 47 ITreeSelection ts = (ITreeSelection) selection; 48 TreePath[] paths = ts.getPaths(); 49 for (int i = 0; i < paths.length; i++) { 50 TreePath path = paths[i]; 51 ResourceTraversal[] traversals = getTraversals(path, Policy.subMonitorFor(monitor, 100)); 52 traversal.addTraversals(traversals); 53 } 54 } else { 55 for (Iterator iter = selection.iterator(); iter.hasNext();) { 56 Object element = (Object ) iter.next(); 57 ResourceTraversal[] traversals = getTraversals(element, Policy.subMonitorFor(monitor, 100)); 58 traversal.addTraversals(traversals); 59 } 60 } 61 return traversal.asTraversals(); 62 } finally { 63 monitor.done(); 64 } 65 } 66 67 73 private ResourceTraversal[] getTraversals(Object element, IProgressMonitor monitor) throws CoreException { 74 ResourceMapping mapping = Utils.getResourceMapping(element); 75 if (mapping != null) 76 return mapping.getTraversals(getResourceMappingContext(), monitor); 77 return null; 78 } 79 80 protected ResourceMappingContext getResourceMappingContext() { 81 return new SynchronizationResourceMappingContext(getSynchronizationContext()); 82 } 83 84 protected ResourceModelTraversalCalculator getTraversalCalculator() { 85 return ResourceModelTraversalCalculator.getTraversalCalculator(getConfiguration()); 86 } 87 88 94 private ResourceTraversal[] getTraversals(TreePath path, IProgressMonitor monitor) throws CoreException { 95 if (path.getSegmentCount() > 0) { 96 DiffChangeSet set = getChangeSet(path); 97 Object o = path.getLastSegment(); 98 if (set != null) { 99 if (path.getSegmentCount() == 1) { 100 return new ResourceTraversal[] { new ResourceTraversal(set.getResources(), IResource.DEPTH_ZERO, IResource.NONE) }; 101 } 102 if (o instanceof IResource) { 103 IResource resource = (IResource) o; 104 int depth = getTraversalCalculator().getLayoutDepth(resource, path); 105 IDiff[] diffs = set.getDiffTree().getDiffs(resource, depth); 106 Set resources = new HashSet(); 107 for (int i = 0; i < diffs.length; i++) { 108 IDiff diff = diffs[i]; 109 IResource r = ResourceDiffTree.getResourceFor(diff); 110 if (r != null) 111 resources.add(r); 112 } 113 return new ResourceTraversal[] { new ResourceTraversal((IResource[]) resources.toArray(new IResource[resources.size()]), IResource.DEPTH_ZERO, IResource.NONE) }; 114 } 115 } 116 if (getTraversalCalculator().isResourcePath(path)) { 117 IResource resource = (IResource) o; 118 return getTraversalCalculator().getTraversals(resource, path); 119 } 120 return getTraversals(o, monitor); 121 } 122 return null; 123 } 124 125 private DiffChangeSet getChangeSet(TreePath path) { 126 Object o = path.getFirstSegment(); 127 if (o instanceof DiffChangeSet) { 128 return (DiffChangeSet) o; 129 } 130 return null; 131 } 132 133 } 134 | Popular Tags |