Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 28d4f3e050101a251471d879956936f7f5e52a32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.viewers;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * OwnerDrawLabelProvider is an abstract implementation of a label provider that
 * handles custom draw.
 * 
 * <p>
 * <b>This class is intended to be subclassed by implementors.</b>
 * </p>
 * @param <E> Type of an element of the model
 * @param <I> Type of the input
 * 
 * @since 3.3
 * 
 */
public abstract class OwnerDrawLabelProvider<E,I> extends CellLabelProvider<E,I> {

	static class OwnerDrawListener<E,I> implements Listener {
		Set<ViewerColumn<E,I>> enabledColumns = new HashSet<ViewerColumn<E,I>>();
		int enabledGlobally = 0;
		private ColumnViewer<E,I> viewer;

		OwnerDrawListener(ColumnViewer<E,I> viewer) {
			this.viewer = viewer;
		}

		public void handleEvent(Event event) {
			ViewerColumn<E,I> column = viewer.getViewerColumn(event.index);
			if (column != null && (enabledGlobally > 0 || enabledColumns.contains(column))) {
				CellLabelProvider<E,I> provider = column.getLabelProvider();
				if (provider instanceof OwnerDrawLabelProvider) {
					@SuppressWarnings("unchecked")
					E element = (E) event.item.getData();
					OwnerDrawLabelProvider<E,I> ownerDrawProvider = (OwnerDrawLabelProvider<E,I>) provider;
					switch (event.type) {
					case SWT.MeasureItem:
						ownerDrawProvider.measure(event, element);
						break;
					case SWT.PaintItem:
						ownerDrawProvider.paint(event, element);
						break;
					case SWT.EraseItem:
						ownerDrawProvider.erase(event, element);
						break;
					}
				}
			}
		}
	}

	private static final String OWNER_DRAW_LABEL_PROVIDER_LISTENER = "owner_draw_label_provider_listener"; //$NON-NLS-1$

	/**
	 * Set up the owner draw callbacks for the viewer.
	 * 
	 * @param viewer
	 *            the viewer the owner draw is set up
	 * 
	 * @deprecated Since 3.4, the default implementation of
	 *             {@link CellLabelProvider#initialize(ColumnViewer, ViewerColumn)}
	 *             in this class will set up the necessary owner draw callbacks
	 *             automatically. Calls to this method can be removed.
	 */
	@Deprecated
	public static void setUpOwnerDraw(final ColumnViewer viewer) {
		getOrCreateOwnerDrawListener(viewer).enabledGlobally++;
	}

	private static <E,I> OwnerDrawListener<E,I>  getOrCreateOwnerDrawListener(
			final ColumnViewer<E,I> viewer) {
		Control control = viewer.getControl();
		@SuppressWarnings("unchecked")
		OwnerDrawListener<E,I> listener = (OwnerDrawListener<E,I>) control
				.getData(OWNER_DRAW_LABEL_PROVIDER_LISTENER);
		if (listener == null) {
			listener = new OwnerDrawListener<E,I>(viewer);
			control.setData(OWNER_DRAW_LABEL_PROVIDER_LISTENER, listener);
			control.addListener(SWT.MeasureItem, listener);
			control.addListener(SWT.EraseItem, listener);
			control.addListener(SWT.PaintItem, listener);
		}
		return listener;
	}

	/**
	 * Create a new instance of the receiver based on a column viewer.
	 * 
	 */
	public OwnerDrawLabelProvider() {

	}

	@Override
	public void dispose(ColumnViewer<E,I> viewer, ViewerColumn<E,I> column) {
		if (!viewer.getControl().isDisposed()) {
			setOwnerDrawEnabled(viewer, column, false);
		}
		super.dispose(viewer, column);
	}

	/**
	 * This implementation of
	 * {@link CellLabelProvider#initialize(ColumnViewer, ViewerColumn)}
	 * delegates to {@link #initialize(ColumnViewer, ViewerColumn, boolean)}
	 * with a value of <code>true</code> for <code>enableOwnerDraw</code>.
	 * Subclasses may override this method but should either call the super
	 * implementation or, alternatively,
	 * {@link #initialize(ColumnViewer, ViewerColumn, boolean)}.
	 */
	@Override
	protected void initialize(ColumnViewer<E,I> viewer, ViewerColumn<E,I> column) {
		this.initialize(viewer, column, true);
	}

	/**
	 * May be called from subclasses that override
	 * {@link #initialize(ColumnViewer, ViewerColumn)} but want to customize
	 * whether owner draw will be enabled. This method calls
	 * <code>super.initialize(ColumnViewer, ViewerColumn)</code>, and then
	 * enables or disables owner draw by calling
	 * {@link #setOwnerDrawEnabled(ColumnViewer, ViewerColumn, boolean)}.
	 * 
	 * @param viewer
	 *            the viewer
	 * @param column
	 *            the column, or <code>null</code> if a column is not
	 *            available.
	 * @param enableOwnerDraw
	 *            <code>true</code> if owner draw should be enabled for the
	 *            given viewer and column, <code>false</code> otherwise.
	 * 
	 * @since 3.4
	 */
	final protected void initialize(ColumnViewer<E,I> viewer, ViewerColumn<E,I> column,
			boolean enableOwnerDraw) {
		super.initialize(viewer, column);
		setOwnerDrawEnabled(viewer, column, enableOwnerDraw);
	}

	@Override
	public void update(ViewerCell<E> cell) {
		// Force a redraw
		Rectangle cellBounds = cell.getBounds();
		cell.getControl().redraw(cellBounds.x, cellBounds.y, cellBounds.width,
				cellBounds.height, true);

	}

	/**
	 * Handle the erase event. The default implementation colors the background
	 * of selected areas with {@link SWT#COLOR_LIST_SELECTION} and foregrounds
	 * with {@link SWT#COLOR_LIST_SELECTION_TEXT}. Note that this
	 * implementation causes non-native behavior on some platforms. Subclasses
	 * should override this method and <b>not</b> call the super
	 * implementation.
	 * 
	 * @param event
	 *            the erase event
	 * @param element
	 *            the model object
	 * @see SWT#EraseItem
	 * @see SWT#COLOR_LIST_SELECTION
	 * @see SWT#COLOR_LIST_SELECTION_TEXT
	 */
	protected void erase(Event event, E element) {

		Rectangle bounds = event.getBounds();
		if ((event.detail & SWT.SELECTED) != 0) {

			Color oldForeground = event.gc.getForeground();
			Color oldBackground = event.gc.getBackground();

			event.gc.setBackground(event.item.getDisplay().getSystemColor(
					SWT.COLOR_LIST_SELECTION));
			event.gc.setForeground(event.item.getDisplay().getSystemColor(
					SWT.COLOR_LIST_SELECTION_TEXT));
			event.gc.fillRectangle(bounds);
			/* restore the old GC colors */
			event.gc.setForeground(oldForeground);
			event.gc.setBackground(oldBackground);
			/* ensure that default selection is not drawn */
			event.detail &= ~SWT.SELECTED;

		}

	}

	/**
	 * Handle the measure event.
	 * 
	 * @param event
	 *            the measure event
	 * @param element
	 *            the model element
	 * @see SWT#MeasureItem
	 */
	protected abstract void measure(Event event, E element);

	/**
	 * Handle the paint event.
	 * 
	 * @param event
	 *            the paint event
	 * @param element
	 *            the model element
	 * @see SWT#PaintItem
	 */
	protected abstract void paint(Event event, E element);

	/**
	 * Enables or disables owner draw for the given viewer and column. This
	 * method will attach or remove a listener to the underlying control as
	 * necessary. This method is called from
	 * {@link #initialize(ColumnViewer, ViewerColumn)} and
	 * {@link #dispose(ColumnViewer, ViewerColumn)} but may be called from
	 * subclasses to enable or disable owner draw dynamically.
	 * 
	 * @param viewer
	 *            the viewer
	 * @param column
	 *            the column, or <code>null</code> if a column is not
	 *            available
	 * @param enabled
	 *            <code>true</code> if owner draw should be enabled,
	 *            <code>false</code> otherwise
	 * 
	 * @since 3.4
	 */
	protected void setOwnerDrawEnabled(ColumnViewer<E,I> viewer,
			ViewerColumn<E,I> column, boolean enabled) {
		if (enabled) {
			OwnerDrawListener<E,I> listener = getOrCreateOwnerDrawListener(viewer);
			if (column == null) {
				listener.enabledGlobally++;
			} else {
				listener.enabledColumns.add(column);
			}
		} else {
			@SuppressWarnings("unchecked")
			OwnerDrawListener<E,I> listener = (OwnerDrawListener<E,I>) viewer
					.getControl().getData(OWNER_DRAW_LABEL_PROVIDER_LISTENER);
			if (listener != null) {
				if (column == null) {
					listener.enabledGlobally--;
				} else {
					listener.enabledColumns.remove(column);
				}
				if (listener.enabledColumns.isEmpty()
						&& listener.enabledGlobally <= 0) {
					viewer.getControl().removeListener(SWT.MeasureItem,
							listener);
					viewer.getControl().removeListener(SWT.EraseItem, listener);
					viewer.getControl().removeListener(SWT.PaintItem, listener);
					viewer.getControl().setData(
							OWNER_DRAW_LABEL_PROVIDER_LISTENER, null);
				}
			}
		}
	}

}

Back to the top