]> gerrit.simantics Code Review - simantics/district.git/blob
5f689de994aeabc64d00510397611c53edf7daac
[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.utils.datastructures.hints.IHintContext.Key;
21 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
22
23 public class DistrictFinderVisualisationParticipant extends AbstractDiagramParticipant {
24
25     private static final String TOPIC = "org/simantics/district/selection/elementQuerySelection";
26     
27     private IEventBroker broker;
28
29     public DistrictFinderVisualisationParticipant(IEventBroker broker) {
30         this.broker = broker;
31     }
32     
33     public static final Key COLOR_KEY = new KeyOf(Color.class, "COLOR");
34     
35     @Override
36     public void addedToContext(ICanvasContext ctx) {
37         super.addedToContext(ctx);
38         
39         broker.subscribe(TOPIC, event -> {
40             StructuredSelection selection = (StructuredSelection) event.getProperty(IEventBroker.DATA);
41             Iterator<?> iterator = selection.iterator();
42             while (iterator.hasNext()) {
43                 AdaptableHintContext next = (AdaptableHintContext) iterator.next();
44                 Resource resource = next.getHint(SelectionHints.KEY_MAIN);
45                 Color colorr = next.getHint(COLOR_KEY);
46                 IElement element = DiagramNodeUtil.findElement(ctx, resource);
47                 final G2DParentNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
48                 if (node != null) {
49                     // color is a bit special - if not present then reset with null value
50                     ((DistrictNetworkEdgeNode) node).setDynamicColor(colorr);
51                     node.repaint();
52                 } else {
53                     final G2DParentNode vertexNode = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
54                     ((DistrictNetworkVertexNode) vertexNode).setDynamicColor(colorr);
55                     vertexNode.repaint();
56                 }
57             }
58         });
59     }
60     
61     @Override
62     public void removedFromContext(ICanvasContext ctx) {
63         super.removedFromContext(ctx);
64     }
65 }