1 package org.simantics.district.network.ui.adapters;
3 import java.awt.geom.AffineTransform;
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.g2d.canvas.ICanvasContext;
19 import org.simantics.g2d.diagram.DiagramHints;
20 import org.simantics.g2d.diagram.IDiagram;
21 import org.simantics.g2d.element.ElementClass;
22 import org.simantics.g2d.element.ElementUtils;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
25 import org.simantics.g2d.layers.ILayersEditor;
27 public class DistrictNetworkVertexElementFactory extends SyncElementFactory {
29 public static final ElementClass CLASS = DistrictNetworkVertexElement.CLASS;
30 private DistrictNetworkResource DN;
31 private DiagramResource DIA;
33 public DistrictNetworkVertexElementFactory(ReadGraph graph) {
34 this.DN = DistrictNetworkResource.getInstance(graph);
35 this.DIA = DiagramResource.getInstance(graph);
39 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
40 procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
44 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
49 public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource vertexResource, IElement element) throws DatabaseException {
51 double[] coords = graph.getRelatedValue(vertexResource, DIA.HasLocation);
52 DistrictNetworkVertex vertex = new DistrictNetworkVertex(coords);
54 Resource mapping = graph.getSingleObject(vertexResource, DistrictNetworkResource.getInstance(graph).HasMapping);
55 element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);
56 element.setHint(DistrictNetworkVertexElement.KEY_DN_VERTEX, vertex);
58 // TODO: set element transform based on layer SRS and coords
60 double x = ModelledCRS.longitudeToX(coords[0]);
61 double y = ModelledCRS.latitudeToY(coords[1]);
63 double scale = 100000;
65 AffineTransform at = new AffineTransform(1, 0, 0, 1, x * scale, y * scale);
66 ElementUtils.setTransform(element, at);
68 IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
69 GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
71 ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
72 layerManager.loadLayersForElement(graph, layersEditor, element, vertexResource);