blob: f724dd8b73871122666fd34e7d1ac35f192c7bcf (
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
|
/*******************************************************************************
* Copyright (c) 2007, 2016 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<tom.schindl@bestsolution.at> - initial API and implementation
* Wayne Beaton - bug 185540
* Lars Vogel <Lars.Vogel@vogella.com> - Bug 413427, 475361, 489234
* Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
/**
* Snippet to present editor different CellEditors within one column in 3.2
* for 3.3 and above please use the new EditingSupport class
*
* @author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class Snippet034CellEditorPerRow {
private class MyEditingSupport extends EditingSupport {
private CellEditor textEditor;
private CellEditor dropDownEditor;
public MyEditingSupport(TableViewer viewer) {
super(viewer);
textEditor = new TextCellEditor(viewer.getTable());
String[] elements = new String[10];
for (int i = 0; i < 10; i++) {
elements[i] = i+"";
}
dropDownEditor = new ComboBoxCellEditor(viewer.getTable(),elements);
}
@Override
protected boolean canEdit(Object element) {
return ((MyModel) element).counter % 2 == 0;
}
@Override
protected CellEditor getCellEditor(Object element) {
if( element instanceof MyModel2 ) {
return dropDownEditor;
} else {
return textEditor;
}
}
@Override
protected Object getValue(Object element) {
if( element instanceof MyModel2 ) {
return Integer.valueOf(((MyModel) element).counter);
} else {
return ((MyModel) element).counter + "";
}
}
@Override
protected void setValue(Object element, Object value) {
((MyModel)element).counter = Integer.parseInt(value.toString());
getViewer().update(element, null);
}
}
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 MyModel2 extends MyModel {
public MyModel2(int counter) {
super(counter);
}
@Override
public String toString() {
return "Special Item " + this.counter;
}
}
public Snippet034CellEditorPerRow(Shell shell) {
final Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
final TableViewer v = new TableViewer(table);
v.getTable().setLinesVisible(true);
TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setText("Column 1");
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return element.toString();
}
});
column.setEditingSupport(new MyEditingSupport(v));
v.setContentProvider(ArrayContentProvider.getInstance());
v.setInput(createModel());
v.getTable().setHeaderVisible(true);
}
private List<MyModel> createModel() {
List<MyModel> elements = new ArrayList<>();
for (int i = 0; i < 10; i++) {
elements.add(new MyModel(i));
}
for (int i = 0; i < 10; i++) {
elements.add(new MyModel2(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 Snippet034CellEditorPerRow(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|