]> gerrit.simantics Code Review - simantics/district.git/blob
a88d7ba94d362ab8c4c752d9682e34f92c474df0
[simantics/district.git] /
1 package org.simantics.district.network.ui.participants;
2
3 import java.awt.Color;
4 import java.util.Iterator;
5
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;
23
24 public class DistrictFinderVisualisationParticipant extends AbstractDiagramParticipant {
25
26     private static final String TOPIC = "org/simantics/district/selection/elementQuerySelection";
27     private static final String TOPIC2 = "org/simantics/district/visualisationTrigger";
28     
29     private IEventBroker broker;
30
31     public DistrictFinderVisualisationParticipant(IEventBroker broker) {
32         this.broker = broker;
33     }
34     
35     public static final Key COLOR_KEY = new KeyOf(Color.class, "COLOR");
36     
37     @Override
38     public void addedToContext(ICanvasContext ctx) {
39         super.addedToContext(ctx);
40         
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);
50                 if (node != null) {
51                     // color is a bit special - if not present then reset with null value
52                     ((DistrictNetworkEdgeNode) node).setDynamicColor(colorr);
53                     node.repaint();
54                 } else {
55                     final G2DParentNode vertexNode = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
56                     ((DistrictNetworkVertexNode) vertexNode).setDynamicColor(colorr);
57                     vertexNode.repaint();
58                 }
59             }
60         });
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);
67             if (node != null) {
68                 // color is a bit special - if not present then reset with null value
69                 ((DistrictNetworkEdgeNode) node).setEventColor(colorr);
70                 node.repaint();
71             } else {
72                 final G2DParentNode vertexNode = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
73                 ((DistrictNetworkVertexNode) vertexNode).setEventColor(colorr);
74                 vertexNode.repaint();
75             }
76         });
77     }
78     
79     @Override
80     public void removedFromContext(ICanvasContext ctx) {
81         super.removedFromContext(ctx);
82     }
83 }