]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultKeyListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultKeyListener.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultKeyListener.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultKeyListener.java
new file mode 100644 (file)
index 0000000..7e5de46
--- /dev/null
@@ -0,0 +1,118 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.jface.viewers.IPostSelectionProvider;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.model.queries.IsNodeContextModifiable;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.management.ISessionContextProvider;\r
+import org.simantics.ui.workbench.action.ChooseActionRequest;\r
+import org.simantics.utils.datastructures.Function;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * The default keyboard handler for {@link GraphExplorer}.\r
+ * \r
+ * <p>\r
+ * Carriage return presses are handled similarly to mouse double clicks. F2\r
+ * initiates in-line editing of the selection when possible.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DefaultKeyListener extends KeyAdapter {\r
+\r
+    private final ISessionContextProvider contextProvider;\r
+    private final GraphExplorer          explorer;\r
+    private final Function<String[]>      editingColumnResolver;\r
+\r
+    /**\r
+     * @param contextProvider\r
+     * @param explorer\r
+     * @param editingColumnResolver\r
+     */\r
+    public DefaultKeyListener(ISessionContextProvider contextProvider, GraphExplorer explorer, Function<String[]> editingColumnResolver) {\r
+        assert contextProvider != null;\r
+        assert explorer != null;\r
+        assert editingColumnResolver != null;\r
+\r
+        this.contextProvider = contextProvider;\r
+        this.explorer = explorer;\r
+        this.editingColumnResolver = editingColumnResolver;\r
+    }\r
+\r
+    @Override\r
+    public void keyPressed(KeyEvent e) {\r
+        if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {\r
+            if (explorer.isDisposed())\r
+                return;\r
+\r
+            ISessionContext sessionContext = contextProvider.getSessionContext();\r
+            if (sessionContext == null)\r
+                return;\r
+\r
+            IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+\r
+            ISelection input = selectionProvider.getSelection();\r
+            if (input.isEmpty())\r
+                return;\r
+//            Resource resource = ResourceAdaptionUtils.toSingleResource(selectionProvider.getSelection());\r
+//            if (resource == null)\r
+//                return;\r
+            final String perspectiveId = WorkbenchUtils.getCurrentPerspectiveId();\r
+            Control control = explorer.getControl();\r
+            Shell shell = control.getShell();\r
+\r
+            // Try the doubleClick-extensions\r
+            sessionContext.getSession().asyncRequest(new ChooseActionRequest(shell, explorer, input, perspectiveId));\r
+        } else if (e.keyCode == SWT.F2) {\r
+\r
+            IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+            NodeContext context = ISelectionUtils.filterSingleSelection(selectionProvider.getSelection(), NodeContext.class);\r
+            if (context == null || !isEditable(context))\r
+                return;\r
+\r
+            for (String column : editingColumnResolver.execute(context)) {\r
+                // Prefix column key with '#' to indicate that\r
+                // textual editing is preferred if supported.\r
+                String error = explorer.startEditing(context, "#" + column); \r
+                if (error == null)\r
+                    return;\r
+            }\r
+            return;\r
+        }\r
+    }\r
+\r
+    private boolean isEditable(NodeContext context) {\r
+        try {\r
+            return Simantics.getSession().syncRequest(new IsNodeContextModifiable(context));\r
+        } catch (DatabaseException e) {\r
+            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+                    "Failed to check node context modifiability, see exception for details.", e));\r
+            return false;\r
+        }\r
+    }\r
+\r
+}\r