1 package org.simantics.browsing.ui.swt;
3 import java.util.regex.Pattern;
5 import org.eclipse.swt.events.KeyAdapter;
6 import org.eclipse.swt.events.KeyEvent;
7 import org.eclipse.swt.widgets.Tree;
8 import org.eclipse.swt.widgets.TreeItem;
9 import org.simantics.browsing.ui.GraphExplorer;
12 * Selects tree items based on pressed key events.<p>
14 * The default implementation of SWT.Tree (Windows?) uses only the the first column when matching the items.<p>
16 * This implementation checks all columns. Override <pre>matches(), matchesColumn()</pre> for customized behavior.<p>
18 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
21 public class KeyToSelectionAdapter extends KeyAdapter {
23 private static final int KEY_INPUT_DELAY = 500;
25 private final GraphExplorer explorer;
27 private String matcher = "";
28 private int prevEvent = 0;
29 private int columns = 0;
31 protected Pattern alphaNum;
34 * @param contextProvider
37 public KeyToSelectionAdapter(GraphExplorer explorer) {
38 assert explorer != null;
40 this.explorer = explorer;
41 this.alphaNum = Pattern.compile("\\p{Alnum}");
45 public void keyPressed(KeyEvent e) {
46 if (explorer.isDisposed())
50 if (!alphaNum.matcher(Character.toString(e.character)).matches())
52 // concatenate / replace matcher.
53 if ((e.time - prevEvent) > KEY_INPUT_DELAY )
56 matcher = matcher += Character.toString(e.character);
59 Tree tree = explorer.getControl();
60 columns = tree.getColumnCount();
61 TreeItem[] selected = tree.getSelection();
63 item = find(tree, selected);
65 if (item == null && matcher.length() > 1) {
66 matcher = matcher.substring(matcher.length()-1);
67 item = find(tree, selected);
75 // without this the default handling would take over.
79 private TreeItem previous = null;
80 private boolean foundPrev = false;
82 private TreeItem find(Tree tree, TreeItem[] selected) {
85 TreeItem items[] = tree.getItems();
86 if (selected.length == 0) {
89 item = findItem(items, 0);
92 previous = selected[0];
94 item = findItem(items, 0);
98 item = findItem(items, 0);
104 private TreeItem findItem(TreeItem items[], int depth) {
105 for (int i = 0; i < items.length; i++) {
106 TreeItem item = items[i];
107 if (item != previous) {
108 if (foundPrev && matches(item, depth, columns, matcher))
114 TreeItem childItem = findItem(item.getItems(),depth+1);
115 if (childItem != null)
125 * @param depth Depth of the item in the tree.
126 * @param columns Number of columns.
127 * @param string Matching string.
130 protected boolean matches(TreeItem item, int depth, int columns, String matcher) {
131 for (int c = 0; c < columns; c++) {
132 if (matchesColumn(item, c, matcher)) {
146 protected boolean matchesColumn(TreeItem item, int column, String matcher) {
147 String text = item.getText(column);
148 if (text.toLowerCase().startsWith(matcher)) {