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