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.gui.MapFrame;
|
---|
41 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
42 |
|
---|
43 | import com.innovant.josm.plugin.routing.RoutingLayer;
|
---|
44 | import com.innovant.josm.plugin.routing.RoutingModel;
|
---|
45 | import com.innovant.josm.plugin.routing.RoutingPlugin;
|
---|
46 | import com.innovant.josm.plugin.routing.gui.RoutingDialog;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Accounts for the selection or unselection of the routing tool in the tool bar,
|
---|
50 | * and the mouse events when this tool is selected
|
---|
51 | * @author Juangui
|
---|
52 | * @author Jose Vidal
|
---|
53 | *
|
---|
54 | */
|
---|
55 | public class MoveRouteNodeAction extends MapMode {
|
---|
56 | /**
|
---|
57 | * Serial.
|
---|
58 | */
|
---|
59 | private static final long serialVersionUID = 1L;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Square of the distance radius where route nodes can be selected for dragging
|
---|
63 | */
|
---|
64 | private static final int DRAG_SQR_RADIUS = 100;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Logger.
|
---|
68 | */
|
---|
69 | static Logger logger = Logger.getLogger(RoutingLayer.class);
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Routing Dialog.
|
---|
73 | */
|
---|
74 | private RoutingDialog routingDialog;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Index of dragged node
|
---|
78 | */
|
---|
79 | private int index;
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Constructor
|
---|
83 | * @param mapFrame
|
---|
84 | */
|
---|
85 | public MoveRouteNodeAction(MapFrame mapFrame) {
|
---|
86 | // TODO Use constructor with shortcut
|
---|
87 | super(tr("Routing"), "move",
|
---|
88 | tr("Click and drag to move destination"),
|
---|
89 | mapFrame, ImageProvider.getCursor("normal", "move"));
|
---|
90 | this.routingDialog = RoutingPlugin.getInstance().getRoutingDialog();
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override public void enterMode() {
|
---|
94 | super.enterMode();
|
---|
95 | Main.map.mapView.addMouseListener(this);
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override public void exitMode() {
|
---|
99 | super.exitMode();
|
---|
100 | Main.map.mapView.removeMouseListener(this);
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override public void mousePressed(MouseEvent e) {
|
---|
104 | // If left button is pressed
|
---|
105 | if (e.getButton() == MouseEvent.BUTTON1) {
|
---|
106 | if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
|
---|
107 | RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
|
---|
108 | RoutingModel routingModel = layer.getRoutingModel();
|
---|
109 | // Search for the nearest node in the list
|
---|
110 | List<Node> nl = routingModel.getSelectedNodes();
|
---|
111 | index = -1;
|
---|
112 | double dmax = DRAG_SQR_RADIUS; // maximum distance, in pixels
|
---|
113 | for (int i=0;i<nl.size();i++) {
|
---|
114 | Node node = nl.get(i);
|
---|
115 | double d = Main.map.mapView.getPoint(node.eastNorth).distanceSq(e.getPoint());
|
---|
116 | if (d < dmax) {
|
---|
117 | dmax = d;
|
---|
118 | index = i;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | if (index>=0)
|
---|
122 | logger.debug("Moved from node " + nl.get(index));
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override public void mouseReleased(MouseEvent e) {
|
---|
128 | // If left button is released and a route node is being dragged
|
---|
129 | if ((e.getButton() == MouseEvent.BUTTON1) && (index>=0)) {
|
---|
130 | searchAndReplaceNode(e.getPoint());
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override public void mouseDragged(MouseEvent e) {
|
---|
135 | }
|
---|
136 |
|
---|
137 | private void searchAndReplaceNode(Point point) {
|
---|
138 | if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
|
---|
139 | RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
|
---|
140 | RoutingModel routingModel = layer.getRoutingModel();
|
---|
141 | // Search for nearest highway node
|
---|
142 | Node node = null;
|
---|
143 | node = layer.getNearestHighwayNode(point);
|
---|
144 | if (node == null) {
|
---|
145 | logger.debug("Didn't found a close node to move to.");
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | logger.debug("Moved to node " + node);
|
---|
149 | routingModel.removeNode(index);
|
---|
150 | routingDialog.removeNode(index);
|
---|
151 | routingModel.insertNode(index, node);
|
---|
152 | routingDialog.insertNode(index, node);
|
---|
153 | Main.map.repaint();
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|