package org.simantics.district.network.ui.adapters; import org.simantics.Simantics; import org.simantics.db.AsyncReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.diagram.adapter.DiagramClassAdapter; import org.simantics.diagram.synchronization.IModifiableSynchronizationContext; import org.simantics.diagram.synchronization.SynchronizationHints; import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; import org.simantics.diagram.synchronization.graph.layer.GraphLayer; import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; import org.simantics.g2d.diagram.DiagramClass; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.diagram.handler.SubstituteElementClass; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.handler.ElementLayerListener; import org.simantics.g2d.layers.ILayer; public class DistrictDiagramClassAdapter extends DiagramClassAdapter { static final SubstituteElementClass DINSTANCE = new DistrictDiagramSubstituteElementClass(); @Override public void adapt(AsyncReadGraph g, Resource source, Resource r, AsyncProcedure procedure) { procedure.execute(g, INSTANCE.newClassWith(DINSTANCE)); } static class DistrictDiagramElementLayerListenerImpl implements ElementLayerListener { private static final long serialVersionUID = 7723435000661141433L; @Override public void visibilityChanged(IElement e, ILayer layer, boolean visible) { IModifiableSynchronizationContext context = e.getDiagram().getHint(SynchronizationHints.CONTEXT); GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); GraphLayer gl = layerManager.getGraphLayer(layer.getName()); if (gl != null) { changeTag(e, gl.getVisible(), visible); } } @Override public void focusabilityChanged(IElement e, ILayer layer, boolean focusable) { IModifiableSynchronizationContext context = e.getDiagram().getHint(SynchronizationHints.CONTEXT); GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); GraphLayer gl = layerManager.getGraphLayer(layer.getName()); if (gl != null) { changeTag(e, gl.getFocusable(), focusable); } } void changeTag(IElement e, Resource tag, boolean set) { Object object = e.getHint(ElementHints.KEY_OBJECT); Resource tagged; if (object instanceof Resource) tagged = (Resource) object; else tagged = null; if (tagged == null) return; Simantics.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { DiagramGraphUtil.tag(graph, tagged, tag, set); } }); } } static class DistrictDiagramSubstituteElementClass implements SubstituteElementClass { static final ElementLayerListener LAYER_LISTENER = new DistrictDiagramElementLayerListenerImpl(); @Override public ElementClass substitute(IDiagram d, ElementClass ec) { return ec.newClassWith(false, LAYER_LISTENER); } } }