Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/DecoratingStyledCellLabelProvider.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | |
children | 4878bef4a38e |
comparison
equal
deleted
inserted
replaced
69:07b9d96fd764 | 70:46a6e0e6ccd4 |
---|---|
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 dwtx.jface.viewers.DecoratingStyledCellLabelProvider; | |
14 | |
15 | |
16 import dwt.graphics.Color; | |
17 import dwt.graphics.Font; | |
18 import dwt.graphics.Image; | |
19 import dwtx.core.runtime.Assert; | |
20 import dwtx.jface.viewers.StyledString.Styler; | |
21 | |
22 /** | |
23 * A {@link DecoratingStyledCellLabelProvider} is a | |
24 * {@link DelegatingStyledCellLabelProvider} that uses a nested | |
25 * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} to compute | |
26 * styled text label and image and takes a {@link ILabelDecorator} to decorate | |
27 * the label. | |
28 * | |
29 * <p> | |
30 * Use this label provider as a replacement for the | |
31 * {@link DecoratingLabelProvider} when decorating styled text labels. | |
32 * </p> | |
33 * | |
34 * <p> | |
35 * The {@link DecoratingStyledCellLabelProvider} will try to evaluate the text | |
36 * decoration added by the {@link ILabelDecorator} and will apply the style | |
37 * returned by {@link #getDecorationStyle(Object)} | |
38 * </p> | |
39 * <p> | |
40 * The {@link ILabelDecorator} can optionally implement {@link IColorDecorator} | |
41 * and {@link IFontDecorator} to provide foreground and background color and | |
42 * font decoration. | |
43 * </p> | |
44 * | |
45 * @since 3.4 | |
46 */ | |
47 public class DecoratingStyledCellLabelProvider extends | |
48 DelegatingStyledCellLabelProvider { | |
49 | |
50 private ILabelDecorator decorator; | |
51 private IDecorationContext decorationContext= DecorationContext.DEFAULT_CONTEXT; | |
52 private ILabelProviderListener labelProviderListener; | |
53 | |
54 /** | |
55 * Creates a {@link DecoratingStyledCellLabelProvider} that delegates the | |
56 * requests for styled labels and for images to a | |
57 * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider}. | |
58 * | |
59 * @param labelProvider | |
60 * the styled label provider | |
61 * @param decorator | |
62 * a label decorator or <code>null</code> to not decorate the | |
63 * label | |
64 * @param decorationContext | |
65 * a decoration context or <code>null</code> if the no | |
66 * decorator is configured or the default decorator should be | |
67 * used | |
68 */ | |
69 public DecoratingStyledCellLabelProvider( | |
70 IStyledLabelProvider labelProvider, ILabelDecorator decorator, | |
71 IDecorationContext decorationContext) { | |
72 super(labelProvider); | |
73 | |
74 this.decorator = decorator; | |
75 this.decorationContext = decorationContext !is null ? decorationContext | |
76 : DecorationContext.DEFAULT_CONTEXT; | |
77 | |
78 this.labelProviderListener = new ILabelProviderListener() { | |
79 public void labelProviderChanged(LabelProviderChangedEvent event) { | |
80 fireLabelProviderChanged(event); | |
81 } | |
82 }; | |
83 labelProvider.addListener(this.labelProviderListener); | |
84 if (decorator !is null) | |
85 decorator.addListener(this.labelProviderListener); | |
86 } | |
87 | |
88 /** | |
89 * Returns the decoration context associated with this label provider. It | |
90 * will be passed to the decorator if the decorator is an instance of | |
91 * {@link LabelDecorator}. | |
92 * | |
93 * @return the decoration context associated with this label provider | |
94 */ | |
95 public IDecorationContext getDecorationContext() { | |
96 return this.decorationContext; | |
97 } | |
98 | |
99 /** | |
100 * Set the decoration context that will be based to the decorator for this | |
101 * label provider if that decorator implements {@link LabelDecorator}. | |
102 * | |
103 * @param decorationContext | |
104 * the decoration context. | |
105 */ | |
106 public void setDecorationContext(IDecorationContext decorationContext) { | |
107 Assert.isNotNull(decorationContext); | |
108 this.decorationContext = decorationContext; | |
109 } | |
110 | |
111 private bool waitForPendingDecoration(ViewerCell cell) { | |
112 if (this.decorator is null) | |
113 return false; | |
114 | |
115 Object element = cell.getElement(); | |
116 String oldText = cell.getText(); | |
117 | |
118 bool isDecorationPending = false; | |
119 if (this.decorator instanceof LabelDecorator) { | |
120 isDecorationPending = !((LabelDecorator) this.decorator) | |
121 .prepareDecoration(element, oldText, getDecorationContext()); | |
122 } else if (this.decorator instanceof IDelayedLabelDecorator) { | |
123 isDecorationPending = !((IDelayedLabelDecorator) this.decorator) | |
124 .prepareDecoration(element, oldText); | |
125 } | |
126 if (isDecorationPending && oldText.length() is 0) { | |
127 // item is empty: is shown for the first time: don't wait | |
128 return false; | |
129 } | |
130 return isDecorationPending; | |
131 } | |
132 | |
133 public void update(ViewerCell cell) { | |
134 if (waitForPendingDecoration(cell)) { | |
135 return; // wait until the decoration is ready | |
136 } | |
137 super.update(cell); | |
138 } | |
139 | |
140 public Color getForeground(Object element) { | |
141 if (this.decorator instanceof IColorDecorator) { | |
142 Color foreground = ((IColorDecorator) this.decorator) | |
143 .decorateForeground(element); | |
144 if (foreground !is null) | |
145 return foreground; | |
146 } | |
147 return super.getForeground(element); | |
148 } | |
149 | |
150 public Color getBackground(Object element) { | |
151 if (this.decorator instanceof IColorDecorator) { | |
152 Color color = ((IColorDecorator) this.decorator) | |
153 .decorateBackground(element); | |
154 if (color !is null) | |
155 return color; | |
156 } | |
157 return super.getBackground(element); | |
158 } | |
159 | |
160 public Font getFont(Object element) { | |
161 if (this.decorator instanceof IFontDecorator) { | |
162 Font font = ((IFontDecorator) this.decorator).decorateFont(element); | |
163 if (font !is null) | |
164 return font; | |
165 } | |
166 return super.getFont(element); | |
167 } | |
168 | |
169 public Image getImage(Object element) { | |
170 Image image = super.getImage(element); | |
171 if (this.decorator is null) { | |
172 return image; | |
173 } | |
174 Image decorated = null; | |
175 if (this.decorator instanceof LabelDecorator) { | |
176 decorated = ((LabelDecorator) this.decorator).decorateImage(image, | |
177 element, getDecorationContext()); | |
178 } else { | |
179 decorated = this.decorator.decorateImage(image, element); | |
180 } | |
181 if (decorated !is null) | |
182 return decorated; | |
183 | |
184 return image; | |
185 } | |
186 | |
187 /** | |
188 * Returns the styled text for the label of the given element. | |
189 * | |
190 * @param element | |
191 * the element for which to provide the styled label text | |
192 * @return the styled text string used to label the element | |
193 */ | |
194 protected StyledString getStyledText(Object element) { | |
195 StyledString styledString = super.getStyledText(element); | |
196 if (this.decorator is null) { | |
197 return styledString; | |
198 } | |
199 | |
200 String label = styledString.getString(); | |
201 String decorated; | |
202 if (this.decorator instanceof LabelDecorator) { | |
203 decorated = ((LabelDecorator) this.decorator).decorateText(label, | |
204 element, getDecorationContext()); | |
205 } else { | |
206 decorated = this.decorator.decorateText(label, element); | |
207 } | |
208 if (decorated is null) | |
209 return styledString; | |
210 | |
211 int originalStart = decorated.indexOf(label); | |
212 if (originalStart is -1) { | |
213 return new StyledString(decorated); // the decorator did | |
214 // something wild | |
215 } | |
216 | |
217 if (decorated.length() is label.length()) | |
218 return styledString; | |
219 | |
220 Styler style = getDecorationStyle(element); | |
221 if (originalStart > 0) { | |
222 StyledString newString = new StyledString(decorated | |
223 .substring(0, originalStart), style); | |
224 newString.append(styledString); | |
225 styledString = newString; | |
226 } | |
227 if (decorated.length() > originalStart + label.length()) { // decorator | |
228 // appended | |
229 // something | |
230 return styledString.append(decorated.substring(originalStart | |
231 + label.length()), style); | |
232 } | |
233 return styledString; | |
234 } | |
235 | |
236 /** | |
237 * Sets the {@link StyledString.Styler} to be used for string | |
238 * decorations. By default the | |
239 * {@link StyledString#DECORATIONS_STYLER decoration style}. Clients | |
240 * can override. | |
241 * | |
242 * Note that it is the client's responsibility to react on color changes of | |
243 * the decoration color by refreshing the view | |
244 * | |
245 * @param element | |
246 * the element that has been decorated | |
247 * | |
248 * @return return the decoration style | |
249 */ | |
250 protected Styler getDecorationStyle(Object element) { | |
251 return StyledString.DECORATIONS_STYLER; | |
252 } | |
253 | |
254 /** | |
255 * Returns the decorator or <code>null</code> if no decorator is installed | |
256 * | |
257 * @return the decorator or <code>null</code> if no decorator is installed | |
258 */ | |
259 public ILabelDecorator getLabelDecorator() { | |
260 return this.decorator; | |
261 } | |
262 | |
263 /** | |
264 * Sets the label decorator. Removes all known listeners from the old | |
265 * decorator, and adds all known listeners to the new decorator. The old | |
266 * decorator is not disposed. Fires a label provider changed event | |
267 * indicating that all labels should be updated. Has no effect if the given | |
268 * decorator is identical to the current one. | |
269 * | |
270 * @param newDecorator | |
271 * the label decorator, or <code>null</code> if no decorations | |
272 * are to be applied | |
273 */ | |
274 public void setLabelDecorator(ILabelDecorator newDecorator) { | |
275 ILabelDecorator oldDecorator = this.decorator; | |
276 if (oldDecorator !is newDecorator) { | |
277 if (oldDecorator !is null) | |
278 oldDecorator.removeListener(this.labelProviderListener); | |
279 this.decorator = newDecorator; | |
280 if (newDecorator !is null) { | |
281 newDecorator.addListener(this.labelProviderListener); | |
282 } | |
283 } | |
284 fireLabelProviderChanged(new LabelProviderChangedEvent(this)); | |
285 } | |
286 | |
287 public void addListener(ILabelProviderListener listener) { | |
288 super.addListener(listener); | |
289 if (this.decorator !is null) { | |
290 this.decorator.addListener(this.labelProviderListener); | |
291 } | |
292 } | |
293 | |
294 public void removeListener(ILabelProviderListener listener) { | |
295 super.removeListener(listener); | |
296 if (this.decorator !is null) { | |
297 this.decorator.removeListener(this.labelProviderListener); | |
298 } | |
299 } | |
300 | |
301 public bool isLabelProperty(Object element, String property) { | |
302 if (super.isLabelProperty(element, property)) { | |
303 return true; | |
304 } | |
305 return this.decorator !is null | |
306 && this.decorator.isLabelProperty(element, property); | |
307 } | |
308 | |
309 public void dispose() { | |
310 super.dispose(); | |
311 if (this.decorator !is null) { | |
312 this.decorator.removeListener(this.labelProviderListener); | |
313 this.decorator.dispose(); | |
314 this.decorator = null; | |
315 } | |
316 } | |
317 | |
318 } |