KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > mapping > SynchronizationLabelProvider


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.ui.mapping;
12
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.resources.mapping.ModelProvider;
15 import org.eclipse.jface.viewers.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.team.core.mapping.ISynchronizationContext;
19 import org.eclipse.team.core.mapping.ISynchronizationScope;
20 import org.eclipse.team.internal.ui.TeamUIMessages;
21 import org.eclipse.team.ui.synchronize.AbstractSynchronizeLabelProvider;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.navigator.*;
24
25 /**
26  * A label provider wrapper that adds synchronization image and/or text decorations
27  * to the image and label obtained from the delegate provider.
28  *
29  * @since 3.2
30  */

31 public abstract class SynchronizationLabelProvider extends AbstractSynchronizeLabelProvider implements ICommonLabelProvider, IFontProvider {
32
33     private ISynchronizationScope scope;
34     private ISynchronizationContext context;
35     private ITreeContentProvider contentProvider;
36     private ICommonContentExtensionSite site;
37     
38     private void init(ISynchronizationScope input, ISynchronizationContext context) {
39         this.scope = input;
40         this.context = context;
41     }
42     
43     /* (non-Javadoc)
44      * @see org.eclipse.ui.navigator.ICommonLabelProvider#init(org.eclipse.ui.navigator.ICommonContentExtensionSite)
45      */

46     public void init(ICommonContentExtensionSite site) {
47         this.site = site;
48         contentProvider = site.getExtension().getContentProvider();
49         init((ISynchronizationScope)site.getExtensionStateModel().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_SCOPE),
50                 (ISynchronizationContext)site.getExtensionStateModel().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT));
51     }
52
53     /**
54      * Return the synchronization context associated with the view to which
55      * this label provider applies. A <code>null</code> is returned if
56      * no context is available.
57      * @return the synchronization context or <code>null</code>
58      */

59     public ISynchronizationContext getContext() {
60         return context;
61     }
62
63     /**
64      * Return the resource mapping scope associated with the view to which
65      * this label provider applies. A <code>null</code> is returned if
66      * no scope is available.
67      * @return the resource mapping scope or <code>null</code>
68      */

69     public ISynchronizationScope getScope() {
70         return scope;
71     }
72     
73     /* (non-Javadoc)
74      * @see org.eclipse.ui.navigator.IMementoAware#restoreState(org.eclipse.ui.IMemento)
75      */

76     public void restoreState(IMemento aMemento) {
77         ILabelProvider provider = getDelegateLabelProvider();
78         if (provider instanceof ICommonLabelProvider) {
79             ((ICommonLabelProvider) provider).restoreState(aMemento);
80         }
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.ui.navigator.IMementoAware#saveState(org.eclipse.ui.IMemento)
85      */

86     public void saveState(IMemento aMemento) {
87         ILabelProvider provider = getDelegateLabelProvider();
88         if (provider instanceof ICommonLabelProvider) {
89             ((ICommonLabelProvider) provider).saveState(aMemento);
90         }
91     }
92     
93     /* (non-Javadoc)
94      * @see org.eclipse.ui.navigator.IDescriptionProvider#getDescription(java.lang.Object)
95      */

96     public String JavaDoc getDescription(Object JavaDoc anElement) {
97         ILabelProvider provider = getDelegateLabelProvider();
98         if (provider instanceof IDescriptionProvider) {
99             return ((IDescriptionProvider) provider).getDescription(internalGetElement(anElement));
100         }
101         return null;
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.team.internal.ui.mapping.SynchronizationStateLabelProvider#isDecorationEnabled()
106      */

107     protected boolean isDecorationEnabled() {
108         return getContext() != null;
109     }
110     
111     /* (non-Javadoc)
112      * @see org.eclipse.team.internal.ui.mapping.SynchronizationStateLabelProvider#getImage(java.lang.Object)
113      */

114     public Image getImage(Object JavaDoc element) {
115         Image image = super.getImage(element);
116         if (image == null && internalGetElement(element) instanceof ModelProvider) {
117             image = super.getImage(getModelRoot());
118         }
119         return image;
120     }
121     
122     /**
123      * Return the root object for the model. By default, it is the
124      * workspace root. Subclasses may override. This object is used to
125      * obtain an image for the model provider.
126      * @return the root object for the model
127      */

128     protected Object JavaDoc getModelRoot() {
129         return ResourcesPlugin.getWorkspace().getRoot();
130     }
131     
132     /* (non-Javadoc)
133      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeLabelProvider#getText(java.lang.Object)
134      */

135     public String JavaDoc getText(Object JavaDoc element) {
136         String JavaDoc text = super.getText(element);
137         if (contentProvider instanceof SynchronizationContentProvider) {
138             SynchronizationContentProvider scp = (SynchronizationContentProvider) contentProvider;
139             ISynchronizationContext context = getContext();
140             if (context != null && !scp.isInitialized(context)) {
141                 return NLS.bind(TeamUIMessages.SynchronizationLabelProvider_0, text);
142             }
143         }
144         return text;
145     }
146
147     /**
148      * Return the Common Navigator extension site for this
149      * label provider.
150      * @return the Common Navigator extension site for this
151      * label provider
152      */

153     public ICommonContentExtensionSite getExtensionSite() {
154         return site;
155     }
156     
157     private Object JavaDoc internalGetElement(Object JavaDoc element) {
158         if (element instanceof TreePath) {
159             TreePath tp = (TreePath) element;
160             element = tp.getLastSegment();
161         }
162         return element;
163     }
164 }
165
Popular Tags