]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Add participant for visualizing district finder search results 95/4295/2
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 8 Jun 2020 05:52:36 +0000 (08:52 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 8 Jun 2020 17:30:40 +0000 (17:30 +0000)
gitlab #86

Change-Id: I418faea0ecacaf2f056620c7980992f781611044

org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java [new file with mode: 0644]

index 198389551768a250b22c6be34a293f6f99ba2acb..7c368535d6ebd97526d4103fa485bc8cc1bf3ddf 100644 (file)
@@ -9,6 +9,9 @@ import java.util.concurrent.TimeUnit;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.core.services.events.IEventBroker;
+import org.eclipse.ui.PlatformUI;
 import org.simantics.datatypes.literal.RGB;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
@@ -23,6 +26,7 @@ import org.simantics.district.network.ui.internal.Activator;
 import org.simantics.district.network.ui.nodes.DistrictRenderingPreparationNode;
 import org.simantics.district.network.ui.nodes.DistrictSelectionNode;
 import org.simantics.district.network.ui.participants.DNPointerInteractor;
+import org.simantics.district.network.ui.participants.DistrictFinderVisualisationParticipant;
 import org.simantics.district.network.ui.participants.DynamicVisualisationContributionsParticipant;
 import org.simantics.district.network.ui.participants.MapRulerPainter;
 import org.simantics.district.network.visualisations.DynamicVisualisations;
@@ -82,8 +86,19 @@ public class DistrictDiagramViewer extends DiagramViewer {
         DistrictRenderingPreparationNode prepNode = new DistrictRenderingPreparationNode();
         prepNode.setZIndex(Integer.MIN_VALUE / 2);
         spatialRoot.addNode("districtRenderingPrepareNode", prepNode);
+
+        
+        IEclipseContext workbenchContext = getWorkbenchContext();
+        IEventBroker eventBroker = workbenchContext.get(IEventBroker.class);
+        DistrictFinderVisualisationParticipant districtFinderVisualisationParticipant = new DistrictFinderVisualisationParticipant(eventBroker);
+        ctx.add(districtFinderVisualisationParticipant);
+        
     }
-    
+
+    public static IEclipseContext getWorkbenchContext(){
+        return PlatformUI.getWorkbench().getService(IEclipseContext.class);
+    }
+
     protected String getPopupId() {
         return "#DistrictDiagramPopup";
     }
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java
new file mode 100644 (file)
index 0000000..5f689de
--- /dev/null
@@ -0,0 +1,65 @@
+package org.simantics.district.network.ui.participants;
+
+import java.awt.Color;
+import java.util.Iterator;
+
+import org.eclipse.e4.core.services.events.IEventBroker;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.simantics.browsing.ui.common.AdaptableHintContext;
+import org.simantics.db.Resource;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.diagram.elements.DiagramNodeUtil;
+import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
+import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
+import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
+import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
+import org.simantics.g2d.element.IElement;
+import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
+
+public class DistrictFinderVisualisationParticipant extends AbstractDiagramParticipant {
+
+    private static final String TOPIC = "org/simantics/district/selection/elementQuerySelection";
+    
+    private IEventBroker broker;
+
+    public DistrictFinderVisualisationParticipant(IEventBroker broker) {
+        this.broker = broker;
+    }
+    
+    public static final Key COLOR_KEY = new KeyOf(Color.class, "COLOR");
+    
+    @Override
+    public void addedToContext(ICanvasContext ctx) {
+        super.addedToContext(ctx);
+        
+        broker.subscribe(TOPIC, event -> {
+            StructuredSelection selection = (StructuredSelection) event.getProperty(IEventBroker.DATA);
+            Iterator<?> iterator = selection.iterator();
+            while (iterator.hasNext()) {
+                AdaptableHintContext next = (AdaptableHintContext) iterator.next();
+                Resource resource = next.getHint(SelectionHints.KEY_MAIN);
+                Color colorr = next.getHint(COLOR_KEY);
+                IElement element = DiagramNodeUtil.findElement(ctx, resource);
+                final G2DParentNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
+                if (node != null) {
+                    // color is a bit special - if not present then reset with null value
+                    ((DistrictNetworkEdgeNode) node).setDynamicColor(colorr);
+                    node.repaint();
+                } else {
+                    final G2DParentNode vertexNode = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
+                    ((DistrictNetworkVertexNode) vertexNode).setDynamicColor(colorr);
+                    vertexNode.repaint();
+                }
+            }
+        });
+    }
+    
+    @Override
+    public void removedFromContext(ICanvasContext ctx) {
+        super.removedFromContext(ctx);
+    }
+}