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.BasicStroke;
|
---|
9 | import java.awt.Color;
|
---|
10 | import java.awt.Cursor;
|
---|
11 | import java.awt.Graphics2D;
|
---|
12 | import java.awt.Point;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.awt.event.MouseEvent;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.Collections;
|
---|
18 | import java.util.HashSet;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Set;
|
---|
22 |
|
---|
23 | import javax.swing.JOptionPane;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.command.AddCommand;
|
---|
26 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
---|
27 | import org.openstreetmap.josm.command.Command;
|
---|
28 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
29 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
30 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
31 | import org.openstreetmap.josm.data.Bounds;
|
---|
32 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
33 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
34 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
35 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
36 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
37 | import org.openstreetmap.josm.data.osm.Node;
|
---|
38 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
39 | import org.openstreetmap.josm.data.osm.Way;
|
---|
40 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
41 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
42 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
---|
43 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
---|
44 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
|
---|
45 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
|
---|
46 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
47 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
48 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
49 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
50 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
51 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
52 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
53 | import org.openstreetmap.josm.data.preferences.CachingProperty;
|
---|
54 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
55 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
56 | import org.openstreetmap.josm.data.preferences.StrokeProperty;
|
---|
57 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
58 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
59 | import org.openstreetmap.josm.gui.MapView;
|
---|
60 | import org.openstreetmap.josm.gui.draw.MapViewPath;
|
---|
61 | import org.openstreetmap.josm.gui.draw.SymbolShape;
|
---|
62 | import org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable;
|
---|
63 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
64 | import org.openstreetmap.josm.gui.util.ModifierExListener;
|
---|
65 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
66 | import org.openstreetmap.josm.tools.Logging;
|
---|
67 | import org.openstreetmap.josm.tools.Pair;
|
---|
68 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * A special map mode that is optimized for improving way geometry.
|
---|
72 | * (by efficiently moving, adding and deleting way-nodes)
|
---|
73 | *
|
---|
74 | * @author Alexander Kachkaev <alexander@kachkaev.ru>, 2011
|
---|
75 | */
|
---|
76 | public class ImproveWayAccuracyAction extends MapMode implements DataSelectionListener, DataSetListener, ModifierExListener {
|
---|
77 |
|
---|
78 | private static final String CROSSHAIR = /* ICON(cursor/)*/ "crosshair";
|
---|
79 |
|
---|
80 | enum State {
|
---|
81 | SELECTING, IMPROVING
|
---|
82 | }
|
---|
83 |
|
---|
84 | private State state;
|
---|
85 |
|
---|
86 | private MapView mv;
|
---|
87 |
|
---|
88 | private static final long serialVersionUID = 42L;
|
---|
89 |
|
---|
90 | private transient Way targetWay;
|
---|
91 | private transient Node candidateNode;
|
---|
92 | private transient WaySegment candidateSegment;
|
---|
93 |
|
---|
94 | private Point mousePos;
|
---|
95 | private boolean dragging;
|
---|
96 |
|
---|
97 | private final Cursor cursorSelect = ImageProvider.getCursor(/* ICON(cursor/)*/ "normal", /* ICON(cursor/modifier/)*/ "mode");
|
---|
98 | private final Cursor cursorSelectHover = ImageProvider.getCursor(/* ICON(cursor/)*/ "hand", /* ICON(cursor/modifier/)*/ "mode");
|
---|
99 | private final Cursor cursorImprove = ImageProvider.getCursor(CROSSHAIR, null);
|
---|
100 | private final Cursor cursorImproveAdd = ImageProvider.getCursor(CROSSHAIR, /* ICON(cursor/modifier/)*/ "addnode");
|
---|
101 | private final Cursor cursorImproveDelete = ImageProvider.getCursor(CROSSHAIR, /* ICON(cursor/modifier/)*/ "delete_node");
|
---|
102 | private final Cursor cursorImproveAddLock = ImageProvider.getCursor(CROSSHAIR, /* ICON(cursor/modifier/)*/ "add_node_lock");
|
---|
103 | private final Cursor cursorImproveLock = ImageProvider.getCursor(CROSSHAIR, /* ICON(cursor/modifier/)*/ "lock");
|
---|
104 |
|
---|
105 | private Color guideColor;
|
---|
106 |
|
---|
107 | private static final CachingProperty<BasicStroke> SELECT_TARGET_WAY_STROKE
|
---|
108 | = new StrokeProperty("improvewayaccuracy.stroke.select-target", "2").cached();
|
---|
109 | private static final CachingProperty<BasicStroke> MOVE_NODE_STROKE
|
---|
110 | = new StrokeProperty("improvewayaccuracy.stroke.move-node", "1 6").cached();
|
---|
111 | private static final CachingProperty<BasicStroke> MOVE_NODE_INTERSECTING_STROKE
|
---|
112 | = new StrokeProperty("improvewayaccuracy.stroke.move-node-intersecting", "1 2 6").cached();
|
---|
113 | private static final CachingProperty<BasicStroke> ADD_NODE_STROKE
|
---|
114 | = new StrokeProperty("improvewayaccuracy.stroke.add-node", "1").cached();
|
---|
115 | private static final CachingProperty<BasicStroke> DELETE_NODE_STROKE
|
---|
116 | = new StrokeProperty("improvewayaccuracy.stroke.delete-node", "1").cached();
|
---|
117 | private static final CachingProperty<Integer> DOT_SIZE
|
---|
118 | = new IntegerProperty("improvewayaccuracy.dot-size", 6).cached();
|
---|
119 |
|
---|
120 | private boolean selectionChangedBlocked;
|
---|
121 |
|
---|
122 | protected String oldModeHelpText;
|
---|
123 |
|
---|
124 | private final transient AbstractMapViewPaintable temporaryLayer = new AbstractMapViewPaintable() {
|
---|
125 | @Override
|
---|
126 | public void paint(Graphics2D g, MapView mv, Bounds bbox) {
|
---|
127 | ImproveWayAccuracyAction.this.paint(g, mv, bbox);
|
---|
128 | }
|
---|
129 | };
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Constructs a new {@code ImproveWayAccuracyAction}.
|
---|
133 | * @since 11713
|
---|
134 | */
|
---|
135 | public ImproveWayAccuracyAction() {
|
---|
136 | super(tr("Improve Way Accuracy"), "improvewayaccuracy",
|
---|
137 | tr("Improve Way Accuracy mode"),
|
---|
138 | Shortcut.registerShortcut("mapmode:ImproveWayAccuracy",
|
---|
139 | tr("Mode: {0}", tr("Improve Way Accuracy")),
|
---|
140 | KeyEvent.VK_W, Shortcut.DIRECT), Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
|
---|
141 |
|
---|
142 | readPreferences();
|
---|
143 | }
|
---|
144 |
|
---|
145 | // -------------------------------------------------------------------------
|
---|
146 | // Mode methods
|
---|
147 | // -------------------------------------------------------------------------
|
---|
148 | @Override
|
---|
149 | public void enterMode() {
|
---|
150 | if (!isEnabled()) {
|
---|
151 | return;
|
---|
152 | }
|
---|
153 | super.enterMode();
|
---|
154 | readPreferences();
|
---|
155 |
|
---|
156 | MapFrame map = MainApplication.getMap();
|
---|
157 | mv = map.mapView;
|
---|
158 | mousePos = null;
|
---|
159 | oldModeHelpText = "";
|
---|
160 |
|
---|
161 | if (getLayerManager().getEditDataSet() == null) {
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | updateStateByCurrentSelection();
|
---|
166 |
|
---|
167 | map.mapView.addMouseListener(this);
|
---|
168 | map.mapView.addMouseMotionListener(this);
|
---|
169 | map.mapView.addTemporaryLayer(temporaryLayer);
|
---|
170 | SelectionEventManager.getInstance().addSelectionListener(this);
|
---|
171 | DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IMMEDIATELY);
|
---|
172 |
|
---|
173 | map.keyDetector.addModifierExListener(this);
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | protected void readPreferences() {
|
---|
178 | guideColor = new NamedColorProperty(marktr("improve way accuracy helper line"), Color.RED).get();
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | public void exitMode() {
|
---|
183 | super.exitMode();
|
---|
184 |
|
---|
185 | MapFrame map = MainApplication.getMap();
|
---|
186 | map.mapView.removeMouseListener(this);
|
---|
187 | map.mapView.removeMouseMotionListener(this);
|
---|
188 | map.mapView.removeTemporaryLayer(temporaryLayer);
|
---|
189 | SelectionEventManager.getInstance().removeSelectionListener(this);
|
---|
190 | DatasetEventManager.getInstance().removeDatasetListener(this);
|
---|
191 |
|
---|
192 | map.keyDetector.removeModifierExListener(this);
|
---|
193 | temporaryLayer.invalidate();
|
---|
194 | targetWay = null;
|
---|
195 | candidateNode = null;
|
---|
196 | candidateSegment = null;
|
---|
197 | }
|
---|
198 |
|
---|
199 | @Override
|
---|
200 | protected void updateStatusLine() {
|
---|
201 | String newModeHelpText = getModeHelpText();
|
---|
202 | if (!newModeHelpText.equals(oldModeHelpText)) {
|
---|
203 | oldModeHelpText = newModeHelpText;
|
---|
204 | MapFrame map = MainApplication.getMap();
|
---|
205 | map.statusLine.setHelpText(newModeHelpText);
|
---|
206 | map.statusLine.repaint();
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | public String getModeHelpText() {
|
---|
212 | if (state == State.SELECTING) {
|
---|
213 | if (targetWay != null) {
|
---|
214 | return tr("Click on the way to start improving its shape.");
|
---|
215 | } else {
|
---|
216 | return tr("Select a way that you want to make more accurate.");
|
---|
217 | }
|
---|
218 | } else {
|
---|
219 | if (ctrl) {
|
---|
220 | return tr("Click to add a new node. Release Ctrl to move existing nodes or hold Alt to delete.");
|
---|
221 | } else if (alt) {
|
---|
222 | return tr("Click to delete the highlighted node. Release Alt to move existing nodes or hold Ctrl to add new nodes.");
|
---|
223 | } else {
|
---|
224 | return tr("Click to move the highlighted node. Hold Ctrl to add new nodes, or Alt to delete.");
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | @Override
|
---|
230 | public boolean layerIsSupported(Layer l) {
|
---|
231 | return isEditableDataLayer(l);
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Override
|
---|
235 | protected void updateEnabledState() {
|
---|
236 | setEnabled(getLayerManager().getEditLayer() != null);
|
---|
237 | }
|
---|
238 |
|
---|
239 | // -------------------------------------------------------------------------
|
---|
240 | // MapViewPaintable methods
|
---|
241 | // -------------------------------------------------------------------------
|
---|
242 | /**
|
---|
243 | * Redraws temporary layer. Highlights targetWay in select mode. Draws
|
---|
244 | * preview lines in improve mode and highlights the candidateNode
|
---|
245 | * @param g The graphics
|
---|
246 | * @param mv The map view
|
---|
247 | * @param bbox The bounding box
|
---|
248 | */
|
---|
249 | public void paint(Graphics2D g, MapView mv, Bounds bbox) {
|
---|
250 | if (mousePos == null || (candidateNode != null && candidateNode.getDataSet() == null)) {
|
---|
251 | return;
|
---|
252 | }
|
---|
253 |
|
---|
254 | g.setColor(guideColor);
|
---|
255 |
|
---|
256 | if (state == State.SELECTING && targetWay != null) {
|
---|
257 | // Highlighting the targetWay in Selecting state
|
---|
258 | // Non-native highlighting is used, because sometimes highlighted
|
---|
259 | // segments are covered with others, which is bad.
|
---|
260 | BasicStroke stroke = SELECT_TARGET_WAY_STROKE.get();
|
---|
261 | g.setStroke(stroke);
|
---|
262 |
|
---|
263 | List<Node> nodes = targetWay.getNodes();
|
---|
264 |
|
---|
265 | g.draw(new MapViewPath(mv).append(nodes, false).computeClippedLine(stroke));
|
---|
266 |
|
---|
267 | } else if (state == State.IMPROVING) {
|
---|
268 | // Drawing preview lines and highlighting the node
|
---|
269 | // that is going to be moved.
|
---|
270 | // Non-native highlighting is used here as well.
|
---|
271 |
|
---|
272 | if (ctrl && candidateSegment != null) {
|
---|
273 | paintAddNodeStroke(g);
|
---|
274 | } else if (!alt && !ctrl && candidateNode != null) {
|
---|
275 | paintMoveNodeStroke(g);
|
---|
276 | } else if (alt && !ctrl && candidateNode != null) {
|
---|
277 | paintDeleteStroke(g);
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Paint the add stroke for {@link State#IMPROVING}
|
---|
284 | * @param g The graphics
|
---|
285 | */
|
---|
286 | private void paintAddNodeStroke(Graphics2D g) {
|
---|
287 | g.setStroke(ADD_NODE_STROKE.get());
|
---|
288 | ILatLon p1 = null;
|
---|
289 | ILatLon p2 = null;
|
---|
290 | try {
|
---|
291 | p1 = candidateSegment.getFirstNode();
|
---|
292 | p2 = candidateSegment.getSecondNode();
|
---|
293 | } catch (ArrayIndexOutOfBoundsException e) {
|
---|
294 | Logging.error(e);
|
---|
295 | }
|
---|
296 | paintImprovingPreviewLines(g, p1, p2);
|
---|
297 | }
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Paint the move stroke for {@link State#IMPROVING}
|
---|
301 | * @param g The graphics
|
---|
302 | */
|
---|
303 | private void paintMoveNodeStroke(Graphics2D g) {
|
---|
304 | g.setStroke(MOVE_NODE_STROKE.get());
|
---|
305 | List<Pair<Node, Node>> wpps = targetWay.getNodePairs(false);
|
---|
306 | ILatLon p1 = null;
|
---|
307 | ILatLon p2 = null;
|
---|
308 | for (Pair<Node, Node> wpp : wpps) {
|
---|
309 | if (wpp.a == candidateNode) {
|
---|
310 | p1 = wpp.b;
|
---|
311 | }
|
---|
312 | if (wpp.b == candidateNode) {
|
---|
313 | p2 = wpp.a;
|
---|
314 | }
|
---|
315 | if (p1 != null && p2 != null) {
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | paintImprovingPreviewLines(g, p1, p2);
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Paint the delete stroke for {@link State#IMPROVING}
|
---|
324 | * @param g The graphics
|
---|
325 | */
|
---|
326 | private void paintDeleteStroke(Graphics2D g) {
|
---|
327 | g.setStroke(DELETE_NODE_STROKE.get());
|
---|
328 | List<Node> nodes = targetWay.getNodes();
|
---|
329 | int index = nodes.indexOf(candidateNode);
|
---|
330 | ILatLon p1 = null;
|
---|
331 | ILatLon p2 = null;
|
---|
332 | // Only draw line if node is not first and/or last
|
---|
333 | if (index > 0 && index < (nodes.size() - 1)) {
|
---|
334 | p1 = nodes.get(index - 1);
|
---|
335 | p2 = nodes.get(index + 1);
|
---|
336 | } else if (targetWay.isClosed()) {
|
---|
337 | p1 = targetWay.getNode(1);
|
---|
338 | p2 = targetWay.getNode(nodes.size() - 2);
|
---|
339 | }
|
---|
340 | paintImprovingPreviewLines(g, p1, p2);
|
---|
341 | // TODO: indicate what part that will be deleted? (for end nodes)
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Paint the preview lines for {@link State#IMPROVING}
|
---|
346 | * @param g The graphics
|
---|
347 | * @param p1 The first endpoint
|
---|
348 | * @param p2 The second endpoint
|
---|
349 | */
|
---|
350 | private void paintImprovingPreviewLines(Graphics2D g, ILatLon p1, ILatLon p2) {
|
---|
351 | // Drawing preview lines
|
---|
352 | MapViewPath b = new MapViewPath(mv);
|
---|
353 | if (alt && !ctrl) {
|
---|
354 | // In delete mode
|
---|
355 | if (p1 != null && p2 != null) {
|
---|
356 | b.moveTo(p1);
|
---|
357 | b.lineTo(p2);
|
---|
358 | }
|
---|
359 | } else {
|
---|
360 | // In add or move mode
|
---|
361 | if (p1 != null) {
|
---|
362 | b.moveTo(mousePos.x, mousePos.y);
|
---|
363 | b.lineTo(p1);
|
---|
364 | }
|
---|
365 | if (p2 != null) {
|
---|
366 | b.moveTo(mousePos.x, mousePos.y);
|
---|
367 | b.lineTo(p2);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | g.draw(b.computeClippedLine(g.getStroke()));
|
---|
371 |
|
---|
372 | // Highlighting candidateNode
|
---|
373 | if (candidateNode != null) {
|
---|
374 | p1 = candidateNode;
|
---|
375 | g.fill(new MapViewPath(mv).shapeAround(p1, SymbolShape.SQUARE, DOT_SIZE.get()));
|
---|
376 | }
|
---|
377 |
|
---|
378 | if (!alt && !ctrl && candidateNode != null) {
|
---|
379 | b.reset();
|
---|
380 | drawIntersectingWayHelperLines(mv, b);
|
---|
381 | g.setStroke(MOVE_NODE_INTERSECTING_STROKE.get());
|
---|
382 | g.draw(b.computeClippedLine(g.getStroke()));
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | protected void drawIntersectingWayHelperLines(MapView mv, MapViewPath b) {
|
---|
387 | for (final OsmPrimitive referrer : candidateNode.getReferrers()) {
|
---|
388 | if (!(referrer instanceof Way) || targetWay.equals(referrer)) {
|
---|
389 | continue;
|
---|
390 | }
|
---|
391 | final List<Node> nodes = ((Way) referrer).getNodes();
|
---|
392 | for (int i = 0; i < nodes.size(); i++) {
|
---|
393 | if (!candidateNode.equals(nodes.get(i))) {
|
---|
394 | continue;
|
---|
395 | }
|
---|
396 | if (i > 0) {
|
---|
397 | b.moveTo(mousePos.x, mousePos.y);
|
---|
398 | b.lineTo(nodes.get(i - 1));
|
---|
399 | }
|
---|
400 | if (i < nodes.size() - 1) {
|
---|
401 | b.moveTo(mousePos.x, mousePos.y);
|
---|
402 | b.lineTo(nodes.get(i + 1));
|
---|
403 | }
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | // -------------------------------------------------------------------------
|
---|
409 | // Event handlers
|
---|
410 | // -------------------------------------------------------------------------
|
---|
411 | @Override
|
---|
412 | public void modifiersExChanged(int modifiers) {
|
---|
413 | if (!MainApplication.isDisplayingMapView() || !MainApplication.getMap().mapView.isActiveLayerDrawable()) {
|
---|
414 | return;
|
---|
415 | }
|
---|
416 | updateKeyModifiersEx(modifiers);
|
---|
417 | updateCursorDependentObjectsIfNeeded();
|
---|
418 | updateCursor();
|
---|
419 | updateStatusLine();
|
---|
420 | temporaryLayer.invalidate();
|
---|
421 | }
|
---|
422 |
|
---|
423 | @Override
|
---|
424 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
425 | if (selectionChangedBlocked) {
|
---|
426 | return;
|
---|
427 | }
|
---|
428 | updateStateByCurrentSelection();
|
---|
429 | }
|
---|
430 |
|
---|
431 | @Override
|
---|
432 | public void mouseDragged(MouseEvent e) {
|
---|
433 | dragging = true;
|
---|
434 | mouseMoved(e);
|
---|
435 | }
|
---|
436 |
|
---|
437 | @Override
|
---|
438 | public void mouseMoved(MouseEvent e) {
|
---|
439 | if (!isEnabled()) {
|
---|
440 | return;
|
---|
441 | }
|
---|
442 |
|
---|
443 | mousePos = e.getPoint();
|
---|
444 |
|
---|
445 | updateKeyModifiers(e);
|
---|
446 | updateCursorDependentObjectsIfNeeded();
|
---|
447 | updateCursor();
|
---|
448 | updateStatusLine();
|
---|
449 | temporaryLayer.invalidate();
|
---|
450 | }
|
---|
451 |
|
---|
452 | @Override
|
---|
453 | public void mouseReleased(MouseEvent e) {
|
---|
454 | dragging = false;
|
---|
455 | if (!isEnabled() || e.getButton() != MouseEvent.BUTTON1) {
|
---|
456 | return;
|
---|
457 | }
|
---|
458 |
|
---|
459 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
460 | updateKeyModifiers(e);
|
---|
461 | mousePos = e.getPoint();
|
---|
462 |
|
---|
463 | if (state == State.SELECTING) {
|
---|
464 | if (targetWay != null) {
|
---|
465 | ds.setSelected(targetWay.getPrimitiveId());
|
---|
466 | updateStateByCurrentSelection();
|
---|
467 | }
|
---|
468 | } else if (state == State.IMPROVING) {
|
---|
469 | // Checking if the new coordinate is outside of the world
|
---|
470 | if (new Node(mv.getEastNorth(mousePos.x, mousePos.y)).isOutSideWorld()) {
|
---|
471 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
---|
472 | tr("Cannot add a node outside of the world."),
|
---|
473 | tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
474 | return;
|
---|
475 | }
|
---|
476 |
|
---|
477 | if (ctrl && !alt && candidateSegment != null) {
|
---|
478 | addNode(ds);
|
---|
479 | } else if (alt && !ctrl && candidateNode != null) {
|
---|
480 | deleteHighlightedNode();
|
---|
481 | } else if (candidateNode != null) {
|
---|
482 | moveHighlightedNode();
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | mousePos = null;
|
---|
487 | updateCursor();
|
---|
488 | updateStatusLine();
|
---|
489 | temporaryLayer.invalidate();
|
---|
490 | }
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Add a new node to the currently highlighted segment
|
---|
494 | * @param ds The dataset to add the node to
|
---|
495 | */
|
---|
496 | private void addNode(DataSet ds) {
|
---|
497 | // Add a new node to the highlighted segment.
|
---|
498 | Collection<WaySegment> virtualSegments = new LinkedList<>();
|
---|
499 |
|
---|
500 | // Check if other ways have the same segment.
|
---|
501 | // We have to make sure that we add the new node to all of them.
|
---|
502 | Set<Way> commonParentWays = new HashSet<>(candidateSegment.getFirstNode().getParentWays());
|
---|
503 | commonParentWays.retainAll(candidateSegment.getSecondNode().getParentWays());
|
---|
504 | for (Way w : commonParentWays) {
|
---|
505 | for (int i = 0; i < w.getNodesCount() - 1; i++) {
|
---|
506 | WaySegment testWS = new WaySegment(w, i);
|
---|
507 | if (testWS.isSimilar(candidateSegment)) {
|
---|
508 | virtualSegments.add(testWS);
|
---|
509 | }
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | Collection<Command> virtualCmds = new LinkedList<>();
|
---|
514 | // Create the new node
|
---|
515 | Node virtualNode = new Node(mv.getEastNorth(mousePos.x, mousePos.y));
|
---|
516 | virtualCmds.add(new AddCommand(ds, virtualNode));
|
---|
517 |
|
---|
518 | // Adding the node to all segments found
|
---|
519 | for (WaySegment virtualSegment : virtualSegments) {
|
---|
520 | Way w = virtualSegment.getWay();
|
---|
521 | List<Node> modNodes = w.getNodes();
|
---|
522 | modNodes.add(virtualSegment.getUpperIndex(), virtualNode);
|
---|
523 | virtualCmds.add(new ChangeNodesCommand(w, modNodes));
|
---|
524 | }
|
---|
525 |
|
---|
526 | // Finishing the sequence command
|
---|
527 | String text = trn("Add a new node to way",
|
---|
528 | "Add a new node to {0} ways",
|
---|
529 | virtualSegments.size(), virtualSegments.size());
|
---|
530 |
|
---|
531 | UndoRedoHandler.getInstance().add(new SequenceCommand(text, virtualCmds));
|
---|
532 | }
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Delete the highlighted node
|
---|
536 | */
|
---|
537 | private void deleteHighlightedNode() {
|
---|
538 | // Deleting the highlighted node
|
---|
539 |
|
---|
540 | //check to see if node is in use by more than one object
|
---|
541 | long referrersCount = candidateNode.referrers(OsmPrimitive.class).count();
|
---|
542 | long referrerWayCount = candidateNode.referrers(Way.class).count();
|
---|
543 | if (referrersCount != 1 || referrerWayCount != 1) {
|
---|
544 | // detach node from way
|
---|
545 | final List<Node> nodes = targetWay.getNodes();
|
---|
546 | nodes.remove(candidateNode);
|
---|
547 | if (nodes.size() < 2) {
|
---|
548 | final Command deleteCmd = DeleteCommand.delete(Collections.singleton(targetWay), true);
|
---|
549 | if (deleteCmd != null) {
|
---|
550 | UndoRedoHandler.getInstance().add(deleteCmd);
|
---|
551 | }
|
---|
552 | } else {
|
---|
553 | UndoRedoHandler.getInstance().add(new ChangeNodesCommand(targetWay, nodes));
|
---|
554 | }
|
---|
555 | } else if (candidateNode.isTagged()) {
|
---|
556 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
---|
557 | tr("Cannot delete node that has tags"),
|
---|
558 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
559 | } else {
|
---|
560 | final Command deleteCmd = DeleteCommand.delete(Collections.singleton(candidateNode), true);
|
---|
561 | if (deleteCmd != null) {
|
---|
562 | UndoRedoHandler.getInstance().add(deleteCmd);
|
---|
563 | }
|
---|
564 | }
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Move the highlighted node
|
---|
569 | */
|
---|
570 | private void moveHighlightedNode() {
|
---|
571 | // Moving the highlighted node
|
---|
572 | EastNorth nodeEN = candidateNode.getEastNorth();
|
---|
573 | EastNorth cursorEN = mv.getEastNorth(mousePos.x, mousePos.y);
|
---|
574 |
|
---|
575 | UndoRedoHandler.getInstance().add(
|
---|
576 | new MoveCommand(candidateNode, cursorEN.east() - nodeEN.east(), cursorEN.north() - nodeEN.north()));
|
---|
577 |
|
---|
578 | SelectAction.checkCommandForLargeDistance(UndoRedoHandler.getInstance().getLastCommand());
|
---|
579 | }
|
---|
580 |
|
---|
581 | @Override
|
---|
582 | public void mouseExited(MouseEvent e) {
|
---|
583 | if (!isEnabled()) {
|
---|
584 | return;
|
---|
585 | }
|
---|
586 |
|
---|
587 | if (!dragging) {
|
---|
588 | mousePos = null;
|
---|
589 | }
|
---|
590 | temporaryLayer.invalidate();
|
---|
591 | }
|
---|
592 |
|
---|
593 | // -------------------------------------------------------------------------
|
---|
594 | // Custom methods
|
---|
595 | // -------------------------------------------------------------------------
|
---|
596 | /**
|
---|
597 | * Sets new cursor depending on state, mouse position
|
---|
598 | */
|
---|
599 | private void updateCursor() {
|
---|
600 | if (!isEnabled()) {
|
---|
601 | mv.setNewCursor(null, this);
|
---|
602 | return;
|
---|
603 | }
|
---|
604 |
|
---|
605 | if (state == State.SELECTING) {
|
---|
606 | mv.setNewCursor(targetWay == null ? cursorSelect
|
---|
607 | : cursorSelectHover, this);
|
---|
608 | } else if (state == State.IMPROVING) {
|
---|
609 | updateCursorImproving();
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Update the mouse cursor for the {@link State#IMPROVING} mode
|
---|
615 | */
|
---|
616 | private void updateCursorImproving() {
|
---|
617 | if (alt && !ctrl) {
|
---|
618 | mv.setNewCursor(cursorImproveDelete, this);
|
---|
619 | } else if (shift || dragging) {
|
---|
620 | if (ctrl) {
|
---|
621 | mv.setNewCursor(cursorImproveAddLock, this);
|
---|
622 | } else {
|
---|
623 | mv.setNewCursor(cursorImproveLock, this);
|
---|
624 | }
|
---|
625 | } else if (ctrl && !alt) {
|
---|
626 | mv.setNewCursor(cursorImproveAdd, this);
|
---|
627 | } else {
|
---|
628 | mv.setNewCursor(cursorImprove, this);
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * Updates these objects under cursor: targetWay, candidateNode,
|
---|
634 | * candidateSegment
|
---|
635 | */
|
---|
636 | public void updateCursorDependentObjectsIfNeeded() {
|
---|
637 | if (state == State.IMPROVING && (shift || dragging)
|
---|
638 | && !(candidateNode == null && candidateSegment == null)) {
|
---|
639 | return;
|
---|
640 | }
|
---|
641 |
|
---|
642 | if (mousePos == null) {
|
---|
643 | candidateNode = null;
|
---|
644 | candidateSegment = null;
|
---|
645 | return;
|
---|
646 | }
|
---|
647 |
|
---|
648 | if (state == State.SELECTING) {
|
---|
649 | targetWay = ImproveWayAccuracyHelper.findWay(mv, mousePos);
|
---|
650 | } else if (state == State.IMPROVING) {
|
---|
651 | if (ctrl && !alt) {
|
---|
652 | candidateSegment = ImproveWayAccuracyHelper.findCandidateSegment(mv,
|
---|
653 | targetWay, mousePos);
|
---|
654 | candidateNode = null;
|
---|
655 | } else {
|
---|
656 | candidateNode = ImproveWayAccuracyHelper.findCandidateNode(mv,
|
---|
657 | targetWay, mousePos);
|
---|
658 | candidateSegment = null;
|
---|
659 | }
|
---|
660 | }
|
---|
661 | }
|
---|
662 |
|
---|
663 | /**
|
---|
664 | * Switches to Selecting state
|
---|
665 | */
|
---|
666 | public void startSelecting() {
|
---|
667 | state = State.SELECTING;
|
---|
668 |
|
---|
669 | targetWay = null;
|
---|
670 |
|
---|
671 | temporaryLayer.invalidate();
|
---|
672 | updateStatusLine();
|
---|
673 | }
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Switches to Improving state
|
---|
677 | *
|
---|
678 | * @param targetWay Way that is going to be improved
|
---|
679 | */
|
---|
680 | public void startImproving(Way targetWay) {
|
---|
681 | state = State.IMPROVING;
|
---|
682 |
|
---|
683 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
684 | Collection<OsmPrimitive> currentSelection = ds.getSelected();
|
---|
685 | if (currentSelection.size() != 1
|
---|
686 | || !currentSelection.iterator().next().equals(targetWay)) {
|
---|
687 | selectionChangedBlocked = true;
|
---|
688 | ds.clearSelection();
|
---|
689 | ds.setSelected(targetWay.getPrimitiveId());
|
---|
690 | selectionChangedBlocked = false;
|
---|
691 | }
|
---|
692 |
|
---|
693 | this.targetWay = targetWay;
|
---|
694 | this.candidateNode = null;
|
---|
695 | this.candidateSegment = null;
|
---|
696 |
|
---|
697 | temporaryLayer.invalidate();
|
---|
698 | updateStatusLine();
|
---|
699 | }
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * Updates the state according to the current selection. Goes to Improve
|
---|
703 | * state if a single way or node is selected. Extracts a way by a node in
|
---|
704 | * the second case.
|
---|
705 | */
|
---|
706 | private void updateStateByCurrentSelection() {
|
---|
707 | final DataSet ds = getLayerManager().getEditDataSet();
|
---|
708 | if (ds != null && selectWay(ds)) {
|
---|
709 | return;
|
---|
710 | }
|
---|
711 |
|
---|
712 | // Starting selecting by default
|
---|
713 | startSelecting();
|
---|
714 | }
|
---|
715 |
|
---|
716 | /**
|
---|
717 | * Select the initial way
|
---|
718 | * @param ds The dataset to get the selection from
|
---|
719 | * @return {@code true} if a way was selected
|
---|
720 | */
|
---|
721 | private boolean selectWay(DataSet ds) {
|
---|
722 | final List<Node> nodeList = new ArrayList<>();
|
---|
723 | final List<Way> wayList = new ArrayList<>();
|
---|
724 | final Collection<OsmPrimitive> sel = ds.getSelected();
|
---|
725 |
|
---|
726 | // Collecting nodes and ways from the selection
|
---|
727 | for (OsmPrimitive p : sel) {
|
---|
728 | if (p instanceof Way) {
|
---|
729 | wayList.add((Way) p);
|
---|
730 | }
|
---|
731 | if (p instanceof Node) {
|
---|
732 | nodeList.add((Node) p);
|
---|
733 | }
|
---|
734 | }
|
---|
735 |
|
---|
736 | if (wayList.size() == 1) {
|
---|
737 | // Starting improving the single selected way
|
---|
738 | startImproving(wayList.get(0));
|
---|
739 | return true;
|
---|
740 | } else if (nodeList.size() == 1) {
|
---|
741 | // Starting improving the only way of the single selected node
|
---|
742 | List<OsmPrimitive> r = nodeList.get(0).getReferrers();
|
---|
743 | r.removeIf(osm -> !osm.isUsable());
|
---|
744 | if (r.size() == 1 && (r.get(0) instanceof Way)) {
|
---|
745 | startImproving((Way) r.get(0));
|
---|
746 | return true;
|
---|
747 | }
|
---|
748 | }
|
---|
749 | return false;
|
---|
750 | }
|
---|
751 |
|
---|
752 | @Override
|
---|
753 | public void primitivesRemoved(PrimitivesRemovedEvent event) {
|
---|
754 | if (event.getPrimitives().contains(candidateNode) || event.getPrimitives().contains(targetWay)) {
|
---|
755 | updateCursorDependentObjectsIfNeeded();
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 | @Override
|
---|
760 | public void primitivesAdded(PrimitivesAddedEvent event) {
|
---|
761 | // Do nothing
|
---|
762 | }
|
---|
763 |
|
---|
764 | @Override
|
---|
765 | public void tagsChanged(TagsChangedEvent event) {
|
---|
766 | // Do nothing
|
---|
767 | }
|
---|
768 |
|
---|
769 | @Override
|
---|
770 | public void nodeMoved(NodeMovedEvent event) {
|
---|
771 | // Do nothing
|
---|
772 | }
|
---|
773 |
|
---|
774 | @Override
|
---|
775 | public void wayNodesChanged(WayNodesChangedEvent event) {
|
---|
776 | // Do nothing
|
---|
777 | }
|
---|
778 |
|
---|
779 | @Override
|
---|
780 | public void relationMembersChanged(RelationMembersChangedEvent event) {
|
---|
781 | // Do nothing
|
---|
782 | }
|
---|
783 |
|
---|
784 | @Override
|
---|
785 | public void otherDatasetChange(AbstractDatasetChangedEvent event) {
|
---|
786 | // Do nothing
|
---|
787 | }
|
---|
788 |
|
---|
789 | @Override
|
---|
790 | public void dataChanged(DataChangedEvent event) {
|
---|
791 | // Do nothing
|
---|
792 | }
|
---|
793 | }
|
---|