X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.nattable%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fnattable%2FKeyToSelectionAdapter.java;fp=bundles%2Forg.simantics.browsing.ui.nattable%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fnattable%2FKeyToSelectionAdapter.java;h=9d43c351fce553db9c5783a9a41063e53ff0284a;hb=145a2884933f2ffdd48d6835729e58f1152d274e;hp=0000000000000000000000000000000000000000;hpb=bd5bc6e45f700e755b61bd112631796631330ecb;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/KeyToSelectionAdapter.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/KeyToSelectionAdapter.java new file mode 100644 index 000000000..9d43c351f --- /dev/null +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/KeyToSelectionAdapter.java @@ -0,0 +1,163 @@ +package org.simantics.browsing.ui.nattable; + +import java.util.Collection; +import java.util.List; +import java.util.regex.Pattern; + +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.nebula.widgets.nattable.NatTable; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.simantics.browsing.ui.GraphExplorer; +import org.simantics.utils.ui.AdaptionUtils; + +/** + * Selects tree items based on pressed key events.

+ * + * The default implementation of SWT.Tree (Windows?) uses only the the first column when matching the items.

+ * + * This implementation checks all columns. Override

matches(), matchesColumn()
for customized behavior.

+ * + * @author Marko Luukkainen + * + */ +public class KeyToSelectionAdapter extends KeyAdapter { + + private static final int KEY_INPUT_DELAY = 500; + + private final NatTableGraphExplorer explorer; + + private String matcher = ""; + private int prevEvent = 0; + private int columns = 0; + + protected Pattern alphaNum; + + /** + * @param contextProvider + * @param explorer + */ + public KeyToSelectionAdapter(GraphExplorer explorer) { + assert explorer != null; + + this.explorer = (NatTableGraphExplorer)explorer; + this.alphaNum = Pattern.compile("\\p{Alnum}"); + } + + @Override + public void keyPressed(KeyEvent e) { + if (explorer.isDisposed()) + return; + + + if (!alphaNum.matcher(Character.toString(e.character)).matches()) + return; + // concatenate / replace matcher. + if ((e.time - prevEvent) > KEY_INPUT_DELAY ) + matcher = ""; + prevEvent = e.time; + matcher = matcher += Character.toString(e.character); + + + //TreeItem item = null; + NatTable tree = explorer.getControl(); + columns = explorer.getColumns().length; + + IStructuredSelection sel = (IStructuredSelection)explorer.getWidgetSelection(); + Collection selected = AdaptionUtils.adaptToCollection(sel, RowSelectionItem.class); + + + TreeNode item = find(tree, selected); + + if (item == null && matcher.length() > 1) { + matcher = matcher.substring(matcher.length()-1); + item = find(tree, selected); + } + + if (item != null) { + explorer.select(item); + explorer.show(item); +// tree.select(item); +// tree.showItem(item); + + + } + // without this the default handling would take over. + e.doit = false; + } + + private TreeNode previous = null; + private boolean foundPrev = false; + + private TreeNode find(NatTable tree, Collection selected) { + TreeNode item = null; + + List items = explorer.getItems(); + + if (selected.size() == 0) { + previous = null; + foundPrev = true; + item = findItem(items); + + } else { + previous = selected.iterator().next().item; + foundPrev = false; + item = findItem(items); + if (item == null) { + previous = null; + foundPrev = true; + item = findItem(items); + } + } + return item; + } + + private TreeNode findItem(List items) { + for (int i = 0; i < items.size(); i++) { + TreeNode item = items.get(i); + if (item != previous) { + if (foundPrev && matches(item, columns, matcher)) + return item; + + } else { + foundPrev = true; + } + } + return null; + } + + + /** + * + * @param item + * @param depth Depth of the item in the tree. + * @param columns Number of columns. + * @param string Matching string. + * @return + */ + protected boolean matches(TreeNode item, int columns, String matcher) { + for (int c = 0; c < columns; c++) { + if (matchesColumn(item, c, matcher)) { + return true; + } + } + return false; + } + + /** + * + * @param item + * @param column + * @param matcher + * @return + */ + protected boolean matchesColumn(TreeNode item, int column, String matcher) { + String text = item.getValueString(column); + if (text.toLowerCase().startsWith(matcher)) { + return true; + } + return false; + } + + +}