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