KickJava   Java API By Example, From Geeks To Geeks.

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


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.runtime.IPath;
15 import org.eclipse.team.core.Team;
16 import org.eclipse.team.core.TeamException;
17 import org.eclipse.team.core.subscribers.ISubscriberChangeEvent;
18 import org.eclipse.team.core.subscribers.SubscriberChangeEvent;
19 import org.eclipse.team.internal.core.TeamPlugin;
20 import org.eclipse.team.internal.core.subscribers.ThreeWayBaseTree;
21
22 /**
23  * A resource variant tree subscriber whose trees use an underlying
24  * <code>ThreeWaySycnrhonizer</code> to store and manage the
25  * synchronization state for the local workspace. Subclasses need to
26  * provide a subclass of <code>ThreeWayRemoteTree</code> and a method
27  * to create resource variant handles from the bytes cached in the
28  * <code>ThreeWaySynchronizer</code>.
29  *
30  * @see ThreeWaySynchronizer
31  * @see ThreeWayRemoteTree
32  * @see CachedResourceVariant
33  *
34  * @since 3.0
35  */

36 public abstract class ThreeWaySubscriber extends ResourceVariantTreeSubscriber implements ISynchronizerChangeListener {
37     
38     private ThreeWayResourceComparator comparator;
39     private ThreeWayBaseTree baseTree;
40     private ThreeWayRemoteTree remoteTree;
41     private ThreeWaySynchronizer synchronizer;
42     
43     /**
44      * Create a three-way subscriber that uses the given synchronizer
45      * to manage the synchronization state of local resources
46      * and their variants
47      * @param synchronizer the three-way synchronizer for this subscriber
48      */

49     protected ThreeWaySubscriber(ThreeWaySynchronizer synchronizer) {
50         this.synchronizer = synchronizer;
51         baseTree = new ThreeWayBaseTree(this);
52         getSynchronizer().addListener(this);
53     }
54     
55     /* (non-Javadoc)
56      * @see org.eclipse.team.core.variants.ResourceVariantTreeSubscriber#getBaseTree()
57      */

58     protected final IResourceVariantTree getBaseTree() {
59         return baseTree;
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.team.core.variants.ResourceVariantTreeSubscriber#getRemoteTree()
64      */

65     protected final IResourceVariantTree getRemoteTree() {
66         if (remoteTree == null) {
67             remoteTree = createRemoteTree();
68         }
69         return remoteTree;
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.team.core.subscribers.Subscriber#getResourceComparator()
74      */

75     public final IResourceVariantComparator getResourceComparator() {
76         if (comparator == null) {
77             comparator = new ThreeWayResourceComparator(this.getSynchronizer());
78         }
79         return comparator;
80     }
81     
82     /* (non-Javadoc)
83      * @see org.eclipse.team.core.variants.ISynchronizerChangeListener#syncStateChanged(org.eclipse.core.resources.IResource[])
84      */

85     public void syncStateChanged(IResource[] resources) {
86         fireTeamResourceChange(SubscriberChangeEvent.asSyncChangedDeltas(this, resources));
87     }
88     
89     /**
90      * Returns <code>false</code> for resources that are not children
91      * of a subscriber root, are ignored by the subscriber's synchronizer
92      * or are ignored by the <code>Team.ignoreHist(IResource)</code>. Returns
93      * <code>true</code> otherwise.
94      * @see org.eclipse.team.core.subscribers.Subscriber#isSupervised(IResource)
95      */

96     public boolean isSupervised(IResource resource) throws TeamException {
97         if (!isChildOfRoot(resource)) return false;
98         if (getSynchronizer().isIgnored(resource)) return false;
99         if (Team.isIgnoredHint(resource)) return false;
100         return true;
101     }
102     
103     /**
104      * Return the three-way synchronizer of this subscriber.
105      * @return the three-way synchronizer of this subscriber.
106      */

107     public ThreeWaySynchronizer getSynchronizer() {
108         return synchronizer;
109     }
110     
111     /**
112      * Create the resource variant for the given local resource from the
113      * given bytes. The bytes are those that were previously returned
114      * from a call to <code>IResourceVariant#asBytes()</code>.
115      * @param resource the local resource
116      * @param bytes the bytes that identify a variant of the resource
117      * @return the resource variant handle recreated from the bytes
118      * @throws TeamException
119      */

120     public abstract IResourceVariant getResourceVariant(IResource resource, byte[] bytes) throws TeamException;
121     
122     /**
123      * Create the three-way remote tree which provides access to the
124      * remote bytes in the three-way synchronizer. This method is invoked
125      * once when the remote tree is first accessed. The returned object is
126      * cached and reused on subsequent accesses.
127      * @return the remote tree
128      */

129     protected abstract ThreeWayRemoteTree createRemoteTree();
130     
131     /**
132      * Convenience method that can be used by subclasses to notify listeners
133      * when a root is added or removed from the subscriber. The added
134      * parameter should be <code>true</code> if the root was added and <code>false</code>
135      * if it was removed.
136      * @param resource the added or removed root
137      * @param added <code>true</code> if the root was added and <code>false</code>
138      * if it was removed.
139      */

140     protected void handleRootChanged(IResource resource, boolean added) {
141         if (added) {
142             rootAdded(resource);
143         } else {
144             rootRemoved(resource);
145         }
146     }
147     
148     private void rootAdded(IResource resource) {
149         SubscriberChangeEvent delta = new SubscriberChangeEvent(this, ISubscriberChangeEvent.ROOT_ADDED, resource);
150         fireTeamResourceChange(new SubscriberChangeEvent[] { delta });
151     }
152
153     private void rootRemoved(IResource resource) {
154         try {
155             getSynchronizer().flush(resource, IResource.DEPTH_INFINITE);
156         } catch (TeamException e) {
157             TeamPlugin.log(e);
158         }
159         SubscriberChangeEvent delta = new SubscriberChangeEvent(this, ISubscriberChangeEvent.ROOT_REMOVED, resource);
160         fireTeamResourceChange(new SubscriberChangeEvent[] { delta });
161     }
162     
163     private boolean isChildOfRoot(IResource resource) {
164         IResource[] roots = roots();
165         IPath fullPath = resource.getFullPath();
166         for (int i = 0; i < roots.length; i++) {
167             IResource root = roots[i];
168             if (root.getFullPath().isPrefixOf(fullPath)) {
169                 return true;
170             }
171         }
172         return false;
173     }
174 }
175
Popular Tags