]> gerrit.simantics Code Review - simantics/district.git/blob
9df6e5efbc2646a67fd165b43d90c1d436f44937
[simantics/district.git] /
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.ICanvasContext;
11 import org.simantics.g2d.canvas.ICanvasParticipant;
12 import org.simantics.g2d.diagram.handler.PickRequest.PickSorter;
13 import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;
14 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;
15 import org.simantics.g2d.element.ElementHints;
16 import org.simantics.g2d.element.IElement;
17 import org.simantics.scenegraph.g2d.IG2DNode;
18 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
19 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;
20 import org.simantics.scenegraph.g2d.events.KeyEvent;
21 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
22
23 public class DNPointerInteractor extends PointerInteractor {
24
25     private RoutingMode routingMode;
26
27     private static class DNPickSorter implements PickSorter {
28
29         @Override
30         public void sort(List<IElement> elements) {
31             Collections.sort(elements, (e1, e2) -> {
32                 IG2DNode e1node = getNode(e1);
33                 IG2DNode e2node = getNode(e2);
34                 if (e1node.getZIndex() < e2node.getZIndex())
35                     return -1;
36                 else if (e1node.getZIndex() > e2node.getZIndex())
37                     return 1;
38                 return 0;
39             });
40         }
41
42         private static IG2DNode getNode(IElement element) {
43             IG2DNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
44             if (node == null)
45                 node = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
46             if (node == null) {
47                 node = element.getHint(ElementHints.KEY_SG_NODE);
48                 if (node instanceof SingleElementNode) {
49                     SingleElementNode snode = (SingleElementNode) node;
50                     node = snode.getNodes().iterator().next();
51                 }
52             }
53             return node;
54         }
55     }
56
57     public DNPointerInteractor() {
58         super(new DNPickSorter());
59     }
60
61     @Override
62     protected ICanvasParticipant createConnectTool(TerminalInfo ti, int mouseId, Point2D startCanvasPos) {
63         return super.createConnectTool(ti, mouseId, startCanvasPos);
64     }
65
66     @Override
67     protected ICanvasParticipant createTranslateTool(int mouseId, Point2D startCanvasPos, Point2D curCanvasPos,
68             Set<IElement> elementsToDrag) {
69         return new DNTranslateMode(startCanvasPos, curCanvasPos, mouseId, elementsToDrag);
70     }
71
72     @EventHandler(priority = 1000)
73     public boolean enterroutingMode(KeyEvent ke) {
74         if (Character.toLowerCase(ke.character) == 't' && ke instanceof KeyReleasedEvent) {
75             ICanvasContext canvasContext = getContext();
76             if (routingMode == null || routingMode.isRemoved()) {
77                 routingMode = new RoutingMode(0);
78                 canvasContext.add(routingMode);
79             }
80         }
81         return false;
82     }
83
84     @Override
85     public double getPickDistance() {
86         return 0.00001;
87     }
88 }