KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > WrappedViewerLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.jface.viewers;
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.graphics.Image;
18
19 /**
20  * The WrappedViewerLabelProvider is a label provider that allows
21  * {@link ILabelProvider}, {@link IColorProvider} and {@link IFontProvider} to
22  * be mapped to a ColumnLabelProvider.
23  *
24  * @since 3.3
25  *
26  */

27 class WrappedViewerLabelProvider extends ColumnLabelProvider {
28
29     private static ILabelProvider defaultLabelProvider = new LabelProvider();
30
31     private ILabelProvider labelProvider = defaultLabelProvider;
32
33     private IColorProvider colorProvider;
34
35     private IFontProvider fontProvider;
36
37     private IViewerLabelProvider viewerLabelProvider;
38
39     private ITreePathLabelProvider treePathLabelProvider;
40
41     /**
42      * Create a new instance of the receiver based on labelProvider.
43      *
44      * @param labelProvider
45      */

46     public WrappedViewerLabelProvider(IBaseLabelProvider labelProvider) {
47         super();
48         setProviders(labelProvider);
49     }
50
51     /**
52      * Set the any providers for the receiver that can be adapted from provider.
53      *
54      * @param provider
55      * {@link Object}
56      */

57     public void setProviders(Object JavaDoc provider) {
58         if (provider instanceof ITreePathLabelProvider)
59             treePathLabelProvider = ((ITreePathLabelProvider) provider);
60
61         if (provider instanceof IViewerLabelProvider)
62             viewerLabelProvider = ((IViewerLabelProvider) provider);
63
64         if (provider instanceof ILabelProvider)
65             labelProvider = ((ILabelProvider) provider);
66
67         if (provider instanceof IColorProvider)
68             colorProvider = (IColorProvider) provider;
69
70         if (provider instanceof IFontProvider)
71             fontProvider = (IFontProvider) provider;
72
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
79      */

80     public Font getFont(Object JavaDoc element) {
81         if (fontProvider == null) {
82             return null;
83         }
84
85         return fontProvider.getFont(element);
86
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
93      */

94     public Color getBackground(Object JavaDoc element) {
95         if (colorProvider == null) {
96             return null;
97         }
98
99         return colorProvider.getBackground(element);
100     }
101
102     /*
103      * (non-Javadoc)
104      *
105      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
106      */

107     public String JavaDoc getText(Object JavaDoc element) {
108         return getLabelProvider().getText(element);
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
115      */

116     public Image getImage(Object JavaDoc element) {
117         return getLabelProvider().getImage(element);
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
124      */

125     public Color getForeground(Object JavaDoc element) {
126         if (colorProvider == null) {
127             return null;
128         }
129
130         return colorProvider.getForeground(element);
131     }
132
133     /**
134      * Get the label provider
135      *
136      * @return {@link ILabelProvider}
137      */

138     ILabelProvider getLabelProvider() {
139         return labelProvider;
140     }
141
142     /**
143      * Get the color provider
144      *
145      * @return {@link IColorProvider}
146      */

147     IColorProvider getColorProvider() {
148         return colorProvider;
149     }
150
151     /**
152      * Get the font provider
153      *
154      * @return {@link IFontProvider}.
155      */

156     IFontProvider getFontProvider() {
157         return fontProvider;
158     }
159
160     public void update(ViewerCell cell) {
161         Object JavaDoc element = cell.getElement();
162         if(viewerLabelProvider == null && treePathLabelProvider == null){
163             // inlined super implementation with performance optimizations
164
cell.setText(getText(element));
165             Image image = getImage(element);
166             cell.setImage(image);
167             if (colorProvider != null) {
168                 cell.setBackground(getBackground(element));
169                 cell.setForeground(getForeground(element));
170             }
171             if (fontProvider != null) {
172                 cell.setFont(getFont(element));
173             }
174             return;
175         }
176         
177         ViewerLabel label = new ViewerLabel(cell.getText(), cell.getImage());
178         
179         if (treePathLabelProvider != null) {
180             TreePath treePath = cell.getViewerRow().getTreePath();
181
182             Assert.isNotNull(treePath);
183             treePathLabelProvider.updateLabel(label, treePath);
184         } else if (viewerLabelProvider != null) {
185             viewerLabelProvider.updateLabel(label, element);
186         }
187         if (!label.hasNewForeground() && colorProvider != null)
188             label.setForeground(getForeground(element));
189         
190         if (!label.hasNewBackground() && colorProvider != null)
191             label.setBackground(getBackground(element));
192         
193         if (!label.hasNewFont() && fontProvider != null)
194             label.setFont(getFont(element));
195         
196         applyViewerLabel(cell, label);
197     }
198
199     private void applyViewerLabel(ViewerCell cell, ViewerLabel label) {
200         if (label.hasNewText()) {
201             cell.setText(label.getText());
202         }
203         if (label.hasNewImage()) {
204             cell.setImage(label.getImage());
205         }
206         if (colorProvider!= null || label.hasNewBackground()) {
207             cell.setBackground(label.getBackground());
208         }
209         if (colorProvider!= null || label.hasNewForeground()) {
210             cell.setForeground(label.getForeground());
211         }
212         if (fontProvider!= null || label.hasNewFont()) {
213             cell.setFont(label.getFont());
214         }
215     }
216 }
217
Popular Tags