]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/DefaultElementDoubleClickHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / DefaultElementDoubleClickHandler.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/DefaultElementDoubleClickHandler.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/DefaultElementDoubleClickHandler.java
new file mode 100644 (file)
index 0000000..e94be21
--- /dev/null
@@ -0,0 +1,148 @@
+/*******************************************************************************\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.modeling.ui.diagramEditor.handlers;\r
+\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.action.IAction;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.PartInitException;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
+import org.simantics.ui.workbench.action.ChooseActionRequest;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+import org.simantics.utils.ui.action.IPriorityAction;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * Really simple support for activating property view from diagram when a single\r
+ * element is double clicked.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DefaultElementDoubleClickHandler extends AbstractCanvasParticipant {\r
+\r
+    @Dependency\r
+    Selection selection;\r
+\r
+    public boolean accept(IElement element) {\r
+       return true;\r
+    }\r
+    \r
+    @EventHandler(priority = -10)\r
+    public boolean handleDoubleClick(MouseDoubleClickedEvent me) {\r
+       \r
+        Set<IElement> sel = selection.getSelection(0);\r
+        if (sel.size() == 1) {\r
+               \r
+            IElement e = sel.iterator().next();\r
+            if(!accept(e)) return false;\r
+            \r
+            final Object data = e.getHint(ElementHints.KEY_OBJECT);\r
+\r
+            final Display display = PlatformUI.getWorkbench().getDisplay();\r
+            if (display == null)\r
+                return false;\r
+\r
+            // See if there's any highest-priority editor adapter that we could use for the object:\r
+            try {\r
+                Boolean consumed = Simantics.getSession().sync(new UniqueRead<Boolean>() {\r
+                    @Override\r
+                    public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                        IPriorityAction[] actions = ChooseActionRequest.findActions(graph, null, data, "", false, false);\r
+                        IPriorityAction priorityAction = (IPriorityAction) ChooseActionRequest.chooseAction(null, actions, null, true);\r
+                        if (priorityAction != null && priorityAction.getPriority() >= 10) {\r
+                            //System.out.println("HIGH PRIORITY ACTION(" + priorityAction.getPriority() + "): " + priorityAction);\r
+                            display.asyncExec( actionAsRunnable( priorityAction) );\r
+                            return true;\r
+                        } else if (data instanceof Resource) {\r
+                            PlatformUI.getWorkbench().getDisplay().asyncExec( revealView("org.simantics.browsing.ui.graph.propertyView") );\r
+                            return true;\r
+                        }\r
+                        return false;\r
+                    }\r
+                });\r
+                return consumed;\r
+            } catch (DatabaseException ex) {\r
+                ErrorLogger.defaultLogError(ex);\r
+            }\r
+        }\r
+\r
+        return false;\r
+    }\r
+\r
+    public static Runnable revealView(final String viewIdPart) {\r
+        return new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                try {\r
+                    WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);\r
+                } catch (PartInitException e) {\r
+                    ExceptionUtils.logAndShowError(e);\r
+                }\r
+            }\r
+        };\r
+    }\r
+\r
+    public static Runnable revealAndShowView(final String viewIdPart) {\r
+        return new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                try {\r
+                    WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);\r
+\r
+                    // Give the post selection listeners time to settle.\r
+                    PlatformUI.getWorkbench().getDisplay().timerExec( 500, showView(viewIdPart) );\r
+                } catch (PartInitException e) {\r
+                    ExceptionUtils.logAndShowError(e);\r
+                }\r
+            }\r
+        };\r
+    }\r
+\r
+    public static Runnable showView(final String viewIdPart) {\r
+        return new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                try {\r
+                    CommandUtil.showView(viewIdPart);\r
+                } catch (ExecutionException e) {\r
+                    ExceptionUtils.logAndShowError(e);\r
+                }\r
+            }\r
+        };\r
+    }\r
+\r
+    public static Runnable actionAsRunnable(final IAction action) {\r
+        return new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                action.run();\r
+            }\r
+        };\r
+    }\r
+\r
+}\r