--- /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.Color;\r
+import java.awt.Stroke;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Line2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementHints;\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.ShapeNode;\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
+\r
+/**\r
+ * Initializes the scene graph of another diagram inside the internal bounds of\r
+ * the element.\r
+ * \r
+ * <p>\r
+ * Used keys for input element:\r
+ * <ul>\r
+ * <li>DiagramHints.KEY_DIAGRAM</li>\r
+ * <li>DiagramPainter.KEY_VIEWPORT</li>\r
+ * </ul>\r
+ * \r
+ * <p>\r
+ * Supports:\r
+ * <ul>\r
+ * <li>BorderColor</li>\r
+ * <li>FillColor</li>\r
+ * </ul>\r
+ * \r
+ * @see MarginUtils for determining viewport or\r
+ * @see ElementViewport lock viewport on an element\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class DiagramPainter implements SceneGraph {\r
+\r
+ public static final Key KEY_VIEWPORT = new KeyOf(AffineTransform.class, "DiagramPainter.VIEWPORT");\r
+\r
+ private static final long serialVersionUID = 6500535141363250713L;\r
+\r
+ public static final DiagramPainter INSTANCE = new DiagramPainter();\r
+\r
+ private static final Color FILM_COLOR = new Color(0.0f, 0.0f, 0.0f, 0.3f);\r
+\r
+ private 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
+ G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);\r
+ if (node == null) {\r
+ node = parent.addNode(G2DParentNode.class);\r
+ e.setHint(SG_NODE, node);\r
+ }\r
+\r
+ IDiagram d = e.getHint(DiagramHints.KEY_DIAGRAM);\r
+ Rectangle2D bounds = ElementUtils.getElementBounds(e);\r
+ AffineTransform viewport = e.getHint(KEY_VIEWPORT);\r
+ Color bc = ElementUtils.getBorderColor(e);\r
+ Color fc = ElementUtils.getFillColor(e);\r
+\r
+ node.removeNodes();\r
+\r
+ if (fc != null) {\r
+ ShapeNode fcn = node.addNode(ShapeNode.class);\r
+ fcn.setColor(fc);\r
+ fcn.setFill(true);\r
+ fcn.setShape(bounds);\r
+ }\r
+\r
+ if (bc != null) {\r
+ ShapeNode bcn = node.addNode(ShapeNode.class);\r
+ Stroke stroke = e.getHint(ElementHints.KEY_STROKE);\r
+ if (stroke != null)\r
+ bcn.setStroke(stroke);\r
+ bcn.setScaleStroke(true);\r
+ bcn.setColor(bc);\r
+ bcn.setShape(bounds);\r
+ }\r
+\r
+ // No diagram\r
+ if (d==null) {\r
+ ShapeNode shape = node.addNode(ShapeNode.class);\r
+ ShapeNode line1 = node.addNode(ShapeNode.class);\r
+ ShapeNode line2 = node.addNode(ShapeNode.class);\r
+\r
+ shape.setColor(FILM_COLOR);\r
+ shape.setFill(true);\r
+ shape.setShape(bounds);\r
+\r
+ line1.setColor(Color.WHITE);\r
+ line1.setShape(new Line2D.Double(bounds.getMinX(), bounds.getMinX(), bounds.getMaxX(), bounds.getMaxY()));\r
+\r
+ line2.setColor(Color.WHITE);\r
+ line2.setShape(new Line2D.Double(bounds.getMaxX(), bounds.getMinX(), bounds.getMinX(), bounds.getMaxY()));\r
+ } else {\r
+ if (viewport==null)\r
+ viewport = new AffineTransform();\r
+// FIXME: we need to get elementpainter..\r
+// ElementPainter ep = ctx.getSingleItem(ElementPainter.class);\r
+// ep.paintDiagram(node, d, null);\r
+ }\r
+ }\r
+}\r