KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IWorkspaceRunnable;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.team.core.TeamException;
18
19 /**
20  * A resource variant tree that caches and obtains its bytes from the remote slot
21  * in a three-way synchronizer. Clients must subclass to provide remote resource
22  * variant refresh functionality.
23  *
24  * @see ThreeWaySubscriber
25  *
26  * @since 3.0
27  */

28 public abstract class ThreeWayRemoteTree extends ResourceVariantTree {
29
30     private ThreeWaySubscriber subscriber;
31
32     /*
33      * A resource variant byte store that accesses the remote bytes
34      * from a three-way synchronizer. Both access and modification
35      * are supported.
36      */

37     static class RemoteResourceVariantByteStore extends ResourceVariantByteStore {
38         private ThreeWaySynchronizer synchronizer;
39         public RemoteResourceVariantByteStore(ThreeWaySynchronizer synchronizer) {
40             this.synchronizer = synchronizer;
41         }
42         public void dispose() {
43             // Nothing to do as contents are owned by the TargetSynchronizer
44
}
45         public byte[] getBytes(IResource resource) throws TeamException {
46             return getSynchronizer().getRemoteBytes(resource);
47         }
48         public boolean setBytes(IResource resource, byte[] bytes) throws TeamException {
49             return getSynchronizer().setRemoteBytes(resource, bytes);
50         }
51         public boolean flushBytes(IResource resource, int depth) throws TeamException {
52             // This method is invoked when the remote bytes are stale and should be removed
53
// This is handled by the ThreeWaySynchronizer so nothing needs to be done here.
54
return false;
55         }
56         public boolean isVariantKnown(IResource resource) throws TeamException {
57             return getSynchronizer().hasSyncBytes(resource);
58         }
59         public boolean deleteBytes(IResource resource) throws TeamException {
60             return getSynchronizer().removeRemoteBytes(resource);
61         }
62         public IResource[] members(IResource resource) throws TeamException {
63             return synchronizer.members(resource);
64         }
65         private ThreeWaySynchronizer getSynchronizer() {
66             return synchronizer;
67         }
68     }
69     
70     /**
71      * Create a remote resource variant tree that stores and obtains
72      * it's bytes from the remote slot of the synchronizer of the
73      * given subscriber
74      * @param subscriber a three-way subscriber
75      */

76     public ThreeWayRemoteTree(ThreeWaySubscriber subscriber) {
77         super(new RemoteResourceVariantByteStore(subscriber.getSynchronizer()));
78         this.subscriber = subscriber;
79     }
80     
81     /* (non-Javadoc)
82      * @see org.eclipse.team.internal.core.subscribers.caches.IResourceVariantTree#roots()
83      */

84     public IResource[] roots() {
85         return getSubscriber().roots();
86     }
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.team.internal.core.subscribers.caches.IResourceVariantTree#getResourceVariant(org.eclipse.core.resources.IResource)
90      */

91     public IResourceVariant getResourceVariant(IResource resource) throws TeamException {
92         return getSubscriber().getResourceVariant(resource, getByteStore().getBytes(resource));
93     }
94
95     /**
96      * Return the subscriber associated with this resource variant tree.
97      * @return the subscriber associated with this resource variant tree
98      */

99     protected ThreeWaySubscriber getSubscriber() {
100         return subscriber;
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.team.core.variants.AbstractResourceVariantTree#collectChanges(org.eclipse.core.resources.IResource, org.eclipse.team.core.variants.IResourceVariant, int, org.eclipse.core.runtime.IProgressMonitor)
105      */

106     protected IResource[] collectChanges(final IResource local,
107             final IResourceVariant remote, final int depth, IProgressMonitor monitor)
108             throws TeamException {
109         final IResource[][] resources = new IResource[][] { null };
110         getSubscriber().getSynchronizer().run(local, new IWorkspaceRunnable() {
111             public void run(IProgressMonitor monitor) throws CoreException {
112                 resources[0] = ThreeWayRemoteTree.super.collectChanges(local, remote, depth, monitor);
113             }
114         }, monitor);
115         return resources[0];
116     }
117 }
118
Popular Tags