KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > SynchronizeModelElementLabelProvider


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.internal.ui.synchronize;
12
13 import java.util.*;
14
15 import org.eclipse.compare.CompareConfiguration;
16 import org.eclipse.compare.structuremergeviewer.DiffNode;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.*;
23 import org.eclipse.team.core.synchronize.SyncInfo;
24 import org.eclipse.team.internal.ui.*;
25 import org.eclipse.team.ui.ISharedImages;
26 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
27 import org.eclipse.ui.model.WorkbenchLabelProvider;
28
29 /**
30  * A label provider that decorates viewers showing
31  * {@link ISynchronizeModelElement}.
32  *
33  * @since 3.0
34  */

35 public class SynchronizeModelElementLabelProvider extends LabelProvider implements IColorProvider, IFontProvider {
36
37     // Cache for folder images that have been overlayed with conflict icon
38
private Map fgImageCache;
39     
40     // Contains direction images
41
CompareConfiguration compareConfig = new CompareConfiguration();
42     
43     // Used as the base label provider for retreiving image and text from
44
// the workbench adapter.
45
private WorkbenchLabelProvider workbenchLabelProvider = new WorkbenchLabelProvider();
46     
47     // Font used to display busy elements
48
private Font busyFont;
49
50     public SynchronizeModelElementLabelProvider() {
51     }
52
53     /*
54      * (non-Javadoc)
55      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
56      */

57     public Color getForeground(Object JavaDoc element) {
58         return null;
59     }
60
61     /*
62      * (non-Javadoc)
63      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
64      */

65     public Color getBackground(Object JavaDoc element) {
66         return null;
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
71      */

72     public Font getFont(Object JavaDoc element) {
73         if (element instanceof ISynchronizeModelElement) {
74             ISynchronizeModelElement node = (ISynchronizeModelElement)element;
75             if(node.getProperty(ISynchronizeModelElement.BUSY_PROPERTY)) {
76                 if (busyFont == null) {
77                     Font defaultFont = JFaceResources.getDefaultFont();
78                     FontData[] data = defaultFont.getFontData();
79                     for (int i = 0; i < data.length; i++) {
80                         data[i].setStyle(SWT.ITALIC);
81                     }
82                     busyFont = new Font(TeamUIPlugin.getStandardDisplay(), data);
83                 }
84                 return busyFont;
85             }
86         }
87         return null;
88     }
89
90     /*
91      * (non-Javadoc)
92      * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
93      */

94     public Image getImage(Object JavaDoc element) {
95         Image base = workbenchLabelProvider.getImage(element);
96         if (base != null) {
97             if (element instanceof ISynchronizeModelElement) {
98                 ISynchronizeModelElement syncNode = (ISynchronizeModelElement) element;
99                 int kind = syncNode.getKind();
100                 Image decoratedImage;
101                 decoratedImage = getCompareImage(base, kind);
102                 // The reason we still overlay the compare image is to
103
// ensure that the image width for all images shown in the viewer
104
// are consistent.
105
return propagateConflicts(decoratedImage, syncNode);
106             }
107         }
108         return base;
109     }
110
111     /*
112      * (non-Javadoc)
113      * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
114      */

115     public String JavaDoc getText(Object JavaDoc element) {
116         String JavaDoc base = workbenchLabelProvider.getText(element);
117         if (element instanceof DiffNode) {
118             if (TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.SYNCVIEW_VIEW_SYNCINFO_IN_LABEL)) {
119                 // if the folder is already conflicting then don't bother
120
// propagating the conflict
121
int kind = ((DiffNode) element).getKind();
122                 if (kind != SyncInfo.IN_SYNC) {
123                     String JavaDoc syncKindString = SyncInfo.kindToString(kind);
124                     return NLS.bind(TeamUIMessages.TeamSubscriberSyncPage_labelWithSyncKind, new String JavaDoc[] { base, syncKindString }); //
125
}
126             }
127         }
128         return base;
129     }
130
131     protected Image getCompareImage(Image base, int kind) {
132         switch (kind & SyncInfo.DIRECTION_MASK) {
133             case SyncInfo.OUTGOING :
134                 kind = (kind & ~SyncInfo.OUTGOING) | SyncInfo.INCOMING;
135                 break;
136             case SyncInfo.INCOMING :
137                 kind = (kind & ~SyncInfo.INCOMING) | SyncInfo.OUTGOING;
138                 break;
139         }
140         return compareConfig.getImage(base, kind);
141     }
142
143     private Image propagateConflicts(Image base, ISynchronizeModelElement element) {
144
145         ImageDescriptor[] overlayImages = new ImageDescriptor[4];
146         boolean hasOverlay = false;
147         
148         // Decorate with the busy indicator
149
if (element.getProperty(ISynchronizeModelElement.BUSY_PROPERTY)) {
150             overlayImages[IDecoration.TOP_LEFT] = TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_HOURGLASS_OVR);
151             hasOverlay = true;
152         }
153         // Decorate with propagated conflicts and problem markers
154
int kind = element.getKind();
155         if ((kind & SyncInfo.DIRECTION_MASK) != SyncInfo.CONFLICTING) {
156             // if the folder is already conflicting then don't bother propagating
157
// the conflict
158
if (hasDecendantConflicts(element)) {
159                 overlayImages[IDecoration.BOTTOM_RIGHT] = TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_CONFLICT_OVR);
160                 hasOverlay = true;
161             }
162         }
163         if (hasErrorMarker(element)) {
164             overlayImages[IDecoration.BOTTOM_LEFT] = TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_ERROR_OVR);
165             hasOverlay = true;
166         } else if (hasWarningMarker(element)) {
167             overlayImages[IDecoration.BOTTOM_LEFT] = TeamUIPlugin.getImageDescriptor(ISharedImages.IMG_WARNING_OVR);
168             hasOverlay = true;
169         }
170         if (hasOverlay) {
171             ImageDescriptor overlay = new DecorationOverlayIcon(base, overlayImages, new Point(base.getBounds().width, base.getBounds().height));
172             if (fgImageCache == null) {
173                 fgImageCache = new HashMap(10);
174             }
175             Image conflictDecoratedImage = (Image) fgImageCache.get(overlay);
176             if (conflictDecoratedImage == null) {
177                 conflictDecoratedImage = overlay.createImage();
178                 fgImageCache.put(overlay, conflictDecoratedImage);
179             }
180             return conflictDecoratedImage;
181         }
182         return base;
183     }
184     
185     /**
186      * Return whether this diff node has descendant conflicts in the view in
187      * which it appears.
188      * @return whether the node has descendant conflicts
189      */

190     private boolean hasDecendantConflicts(ISynchronizeModelElement node) {
191         return node.getProperty(ISynchronizeModelElement.PROPAGATED_CONFLICT_PROPERTY);
192     }
193     
194     /**
195      * Return whether this diff node has descendant conflicts in the view in which it appears.
196      * @return whether the node has descendant conflicts
197      */

198     private boolean hasErrorMarker(ISynchronizeModelElement node) {
199         return node.getProperty(ISynchronizeModelElement.PROPAGATED_ERROR_MARKER_PROPERTY);
200     }
201     
202     /**
203      * Return whether this diff node has descendant conflicts in the view in which it appears.
204      * @return whether the node has descendant conflicts
205      */

206     private boolean hasWarningMarker(ISynchronizeModelElement node) {
207         return node.getProperty(ISynchronizeModelElement.PROPAGATED_WARNING_MARKER_PROPERTY);
208     }
209
210     /*
211      * (non-Javadoc)
212      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
213      */

214     public void dispose() {
215         workbenchLabelProvider.dispose();
216         if(busyFont != null) {
217             busyFont.dispose();
218         }
219         compareConfig.dispose();
220         if (fgImageCache != null) {
221             Iterator it = fgImageCache.values().iterator();
222             while (it.hasNext()) {
223                 Image element = (Image) it.next();
224                 element.dispose();
225             }
226         }
227     }
228 }
229
Popular Tags