1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.elementclass.canvas;
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;
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;
35 * Initializes the scene graph of another diagram inside the internal bounds of
39 * Used keys for input element:
41 * <li>DiagramHints.KEY_DIAGRAM</li>
42 * <li>DiagramPainter.KEY_VIEWPORT</li>
48 * <li>BorderColor</li>
52 * @see MarginUtils for determining viewport or
53 * @see ElementViewport lock viewport on an element
55 * @author Toni Kalajainen
57 public class DiagramPainter implements SceneGraph {
59 public static final Key KEY_VIEWPORT = new KeyOf(AffineTransform.class, "DiagramPainter.VIEWPORT");
61 private static final long serialVersionUID = 6500535141363250713L;
63 public static final DiagramPainter INSTANCE = new DiagramPainter();
65 private static final Color FILM_COLOR = new Color(0.0f, 0.0f, 0.0f, 0.3f);
67 private static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
70 public void cleanup(IElement e) {
71 ElementUtils.removePossibleNode(e, SG_NODE);
75 public void init(IElement e, G2DParentNode parent) {
76 G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);
78 node = parent.addNode(G2DParentNode.class);
79 e.setHint(SG_NODE, node);
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);
91 ShapeNode fcn = node.addNode(ShapeNode.class);
98 ShapeNode bcn = node.addNode(ShapeNode.class);
99 Stroke stroke = e.getHint(ElementHints.KEY_STROKE);
101 bcn.setStroke(stroke);
102 bcn.setScaleStroke(true);
104 bcn.setShape(bounds);
109 ShapeNode shape = node.addNode(ShapeNode.class);
110 ShapeNode line1 = node.addNode(ShapeNode.class);
111 ShapeNode line2 = node.addNode(ShapeNode.class);
113 shape.setColor(FILM_COLOR);
115 shape.setShape(bounds);
117 line1.setColor(Color.WHITE);
118 line1.setShape(new Line2D.Double(bounds.getMinX(), bounds.getMinX(), bounds.getMaxX(), bounds.getMaxY()));
120 line2.setColor(Color.WHITE);
121 line2.setShape(new Line2D.Double(bounds.getMaxX(), bounds.getMinX(), bounds.getMinX(), bounds.getMaxY()));
124 viewport = new AffineTransform();
125 // FIXME: we need to get elementpainter..
126 // ElementPainter ep = ctx.getSingleItem(ElementPainter.class);
127 // ep.paintDiagram(node, d, null);