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

Last change on this file was 36085, checked in by taylor.smock, 12 months ago

Fix #22968: ClassCastException in RoutingGraph#applyAlgorithm

The problem pretty much comes down to the active layer not being the routing
layer.

This additionally fixes many SonarLint issues and drops a dependency on log4j.

File size: 4.1 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.openstreetmap.josm.actions.mapmode.MapMode;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.gui.MainApplication;
13import org.openstreetmap.josm.gui.layer.Layer;
14import org.openstreetmap.josm.tools.ImageProvider;
15
16import com.innovant.josm.plugin.routing.RoutingLayer;
17import com.innovant.josm.plugin.routing.RoutingModel;
18import com.innovant.josm.plugin.routing.RoutingPlugin;
19import com.innovant.josm.plugin.routing.gui.RoutingDialog;
20import org.openstreetmap.josm.tools.Logging;
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 * Index of dragged node
38 */
39 private int index;
40
41 /**
42 * Constructor
43 */
44 public MoveRouteNodeAction() {
45 // TODO Use constructor with shortcut
46 super(tr("Routing"), "move",
47 tr("Click and drag to move destination"),
48 ImageProvider.getCursor("normal", "move"));
49 }
50
51 @Override public void enterMode() {
52 super.enterMode();
53 MainApplication.getMap().mapView.addMouseListener(this);
54 }
55
56 @Override public void exitMode() {
57 super.exitMode();
58 MainApplication.getMap().mapView.removeMouseListener(this);
59 }
60
61 @Override public void mousePressed(MouseEvent e) {
62 // If left button is pressed
63 if (e.getButton() == MouseEvent.BUTTON1 && MainApplication.getLayerManager().getActiveLayer() instanceof RoutingLayer) {
64 requestFocusInMapView();
65 RoutingLayer layer = (RoutingLayer) MainApplication.getLayerManager().getActiveLayer();
66 RoutingModel routingModel = layer.getRoutingModel();
67 // Search for the nearest node in the list
68 List<Node> nl = routingModel.getSelectedNodes();
69 index = -1;
70 double dmax = DRAG_SQR_RADIUS; // maximum distance, in pixels
71 for (int i = 0; i < nl.size(); i++) {
72 Node node = nl.get(i);
73 double d = MainApplication.getMap().mapView.getPoint(node).distanceSq(e.getPoint());
74 if (d < dmax) {
75 dmax = d;
76 index = i;
77 }
78 }
79 if (index >= 0)
80 Logging.trace("Moved from node {0}", nl.get(index));
81 }
82 }
83
84 @Override public void mouseReleased(MouseEvent e) {
85 // If left button is released and a route node is being dragged
86 if ((e.getButton() == MouseEvent.BUTTON1) && (index >= 0)) {
87 searchAndReplaceNode(e.getPoint());
88 }
89 }
90
91 @Override public void mouseDragged(MouseEvent e) {
92 }
93
94 private void searchAndReplaceNode(Point point) {
95 if (MainApplication.getLayerManager().getActiveLayer() instanceof RoutingLayer) {
96 RoutingLayer layer = (RoutingLayer) MainApplication.getLayerManager().getActiveLayer();
97 RoutingModel routingModel = layer.getRoutingModel();
98 RoutingDialog routingDialog = RoutingPlugin.getInstance().getRoutingDialog();
99 // Search for nearest highway node
100 Node node = null;
101 node = layer.getNearestHighwayNode(point);
102 if (node == null) {
103 Logging.trace("Didn't found a close node to move to.");
104 return;
105 }
106 Logging.trace("Moved to node {0}", node);
107 routingModel.removeNode(index);
108 routingDialog.removeNode(index);
109 routingModel.insertNode(index, node);
110 routingDialog.insertNode(index, node);
111 MainApplication.getMap().repaint();
112 }
113 }
114
115 @Override public boolean layerIsSupported(Layer l) {
116 return l instanceof RoutingLayer;
117 }
118}
Note: See TracBrowser for help on using the repository browser.