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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.elementclass.canvas;
\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
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
35 * Initializes the scene graph of another diagram inside the internal bounds of
\r
39 * Used keys for input element:
\r
41 * <li>DiagramHints.KEY_DIAGRAM</li>
\r
42 * <li>DiagramPainter.KEY_VIEWPORT</li>
\r
48 * <li>BorderColor</li>
\r
49 * <li>FillColor</li>
\r
52 * @see MarginUtils for determining viewport or
\r
53 * @see ElementViewport lock viewport on an element
\r
55 * @author Toni Kalajainen
\r
57 public class DiagramPainter implements SceneGraph {
\r
59 public static final Key KEY_VIEWPORT = new KeyOf(AffineTransform.class, "DiagramPainter.VIEWPORT");
\r
61 private static final long serialVersionUID = 6500535141363250713L;
\r
63 public static final DiagramPainter INSTANCE = new DiagramPainter();
\r
65 private static final Color FILM_COLOR = new Color(0.0f, 0.0f, 0.0f, 0.3f);
\r
67 private static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
\r
70 public void cleanup(IElement e) {
\r
71 ElementUtils.removePossibleNode(e, SG_NODE);
\r
75 public void init(IElement e, G2DParentNode parent) {
\r
76 G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);
\r
78 node = parent.addNode(G2DParentNode.class);
\r
79 e.setHint(SG_NODE, node);
\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
91 ShapeNode fcn = node.addNode(ShapeNode.class);
\r
94 fcn.setShape(bounds);
\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
104 bcn.setShape(bounds);
\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
113 shape.setColor(FILM_COLOR);
\r
114 shape.setFill(true);
\r
115 shape.setShape(bounds);
\r
117 line1.setColor(Color.WHITE);
\r
118 line1.setShape(new Line2D.Double(bounds.getMinX(), bounds.getMinX(), bounds.getMaxX(), bounds.getMaxY()));
\r
120 line2.setColor(Color.WHITE);
\r
121 line2.setShape(new Line2D.Double(bounds.getMaxX(), bounds.getMinX(), bounds.getMinX(), bounds.getMaxY()));
\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