]> gerrit.simantics Code Review - simantics/district.git/blob
fe3ed93517a56ae90cb86e37dd16b6a5d6d37285
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
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;
25
26 public class DistrictDiagramClassAdapter extends DiagramClassAdapter {
27
28     static final SubstituteElementClass DINSTANCE = new DistrictDiagramSubstituteElementClass();
29
30     @Override
31     public void adapt(AsyncReadGraph g, Resource source, Resource r, AsyncProcedure<DiagramClass> procedure) {
32         procedure.execute(g, INSTANCE.newClassWith(DINSTANCE));
33     }
34
35     static class DistrictDiagramElementLayerListenerImpl implements ElementLayerListener {
36
37         private static final long serialVersionUID = 7723435000661141433L;
38
39         @Override
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());
44             if (gl != null) {
45                 changeTag(e, gl.getVisible(), visible);
46             }
47         }
48
49         @Override
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());
54             if (gl != null) {
55                 changeTag(e, gl.getFocusable(), focusable);
56             }
57         }
58
59         void changeTag(IElement e, Resource tag, boolean set) {
60             Object object = e.getHint(ElementHints.KEY_OBJECT);
61             Resource tagged;
62             if (object instanceof Resource)
63                 tagged = (Resource) object;
64             else
65                 tagged = null;
66             if (tagged == null)
67                 return;
68             
69             Simantics.getSession().asyncRequest(new WriteRequest() {
70                 
71                 @Override
72                 public void perform(WriteGraph graph) throws DatabaseException {
73                     DiagramGraphUtil.tag(graph, tagged, tag, set);
74                 }
75             });
76         }
77     }
78
79     static class DistrictDiagramSubstituteElementClass implements SubstituteElementClass {
80
81         static final ElementLayerListener LAYER_LISTENER = new DistrictDiagramElementLayerListenerImpl();
82
83         @Override
84         public ElementClass substitute(IDiagram d, ElementClass ec) {
85             return ec.newClassWith(LAYER_LISTENER);
86         }
87
88     }
89 }