]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/canvas/DiagramPainter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / canvas / DiagramPainter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.elementclass.canvas;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Stroke;\r
16 import java.awt.geom.AffineTransform;\r
17 import java.awt.geom.Line2D;\r
18 import java.awt.geom.Rectangle2D;\r
19 \r
20 import org.simantics.g2d.diagram.DiagramHints;\r
21 import org.simantics.g2d.diagram.IDiagram;\r
22 import org.simantics.g2d.element.ElementHints;\r
23 import org.simantics.g2d.element.ElementUtils;\r
24 import org.simantics.g2d.element.IElement;\r
25 import org.simantics.g2d.element.SceneGraphNodeKey;\r
26 import org.simantics.g2d.element.handler.SceneGraph;\r
27 import org.simantics.scenegraph.Node;\r
28 import org.simantics.scenegraph.g2d.G2DParentNode;\r
29 import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
30 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
31 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
32 import org.simantics.utils.page.MarginUtils;\r
33 \r
34 /**\r
35  * Initializes the scene graph of another diagram inside the internal bounds of\r
36  * the element.\r
37  * \r
38  * <p>\r
39  * Used keys for input element:\r
40  * <ul>\r
41  * <li>DiagramHints.KEY_DIAGRAM</li>\r
42  * <li>DiagramPainter.KEY_VIEWPORT</li>\r
43  * </ul>\r
44  * \r
45  * <p>\r
46  * Supports:\r
47  * <ul>\r
48  * <li>BorderColor</li>\r
49  * <li>FillColor</li>\r
50  * </ul>\r
51  * \r
52  * @see MarginUtils for determining viewport or\r
53  * @see ElementViewport lock viewport on an element\r
54  * \r
55  * @author Toni Kalajainen\r
56  */\r
57 public class DiagramPainter implements SceneGraph {\r
58 \r
59     public static final Key            KEY_VIEWPORT     = new KeyOf(AffineTransform.class, "DiagramPainter.VIEWPORT");\r
60 \r
61     private static final long          serialVersionUID = 6500535141363250713L;\r
62 \r
63     public static final DiagramPainter INSTANCE         = new DiagramPainter();\r
64 \r
65     private static final Color         FILM_COLOR       = new Color(0.0f, 0.0f, 0.0f, 0.3f);\r
66 \r
67     private static final Key           SG_NODE          = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
68 \r
69     @Override\r
70     public void cleanup(IElement e) {\r
71         ElementUtils.removePossibleNode(e, SG_NODE);\r
72     }\r
73 \r
74     @Override\r
75     public void init(IElement e, G2DParentNode parent) {\r
76         G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);\r
77         if (node == null) {\r
78             node = parent.addNode(G2DParentNode.class);\r
79             e.setHint(SG_NODE, node);\r
80         }\r
81 \r
82         IDiagram d = e.getHint(DiagramHints.KEY_DIAGRAM);\r
83         Rectangle2D bounds = ElementUtils.getElementBounds(e);\r
84         AffineTransform viewport = e.getHint(KEY_VIEWPORT);\r
85         Color bc = ElementUtils.getBorderColor(e);\r
86         Color fc = ElementUtils.getFillColor(e);\r
87 \r
88         node.removeNodes();\r
89 \r
90         if (fc != null) {\r
91             ShapeNode fcn = node.addNode(ShapeNode.class);\r
92             fcn.setColor(fc);\r
93             fcn.setFill(true);\r
94             fcn.setShape(bounds);\r
95         }\r
96 \r
97         if (bc != null) {\r
98             ShapeNode bcn = node.addNode(ShapeNode.class);\r
99             Stroke stroke = e.getHint(ElementHints.KEY_STROKE);\r
100             if (stroke != null)\r
101                 bcn.setStroke(stroke);\r
102             bcn.setScaleStroke(true);\r
103             bcn.setColor(bc);\r
104             bcn.setShape(bounds);\r
105         }\r
106 \r
107         // No diagram\r
108         if (d==null) {\r
109             ShapeNode shape = node.addNode(ShapeNode.class);\r
110             ShapeNode line1 = node.addNode(ShapeNode.class);\r
111             ShapeNode line2 = node.addNode(ShapeNode.class);\r
112 \r
113             shape.setColor(FILM_COLOR);\r
114             shape.setFill(true);\r
115             shape.setShape(bounds);\r
116 \r
117             line1.setColor(Color.WHITE);\r
118             line1.setShape(new Line2D.Double(bounds.getMinX(), bounds.getMinX(), bounds.getMaxX(), bounds.getMaxY()));\r
119 \r
120             line2.setColor(Color.WHITE);\r
121             line2.setShape(new Line2D.Double(bounds.getMaxX(), bounds.getMinX(), bounds.getMinX(), bounds.getMaxY()));\r
122         } else {\r
123             if (viewport==null)\r
124                 viewport = new AffineTransform();\r
125 // FIXME: we need to get elementpainter..\r
126 //            ElementPainter ep = ctx.getSingleItem(ElementPainter.class);\r
127 //            ep.paintDiagram(node, d, null);\r
128         }\r
129     }\r
130 }\r