1 package org.simantics.district.network.ui.adapters;
3 import java.awt.geom.Path2D;
4 import java.awt.geom.Point2D;
6 import org.simantics.db.AsyncReadGraph;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.procedure.AsyncProcedure;
11 import org.simantics.diagram.adapter.SyncElementFactory;
12 import org.simantics.diagram.stubs.DiagramResource;
13 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
14 import org.simantics.diagram.synchronization.SynchronizationHints;
15 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
16 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
17 import org.simantics.district.network.ModelledCRS;
18 import org.simantics.district.network.ontology.DistrictNetworkResource;
19 import org.simantics.district.network.ui.DistrictNetworkEdge;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.DiagramHints;
22 import org.simantics.g2d.diagram.IDiagram;
23 import org.simantics.g2d.element.ElementClass;
24 import org.simantics.g2d.element.ElementUtils;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
27 import org.simantics.g2d.layers.ILayersEditor;
28 import org.simantics.maps.MapScalingTransform;
30 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
32 public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;
34 private DistrictNetworkResource DN;
35 private DiagramResource DIA;
37 public DistrictNetworkEdgeElementFactory(ReadGraph graph) {
38 this.DN = DistrictNetworkResource.getInstance(graph);
39 this.DIA = DiagramResource.getInstance(graph);
43 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
44 procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
48 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
53 public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {
55 Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);
56 Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);
59 double scale = 100000;
61 double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);
63 double startLon = ModelledCRS.longitudeToX(startCoords[0]) * scale;
64 double startLat = ModelledCRS.latitudeToY(startCoords[1]) * scale;
66 double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);
68 double endLon = ModelledCRS.longitudeToX(endCoords[0]) * scale;
69 double endLat = ModelledCRS.latitudeToY(endCoords[1]) * scale;
71 Path2D path = new Path2D.Double();
72 path.moveTo(startLon, startLat);
73 path.lineTo(endLon, endLat);
75 DistrictNetworkEdge edge = new DistrictNetworkEdge(path);
77 Resource mapping = graph.getSingleObject(edgeResource, DistrictNetworkResource.getInstance(graph).HasMapping);
78 element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);
80 element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge);
82 IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
83 GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
85 ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
86 layerManager.loadLayersForElement(graph, layersEditor, element, edgeResource);