1 package org.simantics.district.network.ui.adapters;
\r
3 import java.awt.geom.Path2D;
\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
21 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
\r
23 public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;
\r
25 private DistrictNetworkResource DN;
\r
26 private DiagramResource DIA;
\r
28 public DistrictNetworkEdgeElementFactory(ReadGraph graph) {
\r
29 this.DN = DistrictNetworkResource.getInstance(graph);
\r
30 this.DIA = DiagramResource.getInstance(graph);
\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
39 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
\r
44 public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {
\r
46 Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
\r
48 Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);
\r
49 Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);
\r
51 double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);
\r
52 double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);
\r
54 Path2D path = new Path2D.Double();
\r
55 path.moveTo(startCoords[0], startCoords[1]);
\r
56 path.lineTo(endCoords[0], endCoords[1]);
\r
58 DistrictNetworkEdge edge = new DistrictNetworkEdge(path);
\r
60 element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge);
\r