-package org.simantics.browsing.ui.nattable;\r
-\r
-import java.util.Collection;\r
-import java.util.List;\r
-import java.util.regex.Pattern;\r
-\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.nebula.widgets.nattable.NatTable;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
-import org.simantics.browsing.ui.GraphExplorer;\r
-import org.simantics.utils.ui.AdaptionUtils;\r
-\r
-/**\r
- * Selects tree items based on pressed key events.<p>\r
- * \r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- */\r
-public class KeyToSelectionAdapter extends KeyAdapter {\r
-\r
- private static final int KEY_INPUT_DELAY = 500;\r
- \r
- private final NatTableGraphExplorer explorer;\r
-\r
- private String matcher = "";\r
- private int prevEvent = 0;\r
- private int columns = 0;\r
- \r
- protected Pattern alphaNum;\r
- \r
- /**\r
- * @param contextProvider\r
- * @param explorer\r
- */\r
- public KeyToSelectionAdapter(GraphExplorer explorer) {\r
- assert explorer != null;\r
-\r
- this.explorer = (NatTableGraphExplorer)explorer;\r
- this.alphaNum = Pattern.compile("\\p{Alnum}");\r
- }\r
-\r
- @Override\r
- public void keyPressed(KeyEvent e) {\r
- if (explorer.isDisposed())\r
- return;\r
-\r
- \r
- if (!alphaNum.matcher(Character.toString(e.character)).matches())\r
- return;\r
- // concatenate / replace matcher.\r
- if ((e.time - prevEvent) > KEY_INPUT_DELAY )\r
- matcher = "";\r
- prevEvent = e.time;\r
- matcher = matcher += Character.toString(e.character);\r
-\r
- NatTable tree = explorer.getControl();\r
- columns = explorer.getColumns().length;\r
- \r
- IStructuredSelection sel = (IStructuredSelection)explorer.getWidgetSelection();\r
- Collection<RowSelectionItem> selected = AdaptionUtils.adaptToCollection(sel, RowSelectionItem.class);\r
- \r
- \r
- TreeNode item = find(tree, selected);\r
- \r
- if (item == null && matcher.length() > 1) {\r
- matcher = matcher.substring(matcher.length()-1);\r
- item = find(tree, selected);\r
- }\r
- \r
- if (item != null) {\r
- explorer.select(item);\r
- explorer.show(item);\r
- } \r
- // without this the default handling would take over.\r
- e.doit = false;\r
- }\r
- \r
- private TreeNode previous = null;\r
- private boolean foundPrev = false;\r
- \r
- private TreeNode find(NatTable tree, Collection<RowSelectionItem> selected) {\r
- TreeNode item = null;\r
-\r
- List<TreeNode> items = explorer.getItems();\r
- \r
- if (selected.size() == 0) {\r
- previous = null;\r
- foundPrev = true;\r
- item = findItem(items);\r
-\r
- } else {\r
- previous = selected.iterator().next().item;\r
- foundPrev = false;\r
- item = findItem(items);\r
- if (item == null) {\r
- previous = null;\r
- foundPrev = true;\r
- item = findItem(items);\r
- }\r
- }\r
- return item;\r
- }\r
- \r
- private TreeNode findItem(List<TreeNode> items) {\r
- for (int i = 0; i < items.size(); i++) {\r
- TreeNode item = items.get(i);\r
- if (item != previous) {\r
- if (foundPrev && matches(item, columns, matcher))\r
- return item;\r
- \r
- } else {\r
- foundPrev = true;\r
- }\r
- }\r
- return null;\r
- }\r
- \r
- \r
- /**\r
- * \r
- * @param item\r
- * @param depth Depth of the item in the tree.\r
- * @param columns Number of columns.\r
- * @param string Matching string.\r
- * @return\r
- */\r
- protected boolean matches(TreeNode item, int columns, String matcher) {\r
- for (int c = 0; c < columns; c++) { \r
- if (matchesColumn(item, c, matcher)) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
- \r
- /**\r
- * \r
- * @param item\r
- * @param column\r
- * @param matcher\r
- * @return\r
- */\r
- protected boolean matchesColumn(TreeNode item, int column, String matcher) {\r
- String text = item.getValueString(column);\r
- if (text.toLowerCase().startsWith(matcher)) {\r
- return true;\r
- }\r
- return false;\r
- }\r
-\r
-}\r
+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.<p>
+ *
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+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) {
+ this(explorer,"\\p{Alnum}");
+ }
+
+ public KeyToSelectionAdapter(GraphExplorer explorer, String pattern) {
+ assert explorer != null;
+
+ this.explorer = (NatTableGraphExplorer)explorer;
+ this.alphaNum = Pattern.compile(pattern);
+ }
+
+ public boolean acceptKey(char key) {
+ return alphaNum.matcher(Character.toString(key)).matches();
+ }
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+ if (explorer.isDisposed())
+ return;
+
+ if (!acceptKey(e.character))
+ return;
+ // concatenate / replace matcher.
+ if ((e.time - prevEvent) > KEY_INPUT_DELAY )
+ matcher = "";
+ prevEvent = e.time;
+ matcher = matcher += Character.toString(e.character);
+
+ NatTable tree = explorer.getControl();
+ columns = explorer.getColumns().length;
+
+ IStructuredSelection sel = (IStructuredSelection)explorer.getWidgetSelection();
+ Collection<RowSelectionItem> 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.show(item);
+ explorer.select(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<RowSelectionItem> selected) {
+ TreeNode item = null;
+
+ List<TreeNode> 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<TreeNode> 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 == null)
+ return false;
+ if (text.toLowerCase().startsWith(matcher)) {
+ return true;
+ }
+ return false;
+ }
+
+}