1 package org.simantics.district.network.ui.participants;
3 import java.awt.geom.Point2D;
4 import java.util.Collections;
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;
23 public class DNPointerInteractor extends PointerInteractor {
25 private RoutingMode routingMode;
27 private static class DNPickSorter implements PickSorter {
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())
36 else if (e1node.getZIndex() > e2node.getZIndex())
42 private static IG2DNode getNode(IElement element) {
43 IG2DNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
45 node = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
47 node = element.getHint(ElementHints.KEY_SG_NODE);
48 if (node instanceof SingleElementNode) {
49 SingleElementNode snode = (SingleElementNode) node;
50 node = snode.getNodes().iterator().next();
57 public DNPointerInteractor() {
58 super(new DNPickSorter());
62 protected ICanvasParticipant createConnectTool(TerminalInfo ti, int mouseId, Point2D startCanvasPos) {
63 return super.createConnectTool(ti, mouseId, startCanvasPos);
67 protected ICanvasParticipant createTranslateTool(int mouseId, Point2D startCanvasPos, Point2D curCanvasPos,
68 Set<IElement> elementsToDrag) {
69 return new DNTranslateMode(startCanvasPos, curCanvasPos, mouseId, elementsToDrag);
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);