1 package org.simantics.district.network.ui.participants;
4 import java.util.Iterator;
6 import org.eclipse.e4.core.services.events.IEventBroker;
7 import org.eclipse.jface.viewers.StructuredSelection;
8 import org.simantics.browsing.ui.common.AdaptableHintContext;
9 import org.simantics.db.Resource;
10 import org.simantics.db.layer0.SelectionHints;
11 import org.simantics.diagram.elements.DiagramNodeUtil;
12 import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
13 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
14 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
15 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
16 import org.simantics.g2d.canvas.ICanvasContext;
17 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
18 import org.simantics.g2d.element.IElement;
19 import org.simantics.scenegraph.g2d.G2DParentNode;
20 import org.simantics.scl.runtime.tuple.Tuple2;
21 import org.simantics.utils.datastructures.hints.IHintContext.Key;
22 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
24 public class DistrictFinderVisualisationParticipant extends AbstractDiagramParticipant {
26 private static final String TOPIC = "org/simantics/district/selection/elementQuerySelection";
27 private static final String TOPIC2 = "org/simantics/district/visualisationTrigger";
29 private IEventBroker broker;
31 public DistrictFinderVisualisationParticipant(IEventBroker broker) {
35 public static final Key COLOR_KEY = new KeyOf(Color.class, "COLOR");
38 public void addedToContext(ICanvasContext ctx) {
39 super.addedToContext(ctx);
41 broker.subscribe(TOPIC, event -> {
42 StructuredSelection selection = (StructuredSelection) event.getProperty(IEventBroker.DATA);
43 Iterator<?> iterator = selection.iterator();
44 while (iterator.hasNext()) {
45 AdaptableHintContext next = (AdaptableHintContext) iterator.next();
46 Resource resource = next.getHint(SelectionHints.KEY_MAIN);
47 Color colorr = next.getHint(COLOR_KEY);
48 IElement element = DiagramNodeUtil.findElement(ctx, resource);
49 final G2DParentNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
51 // color is a bit special - if not present then reset with null value
52 ((DistrictNetworkEdgeNode) node).setDynamicColor(colorr);
55 final G2DParentNode vertexNode = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
56 ((DistrictNetworkVertexNode) vertexNode).setDynamicColor(colorr);
61 broker.subscribe(TOPIC2, event -> {
62 Tuple2 data = (Tuple2) event.getProperty(IEventBroker.DATA);
63 Resource resource = (Resource) data.c0;
64 Color colorr = (Color) data.c1;
65 IElement element = DiagramNodeUtil.findElement(ctx, resource);
66 final G2DParentNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
68 // color is a bit special - if not present then reset with null value
69 ((DistrictNetworkEdgeNode) node).setEventColor(colorr);
72 final G2DParentNode vertexNode = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
73 ((DistrictNetworkVertexNode) vertexNode).setEventColor(colorr);
80 public void removedFromContext(ICanvasContext ctx) {
81 super.removedFromContext(ctx);