From: jsimomaa Date: Mon, 8 Jun 2020 05:52:36 +0000 (+0300) Subject: Add participant for visualizing district finder search results X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F95%2F4295%2F2;p=simantics%2Fdistrict.git Add participant for visualizing district finder search results gitlab #86 Change-Id: I418faea0ecacaf2f056620c7980992f781611044 --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java index 19838955..7c368535 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java @@ -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 index 00000000..5f689de9 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java @@ -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); + } +}