]> gerrit.simantics Code Review - simantics/district.git/blob
ff1d844faf936c93a5344db9804a768c85a29991
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;\r
2 \r
3 import java.awt.geom.Path2D;\r
4 \r
5 import org.simantics.db.AsyncReadGraph;\r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.procedure.AsyncProcedure;\r
10 import org.simantics.diagram.adapter.SyncElementFactory;\r
11 import org.simantics.diagram.stubs.DiagramResource;\r
12 import org.simantics.diagram.ui.DiagramModelHints;\r
13 import org.simantics.district.network.ontology.DistrictNetworkResource;\r
14 import org.simantics.district.network.ui.DistrictNetworkEdge;\r
15 import org.simantics.g2d.canvas.ICanvasContext;\r
16 import org.simantics.g2d.diagram.IDiagram;\r
17 import org.simantics.g2d.element.ElementClass;\r
18 import org.simantics.g2d.element.IElement;\r
19 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
20 \r
21 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {\r
22 \r
23     public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;\r
24     \r
25     private DistrictNetworkResource DN;\r
26     private DiagramResource DIA;\r
27     \r
28     public DistrictNetworkEdgeElementFactory(ReadGraph graph) {\r
29         this.DN = DistrictNetworkResource.getInstance(graph);\r
30         this.DIA = DiagramResource.getInstance(graph);\r
31     }\r
32     \r
33     @Override\r
34     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {\r
35         procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
36     }\r
37     \r
38     @Override\r
39     protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
40         return DN.Edge;\r
41     }\r
42     \r
43     @Override\r
44     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {\r
45         \r
46         Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
47         \r
48         Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);\r
49         Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);\r
50         \r
51         double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);\r
52         double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);\r
53         \r
54         Path2D path = new Path2D.Double();\r
55         path.moveTo(startCoords[0], startCoords[1]);\r
56         path.lineTo(endCoords[0], endCoords[1]);\r
57         \r
58         DistrictNetworkEdge edge = new DistrictNetworkEdge(path);\r
59         \r
60         element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge);\r
61     }\r
62 }\r