-
Notifications
You must be signed in to change notification settings - Fork 98
/
CTreeComboSnippet.java
275 lines (226 loc) · 8.52 KB
/
CTreeComboSnippet.java
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
/*******************************************************************************
* Copyright (c) 2019 Laurent CARON
* 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: Laurent CARON (laurent.caron at gmail dot com) - initial API
* and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.ctreecombo.snippets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.nebula.widgets.ctreecombo.CTreeCombo;
import org.eclipse.nebula.widgets.ctreecombo.CTreeComboItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* This snippet demonstrates the CTreeComboSnippet widget
*
*/
public class CTreeComboSnippet {
private static class ItemSelected extends SelectionAdapter {
private final CTreeCombo ctc;
private final String text;
public ItemSelected(CTreeCombo ctc, String text) {
this.ctc = ctc;
this.text = text;
}
@Override
public void widgetSelected(SelectionEvent e) {
listenerGroup.setText("Listener Results - (" + text + ")");
listenerResults.setText(ctc.getText() == null ? "null" : ctc.getText());
}
}
private static Font boldFont;
private static Map<String, Image> images = new HashMap<>();
private static Color darkRed;
private static Color darkBlue;
private static Color darkGreen;
private static List<Country> modelList;
private static Text listenerResults;
private static Group listenerGroup;
private static List<Country> loadModel() {
final List<Country> items = new ArrayList<Country>();
items.add(new Country("Austria", "Thomas Schindl"));
items.add(new Country("France", "Laurent Caron", "Nicolas Richeton"));
items.add(new Country("Germany", "Dirk Fauth", "Johannes Faltermeier"));
items.add(new Country("Italy", "Mirko Paturzo"));
items.add(new Country("Netherlands", "Wim Jongman"));
items.add(new Country("Norway", "Hallvard Traetteberg"));
items.add(new Country("UK", "Jonah Graham", "Matthew Gerring", "Peter Chang"));
items.add(new Country("USA", "Donald Dunne"));
return items;
}
private static void loadSingleDataset(CTreeCombo ctc) {
for (final Country country : modelList) {
final CTreeComboItem item = new CTreeComboItem(ctc, SWT.NONE);
item.setText(country.getName());
for (final String commiter : country.getCommiters()) {
final CTreeComboItem commiterItem = new CTreeComboItem(item, SWT.NONE);
commiterItem.setText(commiter);
}
}
}
private static void loadSingleDatasetWithColorsAndFonts(CTreeCombo ctc) {
int index = 0;
for (final Country country : modelList) {
final CTreeComboItem item = new CTreeComboItem(ctc, SWT.NONE);
item.setText(country.getName());
if (index % 3 == 0) {
item.setForeground(darkBlue);
} else if (index % 3 == 1) {
item.setForeground(darkGreen);
} else {
item.setForeground(darkRed);
}
for (int j = 0; j < country.getCommiters().size(); j++) {
final String commiter = country.getCommiters().get(j);
final CTreeComboItem commiterItem = new CTreeComboItem(item, SWT.NONE);
commiterItem.setText(commiter);
if (j % 2 == 0) {
commiterItem.setFont(boldFont);
}
}
index++;
}
}
private static void loadSingleDatasetWithImages(CTreeCombo ctc) {
for (final Country country : modelList) {
final CTreeComboItem item = new CTreeComboItem(ctc, SWT.NONE);
item.setText(country.getName());
final String key = country.getName().toLowerCase();
item.setImage(images.get(key));
for (final String commiter : country.getCommiters()) {
final CTreeComboItem commiterItem = new CTreeComboItem(item, SWT.NONE);
commiterItem.setText(commiter);
}
}
}
/**
* @param args
*/
public static void main(final String[] args) {
// get display.
final Display display = new Display();
// create bold and italic font.
boldFont = new Font(display, "Arial", 8, SWT.BOLD | SWT.ITALIC);
// create images
final String[] countries = new String[] { "austria", "france", "germany", "italy", "netherlands", "norway", "unitedkingdom", "usa" };
for (final String country : countries) {
final Image img = ImageDescriptor.createFromFile(CTreeComboSnippet.class, "flags/" + country + ".png").createImage();
images.put(country, img);
}
// create colors
darkRed = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
darkBlue = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE);
darkGreen = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN);
// load the model list.
modelList = loadModel();
// create a new shell.
final Shell shell = new Shell(display);
shell.setText("CTreeCombo Snippet (pure SWT)");
shell.setSize(600, 400);
shell.setLayout(new GridLayout());
// create group
final Group group = new Group(shell, SWT.NONE);
group.setLayout(new GridLayout(2, false));
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
group.setText("Sample Group");
// create group
listenerGroup = new Group(shell, SWT.NONE);
listenerGroup.setLayout(new GridLayout(1, false));
listenerGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
listenerGroup.setText("Listener Results");
listenerResults = new Text(listenerGroup, SWT.BORDER | SWT.MULTI);
final GridData gd = new GridData(SWT.FILL, SWT.DEFAULT, true, false);
gd.heightHint = 30;
listenerResults.setLayoutData(gd);
////////////////////////////////////////////////////////////////////////
// Sample #1
////////////////////////////////////////////////////////////////////////
Label label = new Label(group, SWT.NONE);
label.setText("No image, no font:");
// create TableCombo
CTreeCombo ctc = new CTreeCombo(group, SWT.BORDER | SWT.READ_ONLY);
ctc.setLayoutData(new GridData(200, SWT.DEFAULT));
// load the dataset.
loadSingleDataset(ctc);
// add listener
ctc.addSelectionListener(new ItemSelected(ctc, "Sample1"));
////////////////////////////////////////////////////////////////////////
// Sample #2
////////////////////////////////////////////////////////////////////////
label = new Label(group, SWT.NONE);
label.setText("Combo with images");
// create TableCombo
ctc = new CTreeCombo(group, SWT.BORDER | SWT.READ_ONLY);
ctc.setLayoutData(new GridData(200, SWT.DEFAULT));
// load the dataset.
loadSingleDatasetWithImages(ctc);
// add listener
ctc.addSelectionListener(new ItemSelected(ctc, "Sample2"));
////////////////////////////////////////////////////////////////////////
// Sample #3
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Colors && Fonts :");
// create TableCombo
ctc = new CTreeCombo(group, SWT.BORDER | SWT.READ_ONLY);
ctc.setLayoutData(new GridData(200, SWT.DEFAULT));
// load the dataset.
loadSingleDatasetWithColorsAndFonts(ctc);
// add listener
ctc.addSelectionListener(new ItemSelected(ctc, "Sample3"));
////////////////////////////////////////////////////////////////////////
// Sample #4
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Not readonly");
// create TableCombo
ctc = new CTreeCombo(group, SWT.BORDER);
ctc.setLayoutData(new GridData(200, SWT.DEFAULT));
// load the dataset.
loadSingleDatasetWithColorsAndFonts(ctc);
// add listener
ctc.addSelectionListener(new ItemSelected(ctc, "Sample4"));
// open the shell.
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// dispose of the font
boldFont.dispose();
// dispose images
for (final Image img : images.values()) {
img.dispose();
}
// dispose colors
darkRed.dispose();
darkBlue.dispose();
darkGreen.dispose();
// dispose display
display.dispose();
}
}