]> gerrit.simantics Code Review - simantics/district.git/blob
c3058a0edf5518341c6389ca61ad188019a0b85c
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.geom.Path2D;
4 import java.awt.geom.Point2D;
5
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;
29
30 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
31
32     public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;
33     
34     private DistrictNetworkResource DN;
35     private DiagramResource DIA;
36     
37     public DistrictNetworkEdgeElementFactory(ReadGraph graph) {
38         this.DN = DistrictNetworkResource.getInstance(graph);
39         this.DIA = DiagramResource.getInstance(graph);
40     }
41     
42     @Override
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)));
45     }
46     
47     @Override
48     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
49         return DN.Edge;
50     }
51     
52     @Override
53     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {
54         
55         Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);
56         Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);
57
58         // TODO: fix scale..
59         double scale = 100000;
60         
61         double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);
62         
63         double startLon = ModelledCRS.longitudeToX(startCoords[0]) * scale;
64         double startLat = ModelledCRS.latitudeToY(startCoords[1]) * scale;
65         
66         double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);
67
68         double endLon = ModelledCRS.longitudeToX(endCoords[0]) * scale;
69         double endLat = ModelledCRS.latitudeToY(endCoords[1]) * scale;
70         
71         Path2D path = new Path2D.Double();
72         path.moveTo(startLon, startLat);
73         path.lineTo(endLon, endLat);
74         
75         DistrictNetworkEdge edge = new DistrictNetworkEdge(path);
76
77         Resource mapping = graph.getSingleObject(edgeResource, DistrictNetworkResource.getInstance(graph).HasMapping);
78         element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);
79         
80         element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge);
81         
82         IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
83         GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
84         
85         ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
86         layerManager.loadLayersForElement(graph, layersEditor, element, edgeResource);
87         
88     }
89 }