1 11 package org.eclipse.team.core.variants; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IResourceStatus; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.MultiStatus; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.osgi.util.NLS; 29 import org.eclipse.team.core.TeamException; 30 import org.eclipse.team.core.TeamStatus; 31 import org.eclipse.team.core.subscribers.Subscriber; 32 import org.eclipse.team.core.subscribers.SubscriberChangeEvent; 33 import org.eclipse.team.core.synchronize.SyncInfo; 34 import org.eclipse.team.internal.core.*; 35 import org.eclipse.team.internal.core.Policy; 36 import org.eclipse.team.internal.core.TeamPlugin; 37 38 45 public abstract class ResourceVariantTreeSubscriber extends Subscriber { 46 47 50 public SyncInfo getSyncInfo(IResource resource) throws TeamException { 51 if (!isSupervised(resource)) return null; 52 IResourceVariant remoteResource = getRemoteTree().getResourceVariant(resource); 53 IResourceVariant baseResource; 54 if (getResourceComparator().isThreeWay()) { 55 baseResource= getBaseTree().getResourceVariant(resource); 56 } else { 57 baseResource = null; 58 } 59 return getSyncInfo(resource, baseResource, remoteResource); 60 } 61 62 71 protected SyncInfo getSyncInfo(IResource local, IResourceVariant base, IResourceVariant remote) throws TeamException { 72 SyncInfo info = new SyncInfo(local, base, remote, this.getResourceComparator()); 73 info.init(); 74 return info; 75 } 76 77 80 public IResource[] members(IResource resource) throws TeamException { 81 if(resource.getType() == IResource.FILE) { 82 return new IResource[0]; 83 } 84 try { 85 Set allMembers = new HashSet (); 86 try { 87 allMembers.addAll(Arrays.asList(((IContainer)resource).members())); 88 } catch (CoreException e) { 89 if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND) { 90 } else { 92 throw e; 93 } 94 } 95 allMembers.addAll(Arrays.asList(internalMembers(getRemoteTree(), resource))); 96 if (getResourceComparator().isThreeWay()) { 97 allMembers.addAll(Arrays.asList(internalMembers(getBaseTree(), resource))); 98 } 99 for (Iterator iterator = allMembers.iterator(); iterator.hasNext();) { 100 IResource member = (IResource) iterator.next(); 101 if(!member.exists() && !getRemoteTree().hasResourceVariant(member)) { 102 iterator.remove(); 104 } else if (!isSupervised(resource)) { 105 iterator.remove(); 107 } 108 } 109 return (IResource[]) allMembers.toArray(new IResource[allMembers.size()]); 110 } catch (CoreException e) { 111 throw TeamException.asTeamException(e); 112 } 113 } 114 115 118 public void refresh(IResource[] resources, int depth, IProgressMonitor monitor) throws TeamException { 119 monitor = Policy.monitorFor(monitor); 120 List errors = new ArrayList (); 121 try { 122 monitor.beginTask(null, 1000 * resources.length); 123 for (int i = 0; i < resources.length; i++) { 124 IResource resource = resources[i]; 125 if (resource.getProject().isAccessible()) { 126 IStatus status = refresh(resource, depth, Policy.subMonitorFor(monitor, 1000)); 127 if (!status.isOK()) { 128 errors.add(status); 129 } 130 } 131 } 132 } finally { 133 monitor.done(); 134 } 135 if (!errors.isEmpty()) { 136 int numSuccess = resources.length - errors.size(); 137 throw new TeamException(new MultiStatus(TeamPlugin.ID, 0, 138 (IStatus[]) errors.toArray(new IStatus[errors.size()]), 139 NLS.bind(Messages.ResourceVariantTreeSubscriber_1, (new Object [] {getName(), Integer.toString(numSuccess), Integer.toString(resources.length)})), null)); 140 } 141 } 142 143 146 protected abstract IResourceVariantTree getBaseTree(); 147 148 151 protected abstract IResourceVariantTree getRemoteTree(); 152 153 private IStatus refresh(IResource resource, int depth, IProgressMonitor monitor) { 154 monitor = Policy.monitorFor(monitor); 155 try { 156 monitor.beginTask(null, 100); 157 Set allChanges = new HashSet (); 158 if (getResourceComparator().isThreeWay()) { 159 IResource[] baseChanges = getBaseTree().refresh(new IResource[] {resource}, depth, Policy.subMonitorFor(monitor, 25)); 160 allChanges.addAll(Arrays.asList(baseChanges)); 161 } 162 IResource[] remoteChanges = getRemoteTree().refresh(new IResource[] {resource}, depth, Policy.subMonitorFor(monitor, 75)); 163 allChanges.addAll(Arrays.asList(remoteChanges)); 164 IResource[] changedResources = (IResource[]) allChanges.toArray(new IResource[allChanges.size()]); 165 fireTeamResourceChange(SubscriberChangeEvent.asSyncChangedDeltas(this, changedResources)); 166 return Status.OK_STATUS; 167 } catch (TeamException e) { 168 return new TeamStatus(IStatus.ERROR, TeamPlugin.ID, 0, NLS.bind(Messages.ResourceVariantTreeSubscriber_2, new String [] { resource.getFullPath().toString(), e.getMessage() }), e, resource); 169 } finally { 170 monitor.done(); 171 } 172 } 173 174 private IResource[] internalMembers(IResourceVariantTree tree, IResource resource) throws TeamException, CoreException { 175 IResource[] members; 177 try { 178 members = tree.members(resource); 179 } catch (CoreException e) { 180 if (!isSupervised(resource) || e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND) { 181 return new IResource[0]; 184 } 185 throw e; 186 } 187 return members; 188 } 189 } 190 | Popular Tags |