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