1 package org.simantics.district.network.ui.adapters;
3 import org.simantics.Simantics;
4 import org.simantics.db.AsyncReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.common.request.WriteRequest;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.procedure.AsyncProcedure;
10 import org.simantics.diagram.adapter.DiagramClassAdapter;
11 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
12 import org.simantics.diagram.synchronization.SynchronizationHints;
13 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
14 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
15 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
16 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
17 import org.simantics.g2d.diagram.DiagramClass;
18 import org.simantics.g2d.diagram.IDiagram;
19 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
20 import org.simantics.g2d.element.ElementClass;
21 import org.simantics.g2d.element.ElementHints;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.g2d.element.handler.ElementLayerListener;
24 import org.simantics.g2d.layers.ILayer;
26 public class DistrictDiagramClassAdapter extends DiagramClassAdapter {
28 static final SubstituteElementClass DINSTANCE = new DistrictDiagramSubstituteElementClass();
31 public void adapt(AsyncReadGraph g, Resource source, Resource r, AsyncProcedure<DiagramClass> procedure) {
32 procedure.execute(g, INSTANCE.newClassWith(DINSTANCE));
35 static class DistrictDiagramElementLayerListenerImpl implements ElementLayerListener {
37 private static final long serialVersionUID = 7723435000661141433L;
40 public void visibilityChanged(IElement e, ILayer layer, boolean visible) {
41 IModifiableSynchronizationContext context = e.getDiagram().getHint(SynchronizationHints.CONTEXT);
42 GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
43 GraphLayer gl = layerManager.getGraphLayer(layer.getName());
45 changeTag(e, gl.getVisible(), visible);
50 public void focusabilityChanged(IElement e, ILayer layer, boolean focusable) {
51 IModifiableSynchronizationContext context = e.getDiagram().getHint(SynchronizationHints.CONTEXT);
52 GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
53 GraphLayer gl = layerManager.getGraphLayer(layer.getName());
55 changeTag(e, gl.getFocusable(), focusable);
59 void changeTag(IElement e, Resource tag, boolean set) {
60 Object object = e.getHint(ElementHints.KEY_OBJECT);
62 if (object instanceof Resource)
63 tagged = (Resource) object;
69 Simantics.getSession().asyncRequest(new WriteRequest() {
72 public void perform(WriteGraph graph) throws DatabaseException {
73 DiagramGraphUtil.tag(graph, tagged, tag, set);
79 static class DistrictDiagramSubstituteElementClass implements SubstituteElementClass {
81 static final ElementLayerListener LAYER_LISTENER = new DistrictDiagramElementLayerListenerImpl();
84 public ElementClass substitute(IDiagram d, ElementClass ec) {
85 return ec.newClassWith(false, LAYER_LISTENER);