KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.mapping.ResourceMapping;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.team.core.RepositoryProvider;
20 import org.eclipse.team.core.RepositoryProviderType;
21 import org.eclipse.team.internal.ui.Utils;
22 import org.eclipse.team.internal.ui.registry.TeamDecoratorDescription;
23 import org.eclipse.team.internal.ui.registry.TeamDecoratorManager;
24 import org.eclipse.team.ui.mapping.*;
25 import org.eclipse.ui.PlatformUI;
26
27 /**
28  * A team state provider is used by the {@link SynchronizationStateTester} to obtain
29  * the team state for model elements. A team state provider is
30  * associated with a {@link RepositoryProviderType} using the adaptable mechanism. A default
31  * team state provider that uses the subscriber of the type is provided.
32  * <p>
33  * Clients may subclass this class.
34  *
35  * @see IAdapterManager
36  * @see RepositoryProviderType
37  * @since 3.2
38  */

39 public abstract class TeamStateProvider implements ITeamStateProvider {
40
41     private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
42     
43     /**
44      * Determine if the decorator for the element is enabled by consulting the
45      * <code>teamDecorator</code> extension point to determine the decorator
46      * id associated with the resources the element maps to. Subclasses may
47      * override.
48      *
49      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#isDecorationEnabled(java.lang.Object)
50      */

51     public boolean isDecorationEnabled(Object JavaDoc element) {
52         ResourceMapping mapping = Utils.getResourceMapping(element);
53         if (mapping != null) {
54             IProject[] projects = mapping.getProjects();
55             return internalIsDecorationEnabled(projects);
56         }
57         return false;
58     }
59     
60     /**
61      * Determine the decorated state for the element by consulting the
62      * <code>teamDecorator</code> extension point to get the decorated state
63      * mask associated with the resources the element maps to. Subclasses may
64      * override.
65      *
66      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#getDecoratedStateMask(java.lang.Object)
67      */

68     public int getDecoratedStateMask(Object JavaDoc element) {
69         ResourceMapping mapping = Utils.getResourceMapping(element);
70         if (mapping != null) {
71             IProject[] projects = mapping.getProjects();
72             return internalGetDecoratedStateMask(projects);
73         }
74         return 0;
75     }
76     
77     /* (non-Javadoc)
78      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#addDecoratedStateChangeListener(org.eclipse.team.ui.mapping.ITeamStateChangeListener)
79      */

80     public void addDecoratedStateChangeListener(ITeamStateChangeListener listener) {
81         listeners.add(listener);
82     }
83     
84     /* (non-Javadoc)
85      * @see org.eclipse.team.ui.mapping.ITeamStateProvider#removeDecoratedStateChangeListener(org.eclipse.team.ui.mapping.ITeamStateChangeListener)
86      */

87     public void removeDecoratedStateChangeListener(ITeamStateChangeListener listener) {
88         listeners.remove(listener);
89     }
90     
91     /**
92      * Fire the change event to all listeners.
93      * @param event the change event
94      */

95     protected final void fireStateChangeEvent(final ITeamStateChangeEvent event) {
96         Object JavaDoc[] allListeners = listeners.getListeners();
97         for (int i = 0; i < allListeners.length; i++) {
98             final ITeamStateChangeListener listener = (ITeamStateChangeListener)allListeners[i];
99             SafeRunner.run(new ISafeRunnable() {
100                 public void run() throws Exception JavaDoc {
101                     listener.teamStateChanged(event);
102                 }
103                 public void handleException(Throwable JavaDoc exception) {
104                     // Logged by the runner
105
}
106             });
107         }
108     }
109     
110     private int internalGetDecoratedStateMask(IProject[] projects) {
111         int stateMask = 0;
112         String JavaDoc[] providerIds = getProviderIds(projects);
113         for (int i = 0; i < providerIds.length; i++) {
114             String JavaDoc providerId = providerIds[i];
115             stateMask |= internalGetDecoratedStateMask(providerId);
116         }
117         return stateMask;
118     }
119
120     private int internalGetDecoratedStateMask(String JavaDoc providerId) {
121         TeamDecoratorDescription decoratorDescription = TeamDecoratorManager
122                 .getInstance().getDecoratorDescription(providerId);
123         if (decoratorDescription != null)
124             return decoratorDescription.getDecoratedDirectionFlags();
125         return 0;
126     }
127
128     private String JavaDoc[] getProviderIds(IProject[] projects) {
129         Set JavaDoc providerIds = new HashSet JavaDoc();
130         for (int i = 0; i < projects.length; i++) {
131             IProject project = projects[i];
132             String JavaDoc id = getProviderId(project);
133             if (id != null)
134                 providerIds.add(id);
135         }
136         return (String JavaDoc[]) providerIds.toArray(new String JavaDoc[providerIds.size()]);
137     }
138
139     private String JavaDoc getProviderId(IProject project) {
140         RepositoryProvider provider = RepositoryProvider.getProvider(project);
141         if (provider != null)
142             return provider.getID();
143         return null;
144     }
145
146     private boolean internalIsDecorationEnabled(IProject[] projects) {
147         String JavaDoc[] providerIds = getProviderIds(projects);
148         for (int i = 0; i < providerIds.length; i++) {
149             String JavaDoc providerId = providerIds[i];
150             if (internalIsDecorationEnabled(providerId)) {
151                 return true;
152             }
153         }
154         return false;
155     }
156
157     private boolean internalIsDecorationEnabled(String JavaDoc providerId) {
158         String JavaDoc decoratorId = getDecoratorId(providerId);
159         if (decoratorId != null) {
160             return PlatformUI.getWorkbench().getDecoratorManager().getEnabled(
161                     decoratorId);
162         }
163         return false;
164     }
165
166     private String JavaDoc getDecoratorId(String JavaDoc providerId) {
167         TeamDecoratorDescription decoratorDescription = TeamDecoratorManager
168                 .getInstance().getDecoratorDescription(providerId);
169         if (decoratorDescription != null)
170             return decoratorDescription.getDecoratorId();
171         return null;
172     }
173
174 }
175
Popular Tags