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
|
/*******************************************************************************
* Copyright (c) 2006, 2018 Tom Schindl and others.
*
* 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
*
* Contributors:
* Tom Schindl - initial API and implementation
* Lars Vogel <Lars.Vogel@gmail.com> - Bug 414565, 489234
* Simon Scholz <simon.scholz@vogella.com> - Bug 442343
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
/**
* Example usage of none mandatory interfaces of ITableFontProvider and
* ITableColorProvider
*
* @since 3.2
*/
public class Snippet017TableViewerHideShowColumns {
private static class ShrinkThread extends Thread {
private int width = 0;
private TableColumn column;
public ShrinkThread(int width, TableColumn column) {
super();
this.width = width;
this.column = column;
}
@Override
public void run() {
column.getDisplay().syncExec(() -> column.setData("restoredWidth", Integer.valueOf(width)));
for( int i = width; i >= 0; i-- ) {
final int index = i;
column.getDisplay().syncExec(() -> column.setWidth(index));
}
}
}
private static class ExpandThread extends Thread {
private int width = 0;
private TableColumn column;
public ExpandThread(int width, TableColumn column) {
super();
this.width = width;
this.column = column;
}
@Override
public void run() {
for( int i = 0; i <= width; i++ ) {
final int index = i;
column.getDisplay().syncExec(() -> column.setWidth(index));
}
}
}
public static class MyModel {
public int counter;
public MyModel(int counter) {
this.counter = counter;
}
@Override
public String toString() {
return "Item " + this.counter;
}
}
public static class MyLabelProvider extends LabelProvider implements
ITableLabelProvider {
@Override
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
return "Column " + columnIndex + " => " + element;
}
}
public Snippet017TableViewerHideShowColumns(Shell shell) {
final TableViewer v = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
v.setLabelProvider(new MyLabelProvider());
v.setContentProvider(ArrayContentProvider.getInstance());
TableColumn column = new TableColumn(v.getTable(), SWT.NONE);
column.setWidth(200);
column.setText("Column 1");
column = new TableColumn(v.getTable(), SWT.NONE);
column.setWidth(200);
column.setText("Column 2");
column = new TableColumn(v.getTable(), SWT.NONE);
column.setWidth(200);
column.setText("Column 3");
MyModel[] model = createModel();
v.setInput(model);
v.getTable().setLinesVisible(true);
v.getTable().setHeaderVisible(true);
addMenu(v);
}
private void addMenu(TableViewer v) {
final MenuManager mgr = new MenuManager();
Action action;
for( int i = 0; i < v.getTable().getColumnCount(); i++ ) {
final TableColumn column = v.getTable().getColumn(i);
action = new Action(v.getTable().getColumn(i).getText(),SWT.CHECK) {
@Override
public void runWithEvent(Event event) {
if( ! isChecked() ) {
ShrinkThread t = new ShrinkThread(column.getWidth(),column);
t.run();
} else {
ExpandThread t = new ExpandThread(((Integer)column.getData("restoredWidth")).intValue(),column);
t.run();
}
}
};
action.setChecked(true);
mgr.add(action);
}
v.getControl().setMenu(mgr.createContextMenu(v.getControl()));
}
private MyModel[] createModel() {
MyModel[] elements = new MyModel[10];
for (int i = 0; i < 10; i++) {
elements[i] = new MyModel(i);
}
return elements;
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Snippet017TableViewerHideShowColumns(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|