comparison dwtx/jface/viewers/DelegatingStyledCellLabelProvider.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.DelegatingStyledCellLabelProvider;
14
15 import dwt.graphics.Color;
16 import dwt.graphics.Font;
17 import dwt.graphics.Image;
18
19 /**
20 * A {@link DelegatingStyledCellLabelProvider} is a
21 * {@link StyledCellLabelProvider} that delegates requests for the styled string
22 * and the image to a
23 * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider}.
24 *
25 * <p>
26 * Existing label providers can be enhanced by implementing
27 * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} so they can be
28 * used in viewers with styled labels.
29 * </p>
30 *
31 * <p>
32 * The {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} can
33 * optionally implement {@link IColorProvider} and {@link IFontProvider} to
34 * provide foreground and background color and a default font.
35 * </p>
36 *
37 * @since 3.4
38 */
39 public class DelegatingStyledCellLabelProvider extends StyledCellLabelProvider {
40
41 /**
42 * Interface marking a label provider that provides styled text labels and
43 * images.
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 public static interface IStyledLabelProvider extends IBaseLabelProvider {
51
52 /**
53 * Returns the styled text label for the given element
54 *
55 * @param element
56 * the element to evaluate the styled string for
57 *
58 * @return the styled string.
59 */
60 public StyledString getStyledText(Object element);
61
62 /**
63 * Returns the image for the label of the given element. The image is
64 * owned by the label provider and must not be disposed directly.
65 * Instead, dispose the label provider when no longer needed.
66 *
67 * @param element
68 * the element for which to provide the label image
69 * @return the image used to label the element, or <code>null</code>
70 * if there is no image for the given object
71 */
72 public Image getImage(Object element);
73 }
74
75 private IStyledLabelProvider styledLabelProvider;
76
77 /**
78 * Creates a {@link DelegatingStyledCellLabelProvider} that delegates the
79 * requests for the styled labels and the images to a
80 * {@link IStyledLabelProvider}.
81 *
82 * @param labelProvider
83 * the label provider that provides the styled labels and the
84 * images
85 */
86 public DelegatingStyledCellLabelProvider(IStyledLabelProvider labelProvider) {
87 if (labelProvider is null)
88 throw new IllegalArgumentException(
89 "Label provider must not be null"); //$NON-NLS-1$
90
91 this.styledLabelProvider = labelProvider;
92 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see dwtx.jface.viewers.StyledCellLabelProvider#update(dwtx.jface.viewers.ViewerCell)
98 */
99 public void update(ViewerCell cell) {
100 Object element = cell.getElement();
101
102 StyledString styledString = getStyledText(element);
103 cell.setText(styledString.toString());
104 if (isOwnerDrawEnabled()) {
105 cell.setStyleRanges(styledString.getStyleRanges());
106 } else {
107 cell.setStyleRanges(null);
108 }
109
110 cell.setImage(getImage(element));
111 cell.setFont(getFont(element));
112 cell.setForeground(getForeground(element));
113 cell.setBackground(getBackground(element));
114
115 super.update(cell);
116 }
117
118 /**
119 * Provides a foreground color for the given element.
120 *
121 * @param element
122 * the element
123 * @return the foreground color for the element, or <code>null</code> to
124 * use the default foreground color
125 */
126 public Color getForeground(Object element) {
127 if (this.styledLabelProvider instanceof IColorProvider) {
128 return ((IColorProvider) this.styledLabelProvider)
129 .getForeground(element);
130 }
131 return null;
132 }
133
134 /**
135 * Provides a background color for the given element.
136 *
137 * @param element
138 * the element
139 * @return the background color for the element, or <code>null</code> to
140 * use the default background color
141 */
142 public Color getBackground(Object element) {
143 if (this.styledLabelProvider instanceof IColorProvider) {
144 return ((IColorProvider) this.styledLabelProvider)
145 .getBackground(element);
146 }
147 return null;
148 }
149
150 /**
151 * Provides a font for the given element.
152 *
153 * @param element
154 * the element
155 * @return the font for the element, or <code>null</code> to use the
156 * default font
157 */
158 public Font getFont(Object element) {
159 if (this.styledLabelProvider instanceof IFontProvider) {
160 return ((IFontProvider) this.styledLabelProvider).getFont(element);
161 }
162 return null;
163 }
164
165 /**
166 * Returns the image for the label of the given element. The image is owned
167 * by the label provider and must not be disposed directly. Instead, dispose
168 * the label provider when no longer needed.
169 *
170 * @param element
171 * the element for which to provide the label image
172 * @return the image used to label the element, or <code>null</code> if
173 * there is no image for the given object
174 */
175 public Image getImage(Object element) {
176 return this.styledLabelProvider.getImage(element);
177 }
178
179 /**
180 * Returns the styled text for the label of the given element.
181 *
182 * @param element
183 * the element for which to provide the styled label text
184 * @return the styled text string used to label the element
185 */
186 protected StyledString getStyledText(Object element) {
187 return this.styledLabelProvider.getStyledText(element);
188 }
189
190 /**
191 * Returns the styled string provider.
192 *
193 * @return the wrapped label provider
194 */
195 public IStyledLabelProvider getStyledStringProvider() {
196 return this.styledLabelProvider;
197 }
198
199 public void addListener(ILabelProviderListener listener) {
200 super.addListener(listener);
201 this.styledLabelProvider.addListener(listener);
202 }
203
204 public void removeListener(ILabelProviderListener listener) {
205 super.removeListener(listener);
206 this.styledLabelProvider.removeListener(listener);
207 }
208
209 public bool isLabelProperty(Object element, String property) {
210 return this.styledLabelProvider.isLabelProperty(element, property);
211 }
212
213 public void dispose() {
214 super.dispose();
215 this.styledLabelProvider.dispose();
216 }
217
218 }