]> gerrit.simantics Code Review - simantics/district.git/blob
0e0bace92245003ed5b7fec4f22745a4c9a5ede5
[simantics/district.git] /
1 package org.simantics.district.network.ui.participants;
2
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.util.Map;
6 import java.util.concurrent.CompletableFuture;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.ScheduledFuture;
9 import java.util.concurrent.TimeUnit;
10
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;
43
44 public class DynamicVisualisationContributionsParticipant extends AbstractCanvasParticipant {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationContributionsParticipant.class);
47
48     IHintListener hintListener = new HintListenerAdapter() {
49         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
50             ICanvasContext cc = getContext();
51             if (cc != null) {
52                 updateNode();
53                 cc.getContentContext().setDirty();
54             }
55         }
56     };
57     
58     private DynamicVisualisationContributionsNode node;
59     private AffineTransform transform;
60
61     private DistrictNetworkHoverInfoNode hoverInfoNode;
62
63     public DynamicVisualisationContributionsParticipant(AffineTransform tr) {
64         this.transform = tr;
65     }
66
67     @Override
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);
74     }
75     
76     @Override
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);
83     }
84
85     @SGInit
86     public void initSG(G2DParentNode parent) {
87         node = parent.addNode(DynamicVisualisationContributionsNode.ID, DynamicVisualisationContributionsNode.class);
88         node.setTransform(transform);
89         node.setEnabled(true);
90         node.setZIndex(1000);
91         
92         hoverInfoNode = parent.addNode("districtNetworkHoverInfoNode", DistrictNetworkHoverInfoNode.class);
93         hoverInfoNode.setLookupId("districtNetworkHoverInfoNode");
94         hoverInfoNode.setTransform(transform);
95         hoverInfoNode.setZIndex(Integer.MAX_VALUE - 500);
96     }
97     
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);
102             return true;
103         }
104         return false;
105     }
106     
107 //    @Override
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;
113 //            repaint();
114 //            return true;
115 //        } else {
116 //            return super.handleCommand(e);
117 //        }
118 //    }
119     
120     protected void updateNode() {
121         node.setDynamicColoringObjects(getDynamicColoringObjects());
122         node.setColorBarOptions(getColorBarOptions());
123         node.setDynamicSizingObjects(getDynamicSizingObjects());
124         node.setSizeBarOptions(getSizeBarOptions());
125     }
126
127     private Map<String,DynamicColorContribution> getDynamicColoringObjects() {
128         Map<String,DynamicColorContribution> objects = getHint(DistrictDiagramViewer.KEY_MAP_COLORING_OBJECTS);
129         return objects;
130     }
131
132     private ColorBarOptions getColorBarOptions() {
133         ColorBarOptions options = getHint(DistrictDiagramViewer.KEY_MAP_COLOR_BAR_OPTIONS);
134         return options;
135     }
136
137     private Map<String, DynamicSizeContribution> getDynamicSizingObjects() {
138         Map<String, DynamicSizeContribution> objects = getHint(DistrictDiagramViewer.KEY_MAP_SIZING_OBJECTS);
139         return objects;
140     }
141
142     private SizeBarOptions getSizeBarOptions() {
143         SizeBarOptions options = getHint(DistrictDiagramViewer.KEY_MAP_SIZE_BAR_OPTIONS);
144         return options;
145     }
146
147     private ScheduledFuture<?> hoverUpdateSchedule;
148     
149     public void hoverNode(Resource runtimeDiagram, Resource mapElement, G2DParentNode hoveredNode) {
150         IThreadWorkQueue thread = getThread();
151         Simantics.getSession().asyncRequest(new ReadRequest() {
152
153             @Override
154             public void run(ReadGraph graph) throws DatabaseException {
155                 DynamicVisualisation visualisation = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram));
156                 if (visualisation == null)
157                     return;
158                 if (hoverUpdateSchedule != null && !hoverUpdateSchedule.isDone()) {
159                     hoverUpdateSchedule.cancel(false);
160                 }
161                 hoverUpdateSchedule = ThreadUtils.getNonBlockingWorkExecutor().scheduleWithFixedDelay(() -> {
162                     
163                     CompletableFuture<Object> future = new CompletableFuture<>();
164                     try {
165                         Simantics.getSession().syncRequest(new ReadRequest() {
166
167                             @Override
168                             public void run(ReadGraph graph) throws DatabaseException {
169                                 boolean keyVariablesVertexHover = visualisation.isKeyVariablesVertexHover();
170                                 boolean keyVariablesEdgesHover = visualisation.isKeyVariablesEdgesHover();
171                                 
172                                 Resource mapElementInstanceOf = graph.getSingleObject(mapElement, Layer0.getInstance(graph).InstanceOf);
173                                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
174                                 
175                                 boolean doHover = true;
176                                 if (mapElementInstanceOf.equals(DN.Vertex) && !keyVariablesVertexHover) {
177                                     doHover = false;
178                                 } else if (mapElementInstanceOf.equals(DN.Edge) && !keyVariablesEdgesHover) {
179                                     doHover = false;
180                                 }
181                                 final boolean finalDoHover = doHover;
182                                 
183                                 StyleResult results = DistrictNetworkHoverInfoStyle.doCalculateStyleResult(graph, runtimeDiagram, mapElement);
184                                 if (results != null) {
185                                     Point2D location = DistrictNetworkHoverInfoStyle.calculatePoint(graph, mapElement);
186                                     thread.asyncExec(() -> {
187                                         if (isRemoved())
188                                             return;
189                                         if (finalDoHover) {
190                                             hoverInfoNode.setLabels(results.getLabels());
191                                             hoverInfoNode.setOrigin(results.getOrigin());
192                                             
193                                             hoverInfoNode.setMousePosition(location);
194                                             hoverInfoNode.hover2(hoveredNode);
195                                         } else {
196                                             hoverInfoNode.hover2(null);
197                                         }
198                                         future.complete(new Object());
199                                     });
200                                 } else {
201                                     future.complete(new Object());
202                                 }
203                             }
204                         });
205                     } catch (DatabaseException e) {
206                         future.completeExceptionally(e);
207                     }
208                     // this waits until everything is done
209                     try {
210                         future.get();
211                     } catch (InterruptedException | ExecutionException e) {
212                         LOGGER.debug("Interrupted hovering", e);
213                     }
214                 }, 0, visualisation.getInterval(), TimeUnit.MILLISECONDS);
215             }
216         });
217     }
218
219     public boolean doHover(boolean hover, boolean isConnectionTool) {
220         return hoverInfoNode.hover(hover, isConnectionTool);
221     }
222 }