]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/CanvasUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / utils / CanvasUtils.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/CanvasUtils.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/CanvasUtils.java
new file mode 100644 (file)
index 0000000..1a089b0
--- /dev/null
@@ -0,0 +1,135 @@
+/*******************************************************************************\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.g2d.utils;\r
+\r
+import java.util.function.Supplier;\r
+\r
+import org.simantics.g2d.canvas.Hints;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.scenegraph.g2d.events.command.Command;\r
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
+import org.simantics.scenegraph.g2d.events.command.Commands;\r
+import org.simantics.utils.threads.IThreadWorkQueue;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+\r
+/**\r
+ * @author Toni Kalajainen\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class CanvasUtils {\r
+\r
+    /**\r
+     * Enqueue a command in the event queue of the specified canvas context.\r
+     * \r
+     * @param ctx queue context\r
+     * @param commandId ID of the command to queue\r
+     * \r
+     * @see #sendCommand(ICanvasContext, Command)\r
+     */\r
+    public static void sendCommand(ICanvasContext ctx, String commandId)\r
+    {\r
+        sendCommand(ctx, new Command(commandId));\r
+    }\r
+\r
+    /**\r
+     * Enqueue a command in the event queue of the specified canvas context.\r
+     * \r
+     * @param ctx queue context\r
+     * @param cmd the command to queue\r
+     */\r
+    public static void sendCommand(ICanvasContext ctx, Command cmd)\r
+    {\r
+        CommandEvent e = new CommandEvent(ctx, System.currentTimeMillis(), cmd);\r
+        ctx.getEventQueue().queueEvent(e);\r
+    }\r
+\r
+    /**\r
+     * A utility for scheduling a zoom-to-fit operation when an Eclipse\r
+     * workbench editor is initially opened.\r
+     * \r
+     * <p>\r
+     * There are two asynchronous calls, first into the workbench (SWT) UI\r
+     * thread and then into the canvas context thread. This will guarantee that\r
+     * the parenting SWT control has been resized to its actual size instead of\r
+     * the initial (0,0) size which widgets start out at. In order to perform a\r
+     * zoom-to-fit operation we simply have to know the bounds of the parenting\r
+     * control. Thus, we need to ensure that this information is available and\r
+     * correct.\r
+     * \r
+     * @param swtThread\r
+     * @param disposed\r
+     * @param canvasContext\r
+     * @param diagramToFit\r
+     */\r
+    public static void scheduleZoomToFit(IThreadWorkQueue swtThread, final Supplier<Boolean> disposed,\r
+            final ICanvasContext canvasContext, final IDiagram diagramToFit)\r
+    {\r
+        scheduleZoomToFit(swtThread, disposed, canvasContext, diagramToFit, true);\r
+    }\r
+\r
+    /**\r
+     * A utility for scheduling a zoom-to-fit operation when an Eclipse\r
+     * workbench editor is initially opened.\r
+     * \r
+     * <p>\r
+     * There are two asynchronous calls, first into the workbench (SWT) UI\r
+     * thread and then into the canvas context thread. This will guarantee that\r
+     * the parenting SWT control has been resized to its actual size instead of\r
+     * the initial (0,0) size which widgets start out at. In order to perform a\r
+     * zoom-to-fit operation we simply have to know the bounds of the parenting\r
+     * control. Thus, we need to ensure that this information is available and\r
+     * correct.\r
+     * \r
+     * @param swtThread\r
+     * @param disposed\r
+     * @param canvasContext\r
+     * @param diagramToFit\r
+     * @param zoomToFit\r
+     */\r
+    public static void scheduleZoomToFit(IThreadWorkQueue swtThread, final Supplier<Boolean> disposed,\r
+            final ICanvasContext canvasContext, final IDiagram diagramToFit, boolean zoomToFit)\r
+    {\r
+        if (diagramToFit == null)\r
+            throw new IllegalStateException("source diagram is null");\r
+\r
+        Boolean canvasZoomToFit = canvasContext.getHintStack().getHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT);\r
+        diagramToFit.setHint(Hints.KEY_DISABLE_PAINTING, Boolean.TRUE);\r
+        if (zoomToFit && !Boolean.FALSE.equals(canvasZoomToFit))\r
+            diagramToFit.setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.TRUE);\r
+\r
+        ThreadUtils.asyncExec(swtThread, new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (disposed.get() || canvasContext.isDisposed())\r
+                    return;\r
+\r
+                ThreadUtils.asyncExec(canvasContext.getThreadAccess(), new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        if (disposed.get() || canvasContext.isDisposed())\r
+                            return;\r
+\r
+                        Boolean zoomToFit = diagramToFit.removeHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT);\r
+                        diagramToFit.removeHint(Hints.KEY_DISABLE_PAINTING);\r
+\r
+                        if (Boolean.TRUE.equals(zoomToFit))\r
+                            CanvasUtils.sendCommand(canvasContext, Commands.ZOOM_TO_FIT);\r
+                        CanvasUtils.sendCommand(canvasContext, Commands.ENABLE_PAINTING);\r
+                    }\r
+                });\r
+            }\r
+        });\r
+    }\r
+\r
+}\r