]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/CanvasCommandDelegate.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / CanvasCommandDelegate.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/CanvasCommandDelegate.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/CanvasCommandDelegate.java
new file mode 100644 (file)
index 0000000..094fc87
--- /dev/null
@@ -0,0 +1,71 @@
+/*******************************************************************************\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.diagram.handler;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.chassis.ICanvasChassis;\r
+import org.simantics.scenegraph.g2d.events.EventDebugPolicy;\r
+import org.simantics.scenegraph.g2d.events.EventQueue;\r
+import org.simantics.scenegraph.g2d.events.command.Command;\r
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
+\r
+/**\r
+ * A handler for the Eclipse Command framework that delegates ID of the command\r
+ * that triggered the event to the {@link EventQueue} of the\r
+ * {@link ICanvasContext} where the event occurred.\r
+ */\r
+public class CanvasCommandDelegate extends AbstractHandler {\r
+\r
+    private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION;\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        Event e = ((Event)event.getTrigger());\r
+        CommandEvent ce = new CommandEvent(null, e.time, new Command(event.getCommand().getId()));\r
+\r
+        if (DEBUG)\r
+            System.out.println(getClass().getSimpleName() + ": trigger event = " + e + " for command " + ce.command);\r
+\r
+        if (e.widget instanceof ICanvasChassis) {\r
+            ICanvasChassis chassis = (ICanvasChassis) e.widget;\r
+            ICanvasContext canvas = chassis.getCanvasContext();\r
+            if (canvas != null) {\r
+                if (DEBUG)\r
+                    System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + chassis);\r
+                canvas.getEventQueue().queueEvent( ce );\r
+            }\r
+        } else {\r
+            ICanvasContext canvas = null;\r
+            IWorkbenchPart part = HandlerUtil.getActiveEditor(event);\r
+            if (part != null)\r
+                canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);\r
+            if (canvas == null) {\r
+                part = HandlerUtil.getActivePart(event);\r
+                if (part != null)\r
+                    canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);\r
+            }\r
+            if (canvas != null) {\r
+                if (DEBUG)\r
+                    System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + part);\r
+                canvas.getEventQueue().queueEvent(ce);\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+}
\ No newline at end of file