-
Notifications
You must be signed in to change notification settings - Fork 96
/
TableComboSnippet1.java
510 lines (404 loc) · 15.2 KB
/
TableComboSnippet1.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*******************************************************************************
* Copyright (c) 2009 Marty Jones
*
* 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:
* Marty Jones <martybjones@gmail.com> - initial API and implementation
*******************************************************************************/
package org.eclipse.nebula.snippets.tablecombo;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.nebula.widgets.tablecombo.TableCombo;
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.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
/**
* Show basic features of TableCombo
*
*/
public class TableComboSnippet1 {
private static Font boldFont;
private static Image testImage;
private static Image test2Image;
private static Image test3Image;
private static Color darkRed;
private static Color darkBlue;
private static Color darkGreen;
private static List<Model> modelList;
private static Text listenerResults;
private static Group listenerGroup;
/**
* @param args
*/
public static void main(String[] args) {
// get display.
Display display = new Display ();
// create bold and italic font.
boldFont = new Font(display,"Arial",8, SWT.BOLD | SWT.ITALIC);
// create images
testImage = ImageDescriptor.createFromFile(TableComboSnippet1.class,
"in_ec_ov_success_16x16.gif").createImage();
test2Image = ImageDescriptor.createFromFile(TableComboSnippet1.class,
"in_ec_ov_warning_16x16.gif").createImage();
test3Image = ImageDescriptor.createFromFile(TableComboSnippet1.class,
"invalid_build_tool_16x16.gif").createImage();
// 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.
Shell shell = new Shell (display);
shell.setText("TableCombo Snippet 1");
shell.setSize(600, 400);
shell.setLayout(new GridLayout());
// create group
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);
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("Single Column (Mimics Normal Combo Field):");
// create TableCombo
TableCombo tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// load the dataset.
loadSingleDataset(tc.getTable());
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample1"));
////////////////////////////////////////////////////////////////////////
// Sample #2
////////////////////////////////////////////////////////////////////////
label = new Label(group, SWT.NONE);
label.setText("Single Column (With Images)");
// create TableCombo
tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// load the dataset.
loadSingleDatasetWithImages(tc.getTable());
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample2"));
////////////////////////////////////////////////////////////////////////
// Sample #3
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Two Columns:");
// create TableCombo
tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// tell the TableCombo that I want 2 blank columns auto sized.
tc.defineColumns(2);
// set which column will be used for the selected item.
tc.setDisplayColumnIndex(1);
tc.setToolTipText("This is a tool tip.");
// load the dataset.
loadTwoColumnDataset(tc.getTable());
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample3"));
////////////////////////////////////////////////////////////////////////
// Sample #4
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Two Columns (With Colors && Fonts):");
// create TableCombo
tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// tell the TableCombo that I want 2 blank columns auto sized.
tc.defineColumns(2);
// set which column will be used for the selected item.
tc.setDisplayColumnIndex(1);
// load the dataset.
loadTwoColumnDatasetWithColorsAndFonts(tc.getTable());
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample4"));
////////////////////////////////////////////////////////////////////////
// Sample #5
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Three Columns (With Colors && Fonts && Header):");
// create TableCombo
tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// tell the TableCombo that I want 3 columns autosized with the following column headers.
tc.defineColumns(new String[] { "Id", "Description", "Computed"});
// set which column will be used for the selected item.
tc.setDisplayColumnIndex(2);
// turn on the table header.
tc.setShowTableHeader(true);
// load the dataset.
loadThreeColumnDatasetWithColorsAndFonts(tc.getTable());
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample5"));
////////////////////////////////////////////////////////////////////////
// Sample #6
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Three Columns (First Column, Fixed Width):");
// create TableCombo
tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// tell the TableCombo that I want 3 columns autosized with the following column headers.
tc.defineColumns(new String[] { "Id", "Description", "Computed"},
new int[] { 50 , SWT.DEFAULT, SWT.DEFAULT});
// set which column will be used for the selected item.
tc.setDisplayColumnIndex(2);
// turn on the table header.
tc.setShowTableHeader(true);
// load the dataset.
loadThreeColumnDatasetWithColorsAndFonts(tc.getTable());
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample6"));
////////////////////////////////////////////////////////////////////////
// Sample #7
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Three Columns (With Table Width 75%):");
// create TableCombo
tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// tell the TableCombo that I want 3 columns autosized with the following column headers.
tc.defineColumns(new String[] { "Id", "Description", "Computed"});
// set which column will be used for the selected item.
tc.setDisplayColumnIndex(2);
// turn on the table header.
tc.setShowTableHeader(true);
// load the dataset.
loadThreeColumnDatasetWithColorsAndFonts(tc.getTable());
// set the table width % to 75%
tc.setTableWidthPercentage(75);
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample7"));
////////////////////////////////////////////////////////////////////////
// Sample #8
////////////////////////////////////////////////////////////////////////
// create label
label = new Label(group, SWT.NONE);
label.setText("Keep popup open");
// create TableCombo
tc = new TableCombo(group, SWT.BORDER | SWT.READ_ONLY);
tc.setLayoutData(new GridData(125, SWT.DEFAULT));
// tell the TableCombo that I want 3 columns autosized with the following column headers.
tc.defineColumns(new String[] { "Id", "Description", "Computed"});
// set which column will be used for the selected item.
tc.setDisplayColumnIndex(2);
// turn on the table header.
tc.setShowTableHeader(true);
// load the dataset.
loadThreeColumnDatasetWithColorsAndFonts(tc.getTable());
// add listener
tc.addSelectionListener(new ItemSelected(tc, "Sample8"));
// keep popup open after selecting an element
tc.setClosePopupAfterSelection(false);
// open the shell.
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
// dispose of the font
boldFont.dispose();
// dispose images
testImage.dispose();
test2Image.dispose();
test3Image.dispose();
// dispose colors
darkRed.dispose();
darkBlue.dispose();
darkGreen.dispose();
// dispose display
display.dispose ();
}
/**
* load a list of rows with a single column
* @return
*/
private static List<TableItem> loadSingleDataset(Table table) {
List<TableItem> rowList = new ArrayList<TableItem>();
int total = (modelList == null ? 0 : modelList.size());
for (int index=0; index < total; index++) {
TableItem ti = new TableItem(table, SWT.NONE);
Model model = (Model)modelList.get(index);
ti.setText(model.getDescription());
rowList.add(ti);
}
return rowList;
}
/**
* load a list of rows with a single column that includes images
* @return
*/
private static List<TableItem> loadSingleDatasetWithImages(Table table) {
List<TableItem> list = loadSingleDataset(table);
int total = (list == null ? 0 : list.size());
for (int index=0; index < total; index++) {
TableItem ti = ((TableItem)(list.get(index)));
if (index == 1 || index == 7 || index == 13 || index == 19) {
ti.setImage(0, testImage);
}
else if (index == 3 || index == 9 || index == 15) {
ti.setImage(0, test2Image);
}
else if (index == 5 || index == 11 || index == 17) {
ti.setImage(0, test3Image);
}
}
return list;
}
/**
* load a list of rows with 2 columns in each row.
* @return
*/
private static List<TableItem> loadTwoColumnDataset(Table table) {
List<TableItem> rowList = new ArrayList<TableItem>();
int total = (modelList == null ? 0 : modelList.size());
for (int index=0; index < total; index++) {
TableItem ti = new TableItem(table, SWT.NONE);
Model model = (Model)modelList.get(index);
ti.setText(new String[] { String.valueOf(model.getId()), model.getDescription() });
rowList.add(ti);
}
return rowList;
}
/**
* load a list of rows with 2 columns that includes colors and fonts.
* @return
*/
private static List<TableItem> loadTwoColumnDatasetWithColorsAndFonts(Table table) {
List<TableItem> list = loadTwoColumnDataset(table);
int total = (list == null ? 0 : list.size());
for (int index=0; index < total; index++) {
TableItem ti = ((TableItem)(list.get(index)));
if (index == 0 || index == 14) {
ti.setForeground(darkRed);
ti.setFont(boldFont);
}
else if (index == 4 || index == 19) {
ti.setForeground(darkBlue);
ti.setFont(boldFont);
}
else if (index == 9) {
ti.setForeground(darkGreen);
ti.setFont(boldFont);
}
}
return list;
}
/**
* load a list of rows with 3 columns
* @return
*/
private static List<TableItem> loadThreeColumnDataset(Table table) {
List<TableItem> rowList = new ArrayList<TableItem>();
int total = (modelList == null ? 0 : modelList.size());
for (int index=0; index < total; index++) {
TableItem ti = new TableItem(table, SWT.NONE);
Model model = (Model)modelList.get(index);
ti.setText(new String[] { String.valueOf(model.getId()), model.getDescription(),
String.valueOf(model.getId()) + " - " + model.getDescription() });
rowList.add(ti);
}
return rowList;
}
/**
* load a list of rows with 3 columns that includes colors and fonts.
* @return
*/
private static List<TableItem> loadThreeColumnDatasetWithColorsAndFonts(Table table) {
List<TableItem> list = loadThreeColumnDataset(table);
int total = (list == null ? 0 : list.size());
for (int index=0; index < total; index++) {
TableItem ti = ((TableItem)(list.get(index)));
if (index == 0 || index == 14) {
ti.setForeground(darkRed);
ti.setFont(boldFont);
}
else if (index == 4 || index == 19) {
ti.setForeground(darkBlue);
ti.setFont(boldFont);
} else if (index==6) {
ti.setForeground(table.getDisplay().getSystemColor(SWT.COLOR_WHITE));
ti.setBackground(table.getDisplay().getSystemColor(SWT.COLOR_BLACK));
} else if (index == 9) {
ti.setForeground(darkGreen);
ti.setFont(boldFont);
}
}
return list;
}
/**
* load the Model data.
* @return
*/
private static List<Model> loadModel() {
List<Model> items = new ArrayList<Model>();
items.add(new Model(1, "One"));
items.add(new Model(2, "Two"));
items.add(new Model(3, "Three"));
items.add(new Model(4, "Four"));
items.add(new Model(5, "Five"));
items.add(new Model(6, "Six"));
items.add(new Model(7, "Seven"));
items.add(new Model(8, "Eight"));
items.add(new Model(9, "Nine"));
items.add(new Model(10, "Ten"));
items.add(new Model(11, "Eleven"));
items.add(new Model(12, "Twelve"));
items.add(new Model(13, "Thirteen"));
items.add(new Model(14, "Fourteen"));
items.add(new Model(15, "Fiveteen"));
items.add(new Model(16, "Sixteen"));
items.add(new Model(17, "Seventeen"));
items.add(new Model(18, "Eighteen"));
items.add(new Model(19, "Nineteen"));
items.add(new Model(20, "Twenty"));
return items;
}
private static class ItemSelected extends SelectionAdapter {
private TableCombo tc;
private String text;
public ItemSelected(TableCombo tc, String text) {
this.tc = tc;
this.text = text;
}
public void widgetSelected(SelectionEvent e) {
listenerGroup.setText("Listener Results - (" + text + ")");
listenerResults.setText(tc.getText() == null ? "null" : tc.getText());
}
}
}