1 package org.simantics.district.network.ui.adapters;
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;
25 public class DistrictNetworkVertexElementFactory extends SyncElementFactory {
27 public static final ElementClass CLASS = DistrictNetworkVertexElement.CLASS;
28 private DistrictNetworkResource DN;
29 private DiagramResource DIA;
31 public DistrictNetworkVertexElementFactory(ReadGraph graph) {
32 this.DN = DistrictNetworkResource.getInstance(graph);
33 this.DIA = DiagramResource.getInstance(graph);
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)));
42 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
47 public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource vertexResource, IElement element) throws DatabaseException {
48 if (!graph.hasStatement(vertexResource))
49 return; // already deleted
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 // set scaling transform for vertices
59 ElementUtils.setTransform(element, MapScalingTransform.INSTANCE);
61 IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
62 GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
64 ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
65 layerManager.loadLayersForElement(graph, layersEditor, element, vertexResource);