1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.Color;
|
---|
9 | import java.awt.Cursor;
|
---|
10 | import java.awt.Graphics2D;
|
---|
11 | import java.awt.Point;
|
---|
12 | import java.awt.Stroke;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.awt.event.MouseEvent;
|
---|
15 | import java.awt.geom.GeneralPath;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.LinkedList;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.command.AddCommand;
|
---|
25 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
26 | import org.openstreetmap.josm.command.Command;
|
---|
27 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
28 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
29 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
30 | import org.openstreetmap.josm.data.Bounds;
|
---|
31 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
32 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
33 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
34 | import org.openstreetmap.josm.data.osm.Node;
|
---|
35 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
36 | import org.openstreetmap.josm.data.osm.Way;
|
---|
37 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
38 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
39 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
40 | import org.openstreetmap.josm.gui.MapView;
|
---|
41 | import org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable;
|
---|
42 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
43 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
44 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
45 | import org.openstreetmap.josm.gui.util.ModifierListener;
|
---|
46 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
47 | import org.openstreetmap.josm.tools.Pair;
|
---|
48 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @author Alexander Kachkaev <alexander@kachkaev.ru>, 2011
|
---|
52 | */
|
---|
53 | public class ImproveWayAccuracyAction extends MapMode implements
|
---|
54 | SelectionChangedListener, ModifierListener {
|
---|
55 |
|
---|
56 | enum State {
|
---|
57 | selecting, improving
|
---|
58 | }
|
---|
59 |
|
---|
60 | private State state;
|
---|
61 |
|
---|
62 | private MapView mv;
|
---|
63 |
|
---|
64 | private static final long serialVersionUID = 42L;
|
---|
65 |
|
---|
66 | private transient Way targetWay;
|
---|
67 | private transient Node candidateNode;
|
---|
68 | private transient WaySegment candidateSegment;
|
---|
69 |
|
---|
70 | private Point mousePos;
|
---|
71 | private boolean dragging;
|
---|
72 |
|
---|
73 | private final Cursor cursorSelect;
|
---|
74 | private final Cursor cursorSelectHover;
|
---|
75 | private final Cursor cursorImprove;
|
---|
76 | private final Cursor cursorImproveAdd;
|
---|
77 | private final Cursor cursorImproveDelete;
|
---|
78 | private final Cursor cursorImproveAddLock;
|
---|
79 | private final Cursor cursorImproveLock;
|
---|
80 |
|
---|
81 | private Color guideColor;
|
---|
82 | private transient Stroke selectTargetWayStroke;
|
---|
83 | private transient Stroke moveNodeStroke;
|
---|
84 | private transient Stroke moveNodeIntersectingStroke;
|
---|
85 | private transient Stroke addNodeStroke;
|
---|
86 | private transient Stroke deleteNodeStroke;
|
---|
87 | private int dotSize;
|
---|
88 |
|
---|
89 | private boolean selectionChangedBlocked;
|
---|
90 |
|
---|
91 | protected String oldModeHelpText;
|
---|
92 |
|
---|
93 | private final AbstractMapViewPaintable temporaryLayer = new AbstractMapViewPaintable() {
|
---|
94 | @Override
|
---|
95 | public void paint(Graphics2D g, MapView mv, Bounds bbox) {
|
---|
96 | ImproveWayAccuracyAction.this.paint(g, mv, bbox);;
|
---|
97 | }
|
---|
98 | };
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Constructs a new {@code ImproveWayAccuracyAction}.
|
---|
102 | * @param mapFrame Map frame
|
---|
103 | */
|
---|
104 | public ImproveWayAccuracyAction(MapFrame mapFrame) {
|
---|
105 | super(tr("Improve Way Accuracy"), "improvewayaccuracy",
|
---|
106 | tr("Improve Way Accuracy mode"),
|
---|
107 | Shortcut.registerShortcut("mapmode:ImproveWayAccuracy",
|
---|
108 | tr("Mode: {0}", tr("Improve Way Accuracy")),
|
---|
109 | KeyEvent.VK_W, Shortcut.DIRECT), mapFrame, Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
|
---|
110 |
|
---|
111 | cursorSelect = ImageProvider.getCursor("normal", "mode");
|
---|
112 | cursorSelectHover = ImageProvider.getCursor("hand", "mode");
|
---|
113 | cursorImprove = ImageProvider.getCursor("crosshair", null);
|
---|
114 | cursorImproveAdd = ImageProvider.getCursor("crosshair", "addnode");
|
---|
115 | cursorImproveDelete = ImageProvider.getCursor("crosshair", "delete_node");
|
---|
116 | cursorImproveAddLock = ImageProvider.getCursor("crosshair",
|
---|
117 | "add_node_lock");
|
---|
118 | cursorImproveLock = ImageProvider.getCursor("crosshair", "lock");
|
---|
119 | readPreferences();
|
---|
120 | }
|
---|
121 |
|
---|
122 | // -------------------------------------------------------------------------
|
---|
123 | // Mode methods
|
---|
124 | // -------------------------------------------------------------------------
|
---|
125 | @Override
|
---|
126 | public void enterMode() {
|
---|
127 | if (!isEnabled()) {
|
---|
128 | return;
|
---|
129 | }
|
---|
130 | super.enterMode();
|
---|
131 | readPreferences();
|
---|
132 |
|
---|
133 | mv = Main.map.mapView;
|
---|
134 | mousePos = null;
|
---|
135 | oldModeHelpText = "";
|
---|
136 |
|
---|
137 | if (getCurrentDataSet() == null) {
|
---|
138 | return;
|
---|
139 | }
|
---|
140 |
|
---|
141 | updateStateByCurrentSelection();
|
---|
142 |
|
---|
143 | Main.map.mapView.addMouseListener(this);
|
---|
144 | Main.map.mapView.addMouseMotionListener(this);
|
---|
145 | Main.map.mapView.addTemporaryLayer(temporaryLayer);
|
---|
146 | DataSet.addSelectionListener(this);
|
---|
147 |
|
---|
148 | Main.map.keyDetector.addModifierListener(this);
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | protected void readPreferences() {
|
---|
153 | guideColor = Main.pref.getColor(marktr("improve way accuracy helper line"), null);
|
---|
154 | if (guideColor == null) guideColor = PaintColors.HIGHLIGHT.get();
|
---|
155 |
|
---|
156 | selectTargetWayStroke = GuiHelper.getCustomizedStroke(Main.pref.get("improvewayaccuracy.stroke.select-target", "2"));
|
---|
157 | moveNodeStroke = GuiHelper.getCustomizedStroke(Main.pref.get("improvewayaccuracy.stroke.move-node", "1 6"));
|
---|
158 | moveNodeIntersectingStroke = GuiHelper.getCustomizedStroke(Main.pref.get("improvewayaccuracy.stroke.move-node-intersecting", "1 2 6"));
|
---|
159 | addNodeStroke = GuiHelper.getCustomizedStroke(Main.pref.get("improvewayaccuracy.stroke.add-node", "1"));
|
---|
160 | deleteNodeStroke = GuiHelper.getCustomizedStroke(Main.pref.get("improvewayaccuracy.stroke.delete-node", "1"));
|
---|
161 | dotSize = Main.pref.getInteger("improvewayaccuracy.dot-size", 6);
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public void exitMode() {
|
---|
166 | super.exitMode();
|
---|
167 |
|
---|
168 | Main.map.mapView.removeMouseListener(this);
|
---|
169 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
170 | Main.map.mapView.removeTemporaryLayer(temporaryLayer);
|
---|
171 | DataSet.removeSelectionListener(this);
|
---|
172 |
|
---|
173 | Main.map.keyDetector.removeModifierListener(this);
|
---|
174 | temporaryLayer.invalidate();
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | protected void updateStatusLine() {
|
---|
179 | String newModeHelpText = getModeHelpText();
|
---|
180 | if (!newModeHelpText.equals(oldModeHelpText)) {
|
---|
181 | oldModeHelpText = newModeHelpText;
|
---|
182 | Main.map.statusLine.setHelpText(newModeHelpText);
|
---|
183 | Main.map.statusLine.repaint();
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public String getModeHelpText() {
|
---|
189 | if (state == State.selecting) {
|
---|
190 | if (targetWay != null) {
|
---|
191 | return tr("Click on the way to start improving its shape.");
|
---|
192 | } else {
|
---|
193 | return tr("Select a way that you want to make more accurate.");
|
---|
194 | }
|
---|
195 | } else {
|
---|
196 | if (ctrl) {
|
---|
197 | return tr("Click to add a new node. Release Ctrl to move existing nodes or hold Alt to delete.");
|
---|
198 | } else if (alt) {
|
---|
199 | return tr("Click to delete the highlighted node. Release Alt to move existing nodes or hold Ctrl to add new nodes.");
|
---|
200 | } else {
|
---|
201 | return tr("Click to move the highlighted node. Hold Ctrl to add new nodes, or Alt to delete.");
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Override
|
---|
207 | public boolean layerIsSupported(Layer l) {
|
---|
208 | return l instanceof OsmDataLayer;
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | protected void updateEnabledState() {
|
---|
213 | setEnabled(getEditLayer() != null);
|
---|
214 | }
|
---|
215 |
|
---|
216 | // -------------------------------------------------------------------------
|
---|
217 | // MapViewPaintable methods
|
---|
218 | // -------------------------------------------------------------------------
|
---|
219 | /**
|
---|
220 | * Redraws temporary layer. Highlights targetWay in select mode. Draws
|
---|
221 | * preview lines in improve mode and highlights the candidateNode
|
---|
222 | * @param g The graphics
|
---|
223 | * @param mv The map view
|
---|
224 | * @param bbox The bounding box
|
---|
225 | */
|
---|
226 | public void paint(Graphics2D g, MapView mv, Bounds bbox) {
|
---|
227 | if (mousePos == null) {
|
---|
228 | return;
|
---|
229 | }
|
---|
230 |
|
---|
231 | g.setColor(guideColor);
|
---|
232 |
|
---|
233 | if (state == State.selecting && targetWay != null) {
|
---|
234 | // Highlighting the targetWay in Selecting state
|
---|
235 | // Non-native highlighting is used, because sometimes highlighted
|
---|
236 | // segments are covered with others, which is bad.
|
---|
237 | g.setStroke(selectTargetWayStroke);
|
---|
238 |
|
---|
239 | List<Node> nodes = targetWay.getNodes();
|
---|
240 |
|
---|
241 | GeneralPath b = new GeneralPath();
|
---|
242 | Point p0 = mv.getPoint(nodes.get(0));
|
---|
243 | Point pn;
|
---|
244 | b.moveTo(p0.x, p0.y);
|
---|
245 |
|
---|
246 | for (Node n : nodes) {
|
---|
247 | pn = mv.getPoint(n);
|
---|
248 | b.lineTo(pn.x, pn.y);
|
---|
249 | }
|
---|
250 | if (targetWay.isClosed()) {
|
---|
251 | b.lineTo(p0.x, p0.y);
|
---|
252 | }
|
---|
253 |
|
---|
254 | g.draw(b);
|
---|
255 |
|
---|
256 | } else if (state == State.improving) {
|
---|
257 | // Drawing preview lines and highlighting the node
|
---|
258 | // that is going to be moved.
|
---|
259 | // Non-native highlighting is used here as well.
|
---|
260 |
|
---|
261 | // Finding endpoints
|
---|
262 | Point p1 = null, p2 = null;
|
---|
263 | if (ctrl && candidateSegment != null) {
|
---|
264 | g.setStroke(addNodeStroke);
|
---|
265 | p1 = mv.getPoint(candidateSegment.getFirstNode());
|
---|
266 | p2 = mv.getPoint(candidateSegment.getSecondNode());
|
---|
267 | } else if (!alt && !ctrl && candidateNode != null) {
|
---|
268 | g.setStroke(moveNodeStroke);
|
---|
269 | List<Pair<Node, Node>> wpps = targetWay.getNodePairs(false);
|
---|
270 | for (Pair<Node, Node> wpp : wpps) {
|
---|
271 | if (wpp.a == candidateNode) {
|
---|
272 | p1 = mv.getPoint(wpp.b);
|
---|
273 | }
|
---|
274 | if (wpp.b == candidateNode) {
|
---|
275 | p2 = mv.getPoint(wpp.a);
|
---|
276 | }
|
---|
277 | if (p1 != null && p2 != null) {
|
---|
278 | break;
|
---|
279 | }
|
---|
280 | }
|
---|
281 | } else if (alt && !ctrl && candidateNode != null) {
|
---|
282 | g.setStroke(deleteNodeStroke);
|
---|
283 | List<Node> nodes = targetWay.getNodes();
|
---|
284 | int index = nodes.indexOf(candidateNode);
|
---|
285 |
|
---|
286 | // Only draw line if node is not first and/or last
|
---|
287 | if (index != 0 && index != (nodes.size() - 1)) {
|
---|
288 | p1 = mv.getPoint(nodes.get(index - 1));
|
---|
289 | p2 = mv.getPoint(nodes.get(index + 1));
|
---|
290 | }
|
---|
291 | // TODO: indicate what part that will be deleted? (for end nodes)
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | // Drawing preview lines
|
---|
296 | GeneralPath b = new GeneralPath();
|
---|
297 | if (alt && !ctrl) {
|
---|
298 | // In delete mode
|
---|
299 | if (p1 != null && p2 != null) {
|
---|
300 | b.moveTo(p1.x, p1.y);
|
---|
301 | b.lineTo(p2.x, p2.y);
|
---|
302 | }
|
---|
303 | } else {
|
---|
304 | // In add or move mode
|
---|
305 | if (p1 != null) {
|
---|
306 | b.moveTo(mousePos.x, mousePos.y);
|
---|
307 | b.lineTo(p1.x, p1.y);
|
---|
308 | }
|
---|
309 | if (p2 != null) {
|
---|
310 | b.moveTo(mousePos.x, mousePos.y);
|
---|
311 | b.lineTo(p2.x, p2.y);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | g.draw(b);
|
---|
315 |
|
---|
316 | // Highlighting candidateNode
|
---|
317 | if (candidateNode != null) {
|
---|
318 | p1 = mv.getPoint(candidateNode);
|
---|
319 | g.fillRect(p1.x - dotSize/2, p1.y - dotSize/2, dotSize, dotSize);
|
---|
320 | }
|
---|
321 |
|
---|
322 | if (!alt && !ctrl && candidateNode != null) {
|
---|
323 | b.reset();
|
---|
324 | drawIntersectingWayHelperLines(mv, b);
|
---|
325 | g.setStroke(moveNodeIntersectingStroke);
|
---|
326 | g.draw(b);
|
---|
327 | }
|
---|
328 |
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | protected void drawIntersectingWayHelperLines(MapView mv, GeneralPath b) {
|
---|
333 | for (final OsmPrimitive referrer : candidateNode.getReferrers()) {
|
---|
334 | if (!(referrer instanceof Way) || targetWay.equals(referrer)) {
|
---|
335 | continue;
|
---|
336 | }
|
---|
337 | final List<Node> nodes = ((Way) referrer).getNodes();
|
---|
338 | for (int i = 0; i < nodes.size(); i++) {
|
---|
339 | if (!candidateNode.equals(nodes.get(i))) {
|
---|
340 | continue;
|
---|
341 | }
|
---|
342 | if (i > 0) {
|
---|
343 | final Point p = mv.getPoint(nodes.get(i - 1));
|
---|
344 | b.moveTo(mousePos.x, mousePos.y);
|
---|
345 | b.lineTo(p.x, p.y);
|
---|
346 | }
|
---|
347 | if (i < nodes.size() - 1) {
|
---|
348 | final Point p = mv.getPoint(nodes.get(i + 1));
|
---|
349 | b.moveTo(mousePos.x, mousePos.y);
|
---|
350 | b.lineTo(p.x, p.y);
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | // -------------------------------------------------------------------------
|
---|
357 | // Event handlers
|
---|
358 | // -------------------------------------------------------------------------
|
---|
359 | @Override
|
---|
360 | public void modifiersChanged(int modifiers) {
|
---|
361 | if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable()) {
|
---|
362 | return;
|
---|
363 | }
|
---|
364 | updateKeyModifiers(modifiers);
|
---|
365 | updateCursorDependentObjectsIfNeeded();
|
---|
366 | updateCursor();
|
---|
367 | updateStatusLine();
|
---|
368 | temporaryLayer.invalidate();
|
---|
369 | }
|
---|
370 |
|
---|
371 | @Override
|
---|
372 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
373 | if (selectionChangedBlocked) {
|
---|
374 | return;
|
---|
375 | }
|
---|
376 | updateStateByCurrentSelection();
|
---|
377 | }
|
---|
378 |
|
---|
379 | @Override
|
---|
380 | public void mouseDragged(MouseEvent e) {
|
---|
381 | dragging = true;
|
---|
382 | mouseMoved(e);
|
---|
383 | }
|
---|
384 |
|
---|
385 | @Override
|
---|
386 | public void mouseMoved(MouseEvent e) {
|
---|
387 | if (!isEnabled()) {
|
---|
388 | return;
|
---|
389 | }
|
---|
390 |
|
---|
391 | mousePos = e.getPoint();
|
---|
392 |
|
---|
393 | updateKeyModifiers(e);
|
---|
394 | updateCursorDependentObjectsIfNeeded();
|
---|
395 | updateCursor();
|
---|
396 | updateStatusLine();
|
---|
397 | temporaryLayer.invalidate();
|
---|
398 | }
|
---|
399 |
|
---|
400 | @Override
|
---|
401 | public void mouseReleased(MouseEvent e) {
|
---|
402 | dragging = false;
|
---|
403 | if (!isEnabled() || e.getButton() != MouseEvent.BUTTON1) {
|
---|
404 | return;
|
---|
405 | }
|
---|
406 |
|
---|
407 | updateKeyModifiers(e);
|
---|
408 | mousePos = e.getPoint();
|
---|
409 |
|
---|
410 | if (state == State.selecting) {
|
---|
411 | if (targetWay != null) {
|
---|
412 | getCurrentDataSet().setSelected(targetWay.getPrimitiveId());
|
---|
413 | updateStateByCurrentSelection();
|
---|
414 | }
|
---|
415 | } else if (state == State.improving && mousePos != null) {
|
---|
416 | // Checking if the new coordinate is outside of the world
|
---|
417 | if (mv.getLatLon(mousePos.x, mousePos.y).isOutSideWorld()) {
|
---|
418 | JOptionPane.showMessageDialog(Main.parent,
|
---|
419 | tr("Cannot add a node outside of the world."),
|
---|
420 | tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
421 | return;
|
---|
422 | }
|
---|
423 |
|
---|
424 | if (ctrl && !alt && candidateSegment != null) {
|
---|
425 | // Adding a new node to the highlighted segment
|
---|
426 | // Important: If there are other ways containing the same
|
---|
427 | // segment, a node must added to all of that ways.
|
---|
428 | Collection<Command> virtualCmds = new LinkedList<>();
|
---|
429 |
|
---|
430 | // Creating a new node
|
---|
431 | Node virtualNode = new Node(mv.getEastNorth(mousePos.x,
|
---|
432 | mousePos.y));
|
---|
433 | virtualCmds.add(new AddCommand(virtualNode));
|
---|
434 |
|
---|
435 | // Looking for candidateSegment copies in ways that are
|
---|
436 | // referenced
|
---|
437 | // by candidateSegment nodes
|
---|
438 | List<Way> firstNodeWays = OsmPrimitive.getFilteredList(
|
---|
439 | candidateSegment.getFirstNode().getReferrers(),
|
---|
440 | Way.class);
|
---|
441 | List<Way> secondNodeWays = OsmPrimitive.getFilteredList(
|
---|
442 | candidateSegment.getFirstNode().getReferrers(),
|
---|
443 | Way.class);
|
---|
444 |
|
---|
445 | Collection<WaySegment> virtualSegments = new LinkedList<>();
|
---|
446 | for (Way w : firstNodeWays) {
|
---|
447 | List<Pair<Node, Node>> wpps = w.getNodePairs(true);
|
---|
448 | for (Way w2 : secondNodeWays) {
|
---|
449 | if (!w.equals(w2)) {
|
---|
450 | continue;
|
---|
451 | }
|
---|
452 | // A way is referenced in both nodes.
|
---|
453 | // Checking if there is such segment
|
---|
454 | int i = -1;
|
---|
455 | for (Pair<Node, Node> wpp : wpps) {
|
---|
456 | ++i;
|
---|
457 | boolean ab = wpp.a.equals(candidateSegment.getFirstNode())
|
---|
458 | && wpp.b.equals(candidateSegment.getSecondNode());
|
---|
459 | boolean ba = wpp.b.equals(candidateSegment.getFirstNode())
|
---|
460 | && wpp.a.equals(candidateSegment.getSecondNode());
|
---|
461 | if (ab || ba) {
|
---|
462 | virtualSegments.add(new WaySegment(w, i));
|
---|
463 | }
|
---|
464 | }
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | // Adding the node to all segments found
|
---|
469 | for (WaySegment virtualSegment : virtualSegments) {
|
---|
470 | Way w = virtualSegment.way;
|
---|
471 | Way wnew = new Way(w);
|
---|
472 | wnew.addNode(virtualSegment.lowerIndex + 1, virtualNode);
|
---|
473 | virtualCmds.add(new ChangeCommand(w, wnew));
|
---|
474 | }
|
---|
475 |
|
---|
476 | // Finishing the sequence command
|
---|
477 | String text = trn("Add a new node to way",
|
---|
478 | "Add a new node to {0} ways",
|
---|
479 | virtualSegments.size(), virtualSegments.size());
|
---|
480 |
|
---|
481 | Main.main.undoRedo.add(new SequenceCommand(text, virtualCmds));
|
---|
482 |
|
---|
483 | } else if (alt && !ctrl && candidateNode != null) {
|
---|
484 | // Deleting the highlighted node
|
---|
485 |
|
---|
486 | //check to see if node is in use by more than one object
|
---|
487 | List<OsmPrimitive> referrers = candidateNode.getReferrers();
|
---|
488 | List<Way> ways = OsmPrimitive.getFilteredList(referrers, Way.class);
|
---|
489 | if (referrers.size() != 1 || ways.size() != 1) {
|
---|
490 | // detach node from way
|
---|
491 | final Way newWay = new Way(targetWay);
|
---|
492 | final List<Node> nodes = newWay.getNodes();
|
---|
493 | nodes.remove(candidateNode);
|
---|
494 | newWay.setNodes(nodes);
|
---|
495 | Main.main.undoRedo.add(new ChangeCommand(targetWay, newWay));
|
---|
496 | } else if (candidateNode.isTagged()) {
|
---|
497 | JOptionPane.showMessageDialog(Main.parent,
|
---|
498 | tr("Cannot delete node that has tags"),
|
---|
499 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
500 | } else {
|
---|
501 | List<Node> nodeList = new ArrayList<>();
|
---|
502 | nodeList.add(candidateNode);
|
---|
503 | Command deleteCmd = DeleteCommand.delete(getEditLayer(), nodeList, true);
|
---|
504 | if (deleteCmd != null) {
|
---|
505 | Main.main.undoRedo.add(deleteCmd);
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 |
|
---|
510 | } else if (candidateNode != null) {
|
---|
511 | // Moving the highlighted node
|
---|
512 | EastNorth nodeEN = candidateNode.getEastNorth();
|
---|
513 | EastNorth cursorEN = mv.getEastNorth(mousePos.x, mousePos.y);
|
---|
514 |
|
---|
515 | Main.main.undoRedo.add(new MoveCommand(candidateNode, cursorEN.east() - nodeEN.east(), cursorEN.north()
|
---|
516 | - nodeEN.north()));
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | mousePos = null;
|
---|
521 | updateCursor();
|
---|
522 | updateStatusLine();
|
---|
523 | temporaryLayer.invalidate();
|
---|
524 | }
|
---|
525 |
|
---|
526 | @Override
|
---|
527 | public void mouseExited(MouseEvent e) {
|
---|
528 | if (!isEnabled()) {
|
---|
529 | return;
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (!dragging) {
|
---|
533 | mousePos = null;
|
---|
534 | }
|
---|
535 | temporaryLayer.invalidate();
|
---|
536 | }
|
---|
537 |
|
---|
538 | // -------------------------------------------------------------------------
|
---|
539 | // Custom methods
|
---|
540 | // -------------------------------------------------------------------------
|
---|
541 | /**
|
---|
542 | * Sets new cursor depending on state, mouse position
|
---|
543 | */
|
---|
544 | private void updateCursor() {
|
---|
545 | if (!isEnabled()) {
|
---|
546 | mv.setNewCursor(null, this);
|
---|
547 | return;
|
---|
548 | }
|
---|
549 |
|
---|
550 | if (state == State.selecting) {
|
---|
551 | mv.setNewCursor(targetWay == null ? cursorSelect
|
---|
552 | : cursorSelectHover, this);
|
---|
553 | } else if (state == State.improving) {
|
---|
554 | if (alt && !ctrl) {
|
---|
555 | mv.setNewCursor(cursorImproveDelete, this);
|
---|
556 | } else if (shift || dragging) {
|
---|
557 | if (ctrl) {
|
---|
558 | mv.setNewCursor(cursorImproveAddLock, this);
|
---|
559 | } else {
|
---|
560 | mv.setNewCursor(cursorImproveLock, this);
|
---|
561 | }
|
---|
562 | } else if (ctrl && !alt) {
|
---|
563 | mv.setNewCursor(cursorImproveAdd, this);
|
---|
564 | } else {
|
---|
565 | mv.setNewCursor(cursorImprove, this);
|
---|
566 | }
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Updates these objects under cursor: targetWay, candidateNode,
|
---|
572 | * candidateSegment
|
---|
573 | */
|
---|
574 | public void updateCursorDependentObjectsIfNeeded() {
|
---|
575 | if (state == State.improving && (shift || dragging)
|
---|
576 | && !(candidateNode == null && candidateSegment == null)) {
|
---|
577 | return;
|
---|
578 | }
|
---|
579 |
|
---|
580 | if (mousePos == null) {
|
---|
581 | candidateNode = null;
|
---|
582 | candidateSegment = null;
|
---|
583 | return;
|
---|
584 | }
|
---|
585 |
|
---|
586 | if (state == State.selecting) {
|
---|
587 | targetWay = ImproveWayAccuracyHelper.findWay(mv, mousePos);
|
---|
588 | } else if (state == State.improving) {
|
---|
589 | if (ctrl && !alt) {
|
---|
590 | candidateSegment = ImproveWayAccuracyHelper.findCandidateSegment(mv,
|
---|
591 | targetWay, mousePos);
|
---|
592 | candidateNode = null;
|
---|
593 | } else {
|
---|
594 | candidateNode = ImproveWayAccuracyHelper.findCandidateNode(mv,
|
---|
595 | targetWay, mousePos);
|
---|
596 | candidateSegment = null;
|
---|
597 | }
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | /**
|
---|
602 | * Switches to Selecting state
|
---|
603 | */
|
---|
604 | public void startSelecting() {
|
---|
605 | state = State.selecting;
|
---|
606 |
|
---|
607 | targetWay = null;
|
---|
608 |
|
---|
609 | temporaryLayer.invalidate();
|
---|
610 | updateStatusLine();
|
---|
611 | }
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Switches to Improving state
|
---|
615 | *
|
---|
616 | * @param targetWay Way that is going to be improved
|
---|
617 | */
|
---|
618 | public void startImproving(Way targetWay) {
|
---|
619 | state = State.improving;
|
---|
620 |
|
---|
621 | Collection<OsmPrimitive> currentSelection = getCurrentDataSet().getSelected();
|
---|
622 | if (currentSelection.size() != 1
|
---|
623 | || !currentSelection.iterator().next().equals(targetWay)) {
|
---|
624 | selectionChangedBlocked = true;
|
---|
625 | getCurrentDataSet().clearSelection();
|
---|
626 | getCurrentDataSet().setSelected(targetWay.getPrimitiveId());
|
---|
627 | selectionChangedBlocked = false;
|
---|
628 | }
|
---|
629 |
|
---|
630 | this.targetWay = targetWay;
|
---|
631 | this.candidateNode = null;
|
---|
632 | this.candidateSegment = null;
|
---|
633 |
|
---|
634 | temporaryLayer.invalidate();
|
---|
635 | updateStatusLine();
|
---|
636 | }
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Updates the state according to the current selection. Goes to Improve
|
---|
640 | * state if a single way or node is selected. Extracts a way by a node in
|
---|
641 | * the second case.
|
---|
642 | *
|
---|
643 | */
|
---|
644 | private void updateStateByCurrentSelection() {
|
---|
645 | final List<Node> nodeList = new ArrayList<>();
|
---|
646 | final List<Way> wayList = new ArrayList<>();
|
---|
647 | final Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
648 |
|
---|
649 | // Collecting nodes and ways from the selection
|
---|
650 | for (OsmPrimitive p : sel) {
|
---|
651 | if (p instanceof Way) {
|
---|
652 | wayList.add((Way) p);
|
---|
653 | }
|
---|
654 | if (p instanceof Node) {
|
---|
655 | nodeList.add((Node) p);
|
---|
656 | }
|
---|
657 | }
|
---|
658 |
|
---|
659 | if (wayList.size() == 1) {
|
---|
660 | // Starting improving the single selected way
|
---|
661 | startImproving(wayList.get(0));
|
---|
662 | return;
|
---|
663 | } else if (nodeList.size() == 1) {
|
---|
664 | // Starting improving the only way of the single selected node
|
---|
665 | List<OsmPrimitive> r = nodeList.get(0).getReferrers();
|
---|
666 | if (r.size() == 1 && (r.get(0) instanceof Way)) {
|
---|
667 | startImproving((Way) r.get(0));
|
---|
668 | return;
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | // Starting selecting by default
|
---|
673 | startSelecting();
|
---|
674 | }
|
---|
675 | }
|
---|