KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > SubscriberTeamStateProvider


1 /*******************************************************************************
2  * Copyright (c) 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.ui.synchronize;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.mapping.*;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.team.core.RepositoryProviderType;
19 import org.eclipse.team.core.diff.IDiff;
20 import org.eclipse.team.core.subscribers.*;
21 import org.eclipse.team.internal.ui.Policy;
22 import org.eclipse.team.internal.ui.Utils;
23 import org.eclipse.team.ui.mapping.*;
24
25 /**
26  * A team state provider that makes use of a {@link Subscriber} to determine the synchronization
27  * state. Repository provider types that have a subscriber will get one of these free through the adaptable mechanism.
28  * If a repository provider type does not have a subscriber, or it a repository provider type wishes to se a custom
29  * provider, they must adapt their {@link RepositoryProviderType} class to an appropriate {@link ITeamStateProvider}.
30  * <p>
31  * Clients may subclass this class.
32  *
33  * @since 3.2
34  */

35 public class SubscriberTeamStateProvider extends TeamStateProvider implements ISubscriberChangeListener {
36
37     private Subscriber subscriber;
38
39     /**
40      * Create a provider that determines the synchronization state
41      * from the subscriber. This method registers this provider as a listener
42      * on the subscriber in order to know when to fire state change events.
43      * @param subscriber the subscriber for this provider
44      */

45     public SubscriberTeamStateProvider(Subscriber subscriber) {
46         this.subscriber = subscriber;
47         subscriber.addListener(this);
48     }
49
50     /* (non-Javadoc)
51      * @see org.eclipse.team.ui.mapping.DecoratedStateProvider#isDecorated(java.lang.Object)
52      */

53     public boolean hasDecoratedState(Object JavaDoc element) throws CoreException {
54         ResourceMapping mapping = Utils.getResourceMapping(element);
55         if (mapping != null) {
56             ResourceTraversal[] traversals = mapping.getTraversals(
57                     ResourceMappingContext.LOCAL_CONTEXT, null);
58             for (int i = 0; i < traversals.length; i++) {
59                 ResourceTraversal traversal = traversals[i];
60                 IResource[] resources = traversal.getResources();
61                 for (int j = 0; j < resources.length; j++) {
62                     IResource resource = resources[j];
63                     if (getSubscriber().isSupervised(resource))
64                         return true;
65                 }
66             }
67         }
68         return false;
69     }
70
71     /**
72      * Obtain the synchronization state of the element. If the model
73      * provider for the element adapts to an
74      * ISynchronizationCompareAdapter, then the adapter is used to determine the
75      * synchronization state. Others, the state is obtained from the subscriber
76      * using {@link Subscriber#getState(ResourceMapping, int, IProgressMonitor)}
77      *
78      * @param element the element
79      * @param stateMask the state mask that indicates which state flags are desired
80      * @param monitor a progress monitor
81      * @return the synchronization state of the element
82      * @throws CoreException
83      */

84     protected final int getSynchronizationState(Object JavaDoc element, int stateMask,
85             IProgressMonitor monitor) throws CoreException {
86         ResourceMapping mapping = Utils.getResourceMapping(element);
87         if (mapping != null) {
88             return getSynchronizationState(mapping, stateMask, monitor);
89         }
90         return IDiff.NO_CHANGE;
91     }
92     
93     private int getSynchronizationState(ResourceMapping mapping, int stateMask, IProgressMonitor monitor) throws CoreException {
94         ISynchronizationCompareAdapter compareAdapter = (ISynchronizationCompareAdapter)Utils.getAdapter(mapping.getModelProvider(), ISynchronizationCompareAdapter.class);
95         try {
96             if (compareAdapter != null) {
97                 int state = compareAdapter.getSynchronizationState(this, mapping, stateMask, monitor);
98                 if (state != -1)
99                     return state;
100             }
101             return getSubscriber().getState(mapping, stateMask, monitor);
102         } catch (CoreException e) {
103             IProject[] projects = mapping.getProjects();
104             for (int i = 0; i < projects.length; i++) {
105                 IProject project = projects[i];
106                 // Only through the exception if the project for the mapping
107
// is accessible
108
if (project.isAccessible()) {
109                     throw e;
110                 }
111             }
112         }
113         return IDiff.NO_CHANGE;
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#getStateDescription(java.lang.Object, int, java.lang.String[], org.eclipse.core.runtime.IProgressMonitor)
118      */

119     public ITeamStateDescription getStateDescription(Object JavaDoc element, int stateMask,
120             String JavaDoc[] properties, IProgressMonitor monitor) throws CoreException {
121         monitor = Policy.monitorFor(monitor);
122         if (stateMask == USE_DECORATED_STATE_MASK)
123             stateMask = getDecoratedStateMask(element);
124         return new TeamStateDescription(getSynchronizationState(element, stateMask, monitor));
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#getResourceMappingContext(java.lang.Object)
129      */

130     public ResourceMappingContext getResourceMappingContext(Object JavaDoc element) {
131         return new SubscriberResourceMappingContext(subscriber, false);
132     }
133     
134     /**
135      * Return the subscriber associated with this tester.
136      *
137      * @return the subscriber associated with this tester.
138      */

139     protected final Subscriber getSubscriber() {
140         return subscriber;
141     }
142
143     /**
144      * Called when the provider is no longer needed. This method stops listening
145      * to the subscriber. Subclasses may extend this method but must call this
146      * method if they do.
147      */

148     public void dispose() {
149         subscriber.removeListener(this);
150     }
151
152     /* (non-Javadoc)
153      * @see org.eclipse.team.core.subscribers.ISubscriberChangeListener#subscriberResourceChanged(org.eclipse.team.core.subscribers.ISubscriberChangeEvent[])
154      */

155     public void subscriberResourceChanged(ISubscriberChangeEvent[] deltas) {
156         fireStateChangeEvent(new TeamStateChangeEvent(deltas));
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#getDecoratedProperties(java.lang.Object)
161      */

162     public String JavaDoc[] getDecoratedProperties(Object JavaDoc element) {
163         return new String JavaDoc[0];
164     }
165 }
166
Popular Tags