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