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