]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/CanvasUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / utils / CanvasUtils.java
index 1a089b02ba6493d50edbaf10ba43429c912b0170..7059c946618445d3b1930e967edc2ba798db8bce 100644 (file)
-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.utils;
+
+import java.util.function.Supplier;
+
+import org.simantics.g2d.canvas.Hints;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.DiagramHints;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.scenegraph.g2d.events.command.Command;
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;
+import org.simantics.scenegraph.g2d.events.command.Commands;
+import org.simantics.utils.threads.IThreadWorkQueue;
+import org.simantics.utils.threads.ThreadUtils;
+
+/**
+ * @author Toni Kalajainen
+ * @author Tuukka Lehtonen
+ */
+public class CanvasUtils {
+
+    /**
+     * Enqueue a command in the event queue of the specified canvas context.
+     * 
+     * @param ctx queue context
+     * @param commandId ID of the command to queue
+     * 
+     * @see #sendCommand(ICanvasContext, Command)
+     */
+    public static void sendCommand(ICanvasContext ctx, String commandId)
+    {
+        sendCommand(ctx, new Command(commandId));
+    }
+
+    /**
+     * Enqueue a command in the event queue of the specified canvas context.
+     * 
+     * @param ctx queue context
+     * @param cmd the command to queue
+     */
+    public static void sendCommand(ICanvasContext ctx, Command cmd)
+    {
+        CommandEvent e = new CommandEvent(ctx, System.currentTimeMillis(), cmd);
+        ctx.getEventQueue().queueEvent(e);
+    }
+
+    /**
+     * A utility for scheduling a zoom-to-fit operation when an Eclipse
+     * workbench editor is initially opened.
+     * 
+     * <p>
+     * There are two asynchronous calls, first into the workbench (SWT) UI
+     * thread and then into the canvas context thread. This will guarantee that
+     * the parenting SWT control has been resized to its actual size instead of
+     * the initial (0,0) size which widgets start out at. In order to perform a
+     * zoom-to-fit operation we simply have to know the bounds of the parenting
+     * control. Thus, we need to ensure that this information is available and
+     * correct.
+     * 
+     * @param swtThread
+     * @param disposed
+     * @param canvasContext
+     * @param diagramToFit
+     */
+    public static void scheduleZoomToFit(IThreadWorkQueue swtThread, final Supplier<Boolean> disposed,
+            final ICanvasContext canvasContext, final IDiagram diagramToFit)
+    {
+        scheduleZoomToFit(swtThread, disposed, canvasContext, diagramToFit, true);
+    }
+
+    /**
+     * A utility for scheduling a zoom-to-fit operation when an Eclipse
+     * workbench editor is initially opened.
+     * 
+     * <p>
+     * There are two asynchronous calls, first into the workbench (SWT) UI
+     * thread and then into the canvas context thread. This will guarantee that
+     * the parenting SWT control has been resized to its actual size instead of
+     * the initial (0,0) size which widgets start out at. In order to perform a
+     * zoom-to-fit operation we simply have to know the bounds of the parenting
+     * control. Thus, we need to ensure that this information is available and
+     * correct.
+     * 
+     * @param swtThread
+     * @param disposed
+     * @param canvasContext
+     * @param diagramToFit
+     * @param zoomToFit
+     */
+    public static void scheduleZoomToFit(IThreadWorkQueue swtThread, final Supplier<Boolean> disposed,
+            final ICanvasContext canvasContext, final IDiagram diagramToFit, boolean zoomToFit)
+    {
+        if (diagramToFit == null)
+            throw new IllegalStateException("source diagram is null");
+
+        Boolean canvasZoomToFit = canvasContext.getHintStack().getHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT);
+        diagramToFit.setHint(Hints.KEY_DISABLE_PAINTING, Boolean.TRUE);
+        if (zoomToFit && !Boolean.FALSE.equals(canvasZoomToFit))
+            diagramToFit.setHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT, Boolean.TRUE);
+
+        ThreadUtils.asyncExec(swtThread, new Runnable() {
+            @Override
+            public void run() {
+                if (disposed.get() || canvasContext.isDisposed())
+                    return;
+
+                ThreadUtils.asyncExec(canvasContext.getThreadAccess(), new Runnable() {
+                    @Override
+                    public void run() {
+                        if (disposed.get() || canvasContext.isDisposed())
+                            return;
+
+                        Boolean zoomToFit = diagramToFit.removeHint(DiagramHints.KEY_INITIAL_ZOOM_TO_FIT);
+                        diagramToFit.removeHint(Hints.KEY_DISABLE_PAINTING);
+
+                        if (Boolean.TRUE.equals(zoomToFit))
+                            CanvasUtils.sendCommand(canvasContext, Commands.ZOOM_TO_FIT);
+                        CanvasUtils.sendCommand(canvasContext, Commands.ENABLE_PAINTING);
+                    }
+                });
+            }
+        });
+    }
+
+}