comparison org.eclipse.jface/src/org/eclipse/jface/viewers/DelegatingStyledCellLabelProvider.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2008 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
14
15 import org.eclipse.jface.viewers.StyledCellLabelProvider;
16 import org.eclipse.jface.viewers.IBaseLabelProvider;
17 import org.eclipse.jface.viewers.StyledString;
18 import org.eclipse.jface.viewers.ViewerCell;
19 import org.eclipse.jface.viewers.ILabelProviderListener;
20 import org.eclipse.jface.viewers.IColorProvider;
21 import org.eclipse.jface.viewers.IFontProvider;
22
23 import org.eclipse.swt.custom.StyleRange;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.graphics.Image;
27
28 import java.lang.all;
29 import java.util.Arrays;
30 import java.util.Set;
31
32 /**
33 * A {@link DelegatingStyledCellLabelProvider} is a
34 * {@link StyledCellLabelProvider} that delegates requests for the styled string
35 * and the image to a
36 * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider}.
37 *
38 * <p>
39 * Existing label providers can be enhanced by implementing
40 * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} so they can be
41 * used in viewers with styled labels.
42 * </p>
43 *
44 * <p>
45 * The {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} can
46 * optionally implement {@link IColorProvider} and {@link IFontProvider} to
47 * provide foreground and background color and a default font.
48 * </p>
49 *
50 * @since 3.4
51 */
52 public class DelegatingStyledCellLabelProvider : StyledCellLabelProvider {
53
54 /**
55 * Interface marking a label provider that provides styled text labels and
56 * images.
57 * <p>
58 * The {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} can
59 * optionally implement {@link IColorProvider} and {@link IFontProvider} to
60 * provide foreground and background color and a default font.
61 * </p>
62 */
63 public static interface IStyledLabelProvider : IBaseLabelProvider {
64
65 /**
66 * Returns the styled text label for the given element
67 *
68 * @param element
69 * the element to evaluate the styled string for
70 *
71 * @return the styled string.
72 */
73 public StyledString getStyledText(Object element);
74
75 /**
76 * Returns the image for the label of the given element. The image is
77 * owned by the label provider and must not be disposed directly.
78 * Instead, dispose the label provider when no longer needed.
79 *
80 * @param element
81 * the element for which to provide the label image
82 * @return the image used to label the element, or <code>null</code>
83 * if there is no image for the given object
84 */
85 public Image getImage(Object element);
86 }
87
88 private IStyledLabelProvider styledLabelProvider;
89
90 /**
91 * Creates a {@link DelegatingStyledCellLabelProvider} that delegates the
92 * requests for the styled labels and the images to a
93 * {@link IStyledLabelProvider}.
94 *
95 * @param labelProvider
96 * the label provider that provides the styled labels and the
97 * images
98 */
99 public this(IStyledLabelProvider labelProvider) {
100 if (labelProvider is null)
101 throw new IllegalArgumentException(
102 "Label provider must not be null"); //$NON-NLS-1$
103
104 this.styledLabelProvider = labelProvider;
105 }
106
107 /*
108 * (non-Javadoc)
109 *
110 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
111 */
112 public void update(ViewerCell cell) {
113 Object element = cell.getElement();
114
115 StyledString styledString = getStyledText(element);
116 String newText= styledString.toString();
117
118 StyleRange[] oldStyleRanges= cell.getStyleRanges();
119 StyleRange[] newStyleRanges= isOwnerDrawEnabled() ? styledString.getStyleRanges() : null;
120
121 if (!Arrays.equals(oldStyleRanges, newStyleRanges)) {
122 cell.setStyleRanges(newStyleRanges);
123 if (cell.getText().equals(newText)) {
124 // make sure there will be a refresh from a change
125 cell.setText(""); //$NON-NLS-1$
126 }
127 }
128
129 cell.setText(newText);
130 cell.setImage(getImage(element));
131 cell.setFont(getFont(element));
132 cell.setForeground(getForeground(element));
133 cell.setBackground(getBackground(element));
134
135 // no super call required. changes on item will trigger the refresh.
136 }
137
138 /**
139 * Provides a foreground color for the given element.
140 *
141 * @param element
142 * the element
143 * @return the foreground color for the element, or <code>null</code> to
144 * use the default foreground color
145 */
146 public Color getForeground(Object element) {
147 if (null !is cast(IColorProvider)this.styledLabelProvider ) {
148 return (cast(IColorProvider) this.styledLabelProvider)
149 .getForeground(element);
150 }
151 return null;
152 }
153
154 /**
155 * Provides a background color for the given element.
156 *
157 * @param element
158 * the element
159 * @return the background color for the element, or <code>null</code> to
160 * use the default background color
161 */
162 public Color getBackground(Object element) {
163 if (null !is cast(IColorProvider)this.styledLabelProvider) {
164 return (cast(IColorProvider) this.styledLabelProvider)
165 .getBackground(element);
166 }
167 return null;
168 }
169
170 /**
171 * Provides a font for the given element.
172 *
173 * @param element
174 * the element
175 * @return the font for the element, or <code>null</code> to use the
176 * default font
177 */
178 public Font getFont(Object element) {
179 if (null !is cast(IFontProvider)this.styledLabelProvider ) {
180 return (cast(IFontProvider) this.styledLabelProvider).getFont(element);
181 }
182 return null;
183 }
184
185 /**
186 * Returns the image for the label of the given element. The image is owned
187 * by the label provider and must not be disposed directly. Instead, dispose
188 * the label provider when no longer needed.
189 *
190 * @param element
191 * the element for which to provide the label image
192 * @return the image used to label the element, or <code>null</code> if
193 * there is no image for the given object
194 */
195 public Image getImage(Object element) {
196 return this.styledLabelProvider.getImage(element);
197 }
198
199 /**
200 * Returns the styled text for the label of the given element.
201 *
202 * @param element
203 * the element for which to provide the styled label text
204 * @return the styled text string used to label the element
205 */
206 protected StyledString getStyledText(Object element) {
207 return this.styledLabelProvider.getStyledText(element);
208 }
209
210 /**
211 * Returns the styled string provider.
212 *
213 * @return the wrapped label provider
214 */
215 public IStyledLabelProvider getStyledStringProvider() {
216 return this.styledLabelProvider;
217 }
218
219 public void addListener(ILabelProviderListener listener) {
220 super.addListener(listener);
221 this.styledLabelProvider.addListener(listener);
222 }
223
224 public void removeListener(ILabelProviderListener listener) {
225 super.removeListener(listener);
226 this.styledLabelProvider.removeListener(listener);
227 }
228
229 public bool isLabelProperty(Object element, String property) {
230 return this.styledLabelProvider.isLabelProperty(element, property);
231 }
232
233 public void dispose() {
234 super.dispose();
235 this.styledLabelProvider.dispose();
236 }
237
238 }