package org.simantics.district.network.ui; import java.util.HashMap; import java.util.Map; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.diagram.synchronization.graph.layer.GraphLayer; import org.simantics.diagram.synchronization.graph.layer.GraphLayerUtil; import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; public class DNGraphLayerUtil implements IGraphLayerUtil { public DNGraphLayerUtil(Resource layer) { } @Override public GraphLayer loadLayer(ReadGraph graph, Resource layer) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); DiagramResource DIA = DiagramResource.getInstance(graph); String name = graph.getRelatedValue(layer, L0.HasName); Resource inLayer = graph.getSingleObject(layer, DN.HasInLayerTag); Resource visible = graph.getSingleObject(layer, DIA.HasVisibleTag); Resource focusable = graph.getSingleObject(layer, DIA.HasFocusableTag); Map properties = new HashMap<>(); properties.put(GraphLayer.PROP_FOCUSABLE, focusable); properties.put(GraphLayer.PROP_VISIBLE, visible); properties.put("IN_LAYER", inLayer); return new GraphLayer(name, layer, properties); } @Override public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); DiagramResource DIA = DiagramResource.getInstance(graph); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); Resource layer = graph.newResource(); graph.claim(layer, L0.InstanceOf, null, DN.Layer); // Assign tagging relations Resource inLayerTag = GraphLayerUtil.newTag(graph, L0, DN.InLayer); // for backwards compatibility Resource visibleTag = GraphLayerUtil.newTag(graph, L0, DIA.IsVisible); Resource focusableTag = GraphLayerUtil.newTag(graph, L0, DIA.IsFocusable); graph.claim(layer, DN.HasInLayerTag, inLayerTag); // for backwards compatibility graph.claim(layer, DIA.HasVisibleTag, visibleTag); graph.claim(layer, DIA.HasFocusableTag, focusableTag); // Assign shared name property for all, the layer and the tags Resource name = graph.newResource(); graph.claim(name, L0.InstanceOf, null, L0.String); graph.claimValue(name, layerName); graph.claim(layer, L0.HasName, name); graph.claim(inLayerTag, L0.HasName, name); graph.claim(visibleTag, L0.HasName, name); graph.claim(focusableTag, L0.HasName, name); graph.claim(layer, DN.HasSpatialRefSystem, DN.EPSG_4326); GraphLayerUtil.setLayerActive(graph, DIA, layer, active); Map properties = new HashMap<>(); properties.put("IN_LAYER", inLayerTag); properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag); properties.put(GraphLayer.PROP_VISIBLE, visibleTag); return new GraphLayer(layerName, layer, properties); } }