source: osm/applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/actions/MoveRouteNodeAction.java@ 33794

Last change on this file since 33794 was 33794, checked in by donvip, 6 years ago

update to JOSM 12840

File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package com.innovant.josm.plugin.routing.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Point;
7import java.awt.event.MouseEvent;
8import java.util.List;
9
10import org.apache.log4j.Logger;
11import org.openstreetmap.josm.actions.mapmode.MapMode;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.gui.MainApplication;
14import org.openstreetmap.josm.gui.layer.Layer;
15import org.openstreetmap.josm.tools.ImageProvider;
16
17import com.innovant.josm.plugin.routing.RoutingLayer;
18import com.innovant.josm.plugin.routing.RoutingModel;
19import com.innovant.josm.plugin.routing.RoutingPlugin;
20import com.innovant.josm.plugin.routing.gui.RoutingDialog;
21
22/**
23 * Accounts for the selection or unselection of the routing tool in the tool bar,
24 * and the mouse events when this tool is selected
25 * @author Juangui
26 * @author Jose Vidal
27 *
28 */
29public class MoveRouteNodeAction extends MapMode {
30
31 /**
32 * Square of the distance radius where route nodes can be selected for dragging
33 */
34 private static final int DRAG_SQR_RADIUS = 100;
35
36 /**
37 * Logger.
38 */
39 static Logger logger = Logger.getLogger(RoutingLayer.class);
40
41 /**
42 * Index of dragged node
43 */
44 private int index;
45
46 /**
47 * Constructor
48 * @param mapFrame map frame
49 */
50 public MoveRouteNodeAction() {
51 // TODO Use constructor with shortcut
52 super(tr("Routing"), "move",
53 tr("Click and drag to move destination"),
54 ImageProvider.getCursor("normal", "move"));
55 }
56
57 @Override public void enterMode() {
58 super.enterMode();
59 MainApplication.getMap().mapView.addMouseListener(this);
60 }
61
62 @Override public void exitMode() {
63 super.exitMode();
64 MainApplication.getMap().mapView.removeMouseListener(this);
65 }
66
67 @Override public void mousePressed(MouseEvent e) {
68 // If left button is pressed
69 if (e.getButton() == MouseEvent.BUTTON1) {
70 if (MainApplication.getLayerManager().getActiveLayer() instanceof RoutingLayer) {
71 requestFocusInMapView();
72 RoutingLayer layer = (RoutingLayer) MainApplication.getLayerManager().getActiveLayer();
73 RoutingModel routingModel = layer.getRoutingModel();
74 // Search for the nearest node in the list
75 List<Node> nl = routingModel.getSelectedNodes();
76 index = -1;
77 double dmax = DRAG_SQR_RADIUS; // maximum distance, in pixels
78 for (int i = 0; i < nl.size(); i++) {
79 Node node = nl.get(i);
80 double d = MainApplication.getMap().mapView.getPoint(node).distanceSq(e.getPoint());
81 if (d < dmax) {
82 dmax = d;
83 index = i;
84 }
85 }
86 if (index >= 0)
87 logger.debug("Moved from node " + nl.get(index));
88 }
89 }
90 }
91
92 @Override public void mouseReleased(MouseEvent e) {
93 // If left button is released and a route node is being dragged
94 if ((e.getButton() == MouseEvent.BUTTON1) && (index >= 0)) {
95 searchAndReplaceNode(e.getPoint());
96 }
97 }
98
99 @Override public void mouseDragged(MouseEvent e) {
100 }
101
102 private void searchAndReplaceNode(Point point) {
103 if (MainApplication.getLayerManager().getActiveLayer() instanceof RoutingLayer) {
104 RoutingLayer layer = (RoutingLayer) MainApplication.getLayerManager().getActiveLayer();
105 RoutingModel routingModel = layer.getRoutingModel();
106 RoutingDialog routingDialog = RoutingPlugin.getInstance().getRoutingDialog();
107 // Search for nearest highway node
108 Node node = null;
109 node = layer.getNearestHighwayNode(point);
110 if (node == null) {
111 logger.debug("Didn't found a close node to move to.");
112 return;
113 }
114 logger.debug("Moved to node " + node);
115 routingModel.removeNode(index);
116 routingDialog.removeNode(index);
117 routingModel.insertNode(index, node);
118 routingDialog.insertNode(index, node);
119 MainApplication.getMap().repaint();
120 }
121 }
122
123 @Override public boolean layerIsSupported(Layer l) {
124 return l instanceof RoutingLayer;
125 }
126}
Note: See TracBrowser for help on using the repository browser.