]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNPointerInteractor.java
Change listening for district elements on network-diagram
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / participants / DNPointerInteractor.java
1 package org.simantics.district.network.ui.participants;
2
3 import java.awt.geom.Point2D;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
9 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
10 import org.simantics.g2d.canvas.ICanvasParticipant;
11 import org.simantics.g2d.diagram.handler.PickRequest.PickSorter;
12 import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;
13 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;
14 import org.simantics.g2d.element.ElementHints;
15 import org.simantics.g2d.element.IElement;
16 import org.simantics.scenegraph.g2d.IG2DNode;
17 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
18
19 public class DNPointerInteractor extends PointerInteractor {
20
21     private static class DNPickSorter implements PickSorter {
22
23         @Override
24         public void sort(List<IElement> elements) {
25             Collections.sort(elements, (e1, e2) -> {
26                 IG2DNode e1node = getNode(e1);
27                 IG2DNode e2node = getNode(e2);
28                 if (e1node.getZIndex() < e2node.getZIndex())
29                     return -1;
30                 else if (e1node.getZIndex() > e2node.getZIndex())
31                     return 1;
32                 return 0;
33             });
34         }
35
36         private static IG2DNode getNode(IElement element) {
37             IG2DNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
38             if (node == null)
39                 node = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
40             if (node == null) {
41                 node = element.getHint(ElementHints.KEY_SG_NODE);
42                 if (node instanceof SingleElementNode) {
43                     SingleElementNode snode = (SingleElementNode) node;
44                     node = snode.getNodes().iterator().next();
45                 }
46             }
47             return node;
48         }
49     }
50
51     public DNPointerInteractor() {
52         super(new DNPickSorter());
53     }
54
55     @Override
56     protected ICanvasParticipant createConnectTool(TerminalInfo ti, int mouseId, Point2D startCanvasPos) {
57         return super.createConnectTool(ti, mouseId, startCanvasPos);
58     }
59
60     @Override
61     protected ICanvasParticipant createTranslateTool(int mouseId, Point2D startCanvasPos, Point2D curCanvasPos,
62             Set<IElement> elementsToDrag) {
63         return new DNTranslateMode(startCanvasPos, curCanvasPos, mouseId, elementsToDrag);
64     }
65 }