-
Notifications
You must be signed in to change notification settings - Fork 96
/
TextAssistSnippet.java
95 lines (77 loc) · 3.21 KB
/
TextAssistSnippet.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
/*******************************************************************************
* Copyright (c) 2011-2020 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@gmail.com)
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.textassist.snippets;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.nebula.widgets.opal.textassist.TextAssist;
import org.eclipse.nebula.widgets.opal.textassist.TextAssistContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* A simple snippet for the TextAssist Widget
*/
public class TextAssistSnippet {
private static boolean useSingleClick = false;
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final TextAssistContentProvider contentProvider = new TextAssistContentProvider() {
private final String[] EUROZONE = new String[] { "Austria", "Belgium", "Cyprus", "Estonia", "Finland", "France", "Germany", "Greece", "Ireland", "Italy", "Luxembourg", "Malta", "Netherlands", "Portugal", "Slovakia", "Slovenia", "Spain" };
@Override
public List<String> getContent(final String entry) {
final List<String> returnedList = new ArrayList<String>();
for (final String country : EUROZONE) {
if (country.toLowerCase().matches(".*?"+ entry.toLowerCase() + ".*?")) {
returnedList.add(country);
}
}
return returnedList;
}
};
final Label lblTextAssist = new Label(shell, SWT.NONE);
lblTextAssist.setText("Text field with text assist:");
final TextAssist textAssist = new TextAssist(shell, SWT.SINGLE | SWT.BORDER);
textAssist.setLayoutData(new GridData(150, SWT.DEFAULT));
textAssist.setContentProvider(contentProvider);
new Label(shell, SWT.NONE);
final Label lblSimpleText = new Label(shell, SWT.NONE);
lblSimpleText.setText("Simple Text field:");
final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setLayoutData(new GridData(150, SWT.DEFAULT));
new Label(shell, SWT.NONE);
final Button button = new Button(shell, SWT.PUSH);
button.setText("Force focus on Text Assist Field");
button.addListener(SWT.Selection, e -> textAssist.setFocus());
new Label(shell, SWT.NONE);
final Button singleClickButton = new Button(shell, SWT.CHECK);
singleClickButton.setText("Use single click to select an entry");
singleClickButton.addListener(SWT.Selection, e -> {
useSingleClick = !useSingleClick;
textAssist.setUseSingleClick(useSingleClick);
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}