Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eff8e92ea2708cad82660c288d186f5a2a80e4be (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
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Text example snippet: implement content assist
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet320 {

public static void main(String [] args) {
	final Display display = new Display();
	final Shell shell = new Shell(display);
	shell.setLayout(new GridLayout());
	final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
	text.setLayoutData(new GridData(150, SWT.DEFAULT));
	shell.pack();
	shell.open();

	final Shell popupShell = new Shell(display, SWT.ON_TOP);
	popupShell.setLayout(new FillLayout());
	final Table table = new Table(popupShell, SWT.SINGLE);
	for (int i = 0; i < 5; i++) {
		new TableItem(table, SWT.NONE);
	}

	text.addListener(SWT.KeyDown, event -> {
		switch (event.keyCode) {
			case SWT.ARROW_DOWN:
				int index = (table.getSelectionIndex() + 1) % table.getItemCount();
				table.setSelection(index);
				event.doit = false;
				break;
			case SWT.ARROW_UP:
				index = table.getSelectionIndex() - 1;
				if (index < 0) index = table.getItemCount() - 1;
				table.setSelection(index);
				event.doit = false;
				break;
			case SWT.CR:
				if (popupShell.isVisible() && table.getSelectionIndex() != -1) {
					text.setText(table.getSelection()[0].getText());
					popupShell.setVisible(false);
				}
				break;
			case SWT.ESC:
				popupShell.setVisible(false);
				break;
		}
	});
	text.addListener(SWT.Modify, event -> {
		String string = text.getText();
		if (string.length() == 0) {
			popupShell.setVisible(false);
		} else {
			TableItem[] items = table.getItems();
			for (int i = 0; i < items.length; i++) {
				items[i].setText(string + '-' + i);
			}
			Rectangle textBounds = display.map(shell, null, text.getBounds());
			popupShell.setBounds(textBounds.x, textBounds.y + textBounds.height, textBounds.width, 150);
			popupShell.setVisible(true);
		}
	});

	table.addListener(SWT.DefaultSelection, event -> {
		text.setText(table.getSelection()[0].getText());
		popupShell.setVisible(false);
	});
	table.addListener(SWT.KeyDown, event -> {
		if (event.keyCode == SWT.ESC) {
			popupShell.setVisible(false);
		}
	});

	Listener focusOutListener = event -> display.asyncExec(() -> {
		if (display.isDisposed()) return;
		Control control = display.getFocusControl();
		if (control == null || (control != text && control != table)) {
			popupShell.setVisible(false);
		}
	});
	table.addListener(SWT.FocusOut, focusOutListener);
	text.addListener(SWT.FocusOut, focusOutListener);

	shell.addListener(SWT.Move, event -> popupShell.setVisible(false));

	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose();
}

}

Back to the top