Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 219393 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jface/viewers/DelegatingStyledCellLabelProvider.java (+210 lines)
Added Link Here
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
 *******************************************************************************/
11
package org.eclipse.jface.viewers;
12
13
import org.eclipse.swt.graphics.Color;
14
import org.eclipse.swt.graphics.Font;
15
import org.eclipse.swt.graphics.Image;
16
17
18
/**
19
 * A {@link DelegatingStyledCellLabelProvider} is a {@link StyledCellLabelProvider}
20
 * that delegates requests for the styled string and the image
21
 * to a {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider}.
22
 * 
23
 * <p>Existing label providers can be enhanced by implementing
24
 * {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} so they can be
25
 *  used in viewers with styled labels.</p>
26
 * 
27
 * <p>The {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} can
28
 * optionally implement {@link IColorProvider} and {@link IFontProvider} to provide
29
 * foreground and background color and a default font.
30
 * </p>
31
 * 
32
 * @since 3.4
33
 */
34
public class DelegatingStyledCellLabelProvider extends StyledCellLabelProvider {
35
36
	/**
37
	 * Interface marking a label provider that provides styled text labels and images.
38
	 * <p>The {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider} can
39
	 * optionally implement {@link IColorProvider} and {@link IFontProvider} to provide
40
	 * foreground and background color and a default font.
41
	 * </p>
42
	 */
43
	public static interface IStyledLabelProvider extends IBaseLabelProvider {
44
45
		/**
46
		 * Returns the styled text label for the given element
47
		 * 
48
		 * @param element the element to evaluate the styled string for
49
		 * 
50
		 * @return the styled string.
51
		 */
52
		public StyledStringBuilder getStyledText(Object element);
53
54
		/**
55
		 * Returns the image for the label of the given element.  The image
56
		 * is owned by the label provider and must not be disposed directly.
57
		 * Instead, dispose the label provider when no longer needed.
58
		 *
59
		 * @param element the element for which to provide the label image
60
		 * @return the image used to label the element, or <code>null</code>
61
		 *   if there is no image for the given object
62
		 */
63
		public Image getImage(Object element);
64
	}
65
66
67
	private IStyledLabelProvider styledLabelProvider;
68
69
	/**
70
	 * Creates a {@link DelegatingStyledCellLabelProvider} that delegates the requests for the
71
	 * styled labels and the images to a {@link IStyledLabelProvider}.
72
	 * 
73
	 * @param labelProvider the label provider that provides the styled labels and the images
74
	 */
75
	public DelegatingStyledCellLabelProvider(IStyledLabelProvider labelProvider) {
76
		if (labelProvider == null)
77
			throw new IllegalArgumentException("Label provider must not be null"); //$NON-NLS-1$
78
79
		this.styledLabelProvider= labelProvider;
80
	}
81
82
	/* (non-Javadoc)
83
	 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
84
	 */
85
	public void update(ViewerCell cell) {
86
		Object element= cell.getElement();
87
88
		StyledStringBuilder styledString= getStyledText(element);
89
		cell.setText(styledString.toString());
90
		if (isOwnerDrawEnabled()) {
91
			cell.setStyleRanges(styledString.toStyleRanges());
92
		} else {
93
			cell.setStyleRanges(null);
94
		}
95
96
		cell.setImage(getImage(element));
97
		cell.setFont(getFont(element));
98
		cell.setForeground(getForeground(element));
99
		cell.setBackground(getBackground(element));
100
101
		super.update(cell);
102
	}
103
104
	/**
105
	 * Provides a foreground color for the given element.
106
	 * 
107
	 * @param element the element
108
	 * @return the foreground color for the element, or <code>null</code> 
109
	 *   to use the default foreground color
110
	 */
111
	public Color getForeground(Object element) {
112
		if (this.styledLabelProvider instanceof IColorProvider) {
113
			return ((IColorProvider) this.styledLabelProvider).getForeground(element);
114
		}
115
		return null;
116
	}
117
118
	/**
119
	 * Provides a background color for the given element.
120
	 * 
121
	 * @param element the element
122
	 * @return the background color for the element, or <code>null</code> 
123
	 *   to use the default background color
124
	 */
125
	public Color getBackground(Object element) {
126
		if (this.styledLabelProvider instanceof IColorProvider) {
127
			return ((IColorProvider) this.styledLabelProvider).getBackground(element);
128
		}
129
		return null;
130
	}
131
132
	/**
133
	 * Provides a font for the given element.
134
	 * 
135
	 * @param element the element
136
	 * @return the font for the element, or <code>null</code> 
137
	 *   to use the default font
138
	 */
139
	public Font getFont(Object element) {
140
		if (this.styledLabelProvider instanceof IFontProvider) {
141
			return ((IFontProvider) this.styledLabelProvider).getFont(element);
142
		}
143
		return null;
144
	}
145
146
	/**
147
	 * Returns the image for the label of the given element.  The image
148
	 * is owned by the label provider and must not be disposed directly.
149
	 * Instead, dispose the label provider when no longer needed.
150
	 *
151
	 * @param element the element for which to provide the label image
152
	 * @return the image used to label the element, or <code>null</code>
153
	 *   if there is no image for the given object
154
	 */
155
	public Image getImage(Object element) {
156
		return this.styledLabelProvider.getImage(element);
157
	}
158
159
	/**
160
	 * Returns the styled text for the label of the given element.
161
	 *
162
	 * @param element the element for which to provide the styled label text
163
	 * @return the styled text string used to label the element
164
	 */
165
	public StyledStringBuilder getStyledText(Object element) {
166
		return this.styledLabelProvider.getStyledText(element);
167
	}
168
169
	/**
170
	 * Returns the styled string provider.
171
	 * 
172
	 * @return  the wrapped label provider
173
	 */
174
	public IStyledLabelProvider getStyledStringProvider() {
175
		return this.styledLabelProvider;
176
	}
177
178
	/* (non-Javadoc)
179
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
180
	 */
181
	public void addListener(ILabelProviderListener listener) {
182
		super.addListener(listener);
183
		this.styledLabelProvider.addListener(listener);
184
	}
185
186
	/* (non-Javadoc)
187
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
188
	 */
189
	public void removeListener(ILabelProviderListener listener) {
190
		super.removeListener(listener);
191
		this.styledLabelProvider.removeListener(listener);
192
	}
193
194
	/* (non-Javadoc)
195
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
196
	 */
197
	public boolean isLabelProperty(Object element, String property) {
198
		return this.styledLabelProvider.isLabelProperty(element, property);
199
	}
200
201
202
	/* (non-Javadoc)
203
	 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#dispose()
204
	 */
205
	public void dispose() {
206
		super.dispose();
207
		this.styledLabelProvider.dispose();
208
	}
209
210
}
(-)src/org/eclipse/jface/viewers/DecoratingStyledCellLabelProvider.java (+326 lines)
Added Link Here
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
 *******************************************************************************/
11
package org.eclipse.jface.viewers;
12
13
import org.eclipse.core.runtime.Assert;
14
import org.eclipse.jface.resource.JFaceResources;
15
import org.eclipse.jface.resource.LocalResourceManager;
16
import org.eclipse.jface.resource.ResourceManager;
17
import org.eclipse.jface.viewers.StyledStringBuilder.Styler;
18
import org.eclipse.swt.graphics.Color;
19
import org.eclipse.swt.graphics.Font;
20
import org.eclipse.swt.graphics.Image;
21
22
23
/**
24
 * A {@link DecoratingStyledCellLabelProvider} is a {@link DelegatingStyledCellLabelProvider}
25
 * that uses a nested {@link DecoratingStyledCellLabelProvider.IStyledLabelProvider}
26
 * to compute styled text label and image and takes a {@link ILabelDecorator} to decorate the label.
27
 * 
28
 * <p>Use this label provider as a replacement for the {@link DecoratingLabelProvider} when
29
 * decorating styled text labels.</p>
30
 *
31
 * <p>The {@link DecoratingStyledCellLabelProvider} will try to evaluate the text decoration added
32
 * by the {@link ILabelDecorator} and will apply the style returned by {@link #getDecorationStyle(Object)}
33
 * </p>
34
 * <p>The {@link ILabelDecorator} can
35
 * optionally implement {@link IColorDecorator} and {@link IFontDecorator} to provide
36
 * foreground and background color and font decoration.
37
 * </p>
38
 * 
39
 * @since 3.4
40
 */
41
public class DecoratingStyledCellLabelProvider extends DelegatingStyledCellLabelProvider {
42
43
	private ILabelDecorator decorator;
44
	private IDecorationContext decorationContext;
45
	private ILabelProviderListener labelProviderListener;
46
	
47
	/**
48
	 * Creates a {@link DecoratingStyledCellLabelProvider} that delegates the requests for
49
	 * styled labels and for images to a {@link DelegatingStyledCellLabelProvider.IStyledLabelProvider}.
50
	 * 
51
	 * @param labelProvider the styled label provider
52
	 * @param decorator a label decorator or <code>null</code> to not decorate the label
53
	 * @param decorationContext a decoration context or <code>null</code> if the no decorator
54
	 * is configured or the default decorator should be used
55
	 */
56
	public DecoratingStyledCellLabelProvider(IStyledLabelProvider labelProvider, ILabelDecorator decorator, IDecorationContext decorationContext) {
57
		super(labelProvider);
58
		
59
		this.decorator= decorator;
60
		this.decorationContext= decorationContext;
61
		if (decorator != null && decorationContext == null) {
62
			decorationContext= createDefaultDecorationContext();
63
		}
64
		this.labelProviderListener= new ILabelProviderListener() {
65
			public void labelProviderChanged(LabelProviderChangedEvent event) {
66
				fireLabelProviderChanged(event);
67
			}
68
		};
69
		labelProvider.addListener(this.labelProviderListener);
70
		if (decorator != null)
71
			decorator.addListener(this.labelProviderListener);
72
	}
73
		
74
	private IDecorationContext createDefaultDecorationContext() {
75
		DecorationContext newContext = new DecorationContext();
76
		newContext.putProperty(DecorationContext.RESOURCE_MANAGER_KEY, new LocalResourceManager(JFaceResources.getResources()));
77
		return newContext;
78
	}
79
	
80
	/**
81
	 * Returns the decoration context associated with this label provider. It
82
	 * will be passed to the decorator if the decorator is an instance of
83
	 * {@link LabelDecorator}.
84
	 * 
85
	 * @return the decoration context associated with this label provider
86
	 */
87
	public IDecorationContext getDecorationContext() {
88
		return this.decorationContext;
89
	}
90
	
91
	/**
92
	 * Set the decoration context that will be based to the decorator for this
93
	 * label provider if that decorator implements {@link LabelDecorator}.
94
	 * 
95
	 * If this decorationContext has a {@link ResourceManager} stored for the
96
	 * {@link DecorationContext#RESOURCE_MANAGER_KEY} property it will be
97
	 * disposed when the label provider is disposed.
98
	 * 
99
	 * @param decorationContext
100
	 *            the decoration context.
101
	 */
102
	public void setDecorationContext(IDecorationContext decorationContext) {
103
		Assert.isNotNull(decorationContext);
104
		this.decorationContext = decorationContext;
105
	}
106
	
107
	private boolean waitForPendingDecoration(ViewerCell cell) {
108
		if (this.decorator == null)
109
			return false;
110
		
111
		Object element= cell.getElement();
112
		String oldText= cell.getText();
113
		
114
		boolean isDecorationPending= false;
115
		if (this.decorator instanceof LabelDecorator) {
116
			isDecorationPending= !((LabelDecorator) this.decorator).prepareDecoration(element, oldText, getDecorationContext());
117
		} else if (this.decorator instanceof IDelayedLabelDecorator) {
118
			isDecorationPending= !((IDelayedLabelDecorator) this.decorator).prepareDecoration(element, oldText);
119
		}
120
		if (isDecorationPending && oldText.length() == 0) {
121
			// item is empty: is shown for the first time: don't wait
122
			return false;
123
		}
124
		return isDecorationPending;
125
	}
126
	
127
	
128
	/* (non-Javadoc)
129
	 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
130
	 */
131
	public void update(ViewerCell cell) {
132
		if (waitForPendingDecoration(cell)) {
133
			return; // wait until the decoration is ready
134
		}
135
		super.update(cell);
136
	}
137
	
138
    /* (non-Javadoc)
139
     * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getForeground(java.lang.Object)
140
     */
141
	public Color getForeground(Object element) {
142
		if (this.decorator instanceof IColorDecorator) {
143
			Color foreground= ((IColorDecorator) this.decorator).decorateForeground(element);
144
			if (foreground != null)
145
				return foreground;
146
		}
147
		return super.getForeground(element);
148
	}
149
	
150
    /* (non-Javadoc)
151
     * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getBackground(java.lang.Object)
152
     */
153
	public Color getBackground(Object element) {
154
		if (this.decorator instanceof IColorDecorator) {
155
			Color color= ((IColorDecorator) this.decorator).decorateBackground(element);
156
			if (color != null)
157
				return color;
158
		}
159
		return super.getBackground(element);
160
	}
161
	
162
    /* (non-Javadoc)
163
     * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getFont(java.lang.Object)
164
     */
165
	public Font getFont(Object element) {
166
		if (this.decorator instanceof IFontDecorator) {
167
			Font font= ((IFontDecorator) this.decorator).decorateFont(element);
168
			if (font != null)
169
				return font;
170
		}
171
		return super.getFont(element);
172
	}
173
	
174
	
175
    /* (non-Javadoc)
176
     * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider#getImage(java.lang.Object)
177
     */
178
	public Image getImage(Object element) {
179
		Image image= super.getImage(element);
180
		Image decorated= null;
181
		if (this.decorator instanceof LabelDecorator) {
182
			decorated= ((LabelDecorator) this.decorator).decorateImage(image, element, getDecorationContext());
183
		} else {
184
			decorated= this.decorator.decorateImage(image, element);
185
		}
186
		if (decorated != null)
187
			return decorated;
188
		
189
		return image;
190
	}
191
192
    /**
193
     * Returns the styled text for the label of the given element.
194
     *
195
     * @param element the element for which to provide the styled label text
196
     * @return the styled text string used to label the element
197
     */
198
	public StyledStringBuilder getStyledText(Object element) {
199
		StyledStringBuilder string= super.getStyledText(element);
200
201
		String label= string.toString();
202
		String decorated;
203
		if (this.decorator instanceof LabelDecorator) {
204
			decorated= ((LabelDecorator) this.decorator).decorateText(label, element, getDecorationContext());
205
		} else {
206
			decorated= this.decorator.decorateText(label, element);
207
		}
208
		if (decorated == null)
209
			return string;
210
		
211
		int originalStart= decorated.indexOf(label);
212
		if (originalStart == -1) {
213
			return new StyledStringBuilder(decorated); // the decorator did something wild
214
		}
215
		
216
		if (decorated.length() == label.length())
217
			return string;
218
219
		Styler style= getDecorationStyle(element);
220
		if (originalStart > 0) {
221
			StyledStringBuilder newString= new StyledStringBuilder(decorated.substring(0, originalStart), style);
222
			newString.append(string);
223
			string= newString;
224
		}
225
		if (decorated.length() > originalStart + label.length()) { // decorator appended something
226
			return string.append(decorated.substring(originalStart + label.length()), style);
227
		}
228
		return string;
229
	}
230
		
231
	/**
232
	 * Sets the {@link StyledStringBuilder.Styler} to be used for string decorations. By default 
233
	 * the {@link StyledStringBuilder#DECORATIONS_STYLER decoration style}. Clients can override.
234
	 * 
235
	 * Note that it is the client's responsibility to react on color changes of the decoration color by
236
	 * refreshing the view
237
	 * 
238
	 * @param element the element that has been decorated
239
	 * 
240
	 * @return return the decoration style
241
	 */
242
	protected Styler getDecorationStyle(Object element) {
243
		return StyledStringBuilder.DECORATIONS_STYLER;
244
	}
245
		
246
	/**
247
	 * Returns the decorator or <code>null</code> if no decorator is installed
248
	 * 
249
	 * @return the decorator or <code>null</code> if no decorator is installed
250
	 */ 
251
	public ILabelDecorator getLabelDecorator() {
252
		return this.decorator;
253
	}
254
	
255
	/**
256
	 * Sets the label decorator. Removes all known listeners from the old
257
	 * decorator, and adds all known listeners to the new decorator. The old
258
	 * decorator is not disposed. Fires a label provider changed event
259
	 * indicating that all labels should be updated. Has no effect if the given
260
	 * decorator is identical to the current one.
261
	 * 
262
	 * @param newDecorator
263
	 *            the label decorator, or <code>null</code> if no decorations
264
	 *            are to be applied
265
	 */
266
	public void setLabelDecorator(ILabelDecorator newDecorator) {
267
		ILabelDecorator oldDecorator= this.decorator;
268
		if (oldDecorator != newDecorator) {
269
			if (oldDecorator != null)
270
				oldDecorator.removeListener(this.labelProviderListener);
271
			this.decorator= newDecorator;
272
			if (newDecorator != null) {
273
				newDecorator.addListener(this.labelProviderListener);
274
			}
275
		}
276
		fireLabelProviderChanged(new LabelProviderChangedEvent(this));
277
	}
278
	
279
	/* (non-Javadoc)
280
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
281
	 */
282
	public void addListener(ILabelProviderListener listener) {
283
		super.addListener(listener);
284
		if (this.decorator != null) {
285
			this.decorator.addListener(this.labelProviderListener);
286
		}
287
	}
288
	
289
	/* (non-Javadoc)
290
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
291
	 */
292
	public void removeListener(ILabelProviderListener listener) {
293
		super.removeListener(listener);
294
		if (this.decorator != null) {
295
			this.decorator.removeListener(this.labelProviderListener);
296
		}
297
	}
298
		
299
	/* (non-Javadoc)
300
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
301
	 */
302
	public boolean isLabelProperty(Object element, String property) {
303
		if (super.isLabelProperty(element, property)) {
304
			return true;
305
		}
306
		return this.decorator != null && this.decorator.isLabelProperty(element, property);
307
	}
308
		
309
	/* (non-Javadoc)
310
	 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#dispose()
311
	 */
312
	public void dispose() {
313
		super.dispose();
314
		if (this.decorationContext != null) {
315
			Object manager = this.decorationContext.getProperty(DecorationContext.RESOURCE_MANAGER_KEY);
316
			if (manager instanceof ResourceManager)
317
				((ResourceManager) manager).dispose();
318
		}
319
		if (this.decorator != null) {
320
			this.decorator.removeListener(this.labelProviderListener);
321
			this.decorator.dispose();
322
			this.decorator= null;
323
		}
324
	}
325
326
}
(-)Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet049SimpleStyledCellLabelProvider.java (-335 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Michael Krkoska - initial API and implementation (bug 188333)
11
 *******************************************************************************/
12
package org.eclipse.jface.snippets.viewers;
13
14
import java.io.File;
15
import java.text.MessageFormat;
16
import java.util.Date;
17
18
import org.eclipse.jface.viewers.CellLabelProvider;
19
import org.eclipse.jface.viewers.ColumnLabelProvider;
20
import org.eclipse.jface.viewers.ColumnViewer;
21
import org.eclipse.jface.viewers.ILabelProvider;
22
import org.eclipse.jface.viewers.ITreeContentProvider;
23
import org.eclipse.jface.viewers.SimpleStyledCellLabelProvider;
24
import org.eclipse.jface.viewers.TreeViewer;
25
import org.eclipse.jface.viewers.TreeViewerColumn;
26
import org.eclipse.jface.viewers.Viewer;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.custom.StyleRange;
29
import org.eclipse.swt.events.SelectionAdapter;
30
import org.eclipse.swt.events.SelectionEvent;
31
import org.eclipse.swt.graphics.Color;
32
import org.eclipse.swt.graphics.Image;
33
import org.eclipse.swt.layout.GridData;
34
import org.eclipse.swt.layout.GridLayout;
35
import org.eclipse.swt.widgets.Button;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Display;
38
import org.eclipse.swt.widgets.Label;
39
import org.eclipse.swt.widgets.Shell;
40
41
42
/**
43
 * Using a {@link SimpleStyledCellLabelProvider} on tree viewer. Compare the result with a native tree viewer.
44
 */
45
public class Snippet049SimpleStyledCellLabelProvider {
46
	
47
	
48
	private static final int SHELL_WIDTH= 640;
49
	private static final Display DISPLAY= Display.getDefault();
50
51
52
	public static void main(String[] args) {
53
54
		Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
55
		shell.setSize(SHELL_WIDTH, 300);
56
		shell.setLayout(new GridLayout(1, false));
57
58
		Snippet049SimpleStyledCellLabelProvider example= new Snippet049SimpleStyledCellLabelProvider();
59
		example.createPartControl(shell);
60
61
		shell.open();
62
63
		while (!shell.isDisposed()) {
64
			if (!DISPLAY.readAndDispatch()) {
65
				DISPLAY.sleep();
66
			}
67
		}
68
		DISPLAY.dispose();
69
	}
70
71
	public Snippet049SimpleStyledCellLabelProvider() {
72
	}
73
74
	public void createPartControl(Composite parent) {
75
		Composite composite= new Composite(parent, SWT.NONE);
76
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
77
		composite.setLayout(new GridLayout(2, true));
78
79
		ExampleLabelProvider labelProvider= new ExampleLabelProvider();
80
		ModifiedDateLabelProvider dateLabelProvider= new ModifiedDateLabelProvider();
81
82
		final ColumnViewer ownerDrawViewer= createViewer("Owner draw viewer:", composite, new DecoratingLabelProvider(labelProvider), new DecoratingDateLabelProvider(dateLabelProvider)); //$NON-NLS-1$
83
84
		final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, labelProvider, dateLabelProvider); //$NON-NLS-1$
85
86
		Composite buttons= new Composite(parent, SWT.NONE);
87
		buttons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
88
		buttons.setLayout(new GridLayout(3, false));
89
		
90
		
91
		Button button1= new Button(buttons, SWT.PUSH);
92
		button1.setText("Refresh Viewers"); //$NON-NLS-1$
93
		button1.addSelectionListener(new SelectionAdapter() {
94
95
			public void widgetSelected(SelectionEvent e) {
96
				ownerDrawViewer.refresh();
97
				normalViewer.refresh();
98
			}
99
		});
100
		
101
		final Button button2= new Button(buttons, SWT.CHECK);
102
		button2.setText("Owner draw on column 1"); //$NON-NLS-1$
103
		button2.setSelection(true);
104
		button2.addSelectionListener(new SelectionAdapter() {
105
106
			public void widgetSelected(SelectionEvent e) {
107
				boolean newState= button2.getSelection();
108
				((DecoratingLabelProvider) ownerDrawViewer.getLabelProvider(0)).setOwnerDrawEnabled(newState);
109
				ownerDrawViewer.refresh();
110
			}
111
		});
112
		
113
		final Button button3= new Button(buttons, SWT.CHECK);
114
		button3.setText("Owner draw on column 2"); //$NON-NLS-1$
115
		button3.setSelection(true);
116
		button3.addSelectionListener(new SelectionAdapter() {
117
118
			public void widgetSelected(SelectionEvent e) {
119
				boolean newState= button3.getSelection();
120
				((DecoratingDateLabelProvider) ownerDrawViewer.getLabelProvider(1)).setOwnerDrawEnabled(newState);
121
				ownerDrawViewer.refresh();
122
			}
123
		});
124
125
	}
126
	
127
	private static class FileSystemRoot {
128
		public File[] getRoots() {
129
			return File.listRoots();
130
		}
131
	}
132
133
	private ColumnViewer createViewer(String description, Composite parent, CellLabelProvider labelProvider1, CellLabelProvider labelProvider2) {
134
135
		Composite composite= new Composite(parent, SWT.NONE);
136
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
137
		composite.setLayout(new GridLayout(1, true));
138
139
		Label label= new Label(composite, SWT.NONE);
140
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
141
		label.setText(description);
142
143
		TreeViewer treeViewer= new TreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
144
		treeViewer.getTree().setHeaderVisible(true);
145
		treeViewer.setContentProvider(new FileSystemContentProvider());
146
		
147
		TreeViewerColumn tvc1 = new TreeViewerColumn(treeViewer, SWT.NONE);
148
		tvc1.getColumn().setText("Name");
149
		tvc1.getColumn().setWidth(200);
150
		tvc1.setLabelProvider(labelProvider1);
151
152
		TreeViewerColumn tvc2 = new TreeViewerColumn(treeViewer, SWT.NONE);
153
		tvc2.getColumn().setText("Date Modified");
154
		tvc2.getColumn().setWidth(200);
155
		tvc2.setLabelProvider(labelProvider2);
156
		
157
		GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
158
		treeViewer.getControl().setLayoutData(data);
159
160
		treeViewer.setInput(new FileSystemRoot());
161
162
		return treeViewer;
163
	}
164
	
165
	/**
166
	 * Implements a {@link SimpleStyledCellLabelProvider} that wraps a normal label
167
	 * provider and adds some decorations in color
168
	 */
169
	private static class DecoratingLabelProvider extends SimpleStyledCellLabelProvider {
170
171
		private static final StyleRange[] NO_RANGES= new StyleRange[0];
172
		private final ILabelProvider fWrappedLabelProvider;
173
		
174
		public DecoratingLabelProvider(ILabelProvider labelProvider) {
175
			fWrappedLabelProvider= labelProvider;
176
		}
177
178
		protected LabelPresentationInfo getLabelPresentationInfo(Object element) {
179
			String text= fWrappedLabelProvider.getText(element);
180
			Image image= fWrappedLabelProvider.getImage(element);
181
182
			
183
			StyleRange[] ranges= NO_RANGES;
184
			if (element instanceof File) {
185
				File file= (File) element;
186
				if (file.isFile()) {
187
					String decoration= MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
188
										
189
					int decorationStart= text.length();
190
					int decorationLength= decoration.length();
191
192
					text+= decoration;
193
					
194
					Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_DARK_BLUE);
195
					
196
					StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null);
197
					ranges= new StyleRange[] { styleRange };
198
				}
199
			}
200
			return new LabelPresentationInfo(text, ranges, image, null, null, null);
201
		}
202
		
203
		public void dispose() {
204
			super.dispose();
205
			fWrappedLabelProvider.dispose();
206
		}
207
	}
208
	
209
	private static class DecoratingDateLabelProvider extends SimpleStyledCellLabelProvider {
210
		
211
		private static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
212
		private static final StyleRange[] NO_RANGES= new StyleRange[0];
213
		private final ILabelProvider fWrappedLabelProvider;
214
		
215
		public DecoratingDateLabelProvider(ILabelProvider labelProvider) {
216
			fWrappedLabelProvider= labelProvider;
217
		}
218
		
219
		protected LabelPresentationInfo getLabelPresentationInfo(Object element) {
220
			String text= fWrappedLabelProvider.getText(element);
221
			Image image= fWrappedLabelProvider.getImage(element);
222
			
223
			StyleRange[] ranges= NO_RANGES;
224
			if (element instanceof File) {
225
				File file= (File) element;
226
				String decoration= " " + DAYS[new Date(file.lastModified()).getDay()]; 
227
				
228
				int decorationStart= text.length();
229
				int decorationLength= decoration.length();
230
				
231
				text+= decoration;
232
				
233
				Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
234
				
235
				StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null);
236
				ranges= new StyleRange[] { styleRange };
237
			}
238
			return new LabelPresentationInfo(text, ranges, image, null, null, null);
239
		}
240
		
241
		public void dispose() {
242
			super.dispose();
243
			fWrappedLabelProvider.dispose();
244
		}
245
	}
246
	
247
248
	/**
249
	 * A simple label provider
250
	 */
251
	private static class ExampleLabelProvider extends ColumnLabelProvider {
252
		
253
		private static int IMAGE_SIZE= 16;
254
		private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
255
		private static final Image IMAGE2= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
256
257
		public Image getImage(Object element) {
258
			if (element instanceof File) {
259
				File file= (File) element;
260
				if (file.isDirectory()) {
261
					return IMAGE1;
262
				} else {
263
					return IMAGE2;
264
				}
265
			}
266
			return null;
267
		}
268
269
		public String getText(Object element) {
270
			if (element instanceof File) {
271
				File file= (File) element;
272
				if (file.getName().length() == 0) {
273
					return file.getAbsolutePath();
274
				}
275
				return file.getName();
276
			}
277
			return "null"; //$NON-NLS-1$
278
		}
279
280
	}
281
	
282
	private static class ModifiedDateLabelProvider extends ColumnLabelProvider {
283
		public String getText(Object element) {
284
			if (element instanceof File) {
285
				File file= (File) element;
286
				return new Date(file.lastModified()).toLocaleString();
287
			}
288
			return "-"; //$NON-NLS-1$
289
		}
290
	}
291
	
292
	private static class FileSystemContentProvider implements ITreeContentProvider {
293
294
		public Object[] getChildren(Object element) {
295
			if (element instanceof File) {
296
				File file= (File) element;
297
				if (file.isDirectory()) {
298
					File[] listFiles= file.listFiles();
299
					if (listFiles != null) {
300
						return listFiles;
301
					}
302
				}
303
			} else if (element instanceof FileSystemRoot) {
304
				return ((FileSystemRoot) element).getRoots();
305
			}
306
			return new Object[0];
307
		}
308
309
		public Object getParent(Object element) {
310
			if (element instanceof File) {
311
				File file= (File) element;
312
				return file.getParentFile();
313
			}
314
			return null;
315
		}
316
317
		public boolean hasChildren(Object element) {
318
			return getChildren(element).length > 0;
319
		}
320
321
		public Object[] getElements(Object inputElement) {
322
			return getChildren(inputElement);
323
		}
324
325
		public void dispose() {
326
		}
327
328
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
329
		}
330
	}
331
332
333
334
335
}
(-)Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet050SimpleStyledCellLabelProvider.java (-245 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Michael Krkoska - initial API and implementation (bug 188333)
11
 *******************************************************************************/
12
package org.eclipse.jface.snippets.viewers;
13
14
import java.io.File;
15
import java.text.MessageFormat;
16
17
import org.eclipse.jface.viewers.ColumnViewer;
18
import org.eclipse.jface.viewers.IBaseLabelProvider;
19
import org.eclipse.jface.viewers.ILabelProvider;
20
import org.eclipse.jface.viewers.ITreeContentProvider;
21
import org.eclipse.jface.viewers.LabelProvider;
22
import org.eclipse.jface.viewers.SimpleStyledCellLabelProvider;
23
import org.eclipse.jface.viewers.TableViewer;
24
import org.eclipse.jface.viewers.Viewer;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.custom.StyleRange;
27
import org.eclipse.swt.graphics.Color;
28
import org.eclipse.swt.graphics.Image;
29
import org.eclipse.swt.layout.GridData;
30
import org.eclipse.swt.layout.GridLayout;
31
import org.eclipse.swt.widgets.Button;
32
import org.eclipse.swt.widgets.Composite;
33
import org.eclipse.swt.widgets.Display;
34
import org.eclipse.swt.widgets.Event;
35
import org.eclipse.swt.widgets.Label;
36
import org.eclipse.swt.widgets.Listener;
37
import org.eclipse.swt.widgets.Shell;
38
39
/**
40
 * Using a {@link SimpleStyledCellLabelProvider} on table viewer. Compare the result with a native table viewer.
41
 */
42
43
public class Snippet050SimpleStyledCellLabelProvider {
44
	
45
	
46
	private static final int SHELL_WIDTH= 640;
47
	private static final Display DISPLAY= Display.getDefault();
48
49
50
	public static void main(String[] args) {
51
52
		Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
53
		shell.setSize(SHELL_WIDTH, 300);
54
		shell.setLayout(new GridLayout(1, false));
55
56
		Snippet050SimpleStyledCellLabelProvider example= new Snippet050SimpleStyledCellLabelProvider();
57
		example.createPartControl(shell);
58
59
		shell.open();
60
61
		while (!shell.isDisposed()) {
62
			if (!DISPLAY.readAndDispatch()) {
63
				DISPLAY.sleep();
64
			}
65
		}
66
		DISPLAY.dispose();
67
	}
68
69
	public Snippet050SimpleStyledCellLabelProvider() {
70
	}
71
72
	public void createPartControl(Composite parent) {
73
		Composite composite= new Composite(parent, SWT.NONE);
74
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
75
		composite.setLayout(new GridLayout(2, true));
76
77
		ExampleLabelProvider labelProvider= new ExampleLabelProvider();
78
79
		final ColumnViewer ownerDrawViewer= createViewer("Owner draw viewer:", composite, new DecoratingLabelProvider(labelProvider)); //$NON-NLS-1$
80
81
		final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, labelProvider); //$NON-NLS-1$
82
83
		Button button= new Button(parent, SWT.NONE);
84
		button.setText("Refresh Viewers"); //$NON-NLS-1$
85
		button.addListener(SWT.Modify, new Listener() {
86
87
			public void handleEvent(Event event) {
88
				ownerDrawViewer.refresh();
89
				normalViewer.refresh();
90
			}
91
		});
92
93
	}
94
95
	private ColumnViewer createViewer(String description, Composite parent, IBaseLabelProvider labelProviders) {
96
97
		Composite composite= new Composite(parent, SWT.NONE);
98
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
99
		composite.setLayout(new GridLayout(1, true));
100
101
		Label label= new Label(composite, SWT.NONE);
102
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
103
		label.setText(description);
104
105
		TableViewer tableViewer= new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
106
		tableViewer.setContentProvider(new FileSystemContentProvider());
107
		tableViewer.setLabelProvider(labelProviders);
108
109
		GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
110
		tableViewer.getControl().setLayoutData(data);
111
		File[] roots = File.listRoots();
112
		File root = null;
113
		for (int i = 0; i < roots.length; i++) {
114
			String[] list = roots[i].list();
115
			if (list != null && list.length > 0) {
116
				root = roots[i];
117
				break;
118
			}
119
		}
120
		if (root == null) {
121
			throw new RuntimeException("couldn't get a non-empty root file");
122
		}
123
		tableViewer.setInput(root);
124
125
		return tableViewer;
126
	}
127
	
128
	/**
129
	 * Implements a {@link SimpleStyledCellLabelProvider} that wraps a normal label
130
	 * provider and adds some decorations in color
131
	 */
132
	private static class DecoratingLabelProvider extends SimpleStyledCellLabelProvider {
133
134
		private static final StyleRange[] NO_RANGES= new StyleRange[0];
135
		private final ILabelProvider fWrappedLabelProvider;
136
		
137
		public DecoratingLabelProvider(ILabelProvider labelProvider) {
138
			fWrappedLabelProvider= labelProvider;
139
		}
140
141
		protected LabelPresentationInfo getLabelPresentationInfo(Object element) {
142
			String text= fWrappedLabelProvider.getText(element);
143
			Image image= fWrappedLabelProvider.getImage(element);
144
145
			
146
			StyleRange[] ranges= NO_RANGES;
147
			if (element instanceof File) {
148
				File file= (File) element;
149
				if (file.isFile()) {
150
					String decoration= MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
151
										
152
					int decorationStart= text.length();
153
					int decorationLength= decoration.length();
154
155
					text+= decoration;
156
					
157
					Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_DARK_BLUE);
158
					
159
					StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null);
160
					ranges= new StyleRange[] { styleRange };
161
				}
162
			}
163
			return new LabelPresentationInfo(text, ranges, image, null, null, null);
164
		}
165
		
166
		public void dispose() {
167
			super.dispose();
168
			fWrappedLabelProvider.dispose();
169
		}
170
	}
171
	
172
173
	/**
174
	 * A simple label provider
175
	 */
176
	private static class ExampleLabelProvider extends LabelProvider {
177
		
178
		private static int IMAGE_SIZE= 16;
179
		private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
180
		private static final Image IMAGE2= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
181
182
		public Image getImage(Object element) {
183
			if (element instanceof File) {
184
				File file= (File) element;
185
				if (file.isDirectory()) {
186
					return IMAGE1;
187
				} else {
188
					return IMAGE2;
189
				}
190
			}
191
			return null;
192
		}
193
194
		public String getText(Object element) {
195
			if (element instanceof File) {
196
				File file= (File) element;
197
				return file.getName();
198
			}
199
			return "null"; //$NON-NLS-1$
200
		}
201
202
	}
203
	
204
	private static class FileSystemContentProvider implements ITreeContentProvider {
205
206
		public Object[] getChildren(Object element) {
207
			if (element instanceof File) {
208
				File file= (File) element;
209
				if (file.isDirectory()) {
210
					File[] listFiles= file.listFiles();
211
					if (listFiles != null) {
212
						return listFiles;
213
					}
214
				}
215
			}
216
			return new Object[0];
217
		}
218
219
		public Object getParent(Object element) {
220
			if (element instanceof File) {
221
				File file= (File) element;
222
				return file.getParentFile();
223
			}
224
			return null;
225
		}
226
227
		public boolean hasChildren(Object element) {
228
			return getChildren(element).length > 0;
229
		}
230
231
		public Object[] getElements(Object inputElement) {
232
			return getChildren(inputElement);
233
		}
234
235
		public void dispose() {
236
		}
237
238
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
239
		}
240
	}
241
242
243
244
245
}
(-)Eclipse (+268 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 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
 *     Michael Krkoska - initial API and implementation (bug 188333)
11
 *******************************************************************************/
12
package org.eclipse.jface.snippets.viewers;
13
14
import java.io.File;
15
import java.text.DateFormat;
16
import java.text.MessageFormat;
17
import java.util.Date;
18
19
import org.eclipse.jface.preference.JFacePreferences;
20
import org.eclipse.jface.resource.JFaceResources;
21
import org.eclipse.jface.viewers.*;
22
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.SelectionAdapter;
25
import org.eclipse.swt.events.SelectionEvent;
26
import org.eclipse.swt.graphics.Image;
27
import org.eclipse.swt.graphics.RGB;
28
import org.eclipse.swt.layout.GridData;
29
import org.eclipse.swt.layout.GridLayout;
30
import org.eclipse.swt.widgets.Button;
31
import org.eclipse.swt.widgets.Composite;
32
import org.eclipse.swt.widgets.Display;
33
import org.eclipse.swt.widgets.Label;
34
import org.eclipse.swt.widgets.Shell;
35
36
37
/**
38
 * Using a {@link DelegatingStyledCellLabelProvider} on tree viewer with multiple columns. Compare the result with a native tree viewer.
39
 */
40
public class Snippet050DelegatingStyledCellLabelProvider {
41
	
42
	
43
	private static final int SHELL_WIDTH= 640;
44
	private static final Display DISPLAY= Display.getDefault();
45
46
47
	public static void main(String[] args) {
48
		
49
		JFaceResources.getColorRegistry().put(JFacePreferences.COUNTER_COLOR, new RGB(0,127,174));
50
		
51
		
52
53
		Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
54
		shell.setSize(SHELL_WIDTH, 300);
55
		shell.setLayout(new GridLayout(1, false));
56
57
		Snippet050DelegatingStyledCellLabelProvider example= new Snippet050DelegatingStyledCellLabelProvider();
58
		example.createPartControl(shell);
59
60
		shell.open();
61
62
		while (!shell.isDisposed()) {
63
			if (!DISPLAY.readAndDispatch()) {
64
				DISPLAY.sleep();
65
			}
66
		}
67
		DISPLAY.dispose();
68
	}
69
70
	public Snippet050DelegatingStyledCellLabelProvider() {
71
	}
72
73
	public void createPartControl(Composite parent) {
74
		Composite composite= new Composite(parent, SWT.NONE);
75
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
76
		composite.setLayout(new GridLayout(2, true));
77
		
78
		final DelegatingStyledCellLabelProvider styledCellLP1= new DelegatingStyledCellLabelProvider(new NameAndSizeLabelProvider());
79
		final DelegatingStyledCellLabelProvider styledCellLP2= new DelegatingStyledCellLabelProvider(new ModifiedDateLabelProvider());
80
		final ColumnViewer ownerDrawViewer= createViewer("Owner draw viewer:", composite, styledCellLP1, styledCellLP2); //$NON-NLS-1$
81
82
		CellLabelProvider normalLP1= new NameAndSizeLabelProvider();
83
		CellLabelProvider normalLP2= new ModifiedDateLabelProvider();
84
		final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, normalLP1, normalLP2); //$NON-NLS-1$
85
86
		Composite buttons= new Composite(parent, SWT.NONE);
87
		buttons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
88
		buttons.setLayout(new GridLayout(3, false));
89
		
90
		
91
		Button button1= new Button(buttons, SWT.PUSH);
92
		button1.setText("Refresh Viewers"); //$NON-NLS-1$
93
		button1.addSelectionListener(new SelectionAdapter() {
94
95
			public void widgetSelected(SelectionEvent e) {
96
				ownerDrawViewer.refresh();
97
				normalViewer.refresh();
98
			}
99
		});
100
		
101
		final Button button2= new Button(buttons, SWT.CHECK);
102
		button2.setText("Owner draw on column 1"); //$NON-NLS-1$
103
		button2.setSelection(true);
104
		button2.addSelectionListener(new SelectionAdapter() {
105
106
			public void widgetSelected(SelectionEvent e) {
107
				boolean newState= button2.getSelection();
108
				styledCellLP1.setOwnerDrawEnabled(newState);
109
				ownerDrawViewer.refresh();
110
			}
111
		});
112
		
113
		final Button button3= new Button(buttons, SWT.CHECK);
114
		button3.setText("Owner draw on column 2"); //$NON-NLS-1$
115
		button3.setSelection(true);
116
		button3.addSelectionListener(new SelectionAdapter() {
117
118
			public void widgetSelected(SelectionEvent e) {
119
				boolean newState= button3.getSelection();
120
				styledCellLP2.setOwnerDrawEnabled(newState);
121
				ownerDrawViewer.refresh();
122
			}
123
		});
124
	}
125
	
126
	private static class FileSystemRoot {
127
		public File[] getRoots() {
128
			return File.listRoots();
129
		}
130
	}
131
132
	private ColumnViewer createViewer(String description, Composite parent, CellLabelProvider labelProvider1, CellLabelProvider labelProvider2) {
133
134
		Composite composite= new Composite(parent, SWT.NONE);
135
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
136
		composite.setLayout(new GridLayout(1, true));
137
138
		Label label= new Label(composite, SWT.NONE);
139
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
140
		label.setText(description);
141
142
		TreeViewer treeViewer= new TreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
143
		treeViewer.getTree().setHeaderVisible(true);
144
		treeViewer.setContentProvider(new FileSystemContentProvider());
145
		
146
		TreeViewerColumn tvc1 = new TreeViewerColumn(treeViewer, SWT.NONE);
147
		tvc1.getColumn().setText("Name"); //$NON-NLS-1$
148
		tvc1.getColumn().setWidth(200);
149
		tvc1.setLabelProvider(labelProvider1);
150
151
		TreeViewerColumn tvc2 = new TreeViewerColumn(treeViewer, SWT.NONE);
152
		tvc2.getColumn().setText("Date Modified"); //$NON-NLS-1$
153
		tvc2.getColumn().setWidth(200);
154
		tvc2.setLabelProvider(labelProvider2);
155
		
156
		GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
157
		treeViewer.getControl().setLayoutData(data);
158
159
		treeViewer.setInput(new FileSystemRoot());
160
161
		return treeViewer;
162
	}
163
164
	/**
165
	 * A simple label provider
166
	 */
167
	private static class NameAndSizeLabelProvider extends ColumnLabelProvider implements IStyledLabelProvider {
168
		
169
		private static int IMAGE_SIZE= 16;
170
		private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
171
		private static final Image IMAGE2= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
172
173
		public Image getImage(Object element) {
174
			if (element instanceof File) {
175
				File file= (File) element;
176
				if (file.isDirectory()) {
177
					return IMAGE1;
178
				} else {
179
					return IMAGE2;
180
				}
181
			}
182
			return null;
183
		}
184
185
		public String getText(Object element) {
186
			return getStyledText(element).toString();
187
		}
188
189
		public StyledStringBuilder getStyledText(Object element) {
190
			StyledStringBuilder styledString= new StyledStringBuilder();
191
			if (element instanceof File) {
192
				File file= (File) element;
193
				if (file.getName().length() == 0) {
194
					styledString.append(file.getAbsolutePath());
195
				} else {
196
					styledString.append(file.getName());
197
				}
198
				if (file.isFile()) {
199
					String decoration= MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
200
					styledString.append(decoration, StyledStringBuilder.COUNTER_STYLER);
201
				}
202
			}	
203
			return styledString;
204
		}
205
	}
206
	
207
	private static class ModifiedDateLabelProvider extends ColumnLabelProvider implements IStyledLabelProvider {
208
		public String getText(Object element) {
209
			return getStyledText(element).toString();
210
		}
211
		
212
		public StyledStringBuilder getStyledText(Object element) {
213
			StyledStringBuilder styledString= new StyledStringBuilder();
214
			if (element instanceof File) {
215
				File file= (File) element;
216
				
217
				String date= DateFormat.getDateInstance().format(new Date(file.lastModified()));
218
				styledString.append(date);
219
				
220
				styledString.append(' ');
221
				
222
				String time = DateFormat.getTimeInstance(3).format(new Date(file.lastModified()));
223
				styledString.append(time, StyledStringBuilder.COUNTER_STYLER);
224
			}
225
			return styledString;
226
		}
227
	}
228
	
229
	private static class FileSystemContentProvider implements ITreeContentProvider {
230
231
		public Object[] getChildren(Object element) {
232
			if (element instanceof File) {
233
				File file= (File) element;
234
				if (file.isDirectory()) {
235
					File[] listFiles= file.listFiles();
236
					if (listFiles != null) {
237
						return listFiles;
238
					}
239
				}
240
			} else if (element instanceof FileSystemRoot) {
241
				return ((FileSystemRoot) element).getRoots();
242
			}
243
			return new Object[0];
244
		}
245
246
		public Object getParent(Object element) {
247
			if (element instanceof File) {
248
				File file= (File) element;
249
				return file.getParentFile();
250
			}
251
			return null;
252
		}
253
254
		public boolean hasChildren(Object element) {
255
			return getChildren(element).length > 0;
256
		}
257
258
		public Object[] getElements(Object inputElement) {
259
			return getChildren(inputElement);
260
		}
261
262
		public void dispose() {
263
		}
264
265
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
266
		}
267
	}
268
}
(-)Eclipse (+151 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 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
 *     Michael Krkoska - initial API and implementation (bug 188333)
11
 *******************************************************************************/
12
package org.eclipse.jface.snippets.viewers;
13
14
import java.io.File;
15
import java.text.MessageFormat;
16
17
import org.eclipse.jface.preference.JFacePreferences;
18
import org.eclipse.jface.resource.JFaceResources;
19
import org.eclipse.jface.viewers.IStructuredContentProvider;
20
import org.eclipse.jface.viewers.StyledCellLabelProvider;
21
import org.eclipse.jface.viewers.StyledStringBuilder;
22
import org.eclipse.jface.viewers.TableViewer;
23
import org.eclipse.jface.viewers.Viewer;
24
import org.eclipse.jface.viewers.ViewerCell;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.graphics.Image;
27
import org.eclipse.swt.graphics.RGB;
28
import org.eclipse.swt.layout.GridData;
29
import org.eclipse.swt.layout.GridLayout;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.swt.widgets.Control;
32
import org.eclipse.swt.widgets.Display;
33
import org.eclipse.swt.widgets.Label;
34
import org.eclipse.swt.widgets.Shell;
35
36
/**
37
 * Using a {@link StyledCellLabelProvider} on table viewer.
38
 */
39
40
public class Snippet049StyledCellLabelProvider {
41
	
42
	
43
	private static final int SHELL_WIDTH= 400;
44
	private static final Display DISPLAY= Display.getDefault();
45
46
47
	public static void main(String[] args) {
48
49
		JFaceResources.getColorRegistry().put(JFacePreferences.COUNTER_COLOR, new RGB(0,127,174));
50
		
51
		Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE);
52
		shell.setSize(SHELL_WIDTH, 400);
53
		shell.setLayout(new GridLayout(1, false));
54
55
		Snippet049StyledCellLabelProvider example= new Snippet049StyledCellLabelProvider();
56
		Control composite= example.createPartControl(shell);
57
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
58
		
59
		shell.open();
60
61
		while (!shell.isDisposed()) {
62
			if (!DISPLAY.readAndDispatch()) {
63
				DISPLAY.sleep();
64
			}
65
		}
66
		DISPLAY.dispose();
67
	}
68
69
	public Snippet049StyledCellLabelProvider() {
70
	}
71
72
	public Composite createPartControl(Composite parent) {
73
		Composite composite= new Composite(parent, SWT.NONE);
74
75
		composite.setLayout(new GridLayout(1, true));
76
77
		Label label= new Label(composite, SWT.NONE);
78
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
79
		label.setText("Viewer with a StyledCellLabelProvider:"); //$NON-NLS-1$
80
81
		ExampleLabelProvider labelProvider= new ExampleLabelProvider();
82
		FileSystemContentProvider contentProvider= new FileSystemContentProvider();
83
		
84
		final TableViewer tableViewer= new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
85
86
		tableViewer.setContentProvider(contentProvider);
87
		tableViewer.setLabelProvider(labelProvider);
88
89
		GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
90
		tableViewer.getControl().setLayoutData(data);
91
		tableViewer.setInput(new Object());
92
93
		return composite;
94
	}
95
	
96
	private static class ExampleLabelProvider extends StyledCellLabelProvider {
97
98
		private static int IMAGE_SIZE= 16;
99
		private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
100
		private static final Image IMAGE2= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE));
101
102
		
103
		public ExampleLabelProvider() {
104
		}
105
		
106
		public void update(ViewerCell cell) {
107
			Object element= cell.getElement();
108
			
109
			if (element instanceof File) {
110
				File file= (File) element;
111
				
112
				StyledStringBuilder styledString= new StyledStringBuilder(file.getName());
113
				String decoration = MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$
114
				styledString.append(decoration, StyledStringBuilder.COUNTER_STYLER);
115
				
116
				cell.setText(styledString.toString());
117
				cell.setStyleRanges(styledString.toStyleRanges());
118
				
119
				if (file.isDirectory()) {
120
					cell.setImage(IMAGE1);
121
				} else {
122
					cell.setImage(IMAGE2);
123
				}
124
			} else {
125
				cell.setText("Unknown element"); //$NON-NLS-1$
126
			}
127
128
			super.update(cell);
129
		}
130
	}
131
132
	private static class FileSystemContentProvider implements IStructuredContentProvider {
133
134
		public Object[] getElements(Object element) {
135
			File[] roots = File.listRoots();
136
			for (int i = 0; i < roots.length; i++) {
137
				File[] list = roots[i].listFiles();
138
				if (list != null && list.length > 0) {
139
					return list;
140
				}
141
			}
142
			return roots;
143
		}
144
145
		public void dispose() {
146
		}
147
148
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
149
		}
150
	}
151
}

Return to bug 219393