KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > SubscriberSyncInfoCollector


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.internal.core.subscribers;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.jobs.Job;
16 import org.eclipse.team.core.subscribers.Subscriber;
17 import org.eclipse.team.core.synchronize.*;
18 import org.eclipse.team.internal.core.Policy;
19
20 /**
21  * This collector maintains a {@link SyncInfoSet} for a particular team subscriber keeping
22  * it up-to-date with both incoming changes and outgoing changes as they occur for
23  * resources in the workspace. The collector can be configured to consider all the subscriber's
24  * roots or only a subset.
25  * <p>
26  * The advantage of this collector is that it processes both resource and team
27  * subscriber deltas in a background thread.
28  * </p>
29  * @since 3.0
30  */

31 public final class SubscriberSyncInfoCollector extends SubscriberResourceCollector {
32
33     private final SyncSetInputFromSubscriber subscriberInput;
34     private SyncSetInputFromSyncSet filteredInput;
35     private SubscriberSyncInfoEventHandler eventHandler;
36     private IResource[] roots;
37     
38     /**
39      * Create a collector that collects out-of-sync resources that are children of
40      * the given roots. If the roots are <code>null</code>, then all out-of-sync resources
41      * from the subscriber are collected. An empty array of roots will cause no resources
42      * to be collected. The <code>start()</code> method must be called after creation
43      * to prime the collector's sync sets.
44      * @param subscriber the Subscriber
45      * @param roots the roots of the out-of-sync resources to be collected
46      */

47     public SubscriberSyncInfoCollector(Subscriber subscriber, IResource[] roots) {
48         super(subscriber);
49         this.roots = roots;
50         this.eventHandler = new SubscriberSyncInfoEventHandler(subscriber, roots);
51         this.subscriberInput = eventHandler.getSyncSetInput();
52         filteredInput = new SyncSetInputFromSyncSet(subscriberInput.getSyncSet(), getEventHandler());
53         filteredInput.setFilter(new SyncInfoFilter() {
54             public boolean select(SyncInfo info, IProgressMonitor monitor) {
55                 return true;
56             }
57         });
58
59     }
60     
61     public void setProgressGroup(IProgressMonitor monitor, int ticks) {
62         getEventHandler().setProgressGroupHint(monitor, ticks);
63     }
64     
65     /**
66      * Start the collector.
67      */

68     public void start() {
69         eventHandler.start();
70     }
71
72     /**
73      * This causes the calling thread to wait any background collection of out-of-sync resources
74      * to stop before returning.
75      * @param monitor a progress monitor
76      */

77     public void waitForCollector(IProgressMonitor monitor) {
78         monitor.worked(1);
79         // wait for the event handler to process changes.
80
while(eventHandler.getEventHandlerJob().getState() != Job.NONE) {
81             monitor.worked(1);
82             try {
83                 Thread.sleep(10);
84             } catch (InterruptedException JavaDoc e) {
85             }
86             Policy.checkCanceled(monitor);
87         }
88         monitor.worked(1);
89     }
90     
91     /**
92      * Clears this collector's sync info sets and causes them to be recreated from the
93      * associated <code>Subscriber</code>. The reset will occur in the background. If the
94      * caller wishes to wait for the reset to complete, they should call
95      * waitForCollector(IProgressMonitor).
96      */

97     public void reset() {
98         eventHandler.reset(getRoots());
99     }
100
101     /**
102      * Disposes of the background job associated with this collector and deregisters
103      * all it's listeners. This method must be called when the collector is no longer
104      * referenced and could be garbage collected.
105      */

106     public void dispose() {
107         eventHandler.shutdown();
108         subscriberInput.disconnect();
109         if(filteredInput != null) {
110             filteredInput.disconnect();
111         }
112         super.dispose();
113     }
114     
115     /**
116      * Return the roots that are being considered by this collector.
117      * By default, the collector is interested in the roots of its
118      * subscriber. However, the set can be reduced using {@link #setRoots(IResource[])}.
119      * @return the roots
120      */

121     public IResource[] getRoots() {
122         if (roots == null) {
123             return super.getRoots();
124         } else {
125             return roots;
126         }
127     }
128     
129     /*
130      * Returns whether the collector is configured to collect for
131      * all roots of the subscriber or not
132      * @return <code>true</code> if the collector is considering all
133      * roots of the subscriber and <code>false</code> otherwise
134      */

135     public boolean isAllRootsIncluded() {
136         return roots == null;
137     }
138     
139     /**
140      * Return the event handler that performs the background processing for this collector.
141      * The event handler also serves the purpose of serializing the modifications and adjustments
142      * to the collector's sync sets in order to ensure that the state of the sets is kept
143      * consistent.
144      * @return Returns the eventHandler.
145      */

146     protected SubscriberEventHandler getEventHandler() {
147         return eventHandler;
148     }
149     
150     /**
151      * Return the <code>SyncInfoSet</code> that contains all the all the out-of-sync resources for the
152      * subscriber that are descendants of the roots of this collector. The set will contain only those resources that are children of the roots
153      * of the collector unless the roots of the colletor has been set to <code>null</code>
154      * in which case all out-of-sync resources from the subscriber are collected.
155      * @return the subscriber sync info set
156      */

157     public SyncInfoTree getSubscriberSyncInfoSet() {
158         return subscriberInput.getSyncSet();
159     }
160     
161     public SyncInfoTree getSyncInfoSet() {
162         return filteredInput.getSyncSet();
163     }
164     
165     /**
166      * Set the filter for this collector. Only elements that match the filter will
167      * be in the out sync info set.
168      * @param filter the sync info filter
169      */

170     public void setFilter(SyncInfoFilter filter) {
171         filteredInput.setFilter(filter);
172         filteredInput.reset();
173     }
174
175     public void setRoots(IResource[] roots) {
176         this.roots = roots;
177         reset();
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.team.internal.core.subscribers.SubscriberResourceCollector#hasMembers(org.eclipse.core.resources.IResource)
182      */

183     protected boolean hasMembers(IResource resource) {
184         return getSubscriberSyncInfoSet().hasMembers(resource);
185     }
186
187     /* (non-Javadoc)
188      * @see org.eclipse.team.internal.core.subscribers.SubscriberResourceCollector#remove(org.eclipse.core.resources.IResource)
189      */

190     protected void remove(IResource resource) {
191         eventHandler.remove(resource);
192     }
193
194     /* (non-Javadoc)
195      * @see org.eclipse.team.internal.core.subscribers.SubscriberResourceCollector#change(org.eclipse.core.resources.IResource, int)
196      */

197     protected void change(IResource resource, int depth) {
198         eventHandler.change(resource, depth);
199     }
200 }
201
Popular Tags