package org.simantics.district.network.ui.participants; import java.awt.geom.Point2D; import java.util.Collections; import java.util.List; import java.util.Set; import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement; import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.canvas.ICanvasParticipant; import org.simantics.g2d.diagram.handler.PickRequest.PickSorter; import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor; import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo; import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; import org.simantics.scenegraph.g2d.IG2DNode; import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent; import org.simantics.scenegraph.g2d.events.KeyEvent; import org.simantics.scenegraph.g2d.nodes.SingleElementNode; public class DNPointerInteractor extends PointerInteractor { private RoutingMode routingMode; private static class DNPickSorter implements PickSorter { @Override public void sort(List elements) { Collections.sort(elements, (e1, e2) -> { IG2DNode e1node = getNode(e1); IG2DNode e2node = getNode(e2); if (e1node.getZIndex() < e2node.getZIndex()) return -1; else if (e1node.getZIndex() > e2node.getZIndex()) return 1; return 0; }); } private static IG2DNode getNode(IElement element) { IG2DNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE); if (node == null) node = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); if (node == null) { node = element.getHint(ElementHints.KEY_SG_NODE); if (node instanceof SingleElementNode) { SingleElementNode snode = (SingleElementNode) node; node = snode.getNodes().iterator().next(); } } return node; } } public DNPointerInteractor() { super(new DNPickSorter()); } @Override protected ICanvasParticipant createConnectTool(TerminalInfo ti, int mouseId, Point2D startCanvasPos) { return super.createConnectTool(ti, mouseId, startCanvasPos); } @Override protected ICanvasParticipant createTranslateTool(int mouseId, Point2D startCanvasPos, Point2D curCanvasPos, Set elementsToDrag) { return new DNTranslateMode(startCanvasPos, curCanvasPos, mouseId, elementsToDrag); } @EventHandler(priority = 1000) public boolean enterroutingMode(KeyEvent ke) { if (Character.toLowerCase(ke.character) == 't' && ke instanceof KeyReleasedEvent) { ICanvasContext canvasContext = getContext(); if (routingMode == null || routingMode.isRemoved()) { routingMode = new RoutingMode(0); canvasContext.add(routingMode); } } return false; } }