1 package org.simantics.district.network.ui.participants;
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
6 import java.util.concurrent.CompletableFuture;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.ScheduledFuture;
9 import java.util.concurrent.TimeUnit;
11 import org.simantics.Simantics;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.ReadRequest;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.district.network.ontology.DistrictNetworkResource;
17 import org.simantics.district.network.profile.RuntimeDynamicVisualisationsRequest;
18 import org.simantics.district.network.ui.DistrictDiagramViewer;
19 import org.simantics.district.network.ui.nodes.DistrictNetworkHoverInfoNode;
20 import org.simantics.district.network.ui.nodes.DynamicVisualisationContributionsNode;
21 import org.simantics.district.network.ui.styles.DistrictNetworkHoverInfoStyle;
22 import org.simantics.district.network.ui.styles.DistrictNetworkHoverInfoStyle.StyleResult;
23 import org.simantics.district.network.visualisations.model.ColorBarOptions;
24 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
25 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
26 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
27 import org.simantics.district.network.visualisations.model.SizeBarOptions;
28 import org.simantics.g2d.canvas.ICanvasContext;
29 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
30 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.scenegraph.g2d.G2DParentNode;
33 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
34 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
35 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
36 import org.simantics.utils.datastructures.hints.IHintContext.Key;
37 import org.simantics.utils.datastructures.hints.IHintListener;
38 import org.simantics.utils.datastructures.hints.IHintObservable;
39 import org.simantics.utils.threads.IThreadWorkQueue;
40 import org.simantics.utils.threads.ThreadUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class DynamicVisualisationContributionsParticipant extends AbstractCanvasParticipant {
46 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationContributionsParticipant.class);
48 IHintListener hintListener = new HintListenerAdapter() {
49 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
50 ICanvasContext cc = getContext();
53 cc.getContentContext().setDirty();
58 private DynamicVisualisationContributionsNode node;
59 private AffineTransform transform;
61 private DistrictNetworkHoverInfoNode hoverInfoNode;
63 public DynamicVisualisationContributionsParticipant(AffineTransform tr) {
68 public void addedToContext(ICanvasContext ctx) {
69 super.addedToContext(ctx);
70 getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS, hintListener);
71 getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS, hintListener);
72 getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS, hintListener);
73 getHintStack().addKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS, hintListener);
77 public void removedFromContext(ICanvasContext ctx) {
78 getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS, hintListener);
79 getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS, hintListener);
80 getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS, hintListener);
81 getHintStack().removeKeyHintListener(getThread(), DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS, hintListener);
82 super.removedFromContext(ctx);
86 public void initSG(G2DParentNode parent) {
87 node = parent.addNode(DynamicVisualisationContributionsNode.ID, DynamicVisualisationContributionsNode.class);
88 node.setTransform(transform);
89 node.setEnabled(true);
92 hoverInfoNode = parent.addNode("districtNetworkHoverInfoNode", DistrictNetworkHoverInfoNode.class);
93 hoverInfoNode.setLookupId("districtNetworkHoverInfoNode");
94 hoverInfoNode.setTransform(transform);
95 hoverInfoNode.setZIndex(Integer.MAX_VALUE - 500);
98 @EventHandler(priority = 0)
99 protected boolean handleKeyEvent(CommandEvent e) {
100 if (e.command.equals(DistrictDiagramViewer.MAP_COLOR_BAR_OPTIONS_CHANGE)) {
101 System.out.println(e);
108 // protected boolean handleCommand(CommandEvent e) {
109 // if (e.command.equals(DistrictDiagramViewer.MAP_COLOR_BAR_OPTIONS_CHANGE)) {
110 // ICanvasContext context = (ICanvasContext) e.getContext();
111 // ColorBarOptions options = context.getHintStack().getHint(DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS);
112 // this.colorBarsOptions = options;
116 // return super.handleCommand(e);
120 protected void updateNode() {
121 node.setDynamicColoringObjects(getDynamicColoringObjects());
122 node.setColorBarOptions(getColorBarOptions());
123 node.setDynamicSizingObjects(getDynamicSizingObjects());
124 node.setSizeBarOptions(getSizeBarOptions());
127 private Map<String,DynamicColorContribution> getDynamicColoringObjects() {
128 Map<String,DynamicColorContribution> objects = getHint(DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS);
132 private ColorBarOptions getColorBarOptions() {
133 ColorBarOptions options = getHint(DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS);
137 private Map<String, DynamicSizeContribution> getDynamicSizingObjects() {
138 Map<String, DynamicSizeContribution> objects = getHint(DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS);
142 private SizeBarOptions getSizeBarOptions() {
143 SizeBarOptions options = getHint(DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS);
147 private ScheduledFuture<?> hoverUpdateSchedule;
149 public void hoverNode(Resource runtimeDiagram, Resource mapElement, G2DParentNode hoveredNode) {
150 IThreadWorkQueue thread = getThread();
151 Simantics.getSession().asyncRequest(new ReadRequest() {
154 public void run(ReadGraph graph) throws DatabaseException {
155 DynamicVisualisation visualisation = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram));
156 if (visualisation == null)
158 if (hoverUpdateSchedule != null && !hoverUpdateSchedule.isDone()) {
159 hoverUpdateSchedule.cancel(false);
161 hoverUpdateSchedule = ThreadUtils.getNonBlockingWorkExecutor().scheduleWithFixedDelay(() -> {
163 CompletableFuture<Object> future = new CompletableFuture<>();
165 Simantics.getSession().syncRequest(new ReadRequest() {
168 public void run(ReadGraph graph) throws DatabaseException {
169 boolean keyVariablesVertexHover = visualisation.isKeyVariablesVertexHover();
170 boolean keyVariablesEdgesHover = visualisation.isKeyVariablesEdgesHover();
172 Resource mapElementInstanceOf = graph.getSingleObject(mapElement, Layer0.getInstance(graph).InstanceOf);
173 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
175 boolean doHover = true;
176 if (mapElementInstanceOf.equals(DN.Vertex) && !keyVariablesVertexHover) {
178 } else if (mapElementInstanceOf.equals(DN.Edge) && !keyVariablesEdgesHover) {
181 final boolean finalDoHover = doHover;
183 StyleResult results = DistrictNetworkHoverInfoStyle.doCalculateStyleResult(graph, runtimeDiagram, mapElement);
184 if (results != null) {
185 Point2D location = DistrictNetworkHoverInfoStyle.calculatePoint(graph, mapElement);
186 thread.asyncExec(() -> {
190 hoverInfoNode.setLabels(results.getLabels());
191 hoverInfoNode.setOrigin(results.getOrigin());
193 hoverInfoNode.setMousePosition(location);
194 hoverInfoNode.hover2(hoveredNode);
196 hoverInfoNode.hover2(null);
198 future.complete(new Object());
201 future.complete(new Object());
205 } catch (DatabaseException e) {
206 future.completeExceptionally(e);
208 // this waits until everything is done
211 } catch (InterruptedException | ExecutionException e) {
212 LOGGER.debug("Interrupted hovering", e);
214 }, 0, visualisation.getInterval(), TimeUnit.MILLISECONDS);
219 public boolean doHover(boolean hover, boolean isConnectionTool) {
220 return hoverInfoNode.hover(hover, isConnectionTool);