]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DynamicVisualisationContributionsParticipant.java
Fixed District diagram viewer to allow moving of vertices again.
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / participants / DynamicVisualisationContributionsParticipant.java
1 package org.simantics.district.network.ui.participants;
2
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.awt.geom.Rectangle2D;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.Map;
9 import java.util.Set;
10 import java.util.concurrent.CompletableFuture;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ScheduledFuture;
13 import java.util.concurrent.TimeUnit;
14
15 import org.simantics.Simantics;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.district.network.ontology.DistrictNetworkResource;
21 import org.simantics.district.network.profile.RuntimeDynamicVisualisationsRequest;
22 import org.simantics.district.network.ui.DistrictDiagramViewer;
23 import org.simantics.district.network.ui.nodes.DeferredRenderingNode;
24 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeArrayNode;
25 import org.simantics.district.network.ui.nodes.DistrictNetworkHoverInfoNode;
26 import org.simantics.district.network.ui.nodes.DynamicVisualisationContributionsNode;
27 import org.simantics.district.network.ui.nodes.ElevationServerNode;
28 import org.simantics.district.network.ui.styles.DistrictNetworkHoverInfoStyle;
29 import org.simantics.district.network.ui.styles.DistrictNetworkHoverInfoStyle.StyleResult;
30 import org.simantics.district.network.visualisations.model.ColorBarOptions;
31 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
32 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
33 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
34 import org.simantics.district.network.visualisations.model.SizeBarOptions;
35 import org.simantics.g2d.canvas.ICanvasContext;
36 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
37 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
38 import org.simantics.maps.elevation.server.SingletonTiffTileInterface;
39 import org.simantics.scenegraph.INode;
40 import org.simantics.scenegraph.g2d.G2DParentNode;
41 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
42 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
43 import org.simantics.utils.datastructures.Pair;
44 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
45 import org.simantics.utils.datastructures.hints.IHintContext.Key;
46 import org.simantics.utils.datastructures.hints.IHintListener;
47 import org.simantics.utils.datastructures.hints.IHintObservable;
48 import org.simantics.utils.threads.IThreadWorkQueue;
49 import org.simantics.utils.threads.ThreadUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class DynamicVisualisationContributionsParticipant extends AbstractCanvasParticipant {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationContributionsParticipant.class);
56
57     IHintListener hintListener = new HintListenerAdapter() {
58         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
59             ICanvasContext cc = getContext();
60             if (cc != null) {
61                 updateNode();
62                 cc.getContentContext().setDirty();
63             }
64         }
65     };
66
67     private DynamicVisualisationContributionsNode node;
68     private AffineTransform transform;
69     private DistrictNetworkHoverInfoNode hoverInfoNode;
70     private DeferredRenderingNode deferredEdgeArrowRendererNode;
71
72     private ElevationServerNode showElevationServerBoundingBoxNode;
73
74     public DynamicVisualisationContributionsParticipant(AffineTransform tr) {
75         this.transform = tr;
76     }
77
78     @Override
79     public void addedToContext(ICanvasContext ctx) {
80         super.addedToContext(ctx);
81         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS, hintListener);
82         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS, hintListener);
83         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS, hintListener);
84         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS, hintListener);
85         getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_SHOW_ELEVATION_SERVER, hintListener);
86     }
87
88     @Override
89     public void removedFromContext(ICanvasContext ctx) {
90         // Ensure hover polling is stopped
91         if (hoverUpdateSchedule != null && !hoverUpdateSchedule.isDone()) {
92             hoverUpdateSchedule.cancel(false);
93         }
94
95         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS, hintListener);
96         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS, hintListener);
97         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS, hintListener);
98         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS, hintListener);
99         getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_SHOW_ELEVATION_SERVER, hintListener);
100         super.removedFromContext(ctx);
101     }
102
103     @SGInit
104     public void initSG(G2DParentNode parent) {
105         node = parent.addNode(DynamicVisualisationContributionsNode.ID, DynamicVisualisationContributionsNode.class);
106         node.setTransform(transform);
107         node.setEnabled(true);
108         node.setZIndex(1000);
109
110         hoverInfoNode = parent.addNode("districtNetworkHoverInfoNode", DistrictNetworkHoverInfoNode.class);
111         hoverInfoNode.setLookupId("districtNetworkHoverInfoNode");
112         hoverInfoNode.setTransform(transform);
113         hoverInfoNode.setZIndex(Integer.MAX_VALUE - 500);
114         
115         Pair<String, Class<DeferredRenderingNode>> dearn = DistrictNetworkEdgeArrayNode.renderer();
116         deferredEdgeArrowRendererNode = parent.addNode(dearn.first, dearn.second);
117         
118         showElevationServerBoundingBoxNode = parent.addNode(ElevationServerNode.ID, ElevationServerNode.class);
119         showElevationServerBoundingBoxNode.setTransform(transform);
120     }
121
122     @EventHandler(priority = 0)
123     protected boolean handleKeyEvent(CommandEvent e) {
124         if (e.command.equals(DistrictDiagramViewer.MAP_COLOR_BAR_OPTIONS_CHANGE)) {
125             System.out.println(e);
126             return true;
127         }
128         return false;
129     }
130
131     protected void updateNode() {
132         node.setDynamicColoringObjects(getDynamicColoringObjects());
133         node.setColorBarOptions(getColorBarOptions());
134         node.setDynamicSizingObjects(getDynamicSizingObjects());
135         node.setSizeBarOptions(getSizeBarOptions());
136         
137         showElevationServerBoundingBoxNode.setRectangles(getRectangles());
138     }
139
140     private Collection<Rectangle2D> getRectangles() {
141         Boolean enabled = getHint(DistrictDiagramViewer.KEY_SHOW_ELEVATION_SERVER);
142         if (enabled != null && enabled)
143             return SingletonTiffTileInterface.getBoundingBoxes();
144         return Collections.emptyList();
145     }
146
147     private Map<String, DynamicColorContribution> getDynamicColoringObjects() {
148         return getHint(DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS);
149     }
150
151     private ColorBarOptions getColorBarOptions() {
152         return getHint(DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS);
153     }
154
155     private Map<String, DynamicSizeContribution> getDynamicSizingObjects() {
156         return getHint(DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS);
157     }
158
159     private SizeBarOptions getSizeBarOptions() {
160         return getHint(DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS);
161     }
162
163     private ScheduledFuture<?> hoverUpdateSchedule;
164     private static final Object COMPLETE = new Object();
165
166     public void hoverNode(Resource runtimeDiagram, Resource mapElement, INode hoveredNode, int zoomLevel) {
167         IThreadWorkQueue thread = getThread();
168         Simantics.getSession().asyncRequest(new ReadRequest() {
169             @Override
170             public void run(ReadGraph graph) throws DatabaseException {
171                 DynamicVisualisation visualisation = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram));
172                 if (visualisation == null)
173                     return;
174                 cancelCurrentHoverUpdate();
175                 hoverUpdateSchedule = ThreadUtils.getNonBlockingWorkExecutor().scheduleWithFixedDelay(
176                         () -> updateHoverInfo(runtimeDiagram, mapElement, hoveredNode, zoomLevel, visualisation, thread),
177                         0,
178                         visualisation.getInterval(),
179                         TimeUnit.MILLISECONDS);
180             }
181         });
182     }
183
184     private void updateHoverInfo(Resource runtimeDiagram, Resource mapElement, INode hoveredNode, int zoomLevel, DynamicVisualisation visualisation, IThreadWorkQueue thread) {
185         CompletableFuture<Object> future = new CompletableFuture<>();
186         try {
187             Simantics.getSession().syncRequest(new ReadRequest() {
188                 @Override
189                 public void run(ReadGraph graph) throws DatabaseException {
190                     Set<Resource> mapElementTypes = graph.getTypes(mapElement);
191                     if (mapElementTypes.isEmpty()) {
192                         future.complete(COMPLETE);
193                         return;
194                     }
195
196                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
197
198                     boolean doHover =
199                             (mapElementTypes.contains(DN.Vertex) && visualisation.isKeyVariablesVertexHover())
200                             || (mapElementTypes.contains(DN.Edge) && visualisation.isKeyVariablesEdgesHover());
201
202                     StyleResult results = DistrictNetworkHoverInfoStyle.doCalculateStyleResult(graph, runtimeDiagram, mapElement);
203                     if (results != null) {
204                         Point2D location = DistrictNetworkHoverInfoStyle.calculatePoint(hoveredNode, zoomLevel, null);
205                         thread.asyncExec(() -> {
206                             if (isRemoved())
207                                 return;
208                             if (doHover) {
209                                 hoverInfoNode.setLabels(results.getLabels());
210                                 hoverInfoNode.setOrigin(results.getOrigin());
211                                 hoverInfoNode.setMousePosition(location);
212                                 hoverInfoNode.setHoveredNode(hoveredNode);
213                             } else {
214                                 hoverInfoNode.setHoveredNode(null);
215                             }
216                             future.complete(COMPLETE);
217                         });
218                     } else {
219                         future.complete(COMPLETE);
220                     }
221                 }
222             });
223         } catch (DatabaseException e) {
224             future.completeExceptionally(e);
225         }
226         // this waits until everything is done
227         try {
228             future.get();
229         } catch (InterruptedException | ExecutionException e) {
230             LOGGER.debug("Interrupted hovering", e);
231         }
232     }
233
234     public boolean doHover(boolean hover, boolean isConnectionTool) {
235         if (!hover)
236             cancelCurrentHoverUpdate();
237         return hoverInfoNode.hover(hover, isConnectionTool);
238     }
239
240     private void cancelCurrentHoverUpdate() {
241         if (hoverUpdateSchedule != null && !hoverUpdateSchedule.isDone())
242             hoverUpdateSchedule.cancel(false);
243     }
244
245 }