1 package org.simantics.district.network.ui.participants;
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;
10 import java.util.concurrent.CompletableFuture;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ScheduledFuture;
13 import java.util.concurrent.TimeUnit;
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;
53 public class DynamicVisualisationContributionsParticipant extends AbstractCanvasParticipant {
55 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationContributionsParticipant.class);
57 IHintListener hintListener = new HintListenerAdapter() {
58 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
59 ICanvasContext cc = getContext();
62 cc.getContentContext().setDirty();
67 private DynamicVisualisationContributionsNode node;
68 private AffineTransform transform;
69 private DistrictNetworkHoverInfoNode hoverInfoNode;
70 private DeferredRenderingNode deferredEdgeArrowRendererNode;
72 private ElevationServerNode showElevationServerBoundingBoxNode;
74 public DynamicVisualisationContributionsParticipant(AffineTransform tr) {
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);
89 public void removedFromContext(ICanvasContext ctx) {
90 // Ensure hover polling is stopped
91 if (hoverUpdateSchedule != null && !hoverUpdateSchedule.isDone()) {
92 hoverUpdateSchedule.cancel(false);
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);
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);
110 hoverInfoNode = parent.addNode("districtNetworkHoverInfoNode", DistrictNetworkHoverInfoNode.class);
111 hoverInfoNode.setLookupId("districtNetworkHoverInfoNode");
112 hoverInfoNode.setTransform(transform);
113 hoverInfoNode.setZIndex(Integer.MAX_VALUE - 500);
115 Pair<String, Class<DeferredRenderingNode>> dearn = DistrictNetworkEdgeArrayNode.renderer();
116 deferredEdgeArrowRendererNode = parent.addNode(dearn.first, dearn.second);
118 showElevationServerBoundingBoxNode = parent.addNode(ElevationServerNode.ID, ElevationServerNode.class);
119 showElevationServerBoundingBoxNode.setTransform(transform);
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);
131 protected void updateNode() {
132 node.setDynamicColoringObjects(getDynamicColoringObjects());
133 node.setColorBarOptions(getColorBarOptions());
134 node.setDynamicSizingObjects(getDynamicSizingObjects());
135 node.setSizeBarOptions(getSizeBarOptions());
137 showElevationServerBoundingBoxNode.setRectangles(getRectangles());
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();
147 private Map<String, DynamicColorContribution> getDynamicColoringObjects() {
148 return getHint(DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS);
151 private ColorBarOptions getColorBarOptions() {
152 return getHint(DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS);
155 private Map<String, DynamicSizeContribution> getDynamicSizingObjects() {
156 return getHint(DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS);
159 private SizeBarOptions getSizeBarOptions() {
160 return getHint(DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS);
163 private ScheduledFuture<?> hoverUpdateSchedule;
164 private static final Object COMPLETE = new Object();
166 public void hoverNode(Resource runtimeDiagram, Resource mapElement, INode hoveredNode, int zoomLevel) {
167 IThreadWorkQueue thread = getThread();
168 Simantics.getSession().asyncRequest(new ReadRequest() {
170 public void run(ReadGraph graph) throws DatabaseException {
171 DynamicVisualisation visualisation = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram));
172 if (visualisation == null)
174 cancelCurrentHoverUpdate();
175 hoverUpdateSchedule = ThreadUtils.getNonBlockingWorkExecutor().scheduleWithFixedDelay(
176 () -> updateHoverInfo(runtimeDiagram, mapElement, hoveredNode, zoomLevel, visualisation, thread),
178 visualisation.getInterval(),
179 TimeUnit.MILLISECONDS);
184 private void updateHoverInfo(Resource runtimeDiagram, Resource mapElement, INode hoveredNode, int zoomLevel, DynamicVisualisation visualisation, IThreadWorkQueue thread) {
185 CompletableFuture<Object> future = new CompletableFuture<>();
187 Simantics.getSession().syncRequest(new ReadRequest() {
189 public void run(ReadGraph graph) throws DatabaseException {
190 Set<Resource> mapElementTypes = graph.getTypes(mapElement);
191 if (mapElementTypes.isEmpty()) {
192 future.complete(COMPLETE);
196 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
199 (mapElementTypes.contains(DN.Vertex) && visualisation.isKeyVariablesVertexHover())
200 || (mapElementTypes.contains(DN.Edge) && visualisation.isKeyVariablesEdgesHover());
202 StyleResult results = DistrictNetworkHoverInfoStyle.doCalculateStyleResult(graph, runtimeDiagram, mapElement);
203 if (results != null) {
204 Point2D location = DistrictNetworkHoverInfoStyle.calculatePoint(hoveredNode, zoomLevel, null);
205 thread.asyncExec(() -> {
209 hoverInfoNode.setLabels(results.getLabels());
210 hoverInfoNode.setOrigin(results.getOrigin());
211 hoverInfoNode.setMousePosition(location);
212 hoverInfoNode.setHoveredNode(hoveredNode);
214 hoverInfoNode.setHoveredNode(null);
216 future.complete(COMPLETE);
219 future.complete(COMPLETE);
223 } catch (DatabaseException e) {
224 future.completeExceptionally(e);
226 // this waits until everything is done
229 } catch (InterruptedException | ExecutionException e) {
230 LOGGER.debug("Interrupted hovering", e);
234 public boolean doHover(boolean hover, boolean isConnectionTool) {
236 cancelCurrentHoverUpdate();
237 return hoverInfoNode.hover(hover, isConnectionTool);
240 private void cancelCurrentHoverUpdate() {
241 if (hoverUpdateSchedule != null && !hoverUpdateSchedule.isDone())
242 hoverUpdateSchedule.cancel(false);