]> gerrit.simantics Code Review - simantics/district.git/blob
976abd48596ccf177065addfa8ffbc1914e1904e
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.geom.AffineTransform;
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.ontology.DistrictNetworkResource;
17 import org.simantics.g2d.canvas.ICanvasContext;
18 import org.simantics.g2d.diagram.DiagramHints;
19 import org.simantics.g2d.diagram.IDiagram;
20 import org.simantics.g2d.element.ElementClass;
21 import org.simantics.g2d.element.ElementUtils;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
24 import org.simantics.g2d.layers.ILayersEditor;
25
26 public class DistrictNetworkVertexElementFactory extends SyncElementFactory {
27
28     public static final ElementClass CLASS = DistrictNetworkVertexElement.CLASS;
29     private DistrictNetworkResource DN;
30     private DiagramResource DIA;
31
32     public DistrictNetworkVertexElementFactory(ReadGraph graph) {
33         this.DN = DistrictNetworkResource.getInstance(graph);
34         this.DIA = DiagramResource.getInstance(graph);
35     }
36
37     @Override
38     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
39         procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
40     }
41
42     @Override
43     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
44         return DN.Vertex;
45     }
46
47     @Override
48     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource vertexResource, IElement element) throws DatabaseException {
49         
50         double[] coords = graph.getRelatedValue(vertexResource, DIA.HasLocation);
51         DistrictNetworkVertex vertex = new DistrictNetworkVertex(coords);
52         element.setHint(DistrictNetworkVertexElement.KEY_DN_VERTEX, vertex);
53         
54         // set element transform based on layer SRS and coords
55         AffineTransform at = new AffineTransform(1, 0, 0, 1, coords[0], coords[1]);
56         ElementUtils.setTransform(element, at);
57         
58         IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
59         GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
60         
61         ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
62         layerManager.loadLayersForElement(graph, layersEditor, element, vertexResource);
63     }
64 }