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;
\r
14 import java.awt.BasicStroke;
\r
15 import java.awt.Color;
\r
16 import java.awt.Shape;
\r
17 import java.awt.geom.AffineTransform;
\r
18 import java.awt.geom.Ellipse2D;
\r
19 import java.awt.geom.Rectangle2D;
\r
21 import org.simantics.g2d.element.ElementClass;
\r
22 import org.simantics.g2d.element.ElementUtils;
\r
23 import org.simantics.g2d.element.IElement;
\r
24 import org.simantics.g2d.element.SceneGraphNodeKey;
\r
25 import org.simantics.g2d.element.handler.InternalSize;
\r
26 import org.simantics.g2d.element.handler.Outline;
\r
27 import org.simantics.g2d.element.handler.SceneGraph;
\r
28 import org.simantics.g2d.element.handler.impl.BorderColorImpl;
\r
29 import org.simantics.g2d.element.handler.impl.DefaultTransform;
\r
30 import org.simantics.g2d.element.handler.impl.FillColorImpl;
\r
31 import org.simantics.g2d.element.handler.impl.TextImpl;
\r
32 import org.simantics.g2d.element.handler.impl.TextPainter;
\r
33 import org.simantics.g2d.elementclass.wheel.RotatorHandler;
\r
34 import org.simantics.scenegraph.Node;
\r
35 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
36 import org.simantics.scenegraph.g2d.nodes.ShapeNode;
\r
37 import org.simantics.utils.datastructures.hints.IHintContext.Key;
\r
40 * @author Toni Kalajainen
\r
42 public class ShapeClass {
\r
44 final static BasicStroke STROKE = new BasicStroke(1.0f);
\r
46 public static final ElementClass CIRCLE_CLASS = getShapeClass(new Ellipse2D.Double(-10, -10, 20, 20));
\r
48 public static ElementClass getShapeClass(Shape s)
\r
51 ElementClass.compile(
\r
52 DefaultTransform.INSTANCE,
\r
53 BorderColorImpl.BLACK,
\r
54 FillColorImpl.WHITE,
\r
56 new ShapePainter(s),
\r
58 new RotatorHandler()
\r
63 static class ShapePainter implements SceneGraph, Outline, InternalSize
\r
68 private static final long serialVersionUID = 2328124349273536527L;
\r
72 public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
\r
74 public ShapePainter(Shape shape) {
\r
76 this.bounds = shape.getBounds2D();
\r
80 public void cleanup(IElement e) {
\r
81 Node node = e.removeHint(SG_NODE);
\r
87 public void init(IElement e, G2DParentNode parent) {
\r
88 G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);
\r
90 node = parent.addNode(G2DParentNode.class);
\r
91 e.setHint(SG_NODE, node);
\r
93 AffineTransform at = ElementUtils.getTransform(e);
\r
94 if(at != null) node.setTransform(at);
\r
96 ShapeNode bgnode = node.getOrCreateNode("bgnode", ShapeNode.class);
\r
97 ShapeNode fgnode = node.getOrCreateNode("fgnode", ShapeNode.class);
\r
99 Color fc = ElementUtils.getFillColor(e);
\r
100 Color bc = ElementUtils.getBorderColor(e);
\r
102 fgnode.setColor(fc);
\r
103 fgnode.setFill(true);
\r
104 fgnode.setShape(shape);
\r
106 bgnode.setColor(bc);
\r
107 bgnode.setStroke(STROKE);
\r
108 bgnode.setScaleStroke(true);
\r
109 bgnode.setShape(shape);
\r
113 public Shape getElementShape(IElement e) {
\r
118 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
\r
119 if (size==null) size = new Rectangle2D.Double();
\r
120 size.setFrame(bounds);
\r