KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ResourceModelLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.mapping;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.jface.viewers.*;
18 import org.eclipse.swt.graphics.Font;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.team.core.diff.*;
21 import org.eclipse.team.core.mapping.IResourceDiffTree;
22 import org.eclipse.team.core.mapping.ISynchronizationContext;
23 import org.eclipse.team.internal.ui.*;
24 import org.eclipse.team.internal.ui.synchronize.ImageManager;
25 import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
26 import org.eclipse.team.ui.mapping.SynchronizationLabelProvider;
27 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
28 import org.eclipse.ui.model.WorkbenchLabelProvider;
29 import org.eclipse.ui.navigator.ICommonContentExtensionSite;
30
31 /**
32  * Resource label provider that can decorate using sync state.
33  */

34 public class ResourceModelLabelProvider extends
35         SynchronizationLabelProvider implements IFontProvider, IResourceChangeListener, ITreePathLabelProvider {
36
37     public static final FastDiffFilter CONFLICT_FILTER = new FastDiffFilter() {
38         public boolean select(IDiff diff) {
39             if (diff instanceof IThreeWayDiff) {
40                 IThreeWayDiff twd = (IThreeWayDiff) diff;
41                 return twd.getDirection() == IThreeWayDiff.CONFLICTING;
42             }
43             return false;
44         }
45     };
46     
47     private ILabelProvider provider;
48     private ResourceModelContentProvider contentProvider;
49     private ImageManager localImageManager;
50
51     public void init(ICommonContentExtensionSite site) {
52         ITreeContentProvider aContentProvider = site.getExtension().getContentProvider();
53         if (aContentProvider instanceof ResourceModelContentProvider) {
54             contentProvider = (ResourceModelContentProvider) aContentProvider;
55             ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
56         }
57         super.init(site);
58     }
59     
60     public void dispose() {
61         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
62         if (localImageManager != null)
63             localImageManager.dispose();
64         if (provider != null)
65             provider.dispose();
66         super.dispose();
67     }
68
69     /* (non-Javadoc)
70      * @see org.eclipse.team.internal.ui.mapping.SynchronizationOperationLabelProvider#getBaseLabelProvider()
71      */

72     protected ILabelProvider getDelegateLabelProvider() {
73         if (provider == null)
74             provider = new WorkbenchLabelProvider();
75         return provider ;
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.team.ui.mapping.AbstractSynchronizationLabelProvider#getSyncDelta(java.lang.Object)
80      */

81     protected IDiff getDiff(Object JavaDoc elementOrPath) {
82         IResource resource = getResource(elementOrPath);
83         IResourceDiffTree tree = getDiffTree(elementOrPath);
84         if (tree != null && resource != null) {
85             IDiff delta = tree.getDiff(resource.getFullPath());
86             return delta;
87         }
88         return null;
89     }
90
91     private IResource getResource(Object JavaDoc elementOrPath) {
92         Object JavaDoc element = internalGetElement(elementOrPath);
93         if (element instanceof IResource) {
94             return (IResource) element;
95         }
96         return null;
97     }
98     
99     /* (non-Javadoc)
100      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeLabelProvider#isIncludeOverlays()
101      */

102     protected boolean isIncludeOverlays() {
103         return true;
104     }
105     
106     /* (non-Javadoc)
107      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeLabelProvider#isBusy(java.lang.Object)
108      */

109     protected boolean isBusy(Object JavaDoc elementOrPath) {
110         IResource resource = getResource(elementOrPath);
111         IResourceDiffTree tree = getDiffTree(elementOrPath);
112         if (tree != null && resource != null) {
113             return tree.getProperty(resource.getFullPath(), IDiffTree.P_BUSY_HINT);
114         }
115         return super.isBusy(elementOrPath);
116     }
117     
118     private TreePath internalGetPath(Object JavaDoc elementOrPath) {
119         if (elementOrPath instanceof TreePath) {
120             return (TreePath) elementOrPath;
121         }
122         return null;
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeLabelProvider#hasDecendantConflicts(java.lang.Object)
127      */

128     protected boolean hasDecendantConflicts(Object JavaDoc elementOrPath) {
129         IResource resource = getResource(elementOrPath);
130         IResourceDiffTree tree = getDiffTree(elementOrPath);
131         if (tree != null && resource != null) {
132             int depth = getTraversalCalculator().getLayoutDepth(resource, internalGetPath(elementOrPath));
133             if (depth == IResource.DEPTH_INFINITE || resource.getType() == IResource.FILE)
134                 return tree.getProperty(resource.getFullPath(), IDiffTree.P_HAS_DESCENDANT_CONFLICTS);
135             return tree.hasMatchingDiffs(getTraversalCalculator().getTraversals(resource, internalGetPath(elementOrPath)), CONFLICT_FILTER);
136         }
137         return super.hasDecendantConflicts(elementOrPath);
138     }
139
140     protected IResourceDiffTree getDiffTree(Object JavaDoc elementOrPath) {
141         ISynchronizationContext context = getContext();
142         if (context != null)
143             return context.getDiffTree();
144         return null;
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
149      */

150     public void resourceChanged(IResourceChangeEvent event) {
151         String JavaDoc[] markerTypes = new String JavaDoc[] {IMarker.PROBLEM};
152         final Set JavaDoc handledResources = new HashSet JavaDoc();
153         
154         // Accumulate all distinct resources that have had problem marker
155
// changes
156
for (int idx = 0; idx < markerTypes.length; idx++) {
157             IMarkerDelta[] markerDeltas = event.findMarkerDeltas(markerTypes[idx], true);
158                 for (int i = 0; i < markerDeltas.length; i++) {
159                     IMarkerDelta delta = markerDeltas[i];
160                     IResource resource = delta.getResource();
161                     while (resource != null && resource.getType() != IResource.ROOT && !handledResources.contains(resource)) {
162                         handledResources.add(resource);
163                         resource = resource.getParent();
164                     }
165                 }
166             }
167         
168         if (!handledResources.isEmpty()) {
169             final IResource[] resources = (IResource[]) handledResources.toArray(new IResource[handledResources.size()]);
170             updateLabels(resources);
171         }
172     }
173
174     protected void updateLabels(final Object JavaDoc[] resources) {
175         Utils.asyncExec(new Runnable JavaDoc() {
176             public void run() {
177                 contentProvider.getStructuredViewer().update(
178                         resources, null);
179             }
180         }, contentProvider.getStructuredViewer());
181     }
182     
183     protected String JavaDoc getDelegateText(Object JavaDoc elementOrPath) {
184         if (getConfiguration() != null) {
185             String JavaDoc label = getTraversalCalculator().getLabel(elementOrPath);
186             if (label != null)
187                 return label;
188         }
189         return super.getDelegateText(internalGetElement(elementOrPath));
190     }
191     
192     protected Image getDelegateImage(Object JavaDoc elementOrPath) {
193         if (getConfiguration() != null && getTraversalCalculator().isCompressedFolder(elementOrPath)) {
194             return getImageManager().getImage(TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_COMPRESSED_FOLDER));
195         }
196         return super.getDelegateImage(internalGetElement(elementOrPath));
197     }
198     
199     private Object JavaDoc internalGetElement(Object JavaDoc elementOrPath) {
200         if (elementOrPath instanceof TreePath) {
201             TreePath tp = (TreePath) elementOrPath;
202             return tp.getLastSegment();
203         }
204         return elementOrPath;
205     }
206     
207     protected ResourceModelTraversalCalculator getTraversalCalculator() {
208         return ResourceModelTraversalCalculator.getTraversalCalculator(getConfiguration());
209     }
210
211     private ISynchronizePageConfiguration getConfiguration() {
212         return (ISynchronizePageConfiguration)getExtensionSite().getExtensionStateModel().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_PAGE_CONFIGURATION);
213     }
214     
215     public void updateLabel(ViewerLabel label, TreePath elementPath) {
216         label.setImage(getImage(elementPath));
217         label.setText(getText(elementPath));
218         Font f = getFont(elementPath);
219         if (f != null)
220             label.setFont(f);
221     }
222     
223     protected ImageManager getImageManager() {
224         ISynchronizationContext context = getContext();
225         if (context != null) {
226             return ImageManager.getImageManager(context);
227         }
228         if (localImageManager == null) {
229             localImageManager = new ImageManager();
230         }
231         return localImageManager;
232     }
233
234 }
235
Popular Tags