]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
2851dccdb37a85ca63fc39a6f64b4b0e957d902d
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.diagram;\r
2 \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
8 \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
24 \r
25 public class DrawableElementClass {\r
26         private DrawableElementClass() {}\r
27         \r
28         public static KeyOf KEY_CONTENT = new KeyOf(IDrawable.class, "CONTENT");\r
29         public static AffineTransform IDENTITY_TRANSFORM = new AffineTransform();\r
30                 \r
31         public static WidgetHandler WIDGET_HANDLER_INSTANCE = new WidgetHandler();\r
32         \r
33         private static class WidgetHandler implements Transform, SceneGraph, InternalSize, Move, Outline, Pick2 {\r
34 \r
35                 private static final long serialVersionUID = 1722978969083481038L;\r
36                 \r
37                 @Override\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
44                         }\r
45                 }\r
46                 \r
47                 @Override\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
52                         else\r
53                                 return IDENTITY_TRANSFORM;\r
54                 }\r
55                 \r
56                 @Override\r
57                 public void cleanup(IElement e) {\r
58                         ((IDrawable)e.getHint(KEY_CONTENT)).cleanupSG();        \r
59                 }\r
60 \r
61                 @Override\r
62                 public void init(IElement e, G2DParentNode parent) {\r
63                         ((IDrawable)e.getHint(KEY_CONTENT)).initSG(parent);\r
64                 }\r
65                 \r
66                 @Override\r
67                 public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
68                         if(size == null)\r
69                                 size = new Rectangle2D.Double();\r
70                         ((IDrawable)e.getHint(KEY_CONTENT)).getBounds(size);\r
71                         return size;\r
72                 }\r
73 \r
74                 @Override\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
79                         else\r
80                                 return new Point2D.Double(0.0, 0.0);\r
81                 }\r
82 \r
83                 @Override\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
90                         }\r
91                 }\r
92 \r
93                 @Override\r
94                 public Shape getElementShape(IElement e) {\r
95                         return getBounds(e, null);\r
96                 }\r
97                 \r
98                 @Override\r
99                 public int pick(IElement e, Shape s, PickPolicy policy,\r
100                                 Collection<IElement> result) {\r
101                         if(pickTest(e, s, policy)) {\r
102                                 result.add(e);\r
103                                 return 1;\r
104                         }\r
105                         else\r
106                                 return 0;                       \r
107                 }\r
108 \r
109                 @Override\r
110                 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {\r
111                         IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT);\r
112                         switch(policy) {\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
117                         default:\r
118                                 return false;\r
119                         }                       \r
120                 }\r
121                 \r
122         }\r
123 \r
124         public static ElementClass createElementClass(final Resource resource) { \r
125                 return ElementClass.compile(\r
126                         WIDGET_HANDLER_INSTANCE,\r
127                         new ResourceAdapter() {\r
128 \r
129                                 @Override\r
130                                 public Resource getResource() {\r
131                                         return resource;\r
132                                 }\r
133                                 \r
134                         }\r
135                 );\r
136         }\r
137 }\r