KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > subscribers > SubscriberMergeContext


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.subscribers;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IWorkspaceRunnable;
15 import org.eclipse.core.resources.mapping.ResourceTraversal;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.team.core.diff.DiffFilter;
18 import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
19 import org.eclipse.team.core.mapping.provider.MergeContext;
20 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
21 import org.eclipse.team.core.synchronize.SyncInfo;
22 import org.eclipse.team.internal.core.mapping.GroupProgressMonitor;
23 import org.eclipse.team.internal.core.subscribers.SubscriberDiffTreeEventHandler;
24
25 /**
26  * A merge context that uses a subscriber to populate the diff tree
27  * used by the context. The population of the diff tree is performed
28  * by a handler that runs in a background job.
29  *
30  * @see Subscriber
31  * @see MergeContext
32  *
33  * @since 3.2
34  */

35 public abstract class SubscriberMergeContext extends MergeContext {
36
37     private Subscriber subscriber;
38     private SubscriberDiffTreeEventHandler handler;
39     private final ISynchronizationScopeManager manager;
40     
41     /**
42      * Create a merge context for the given subscriber
43      * @param subscriber the subscriber
44      * @param manager the scope manager
45      */

46     protected SubscriberMergeContext(Subscriber subscriber, ISynchronizationScopeManager manager) {
47         super(manager, getType(subscriber), new ResourceDiffTree());
48         this.subscriber = subscriber;
49         this.manager = manager;
50     }
51
52     private static int getType(Subscriber subscriber) {
53         return subscriber.getResourceComparator().isThreeWay()
54             ? THREE_WAY : TWO_WAY;
55     }
56
57     /**
58      * Initialize the diff tree of this context. This method must
59      * be called before the context is given to clients.
60      */

61     protected void initialize() {
62         handler = new SubscriberDiffTreeEventHandler(subscriber, manager, (ResourceDiffTree)getDiffTree(), getDiffFilter());
63         handler.setJobFamily(this);
64         handler.start();
65     }
66     
67     /**
68      * Return the diff filter used to filter the differences that the merge context will present to clients.
69      * @return the diff filter used to filter the differences that the merge context will present to clients
70      * @since 3.3
71      */

72     protected DiffFilter getDiffFilter() {
73         return null;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.team.core.mapping.ISynchronizationContext#refresh(org.eclipse.core.resources.mapping.ResourceTraversal[], int, org.eclipse.core.runtime.IProgressMonitor)
78      */

79     public void refresh(ResourceTraversal[] traversals, int flags,
80             IProgressMonitor monitor) throws CoreException {
81         GroupProgressMonitor group = getGroup(monitor);
82         if (group != null)
83             handler.setProgressGroupHint(group.getGroup(), group.getTicks());
84         handler.initializeIfNeeded();
85         subscriber.refresh(traversals, monitor);
86     }
87     
88     private GroupProgressMonitor getGroup(IProgressMonitor monitor) {
89         if (monitor instanceof GroupProgressMonitor) {
90             return (GroupProgressMonitor) monitor;
91         }
92         if (monitor instanceof ProgressMonitorWrapper) {
93             ProgressMonitorWrapper wrapper = (ProgressMonitorWrapper) monitor;
94             return getGroup(wrapper.getWrappedProgressMonitor());
95         }
96         return null;
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.team.core.mapping.provider.SynchronizationContext#dispose()
101      */

102     public void dispose() {
103         handler.shutdown();
104         super.dispose();
105     }
106     
107     /**
108      * Return the sync info for the given resource.
109      * @param resource the resource
110      * @return the sync info for the resource obtained from the subscriber
111      * @throws CoreException
112      */

113     protected SyncInfo getSyncInfo(IResource resource) throws CoreException {
114         return handler.getSubscriber().getSyncInfo(resource);
115     }
116
117     /**
118      * Return the subscriber associated with this context.
119      * @return the subscriber associated with this context
120      */

121     public Subscriber getSubscriber() {
122         return subscriber;
123     }
124     
125     /**
126      * Run the given runnable when the background handler
127      * for this context is idle. The given runnable should not lock
128      * the workspace.
129      * @param runnable the runnable
130      */

131     protected void runInBackground(IWorkspaceRunnable runnable) {
132         handler.run(runnable, false);
133     }
134     
135     /* (non-Javadoc)
136      * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
137      */

138     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
139         if (adapter == SubscriberDiffTreeEventHandler.class)
140             return handler;
141         return super.getAdapter(adapter);
142     }
143
144 }
145
Popular Tags