1 | /*
|
---|
2 | * Copyright (C) 2008 Innovant
|
---|
3 | *
|
---|
4 | * This program is free software; you can redistribute it and/or
|
---|
5 | * modify it under the terms of the GNU General Public License
|
---|
6 | * as published by the Free Software Foundation; either version 2
|
---|
7 | * of the License, or (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This program is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
---|
17 | *
|
---|
18 | * For more information, please contact:
|
---|
19 | *
|
---|
20 | * Innovant
|
---|
21 | * juangui@gmail.com
|
---|
22 | * vidalfree@gmail.com
|
---|
23 | *
|
---|
24 | * http://public.grupoinnovant.com/blog
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | package com.innovant.josm.plugin.routing.actions;
|
---|
29 |
|
---|
30 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
31 |
|
---|
32 | import java.awt.Point;
|
---|
33 | import java.awt.event.MouseEvent;
|
---|
34 | import java.util.List;
|
---|
35 |
|
---|
36 | import org.apache.log4j.Logger;
|
---|
37 | import org.openstreetmap.josm.Main;
|
---|
38 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
39 | import org.openstreetmap.josm.data.osm.Node;
|
---|
40 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
41 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
42 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
43 |
|
---|
44 | import com.innovant.josm.plugin.routing.RoutingLayer;
|
---|
45 | import com.innovant.josm.plugin.routing.RoutingModel;
|
---|
46 | import com.innovant.josm.plugin.routing.RoutingPlugin;
|
---|
47 | import com.innovant.josm.plugin.routing.gui.RoutingDialog;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Accounts for the selection or unselection of the routing tool in the tool bar,
|
---|
51 | * and the mouse events when this tool is selected
|
---|
52 | * @author Juangui
|
---|
53 | * @author Jose Vidal
|
---|
54 | *
|
---|
55 | */
|
---|
56 | public class MoveRouteNodeAction extends MapMode {
|
---|
57 | /**
|
---|
58 | * Serial.
|
---|
59 | */
|
---|
60 | private static final long serialVersionUID = 1L;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Square of the distance radius where route nodes can be selected for dragging
|
---|
64 | */
|
---|
65 | private static final int DRAG_SQR_RADIUS = 100;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Logger.
|
---|
69 | */
|
---|
70 | static Logger logger = Logger.getLogger(RoutingLayer.class);
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Routing Model.
|
---|
74 | */
|
---|
75 | private RoutingModel routingModel;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Routing Layer.
|
---|
79 | */
|
---|
80 | private RoutingLayer routingLayer;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Routing Dialog.
|
---|
84 | */
|
---|
85 | private RoutingDialog routingDialog;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Index of dragged node
|
---|
89 | */
|
---|
90 | private int index;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Constructor
|
---|
94 | * @param mapFrame
|
---|
95 | */
|
---|
96 | public MoveRouteNodeAction(MapFrame mapFrame) {
|
---|
97 | // TODO Use constructor with shortcut
|
---|
98 | super(tr("Routing"), "move",
|
---|
99 | tr("Click and drag to move route nodes."),
|
---|
100 | mapFrame, ImageProvider.getCursor("normal", "move"));
|
---|
101 | this.routingLayer = RoutingPlugin.getInstance().getRoutingLayer();
|
---|
102 | this.routingModel = routingLayer.getRoutingModel();
|
---|
103 | this.routingDialog = RoutingPlugin.getInstance().getRoutingDialog();
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override public void enterMode() {
|
---|
107 | super.enterMode();
|
---|
108 | Main.map.mapView.addMouseListener(this);
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override public void exitMode() {
|
---|
112 | super.exitMode();
|
---|
113 | Main.map.mapView.removeMouseListener(this);
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override public void mousePressed(MouseEvent e) {
|
---|
117 | // If left button is pressed
|
---|
118 | if (e.getButton() == MouseEvent.BUTTON1) {
|
---|
119 | // Search for the nearest node in the list
|
---|
120 | List<Node> nl = routingModel.getSelectedNodes();
|
---|
121 | index = -1;
|
---|
122 | double dmax = DRAG_SQR_RADIUS; // maximum distance, in pixels
|
---|
123 | for (int i=0;i<nl.size();i++) {
|
---|
124 | Node node = nl.get(i);
|
---|
125 | double d = Main.map.mapView.getPoint(node.eastNorth).distanceSq(e.getPoint());
|
---|
126 | if (d < dmax) {
|
---|
127 | dmax = d;
|
---|
128 | index = i;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | if (index>=0)
|
---|
132 | logger.debug("Moved from node " + nl.get(index));
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override public void mouseReleased(MouseEvent e) {
|
---|
137 | // If left button is released and a route node is being dragged
|
---|
138 | if ((e.getButton() == MouseEvent.BUTTON1) && (index>=0)) {
|
---|
139 | searchAndReplaceNode(e.getPoint());
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override public void mouseDragged(MouseEvent e) {
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void searchAndReplaceNode(Point point) {
|
---|
147 | // Search for nearest highway node
|
---|
148 | Node node = null;
|
---|
149 | List<WaySegment> wsl = Main.map.mapView.getNearestWaySegments(point);
|
---|
150 | for (WaySegment ws:wsl) {
|
---|
151 | if (ws.way.get("highway")!=null) {
|
---|
152 | // If waysegment belongs to a highway compare the distance from
|
---|
153 | // both waysegment nodes to the point clicked and keep the nearest one
|
---|
154 | Node node0 = ws.way.nodes.get(ws.lowerIndex);
|
---|
155 | Node node1 = ws.way.nodes.get(ws.lowerIndex + 1);
|
---|
156 | double d0 = Main.map.mapView.getPoint(node0.eastNorth).distanceSq(point);
|
---|
157 | double d1 = Main.map.mapView.getPoint(node1.eastNorth).distanceSq(point);
|
---|
158 | if (d0<d1)
|
---|
159 | node = node0;
|
---|
160 | else
|
---|
161 | node = node1;
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | if (node == null) {
|
---|
166 | logger.debug("Didn't found a close node to move to.");
|
---|
167 | return;
|
---|
168 | }
|
---|
169 | logger.debug("Moved to node " + node);
|
---|
170 | routingModel.removeNode(index);
|
---|
171 | routingDialog.removeNode(index);
|
---|
172 | routingModel.insertNode(index, node);
|
---|
173 | routingDialog.insertNode(index,node.id+" ["+node.coor.toDisplayString()+"]");
|
---|
174 | Main.map.repaint();
|
---|
175 | }
|
---|
176 | }
|
---|