KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > variants > ResourceVariantTreeSubscriber


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.core.variants;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
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 /**
39  * A specialization of Subscriber that uses <code>IResourceVariantTree</code> objects
40  * to manage the base (for three-way) and remote trees. Refreshing and obtaining the subscriber
41  * members and resource variants is delegated to the resource variant trees.
42  *
43  * @since 3.0
44  */

45 public abstract class ResourceVariantTreeSubscriber extends Subscriber {
46
47     /* (non-Javadoc)
48      * @see org.eclipse.team.core.subscribers.Subscriber#getSyncInfo(org.eclipse.core.resources.IResource)
49      */

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     /**
63      * Method that creates an instance of SyncInfo for the provided local, base and remote
64      * resource variants.
65      * Can be overridden by subclasses.
66      * @param local the local resource
67      * @param base the base resource variant or <code>null</code>
68      * @param remote the remote resource variant or <code>null</code>
69      * @return the <code>SyncInfo</code> containing the provided resources
70      */

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     /* (non-Javadoc)
78      * @see org.eclipse.team.core.subscribers.Subscriber#members(org.eclipse.core.resources.IResource)
79      */

80     public IResource[] members(IResource resource) throws TeamException {
81         if(resource.getType() == IResource.FILE) {
82             return new IResource[0];
83         }
84         try {
85             Set JavaDoc allMembers = new HashSet JavaDoc();
86             try {
87                 allMembers.addAll(Arrays.asList(((IContainer)resource).members()));
88             } catch (CoreException e) {
89                 if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND) {
90                     // The resource is no longer exists so ignore the exception
91
} 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 JavaDoc iterator = allMembers.iterator(); iterator.hasNext();) {
100                 IResource member = (IResource) iterator.next();
101                 if(!member.exists() && !getRemoteTree().hasResourceVariant(member)) {
102                     // Remove deletion conflicts
103
iterator.remove();
104                 } else if (!isSupervised(resource)) {
105                     // Remove unsupervised resources
106
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     /* (non-Javadoc)
116      * @see org.eclipse.team.core.subscribers.Subscriber#refresh(org.eclipse.core.resources.IResource[], int, org.eclipse.core.runtime.IProgressMonitor)
117      */

118     public void refresh(IResource[] resources, int depth, IProgressMonitor monitor) throws TeamException {
119         monitor = Policy.monitorFor(monitor);
120         List JavaDoc errors = new ArrayList JavaDoc();
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 JavaDoc[] {getName(), Integer.toString(numSuccess), Integer.toString(resources.length)})), null));
140         }
141     }
142     
143     /**
144      * Return the base resource variant tree.
145      */

146     protected abstract IResourceVariantTree getBaseTree();
147
148     /**
149      * Return the remote resource variant tree.
150      */

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 JavaDoc allChanges = new HashSet JavaDoc();
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 JavaDoc[] { 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         // Filter and return only phantoms associated with the remote synchronizer.
176
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                 // The resource is no longer supervised or doesn't exist in any form
182
// so ignore the exception and return that there are no members
183
return new IResource[0];
184             }
185             throw e;
186         }
187         return members;
188     }
189 }
190
Popular Tags