]> gerrit.simantics Code Review - simantics/district.git/blob
2899a0e7cf8db5911e54beb3ad1b9d39db55c2d3
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.geom.Path2D;
4
5 import org.simantics.db.AsyncReadGraph;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.procedure.AsyncProcedure;
10 import org.simantics.diagram.adapter.SyncElementFactory;
11 import org.simantics.diagram.stubs.DiagramResource;
12 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
13 import org.simantics.diagram.synchronization.SynchronizationHints;
14 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
15 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
16 import org.simantics.district.network.ModelledCRS;
17 import org.simantics.district.network.ontology.DistrictNetworkResource;
18 import org.simantics.district.network.ui.DistrictNetworkEdge;
19 import org.simantics.g2d.canvas.ICanvasContext;
20 import org.simantics.g2d.diagram.DiagramHints;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.element.ElementClass;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
25 import org.simantics.g2d.layers.ILayersEditor;
26
27 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
28
29     public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;
30     
31     private DistrictNetworkResource DN;
32     private DiagramResource DIA;
33     
34     public DistrictNetworkEdgeElementFactory(ReadGraph graph) {
35         this.DN = DistrictNetworkResource.getInstance(graph);
36         this.DIA = DiagramResource.getInstance(graph);
37     }
38     
39     @Override
40     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
41         procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
42     }
43     
44     @Override
45     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
46         return DN.Edge;
47     }
48     
49     @Override
50     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {
51         
52         Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);
53         Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);
54         
55         // TODO: fix scale..
56         double scale = 100000;
57         
58         double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);
59         
60         double startLon = ModelledCRS.longitudeToX(startCoords[0]) * scale;
61         double startLat = ModelledCRS.latitudeToY(startCoords[1]) * scale;
62         
63         double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);
64         
65         double endLon = ModelledCRS.longitudeToX(endCoords[0]) * scale;
66         double endLat = ModelledCRS.latitudeToY(endCoords[1]) * scale;
67         
68         Path2D path = new Path2D.Double();
69         path.moveTo(startLon, startLat);
70         path.lineTo(endLon, endLat);
71         
72         DistrictNetworkEdge edge = new DistrictNetworkEdge(path);
73
74         Resource mapping = graph.getSingleObject(edgeResource, DistrictNetworkResource.getInstance(graph).HasMapping);
75         element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);
76         
77         element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge);
78         
79         IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
80         GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
81         
82         ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
83         layerManager.loadLayersForElement(graph, layersEditor, element, edgeResource);
84         
85     }
86 }