]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/canvas/ElementViewport.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / canvas / ElementViewport.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/canvas/ElementViewport.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/canvas/ElementViewport.java
new file mode 100644 (file)
index 0000000..84163af
--- /dev/null
@@ -0,0 +1,64 @@
+/*******************************************************************************\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.elementclass.canvas;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.utils.GeometryUtils;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+import org.simantics.utils.page.MarginUtils;\r
+import org.simantics.utils.page.MarginUtils.Margins;\r
+\r
+/**\r
+ * Lock viewport (KEY_VIEWPORT) of DiagramPainter to an element.\r
+ * \r
+ * e.setHint(DiagramHints.KEY_DIAGRAM, ElementUtils.getDiagram(focusE)); Note!\r
+ * Add this handler _before_ diagram painter in element class\r
+ * \r
+ * Keys: ElementViewport.KEY_ELEMENT\r
+ * \r
+ * @See {@link DiagramPainter}\r
+ * @author Toni Kalajainen\r
+ */\r
+public class ElementViewport implements SceneGraph {\r
+\r
+    public static final Key             KEY_ELEMENT      = new KeyOf(IElement.class);\r
+\r
+    private static final long           serialVersionUID = -849879952227051310L;\r
+\r
+    public static final ElementViewport INSTANCE         = new ElementViewport();\r
+\r
+    private final Margins               margins          = MarginUtils.MARGINS5;\r
+\r
+    @Override\r
+    public void cleanup(IElement e) {\r
+    }\r
+\r
+    @Override\r
+    public void init(IElement e, G2DParentNode parent) {\r
+        // Initializes viewport before DiagramPainter initializes the\r
+        // scenegraph.\r
+        IElement focusE = e.getHint(KEY_ELEMENT);\r
+        Rectangle2D focusBounds = ElementUtils.getElementBounds(focusE);\r
+        Rectangle2D bounds = ElementUtils.getElementBounds(e);\r
+\r
+        AffineTransform at = GeometryUtils.fitArea(bounds, focusBounds, margins);\r
+        e.setHint(DiagramPainter.KEY_VIEWPORT, at);\r
+    }\r
+\r
+}\r