Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9806e06fbdcb87f7901caeaa252ef3d7c5cef987 (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
/*******************************************************************************
 * Copyright (c) 2019 Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal.components;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget;

/**
 * Specialized label provider to put native SWT {@link Control}s into a column
 * of a JFace {@link org.eclipse.jface.viewers.TableViewer TableViewer}.
 */
public abstract class ControlLabelProvider extends ColumnLabelProvider {

	private final Map<TableItem, Editor> editors = new HashMap<>();

	@Override
	public void dispose() {
		editors.clear();
		super.dispose();
	}

	@Override
	public String getText(Object element) {
		return null;
	}

	@Override
	public void update(ViewerCell cell) {
		super.update(cell);
		Object obj = cell.getElement();
		Widget w = cell.getViewerRow().getItem();
		if (w instanceof TableItem) {
			TableItem item = (TableItem) w;
			Table table = item.getParent();
			Editor editor = editors.get(item);
			if (editor == null) {
				Control control = setEditor(cell, table, null, obj);
				if (control == null) {
					return;
				}
				control.pack();
				editor = new Editor(table);
				editor.horizontalAlignment = SWT.CENTER;
				editor.verticalAlignment = SWT.CENTER;
				Point size = control.getSize();
				editor.minimumWidth = size.x;
				editor.minimumHeight = size.y;
				editors.put(item, editor);
				editor.setEditor(control, item, cell.getColumnIndex());
				editor.connect();
			} else {
				Control existing = editor.getEditor();
				Control control = setEditor(cell, table, existing, obj);
				if (control != existing) {
					if (!existing.isDisposed()) {
						existing.dispose();
					}
					if (control == null) {
						editor.dispose();
						return;
					}
					control.pack();
					editor.setEditor(control);
				}
			}
		}
	}

	/**
	 * Creates a new {@link Control}, or updates an existing one.
	 *
	 * @param cell
	 *            that is being dealt with
	 * @param parent
	 *            to use if a new control is created
	 * @param control
	 *            an already existing widget to be updated, or {@code null} if a
	 *            new widget should be created
	 * @param element
	 *            being shown
	 * @return the {@link Control} to show, or {@code null} if no control is to
	 *         be shown.
	 */
	public abstract Control setEditor(ViewerCell cell, Composite parent,
			Control control, Object element);

	private class Editor extends TableEditor {

		private DisposeListener disposer;

		private final Table table;

		public Editor(Table table) {
			super(table);
			this.table = table;
		}

		@Override
		public void layout() {
			if (Util.isGtk() && SWT.getVersion() <= 4924) {
				// Layout is relative to the table's clientArea, which includes
				// the header if one is shown. Results in editors being shown
				// over the column headers.
				//
				// This is a work-around for bug 535978; would be needed only on
				// SWT versions < 4924r7.
				TableItem item = getItem();
				if (item != null) {
					Rectangle rect = item.getBounds();
					if (table.getHeaderVisible()) {
						Control editor = getEditor();
						if (editor != null && !editor.isDisposed()) {
							editor.setVisible(
									rect.y >= table.getHeaderHeight());
						}
					}
				}
			}
			super.layout();
		}

		public void connect() {
			TableItem item = getItem();
			if (item != null) {
				disposer = e -> {
					Control editor = getEditor();
					if (editor != null && !editor.isDisposed()) {
						editor.dispose();
					}
					dispose();
				};
				item.addDisposeListener(disposer);
			}
		}

		private void disconnect() {
			if (disposer != null) {
				TableItem item = getItem();
				if (item != null) {
					editors.remove(item);
					item.removeDisposeListener(disposer);
				}
				disposer = null;
			}
		}

		@Override
		public void dispose() {
			disconnect();
			super.dispose();
		}
	}
}

Back to the top