Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/DecoratingLabelProvider.d @ 43:ea8ff534f622
Fix override and super aliases
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Apr 2008 01:24:25 +0200 |
parents | b6c35faf97c8 |
children | 46a6e0e6ccd4 |
rev | line source |
---|---|
10 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.viewers.DecoratingLabelProvider; | |
14 | |
15 import dwtx.jface.viewers.LabelProvider; | |
16 import dwtx.jface.viewers.ILabelProvider; | |
17 import dwtx.jface.viewers.IViewerLabelProvider; | |
18 import dwtx.jface.viewers.IColorProvider; | |
19 import dwtx.jface.viewers.IFontProvider; | |
20 import dwtx.jface.viewers.ITreePathLabelProvider; | |
21 import dwtx.jface.viewers.ILabelDecorator; | |
22 import dwtx.jface.viewers.IDecorationContext; | |
23 import dwtx.jface.viewers.ILabelProviderListener; | |
24 import dwtx.jface.viewers.ViewerLabel; | |
25 import dwtx.jface.viewers.TreePath; | |
26 import dwtx.jface.viewers.DecorationContext; | |
27 import dwtx.jface.viewers.LabelDecorator; | |
28 import dwtx.jface.viewers.LabelProviderChangedEvent; | |
29 import dwtx.jface.viewers.IDelayedLabelDecorator; | |
30 import dwtx.jface.viewers.IColorDecorator; | |
31 import dwtx.jface.viewers.IFontDecorator; | |
32 | |
33 import dwt.graphics.Color; | |
34 import dwt.graphics.Font; | |
35 import dwt.graphics.Image; | |
36 import dwtx.core.runtime.Assert; | |
37 import dwtx.core.runtime.ListenerList; | |
38 | |
39 import dwt.dwthelper.utils; | |
40 | |
41 /** | |
42 * A decorating label provider is a label provider which combines | |
43 * a nested label provider and an optional decorator. | |
44 * The decorator decorates the label text, image, font and colors provided by | |
45 * the nested label provider. | |
46 */ | |
47 public class DecoratingLabelProvider : LabelProvider, | |
48 ILabelProvider, IViewerLabelProvider, IColorProvider, IFontProvider, ITreePathLabelProvider { | |
49 | |
50 private ILabelProvider provider; | |
51 | |
52 private ILabelDecorator decorator; | |
53 | |
54 // Need to keep our own list of listeners | |
55 private ListenerList listeners; | |
56 | |
57 private IDecorationContext decorationContext; | |
58 | |
59 /** | |
60 * Creates a decorating label provider which uses the given label decorator | |
61 * to decorate labels provided by the given label provider. | |
62 * | |
63 * @param provider the nested label provider | |
64 * @param decorator the label decorator, or <code>null</code> if no decorator is to be used initially | |
65 */ | |
66 public this(ILabelProvider provider, | |
67 ILabelDecorator decorator) { | |
68 decorationContext = DecorationContext.DEFAULT_CONTEXT; | |
69 listeners = new ListenerList(); | |
70 Assert.isNotNull(cast(Object)provider); | |
71 this.provider = provider; | |
72 this.decorator = decorator; | |
73 } | |
74 | |
75 /** | |
76 * The <code>DecoratingLabelProvider</code> implementation of this <code>IBaseLabelProvider</code> method | |
77 * adds the listener to both the nested label provider and the label decorator. | |
78 * | |
79 * @param listener a label provider listener | |
80 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
81 public override void addListener(ILabelProviderListener listener) { |
10 | 82 super.addListener(listener); |
83 provider.addListener(listener); | |
84 if (decorator !is null) { | |
85 decorator.addListener(listener); | |
86 } | |
87 listeners.add(cast(Object)listener); | |
88 } | |
89 | |
90 /** | |
91 * The <code>DecoratingLabelProvider</code> implementation of this <code>IBaseLabelProvider</code> method | |
92 * disposes both the nested label provider and the label decorator. | |
93 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
94 public override void dispose() { |
10 | 95 provider.dispose(); |
96 if (decorator !is null) { | |
97 decorator.dispose(); | |
98 } | |
99 } | |
100 | |
101 /** | |
102 * The <code>DecoratingLabelProvider</code> implementation of this | |
103 * <code>ILabelProvider</code> method returns the image provided | |
104 * by the nested label provider's <code>getImage</code> method, | |
105 * decorated with the decoration provided by the label decorator's | |
106 * <code>decorateImage</code> method. | |
107 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
108 public override Image getImage(Object element) { |
10 | 109 Image image = provider.getImage(element); |
110 if (decorator !is null) { | |
111 if ( auto ld2 = cast(LabelDecorator)decorator ) { | |
112 Image decorated = ld2.decorateImage(image, element, getDecorationContext()); | |
113 if (decorated !is null) { | |
114 return decorated; | |
115 } | |
116 } else { | |
117 Image decorated = decorator.decorateImage(image, element); | |
118 if (decorated !is null) { | |
119 return decorated; | |
120 } | |
121 } | |
122 } | |
123 return image; | |
124 } | |
125 | |
126 /** | |
127 * Returns the label decorator, or <code>null</code> if none has been set. | |
128 * | |
129 * @return the label decorator, or <code>null</code> if none has been set. | |
130 */ | |
131 public ILabelDecorator getLabelDecorator() { | |
132 return decorator; | |
133 } | |
134 | |
135 /** | |
136 * Returns the nested label provider. | |
137 * | |
138 * @return the nested label provider | |
139 */ | |
140 public ILabelProvider getLabelProvider() { | |
141 return provider; | |
142 } | |
143 | |
144 /** | |
145 * The <code>DecoratingLabelProvider</code> implementation of this | |
146 * <code>ILabelProvider</code> method returns the text label provided | |
147 * by the nested label provider's <code>getText</code> method, | |
148 * decorated with the decoration provided by the label decorator's | |
149 * <code>decorateText</code> method. | |
150 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
151 public override String getText(Object element) { |
10 | 152 String text = provider.getText(element); |
153 if (decorator !is null) { | |
154 if ( auto ld2 = cast(LabelDecorator)decorator ) { | |
155 String decorated = ld2.decorateText(text, element, getDecorationContext()); | |
156 if (decorated !is null) { | |
157 return decorated; | |
158 } | |
159 } else { | |
160 String decorated = decorator.decorateText(text, element); | |
161 if (decorated !is null) { | |
162 return decorated; | |
163 } | |
164 } | |
165 } | |
166 return text; | |
167 } | |
168 | |
169 /** | |
170 * The <code>DecoratingLabelProvider</code> implementation of this | |
171 * <code>IBaseLabelProvider</code> method returns <code>true</code> if the corresponding method | |
172 * on the nested label provider returns <code>true</code> or if the corresponding method on the | |
173 * decorator returns <code>true</code>. | |
174 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
175 public override bool isLabelProperty(Object element, String property) { |
10 | 176 if (provider.isLabelProperty(element, property)) { |
177 return true; | |
178 } | |
179 if (decorator !is null && decorator.isLabelProperty(element, property)) { | |
180 return true; | |
181 } | |
182 return false; | |
183 } | |
184 | |
185 /** | |
186 * The <code>DecoratingLabelProvider</code> implementation of this <code>IBaseLabelProvider</code> method | |
187 * removes the listener from both the nested label provider and the label decorator. | |
188 * | |
189 * @param listener a label provider listener | |
190 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
191 public override void removeListener(ILabelProviderListener listener) { |
10 | 192 super.removeListener(listener); |
193 provider.removeListener(listener); | |
194 if (decorator !is null) { | |
195 decorator.removeListener(listener); | |
196 } | |
197 listeners.remove(cast(Object)listener); | |
198 } | |
199 | |
200 /** | |
201 * Sets the label decorator. | |
202 * Removes all known listeners from the old decorator, and adds all known listeners to the new decorator. | |
203 * The old decorator is not disposed. | |
204 * Fires a label provider changed event indicating that all labels should be updated. | |
205 * Has no effect if the given decorator is identical to the current one. | |
206 * | |
207 * @param decorator the label decorator, or <code>null</code> if no decorations are to be applied | |
208 */ | |
209 public void setLabelDecorator(ILabelDecorator decorator) { | |
210 ILabelDecorator oldDecorator = this.decorator; | |
211 if (oldDecorator !is decorator) { | |
212 Object[] listenerList = this.listeners.getListeners(); | |
213 if (oldDecorator !is null) { | |
214 for (int i = 0; i < listenerList.length; ++i) { | |
215 oldDecorator | |
216 .removeListener(cast(ILabelProviderListener) listenerList[i]); | |
217 } | |
218 } | |
219 this.decorator = decorator; | |
220 if (decorator !is null) { | |
221 for (int i = 0; i < listenerList.length; ++i) { | |
222 decorator | |
223 .addListener(cast(ILabelProviderListener) listenerList[i]); | |
224 } | |
225 } | |
226 fireLabelProviderChanged(new LabelProviderChangedEvent(this)); | |
227 } | |
228 } | |
229 | |
230 | |
231 /* | |
232 * (non-Javadoc) | |
233 * @see dwtx.jface.viewers.IViewerLabelProvider#updateLabel(dwtx.jface.viewers.ViewerLabel, java.lang.Object) | |
234 */ | |
235 public void updateLabel(ViewerLabel settings, Object element) { | |
236 | |
237 ILabelDecorator currentDecorator = getLabelDecorator(); | |
238 String oldText = settings.getText(); | |
239 bool decorationReady = true; | |
240 if ( auto delayedDecorator = cast(IDelayedLabelDecorator)currentDecorator ) { | |
241 if (!delayedDecorator.prepareDecoration(element, oldText)) { | |
242 // The decoration is not ready but has been queued for processing | |
243 decorationReady = false; | |
244 } | |
245 } | |
246 // update icon and label | |
247 | |
248 if (decorationReady || oldText is null | |
249 || settings.getText().length is 0) { | |
250 settings.setText(getText(element)); | |
251 } | |
252 | |
253 Image oldImage = settings.getImage(); | |
254 if (decorationReady || oldImage is null) { | |
255 settings.setImage(getImage(element)); | |
256 } | |
257 | |
258 if(decorationReady) { | |
259 updateForDecorationReady(settings,element); | |
260 } | |
261 | |
262 } | |
263 | |
264 /** | |
265 * Decoration is ready. Update anything else for the settings. | |
266 * @param settings The object collecting the settings. | |
267 * @param element The Object being decorated. | |
268 * @since 3.1 | |
269 */ | |
270 protected void updateForDecorationReady(ViewerLabel settings, Object element) { | |
271 | |
272 if( auto colorDecorator = cast(IColorDecorator) decorator ){ | |
273 settings.setBackground(colorDecorator.decorateBackground(element)); | |
274 settings.setForeground(colorDecorator.decorateForeground(element)); | |
275 } | |
276 | |
277 if( auto d = cast(IFontDecorator) decorator ) { | |
278 settings.setFont(d.decorateFont(element)); | |
279 } | |
280 | |
281 } | |
282 | |
283 /* (non-Javadoc) | |
284 * @see dwtx.jface.viewers.IColorProvider#getBackground(java.lang.Object) | |
285 */ | |
286 public Color getBackground(Object element) { | |
287 if( auto p = cast(IColorProvider) provider ) { | |
288 return p.getBackground(element); | |
289 } | |
290 return null; | |
291 } | |
292 | |
293 /* (non-Javadoc) | |
294 * @see dwtx.jface.viewers.IFontProvider#getFont(java.lang.Object) | |
295 */ | |
296 public Font getFont(Object element) { | |
297 if(auto p = cast(IFontProvider)provider ) { | |
298 return p.getFont(element); | |
299 } | |
300 return null; | |
301 } | |
302 | |
303 /* (non-Javadoc) | |
304 * @see dwtx.jface.viewers.IColorProvider#getForeground(java.lang.Object) | |
305 */ | |
306 public Color getForeground(Object element) { | |
307 if(auto p = cast(IColorProvider)provider ) { | |
308 return p.getForeground(element); | |
309 } | |
310 return null; | |
311 } | |
312 | |
313 /** | |
314 * Return the decoration context associated with this label provider. | |
315 * It will be passed to the decorator if the decorator is an | |
316 * instance of {@link LabelDecorator}. | |
317 * @return the decoration context associated with this label provider | |
318 * | |
319 * @since 3.2 | |
320 */ | |
321 public IDecorationContext getDecorationContext() { | |
322 return decorationContext; | |
323 } | |
324 | |
325 /** | |
326 * Set the decoration context that will be based to the decorator | |
327 * for this label provider if that decorator implements {@link LabelDecorator}. | |
328 * @param decorationContext the decoration context. | |
329 * | |
330 * @since 3.2 | |
331 */ | |
332 public void setDecorationContext(IDecorationContext decorationContext) { | |
333 Assert.isNotNull(cast(Object)decorationContext); | |
334 this.decorationContext = decorationContext; | |
335 } | |
336 | |
337 /* (non-Javadoc) | |
338 * @see dwtx.jface.viewers.ITreePathLabelProvider#updateLabel(dwtx.jface.viewers.ViewerLabel, dwtx.jface.viewers.TreePath) | |
339 */ | |
340 public void updateLabel(ViewerLabel settings, TreePath elementPath) { | |
341 ILabelDecorator currentDecorator = getLabelDecorator(); | |
342 String oldText = settings.getText(); | |
343 Object element = elementPath.getLastSegment(); | |
344 bool decorationReady = true; | |
345 if ( auto labelDecorator = cast(LabelDecorator) currentDecorator ) { | |
346 if (!labelDecorator.prepareDecoration(element, oldText, getDecorationContext())) { | |
347 // The decoration is not ready but has been queued for processing | |
348 decorationReady = false; | |
349 } | |
350 } else if ( auto delayedDecorator = cast(IDelayedLabelDecorator) currentDecorator ) { | |
351 if (!delayedDecorator.prepareDecoration(element, oldText)) { | |
352 // The decoration is not ready but has been queued for processing | |
353 decorationReady = false; | |
354 } | |
355 } | |
356 settings.setHasPendingDecorations(!decorationReady); | |
357 // update icon and label | |
358 | |
359 if ( auto pprov = cast(ITreePathLabelProvider) provider ) { | |
360 if (decorationReady || oldText is null | |
361 || settings.getText().length is 0) { | |
362 pprov.updateLabel(settings, elementPath); | |
363 decorateSettings(settings, elementPath); | |
364 } | |
365 } else { | |
366 if (decorationReady || oldText is null | |
367 || settings.getText().length is 0) { | |
368 settings.setText(getText(element)); | |
369 } | |
370 | |
371 Image oldImage = settings.getImage(); | |
372 if (decorationReady || oldImage is null) { | |
373 settings.setImage(getImage(element)); | |
374 } | |
375 | |
376 if(decorationReady) { | |
377 updateForDecorationReady(settings,element); | |
378 } | |
379 } | |
380 | |
381 } | |
382 | |
383 /** | |
384 * Decorate the settings | |
385 * @param settings the settings obtained from the label provider | |
386 * @param elementPath the element path being decorated | |
387 */ | |
388 private void decorateSettings(ViewerLabel settings, TreePath elementPath) { | |
389 Object element = elementPath.getLastSegment(); | |
390 if (decorator !is null) { | |
391 if ( auto labelDecorator = cast(LabelDecorator) decorator ) { | |
392 String text = labelDecorator.decorateText(settings.getText(), element, getDecorationContext()); | |
393 if (text !is null && text.length > 0) | |
394 settings.setText(text); | |
395 Image image = labelDecorator.decorateImage(settings.getImage(), element, getDecorationContext()); | |
396 if (image !is null) | |
397 settings.setImage(image); | |
398 | |
399 } else { | |
400 String text = decorator.decorateText(settings.getText(), element); | |
401 if (text !is null && text.length > 0) | |
402 settings.setText(text); | |
403 Image image = decorator.decorateImage(settings.getImage(), element); | |
404 if (image !is null) | |
405 settings.setImage(image); | |
406 } | |
407 if( auto colorDecorator = cast(IColorDecorator) decorator ){ | |
408 Color background = colorDecorator.decorateBackground(element); | |
409 if (background !is null) | |
410 settings.setBackground(background); | |
411 Color foreground = colorDecorator.decorateForeground(element); | |
412 if (foreground !is null) | |
413 settings.setForeground(foreground); | |
414 } | |
415 | |
416 if( auto fd = cast(IFontDecorator) decorator ) { | |
417 Font font = fd.decorateFont(element); | |
418 if (font !is null) | |
419 settings.setFont(font); | |
420 } | |
421 } | |
422 } | |
423 } |