]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ShapeClass.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / ShapeClass.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;
13
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;
20
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;
38
39 /**
40  * @author Toni Kalajainen
41  */
42 public class ShapeClass {
43
44     final static BasicStroke STROKE = new BasicStroke(1.0f);
45
46     public static final ElementClass CIRCLE_CLASS = getShapeClass(new Ellipse2D.Double(-10, -10, 20, 20));
47
48     public static ElementClass getShapeClass(Shape s)
49     {
50         return
51         ElementClass.compile(
52                 DefaultTransform.INSTANCE,
53                 BorderColorImpl.BLACK,
54                 FillColorImpl.WHITE,
55                 TextImpl.INSTANCE,
56                 new ShapePainter(s),
57                 new TextPainter(),
58                 new RotatorHandler()
59         );
60
61     }
62
63     static class ShapePainter implements SceneGraph, Outline, InternalSize
64     {
65         /**
66          * 
67          */
68         private static final long serialVersionUID = 2328124349273536527L;
69         Shape shape;
70         Rectangle2D bounds;
71
72         public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
73
74         public ShapePainter(Shape shape) {
75             this.shape = shape;
76             this.bounds = shape.getBounds2D();
77         }
78
79         @Override
80         public void cleanup(IElement e) {
81             Node node = e.removeHint(SG_NODE);
82             if (node != null)
83                 node.remove();
84         }
85
86         @Override
87         public void init(IElement e, G2DParentNode parent) {
88             G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);
89             if (node == null) {
90                 node = parent.addNode(G2DParentNode.class);
91                 e.setHint(SG_NODE, node);
92             }
93                         AffineTransform at      = ElementUtils.getTransform(e);
94                         if(at != null) node.setTransform(at);
95
96             ShapeNode bgnode = node.getOrCreateNode("bgnode", ShapeNode.class);
97             ShapeNode fgnode = node.getOrCreateNode("fgnode", ShapeNode.class);
98
99             Color fc = ElementUtils.getFillColor(e);
100             Color bc = ElementUtils.getBorderColor(e);
101
102             fgnode.setColor(fc);
103             fgnode.setFill(true);
104             fgnode.setShape(shape);
105
106             bgnode.setColor(bc);
107             bgnode.setStroke(STROKE);
108             bgnode.setScaleStroke(true);
109             bgnode.setShape(shape);
110         }
111
112         @Override
113         public Shape getElementShape(IElement e) {
114             return shape;
115         }
116
117         @Override
118         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
119             if (size==null) size = new Rectangle2D.Double();
120             size.setFrame(bounds);
121             return size;
122         }
123     }
124 }