1 package org.simantics.browsing.ui.nattable;
3 import java.util.Collection;
5 import java.util.regex.Pattern;
7 import org.eclipse.jface.viewers.IStructuredSelection;
8 import org.eclipse.nebula.widgets.nattable.NatTable;
9 import org.eclipse.swt.events.KeyAdapter;
10 import org.eclipse.swt.events.KeyEvent;
11 import org.simantics.browsing.ui.GraphExplorer;
12 import org.simantics.utils.ui.AdaptionUtils;
15 * Selects tree items based on pressed key events.<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 NatTableGraphExplorer 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 = (NatTableGraphExplorer)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);
58 NatTable tree = explorer.getControl();
59 columns = explorer.getColumns().length;
61 IStructuredSelection sel = (IStructuredSelection)explorer.getWidgetSelection();
62 Collection<RowSelectionItem> selected = AdaptionUtils.adaptToCollection(sel, RowSelectionItem.class);
65 TreeNode item = find(tree, selected);
67 if (item == null && matcher.length() > 1) {
68 matcher = matcher.substring(matcher.length()-1);
69 item = find(tree, selected);
73 explorer.select(item);
76 // without this the default handling would take over.
80 private TreeNode previous = null;
81 private boolean foundPrev = false;
83 private TreeNode find(NatTable tree, Collection<RowSelectionItem> selected) {
86 List<TreeNode> items = explorer.getItems();
88 if (selected.size() == 0) {
91 item = findItem(items);
94 previous = selected.iterator().next().item;
96 item = findItem(items);
100 item = findItem(items);
106 private TreeNode findItem(List<TreeNode> items) {
107 for (int i = 0; i < items.size(); i++) {
108 TreeNode item = items.get(i);
109 if (item != previous) {
110 if (foundPrev && matches(item, columns, matcher))
124 * @param depth Depth of the item in the tree.
125 * @param columns Number of columns.
126 * @param string Matching string.
129 protected boolean matches(TreeNode item, int columns, String matcher) {
130 for (int c = 0; c < columns; c++) {
131 if (matchesColumn(item, c, matcher)) {
145 protected boolean matchesColumn(TreeNode item, int column, String matcher) {
146 String text = item.getValueString(column);
147 if (text.toLowerCase().startsWith(matcher)) {