]> gerrit.simantics Code Review - simantics/district.git/blob
431beb52d60280de30d44d5bf31c11c5a129a18e
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import org.simantics.db.AsyncReadGraph;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.procedure.AsyncProcedure;
8 import org.simantics.diagram.adapter.SyncElementFactory;
9 import org.simantics.diagram.stubs.DiagramResource;
10 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
11 import org.simantics.diagram.synchronization.SynchronizationHints;
12 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
13 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
14 import org.simantics.district.network.ontology.DistrictNetworkResource;
15 import org.simantics.g2d.canvas.ICanvasContext;
16 import org.simantics.g2d.diagram.DiagramHints;
17 import org.simantics.g2d.diagram.IDiagram;
18 import org.simantics.g2d.element.ElementClass;
19 import org.simantics.g2d.element.ElementUtils;
20 import org.simantics.g2d.element.IElement;
21 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
22 import org.simantics.g2d.layers.ILayersEditor;
23 import org.simantics.maps.MapScalingTransform;
24
25 public class DistrictNetworkVertexElementFactory extends SyncElementFactory {
26
27     public static final ElementClass CLASS = DistrictNetworkVertexElement.CLASS;
28     private DistrictNetworkResource DN;
29     private DiagramResource DIA;
30     
31     public DistrictNetworkVertexElementFactory(ReadGraph graph) {
32         this.DN = DistrictNetworkResource.getInstance(graph);
33         this.DIA = DiagramResource.getInstance(graph);
34     }
35
36     @Override
37     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
38         procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
39     }
40
41     @Override
42     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
43         return DN.Vertex;
44     }
45
46     @Override
47     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource vertexResource, IElement element) throws DatabaseException {
48         
49         double[] coords = graph.getRelatedValue(vertexResource, DIA.HasLocation);
50         DistrictNetworkVertex vertex = new DistrictNetworkVertex(coords);
51         
52         Resource mapping = graph.getSingleObject(vertexResource, DistrictNetworkResource.getInstance(graph).HasMapping);
53         element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);
54         element.setHint(DistrictNetworkVertexElement.KEY_DN_VERTEX, vertex);
55         
56         // set scaling transform for vertices
57         ElementUtils.setTransform(element, MapScalingTransform.INSTANCE);
58         
59         IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
60         GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
61         
62         ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
63         layerManager.loadLayersForElement(graph, layersEditor, element, vertexResource);
64     }
65 }