--- /dev/null
+/*******************************************************************************\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.Rectangle2D;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.SceneGraphNodeKey;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.scenegraph.Node;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.ClippingNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * An element handler that "paints" a canvas bound to an element.\r
+ * \r
+ * Keys: CanvasClass.KEY_CANVAS\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class CanvasPainter implements SceneGraph {\r
+\r
+ private static final long serialVersionUID = -182067379834789790L;\r
+\r
+ public static final CanvasPainter INSTANCE = new CanvasPainter();\r
+\r
+ public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
+\r
+ @Override\r
+ public void cleanup(IElement e) {\r
+ ElementUtils.removePossibleNode(e, SG_NODE);\r
+ }\r
+\r
+ @Override\r
+ public void init(IElement e, G2DParentNode parent) {\r
+ ICanvasContext ctx = e.getHint(CanvasClass.KEY_CANVAS);\r
+ if (ctx == null) {\r
+ ElementUtils.removePossibleNode(e, SG_NODE);\r
+ return;\r
+ }\r
+\r
+ ClippingNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, ClippingNode.class);\r
+\r
+ Rectangle2D bounds = ElementUtils.getElementBounds(e);\r
+ node.setClip(bounds);\r
+ }\r
+\r
+}\r