package org.simantics.district.network.ui.adapters; import java.awt.geom.Point2D; import org.simantics.databoard.Bindings; import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.diagram.adapter.SyncElementFactory; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.diagram.synchronization.IModifiableSynchronizationContext; import org.simantics.diagram.synchronization.SynchronizationHints; import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.ui.DistrictNetworkEdge; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; import org.simantics.g2d.layers.ILayersEditor; import org.simantics.maps.MapScalingTransform; public class DistrictNetworkEdgeElementFactory extends SyncElementFactory { public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS; public static final double[] EMPTY = new double[0]; private DistrictNetworkResource DN; private DiagramResource DIA; public DistrictNetworkEdgeElementFactory(ReadGraph graph) { this.DN = DistrictNetworkResource.getInstance(graph); this.DIA = DiagramResource.getInstance(graph); } @Override public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure procedure) { procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType))); } @Override protected Resource getElementClassBaseType(AsyncReadGraph graph) { return DN.Edge; } @Override public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException { if (!graph.hasStatement(edgeResource)) return; // already deleted if (!graph.hasStatement(edgeResource, DN.HasStartVertex)) return; // already deleted Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex); if (!graph.hasStatement(edgeResource, DN.HasEndVertex)) return; // already deleted Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex); double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY); double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY); double[] geometry = EMPTY; try { geometry = graph.getPossibleRelatedValue2(edgeResource, DN.Edge_HasGeometry, Bindings.DOUBLE_ARRAY); } catch (Exception e) { // most likely no geometry available } DistrictNetworkEdge edge = new DistrictNetworkEdge(new Point2D.Double(startCoords[0], startCoords[1]), new Point2D.Double(endCoords[0], endCoords[1]), geometry); Resource mapping = graph.getSingleObject(edgeResource, DistrictNetworkResource.getInstance(graph).HasMapping); element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping); element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge); // set scaling transform ElementUtils.setTransform(element, MapScalingTransform.INSTANCE); IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT); GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR); layerManager.loadLayersForElement(graph, layersEditor, element, edgeResource); } }