1 package org.simantics.sysdyn.ui.diagram;
\r
3 import java.awt.Shape;
\r
4 import java.awt.geom.AffineTransform;
\r
5 import java.awt.geom.Point2D;
\r
6 import java.awt.geom.Rectangle2D;
\r
7 import java.util.Collection;
\r
9 import org.simantics.db.Resource;
\r
10 import org.simantics.diagram.adapter.handler.ResourceAdapter;
\r
11 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
\r
12 import org.simantics.g2d.element.ElementClass;
\r
13 import org.simantics.g2d.element.IElement;
\r
14 import org.simantics.g2d.element.handler.InternalSize;
\r
15 import org.simantics.g2d.element.handler.Move;
\r
16 import org.simantics.g2d.element.handler.Outline;
\r
17 import org.simantics.g2d.element.handler.Pick2;
\r
18 import org.simantics.g2d.element.handler.SceneGraph;
\r
19 import org.simantics.g2d.element.handler.Transform;
\r
20 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
21 import org.simantics.sysdyn.ui.elements.IDrawable;
\r
22 import org.simantics.sysdyn.ui.elements.ITransformable;
\r
23 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
\r
25 public class DrawableElementClass {
\r
26 private DrawableElementClass() {}
\r
28 public static KeyOf KEY_CONTENT = new KeyOf(IDrawable.class, "CONTENT");
\r
29 public static AffineTransform IDENTITY_TRANSFORM = new AffineTransform();
\r
31 public static WidgetHandler WIDGET_HANDLER_INSTANCE = new WidgetHandler();
\r
33 private static class WidgetHandler implements Transform, SceneGraph, InternalSize, Move, Outline, Pick2 {
\r
35 private static final long serialVersionUID = 1722978969083481038L;
\r
38 public void setTransform(IElement e, AffineTransform at) {
\r
39 IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT);
\r
40 if(drawable instanceof ITransformable) {
\r
41 drawable = ((ITransformable)drawable).setTransform(at);
\r
42 e.setHint(KEY_CONTENT, drawable);
\r
43 drawable.updateSG();
\r
48 public AffineTransform getTransform(IElement e) {
\r
49 IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT);
\r
50 if(drawable instanceof ITransformable)
\r
51 return ((ITransformable)drawable).getTransform();
\r
53 return IDENTITY_TRANSFORM;
\r
57 public void cleanup(IElement e) {
\r
58 ((IDrawable)e.getHint(KEY_CONTENT)).cleanupSG();
\r
62 public void init(IElement e, G2DParentNode parent) {
\r
63 ((IDrawable)e.getHint(KEY_CONTENT)).initSG(parent);
\r
67 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
\r
69 size = new Rectangle2D.Double();
\r
70 ((IDrawable)e.getHint(KEY_CONTENT)).getBounds(size);
\r
75 public Point2D getPosition(IElement e) {
\r
76 IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT);
\r
77 if(drawable instanceof ITransformable)
\r
78 return ((ITransformable)drawable).getPosition();
\r
80 return new Point2D.Double(0.0, 0.0);
\r
84 public void moveTo(IElement e, double x, double y) {
\r
85 IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT);
\r
86 if(drawable instanceof ITransformable) {
\r
87 drawable = ((ITransformable)drawable).moveTo(x, y);
\r
88 e.setHint(KEY_CONTENT, drawable);
\r
89 drawable.updateSG();
\r
94 public Shape getElementShape(IElement e) {
\r
95 return getBounds(e, null);
\r
99 public int pick(IElement e, Shape s, PickPolicy policy,
\r
100 Collection<IElement> result) {
\r
101 if(pickTest(e, s, policy)) {
\r
110 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
\r
111 IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT);
\r
113 case PICK_CONTAINED_OBJECTS:
\r
114 return drawable.pickContained(s);
\r
115 case PICK_INTERSECTING_OBJECTS:
\r
116 return drawable.pickIntersecting(s);
\r
124 public static ElementClass createElementClass(final Resource resource) {
\r
125 return ElementClass.compile(
\r
126 WIDGET_HANDLER_INSTANCE,
\r
127 new ResourceAdapter() {
\r
130 public Resource getResource() {
\r