| 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.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 | targetWay = null; |
|---|
| 194 | candidateNode = null; |
|---|
| 195 | candidateSegment = null; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | @Override |
|---|
| 199 | protected void updateStatusLine() { |
|---|
| 200 | String newModeHelpText = getModeHelpText(); |
|---|
| 201 | if (!newModeHelpText.equals(oldModeHelpText)) { |
|---|
| 202 | oldModeHelpText = newModeHelpText; |
|---|
| 203 | MapFrame map = MainApplication.getMap(); |
|---|
| 204 | map.statusLine.setHelpText(newModeHelpText); |
|---|
| 205 | map.statusLine.repaint(); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | @Override |
|---|
| 210 | public String getModeHelpText() { |
|---|
| 211 | if (state == State.SELECTING) { |
|---|
| 212 | if (targetWay != null) { |
|---|
| 213 | return tr("Click on the way to start improving its shape."); |
|---|
| 214 | } else { |
|---|
| 215 | return tr("Select a way that you want to make more accurate."); |
|---|
| 216 | } |
|---|
| 217 | } else { |
|---|
| 218 | if (ctrl) { |
|---|
| 219 | return tr("Click to add a new node. Release Ctrl to move existing nodes or hold Alt to delete."); |
|---|
| 220 | } else if (alt) { |
|---|
| 221 | return tr("Click to delete the highlighted node. Release Alt to move existing nodes or hold Ctrl to add new nodes."); |
|---|
| 222 | } else { |
|---|
| 223 | return tr("Click to move the highlighted node. Hold Ctrl to add new nodes, or Alt to delete."); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | @Override |
|---|
| 229 | public boolean layerIsSupported(Layer l) { |
|---|
| 230 | return isEditableDataLayer(l); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | @Override |
|---|
| 234 | protected void updateEnabledState() { |
|---|
| 235 | setEnabled(getLayerManager().getEditLayer() != null); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | // ------------------------------------------------------------------------- |
|---|
| 239 | // MapViewPaintable methods |
|---|
| 240 | // ------------------------------------------------------------------------- |
|---|
| 241 | /** |
|---|
| 242 | * Redraws temporary layer. Highlights targetWay in select mode. Draws |
|---|
| 243 | * preview lines in improve mode and highlights the candidateNode |
|---|
| 244 | * @param g The graphics |
|---|
| 245 | * @param mv The map view |
|---|
| 246 | * @param bbox The bounding box |
|---|
| 247 | */ |
|---|
| 248 | public void paint(Graphics2D g, MapView mv, Bounds bbox) { |
|---|
| 249 | if (mousePos == null || (candidateNode != null && candidateNode.getDataSet() == null)) { |
|---|
| 250 | return; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | g.setColor(guideColor); |
|---|
| 254 | |
|---|
| 255 | if (state == State.SELECTING && targetWay != null) { |
|---|
| 256 | // Highlighting the targetWay in Selecting state |
|---|
| 257 | // Non-native highlighting is used, because sometimes highlighted |
|---|
| 258 | // segments are covered with others, which is bad. |
|---|
| 259 | BasicStroke stroke = SELECT_TARGET_WAY_STROKE.get(); |
|---|
| 260 | g.setStroke(stroke); |
|---|
| 261 | |
|---|
| 262 | List<Node> nodes = targetWay.getNodes(); |
|---|
| 263 | |
|---|
| 264 | g.draw(new MapViewPath(mv).append(nodes, false).computeClippedLine(stroke)); |
|---|
| 265 | |
|---|
| 266 | } else if (state == State.IMPROVING) { |
|---|
| 267 | // Drawing preview lines and highlighting the node |
|---|
| 268 | // that is going to be moved. |
|---|
| 269 | // Non-native highlighting is used here as well. |
|---|
| 270 | |
|---|
| 271 | // Finding endpoints |
|---|
| 272 | Node p1 = null; |
|---|
| 273 | Node p2 = null; |
|---|
| 274 | if (ctrl && candidateSegment != null) { |
|---|
| 275 | g.setStroke(ADD_NODE_STROKE.get()); |
|---|
| 276 | try { |
|---|
| 277 | p1 = candidateSegment.getFirstNode(); |
|---|
| 278 | p2 = candidateSegment.getSecondNode(); |
|---|
| 279 | } catch (ArrayIndexOutOfBoundsException e) { |
|---|
| 280 | Logging.error(e); |
|---|
| 281 | } |
|---|
| 282 | } else if (!alt && !ctrl && candidateNode != null) { |
|---|
| 283 | g.setStroke(MOVE_NODE_STROKE.get()); |
|---|
| 284 | List<Pair<Node, Node>> wpps = targetWay.getNodePairs(false); |
|---|
| 285 | for (Pair<Node, Node> wpp : wpps) { |
|---|
| 286 | if (wpp.a == candidateNode) { |
|---|
| 287 | p1 = wpp.b; |
|---|
| 288 | } |
|---|
| 289 | if (wpp.b == candidateNode) { |
|---|
| 290 | p2 = wpp.a; |
|---|
| 291 | } |
|---|
| 292 | if (p1 != null && p2 != null) { |
|---|
| 293 | break; |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | } else if (alt && !ctrl && candidateNode != null) { |
|---|
| 297 | g.setStroke(DELETE_NODE_STROKE.get()); |
|---|
| 298 | List<Node> nodes = targetWay.getNodes(); |
|---|
| 299 | int index = nodes.indexOf(candidateNode); |
|---|
| 300 | |
|---|
| 301 | // Only draw line if node is not first and/or last |
|---|
| 302 | if (index > 0 && index < (nodes.size() - 1)) { |
|---|
| 303 | p1 = nodes.get(index - 1); |
|---|
| 304 | p2 = nodes.get(index + 1); |
|---|
| 305 | } else if (targetWay.isClosed()) { |
|---|
| 306 | p1 = targetWay.getNode(1); |
|---|
| 307 | p2 = targetWay.getNode(nodes.size() - 2); |
|---|
| 308 | } |
|---|
| 309 | // TODO: indicate what part that will be deleted? (for end nodes) |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | // Drawing preview lines |
|---|
| 314 | MapViewPath b = new MapViewPath(mv); |
|---|
| 315 | if (alt && !ctrl) { |
|---|
| 316 | // In delete mode |
|---|
| 317 | if (p1 != null && p2 != null) { |
|---|
| 318 | b.moveTo(p1); |
|---|
| 319 | b.lineTo(p2); |
|---|
| 320 | } |
|---|
| 321 | } else { |
|---|
| 322 | // In add or move mode |
|---|
| 323 | if (p1 != null) { |
|---|
| 324 | b.moveTo(mousePos.x, mousePos.y); |
|---|
| 325 | b.lineTo(p1); |
|---|
| 326 | } |
|---|
| 327 | if (p2 != null) { |
|---|
| 328 | b.moveTo(mousePos.x, mousePos.y); |
|---|
| 329 | b.lineTo(p2); |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | g.draw(b.computeClippedLine(g.getStroke())); |
|---|
| 333 | |
|---|
| 334 | // Highlighting candidateNode |
|---|
| 335 | if (candidateNode != null) { |
|---|
| 336 | p1 = candidateNode; |
|---|
| 337 | g.fill(new MapViewPath(mv).shapeAround(p1, SymbolShape.SQUARE, DOT_SIZE.get())); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | if (!alt && !ctrl && candidateNode != null) { |
|---|
| 341 | b.reset(); |
|---|
| 342 | drawIntersectingWayHelperLines(mv, b); |
|---|
| 343 | g.setStroke(MOVE_NODE_INTERSECTING_STROKE.get()); |
|---|
| 344 | g.draw(b.computeClippedLine(g.getStroke())); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | } |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | protected void drawIntersectingWayHelperLines(MapView mv, MapViewPath b) { |
|---|
| 351 | for (final OsmPrimitive referrer : candidateNode.getReferrers()) { |
|---|
| 352 | if (!(referrer instanceof Way) || targetWay.equals(referrer)) { |
|---|
| 353 | continue; |
|---|
| 354 | } |
|---|
| 355 | final List<Node> nodes = ((Way) referrer).getNodes(); |
|---|
| 356 | for (int i = 0; i < nodes.size(); i++) { |
|---|
| 357 | if (!candidateNode.equals(nodes.get(i))) { |
|---|
| 358 | continue; |
|---|
| 359 | } |
|---|
| 360 | if (i > 0) { |
|---|
| 361 | b.moveTo(mousePos.x, mousePos.y); |
|---|
| 362 | b.lineTo(nodes.get(i - 1)); |
|---|
| 363 | } |
|---|
| 364 | if (i < nodes.size() - 1) { |
|---|
| 365 | b.moveTo(mousePos.x, mousePos.y); |
|---|
| 366 | b.lineTo(nodes.get(i + 1)); |
|---|
| 367 | } |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | // ------------------------------------------------------------------------- |
|---|
| 373 | // Event handlers |
|---|
| 374 | // ------------------------------------------------------------------------- |
|---|
| 375 | @Override |
|---|
| 376 | public void modifiersExChanged(int modifiers) { |
|---|
| 377 | if (!MainApplication.isDisplayingMapView() || !MainApplication.getMap().mapView.isActiveLayerDrawable()) { |
|---|
| 378 | return; |
|---|
| 379 | } |
|---|
| 380 | updateKeyModifiersEx(modifiers); |
|---|
| 381 | updateCursorDependentObjectsIfNeeded(); |
|---|
| 382 | updateCursor(); |
|---|
| 383 | updateStatusLine(); |
|---|
| 384 | temporaryLayer.invalidate(); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | @Override |
|---|
| 388 | public void selectionChanged(SelectionChangeEvent event) { |
|---|
| 389 | if (selectionChangedBlocked) { |
|---|
| 390 | return; |
|---|
| 391 | } |
|---|
| 392 | updateStateByCurrentSelection(); |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | @Override |
|---|
| 396 | public void mouseDragged(MouseEvent e) { |
|---|
| 397 | dragging = true; |
|---|
| 398 | mouseMoved(e); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | @Override |
|---|
| 402 | public void mouseMoved(MouseEvent e) { |
|---|
| 403 | if (!isEnabled()) { |
|---|
| 404 | return; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | mousePos = e.getPoint(); |
|---|
| 408 | |
|---|
| 409 | updateKeyModifiers(e); |
|---|
| 410 | updateCursorDependentObjectsIfNeeded(); |
|---|
| 411 | updateCursor(); |
|---|
| 412 | updateStatusLine(); |
|---|
| 413 | temporaryLayer.invalidate(); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | @Override |
|---|
| 417 | public void mouseReleased(MouseEvent e) { |
|---|
| 418 | dragging = false; |
|---|
| 419 | if (!isEnabled() || e.getButton() != MouseEvent.BUTTON1) { |
|---|
| 420 | return; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | DataSet ds = getLayerManager().getEditDataSet(); |
|---|
| 424 | updateKeyModifiers(e); |
|---|
| 425 | mousePos = e.getPoint(); |
|---|
| 426 | |
|---|
| 427 | if (state == State.SELECTING) { |
|---|
| 428 | if (targetWay != null) { |
|---|
| 429 | ds.setSelected(targetWay.getPrimitiveId()); |
|---|
| 430 | updateStateByCurrentSelection(); |
|---|
| 431 | } |
|---|
| 432 | } else if (state == State.IMPROVING) { |
|---|
| 433 | // Checking if the new coordinate is outside of the world |
|---|
| 434 | if (new Node(mv.getEastNorth(mousePos.x, mousePos.y)).isOutSideWorld()) { |
|---|
| 435 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), |
|---|
| 436 | tr("Cannot add a node outside of the world."), |
|---|
| 437 | tr("Warning"), JOptionPane.WARNING_MESSAGE); |
|---|
| 438 | return; |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | if (ctrl && !alt && candidateSegment != null) { |
|---|
| 442 | // Add a new node to the highlighted segment. |
|---|
| 443 | Collection<WaySegment> virtualSegments = new LinkedList<>(); |
|---|
| 444 | |
|---|
| 445 | // Check if other ways have the same segment. |
|---|
| 446 | // We have to make sure that we add the new node to all of them. |
|---|
| 447 | Set<Way> commonParentWays = new HashSet<>(candidateSegment.getFirstNode().getParentWays()); |
|---|
| 448 | commonParentWays.retainAll(candidateSegment.getSecondNode().getParentWays()); |
|---|
| 449 | for (Way w : commonParentWays) { |
|---|
| 450 | for (int i = 0; i < w.getNodesCount() - 1; i++) { |
|---|
| 451 | WaySegment testWS = new WaySegment(w, i); |
|---|
| 452 | if (testWS.isSimilar(candidateSegment)) { |
|---|
| 453 | virtualSegments.add(testWS); |
|---|
| 454 | } |
|---|
| 455 | } |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | Collection<Command> virtualCmds = new LinkedList<>(); |
|---|
| 459 | // Create the new node |
|---|
| 460 | Node virtualNode = new Node(mv.getEastNorth(mousePos.x, mousePos.y)); |
|---|
| 461 | virtualCmds.add(new AddCommand(ds, virtualNode)); |
|---|
| 462 | |
|---|
| 463 | // Adding the node to all segments found |
|---|
| 464 | for (WaySegment virtualSegment : virtualSegments) { |
|---|
| 465 | Way w = virtualSegment.getWay(); |
|---|
| 466 | List<Node> modNodes = w.getNodes(); |
|---|
| 467 | modNodes.add(virtualSegment.getUpperIndex(), virtualNode); |
|---|
| 468 | virtualCmds.add(new ChangeNodesCommand(w, modNodes)); |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | // Finishing the sequence command |
|---|
| 472 | String text = trn("Add a new node to way", |
|---|
| 473 | "Add a new node to {0} ways", |
|---|
| 474 | virtualSegments.size(), virtualSegments.size()); |
|---|
| 475 | |
|---|
| 476 | UndoRedoHandler.getInstance().add(new SequenceCommand(text, virtualCmds)); |
|---|
| 477 | |
|---|
| 478 | } else if (alt && !ctrl && candidateNode != null) { |
|---|
| 479 | // Deleting the highlighted node |
|---|
| 480 | |
|---|
| 481 | //check to see if node is in use by more than one object |
|---|
| 482 | long referrersCount = candidateNode.referrers(OsmPrimitive.class).count(); |
|---|
| 483 | long referrerWayCount = candidateNode.referrers(Way.class).count(); |
|---|
| 484 | if (referrersCount != 1 || referrerWayCount != 1) { |
|---|
| 485 | // detach node from way |
|---|
| 486 | final List<Node> nodes = targetWay.getNodes(); |
|---|
| 487 | nodes.remove(candidateNode); |
|---|
| 488 | if (nodes.size() < 2) { |
|---|
| 489 | final Command deleteCmd = DeleteCommand.delete(Collections.singleton(targetWay), true); |
|---|
| 490 | if (deleteCmd != null) { |
|---|
| 491 | UndoRedoHandler.getInstance().add(deleteCmd); |
|---|
| 492 | } |
|---|
| 493 | } else { |
|---|
| 494 | UndoRedoHandler.getInstance().add(new ChangeNodesCommand(targetWay, nodes)); |
|---|
| 495 | } |
|---|
| 496 | } else if (candidateNode.isTagged()) { |
|---|
| 497 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), |
|---|
| 498 | tr("Cannot delete node that has tags"), |
|---|
| 499 | tr("Error"), JOptionPane.ERROR_MESSAGE); |
|---|
| 500 | } else { |
|---|
| 501 | final Command deleteCmd = DeleteCommand.delete(Collections.singleton(candidateNode), true); |
|---|
| 502 | if (deleteCmd != null) { |
|---|
| 503 | UndoRedoHandler.getInstance().add(deleteCmd); |
|---|
| 504 | } |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | } else if (candidateNode != null) { |
|---|
| 508 | // Moving the highlighted node |
|---|
| 509 | EastNorth nodeEN = candidateNode.getEastNorth(); |
|---|
| 510 | EastNorth cursorEN = mv.getEastNorth(mousePos.x, mousePos.y); |
|---|
| 511 | |
|---|
| 512 | UndoRedoHandler.getInstance().add( |
|---|
| 513 | new MoveCommand(candidateNode, cursorEN.east() - nodeEN.east(), cursorEN.north() - nodeEN.north())); |
|---|
| 514 | |
|---|
| 515 | SelectAction.checkCommandForLargeDistance(UndoRedoHandler.getInstance().getLastCommand()); |
|---|
| 516 | } |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | mousePos = null; |
|---|
| 520 | updateCursor(); |
|---|
| 521 | updateStatusLine(); |
|---|
| 522 | temporaryLayer.invalidate(); |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | @Override |
|---|
| 526 | public void mouseExited(MouseEvent e) { |
|---|
| 527 | if (!isEnabled()) { |
|---|
| 528 | return; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | if (!dragging) { |
|---|
| 532 | mousePos = null; |
|---|
| 533 | } |
|---|
| 534 | temporaryLayer.invalidate(); |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | // ------------------------------------------------------------------------- |
|---|
| 538 | // Custom methods |
|---|
| 539 | // ------------------------------------------------------------------------- |
|---|
| 540 | /** |
|---|
| 541 | * Sets new cursor depending on state, mouse position |
|---|
| 542 | */ |
|---|
| 543 | private void updateCursor() { |
|---|
| 544 | if (!isEnabled()) { |
|---|
| 545 | mv.setNewCursor(null, this); |
|---|
| 546 | return; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | if (state == State.SELECTING) { |
|---|
| 550 | mv.setNewCursor(targetWay == null ? cursorSelect |
|---|
| 551 | : cursorSelectHover, this); |
|---|
| 552 | } else if (state == State.IMPROVING) { |
|---|
| 553 | if (alt && !ctrl) { |
|---|
| 554 | mv.setNewCursor(cursorImproveDelete, this); |
|---|
| 555 | } else if (shift || dragging) { |
|---|
| 556 | if (ctrl) { |
|---|
| 557 | mv.setNewCursor(cursorImproveAddLock, this); |
|---|
| 558 | } else { |
|---|
| 559 | mv.setNewCursor(cursorImproveLock, this); |
|---|
| 560 | } |
|---|
| 561 | } else if (ctrl && !alt) { |
|---|
| 562 | mv.setNewCursor(cursorImproveAdd, this); |
|---|
| 563 | } else { |
|---|
| 564 | mv.setNewCursor(cursorImprove, this); |
|---|
| 565 | } |
|---|
| 566 | } |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | /** |
|---|
| 570 | * Updates these objects under cursor: targetWay, candidateNode, |
|---|
| 571 | * candidateSegment |
|---|
| 572 | */ |
|---|
| 573 | public void updateCursorDependentObjectsIfNeeded() { |
|---|
| 574 | if (state == State.IMPROVING && (shift || dragging) |
|---|
| 575 | && !(candidateNode == null && candidateSegment == null)) { |
|---|
| 576 | return; |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | if (mousePos == null) { |
|---|
| 580 | candidateNode = null; |
|---|
| 581 | candidateSegment = null; |
|---|
| 582 | return; |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | if (state == State.SELECTING) { |
|---|
| 586 | targetWay = ImproveWayAccuracyHelper.findWay(mv, mousePos); |
|---|
| 587 | } else if (state == State.IMPROVING) { |
|---|
| 588 | if (ctrl && !alt) { |
|---|
| 589 | candidateSegment = ImproveWayAccuracyHelper.findCandidateSegment(mv, |
|---|
| 590 | targetWay, mousePos); |
|---|
| 591 | candidateNode = null; |
|---|
| 592 | } else { |
|---|
| 593 | candidateNode = ImproveWayAccuracyHelper.findCandidateNode(mv, |
|---|
| 594 | targetWay, mousePos); |
|---|
| 595 | candidateSegment = null; |
|---|
| 596 | } |
|---|
| 597 | } |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | /** |
|---|
| 601 | * Switches to Selecting state |
|---|
| 602 | */ |
|---|
| 603 | public void startSelecting() { |
|---|
| 604 | state = State.SELECTING; |
|---|
| 605 | |
|---|
| 606 | targetWay = null; |
|---|
| 607 | |
|---|
| 608 | temporaryLayer.invalidate(); |
|---|
| 609 | updateStatusLine(); |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | /** |
|---|
| 613 | * Switches to Improving state |
|---|
| 614 | * |
|---|
| 615 | * @param targetWay Way that is going to be improved |
|---|
| 616 | */ |
|---|
| 617 | public void startImproving(Way targetWay) { |
|---|
| 618 | state = State.IMPROVING; |
|---|
| 619 | |
|---|
| 620 | DataSet ds = getLayerManager().getEditDataSet(); |
|---|
| 621 | Collection<OsmPrimitive> currentSelection = ds.getSelected(); |
|---|
| 622 | if (currentSelection.size() != 1 |
|---|
| 623 | || !currentSelection.iterator().next().equals(targetWay)) { |
|---|
| 624 | selectionChangedBlocked = true; |
|---|
| 625 | ds.clearSelection(); |
|---|
| 626 | ds.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 | private void updateStateByCurrentSelection() { |
|---|
| 644 | final List<Node> nodeList = new ArrayList<>(); |
|---|
| 645 | final List<Way> wayList = new ArrayList<>(); |
|---|
| 646 | final DataSet ds = getLayerManager().getEditDataSet(); |
|---|
| 647 | if (ds != null) { |
|---|
| 648 | final Collection<OsmPrimitive> sel = ds.getSelected(); |
|---|
| 649 | |
|---|
| 650 | // Collecting nodes and ways from the selection |
|---|
| 651 | for (OsmPrimitive p : sel) { |
|---|
| 652 | if (p instanceof Way) { |
|---|
| 653 | wayList.add((Way) p); |
|---|
| 654 | } |
|---|
| 655 | if (p instanceof Node) { |
|---|
| 656 | nodeList.add((Node) p); |
|---|
| 657 | } |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | if (wayList.size() == 1) { |
|---|
| 661 | // Starting improving the single selected way |
|---|
| 662 | startImproving(wayList.get(0)); |
|---|
| 663 | return; |
|---|
| 664 | } else if (nodeList.size() == 1) { |
|---|
| 665 | // Starting improving the only way of the single selected node |
|---|
| 666 | List<OsmPrimitive> r = nodeList.get(0).getReferrers(); |
|---|
| 667 | if (r.size() == 1 && (r.get(0) instanceof Way)) { |
|---|
| 668 | startImproving((Way) r.get(0)); |
|---|
| 669 | return; |
|---|
| 670 | } |
|---|
| 671 | } |
|---|
| 672 | } |
|---|
| 673 | |
|---|
| 674 | // Starting selecting by default |
|---|
| 675 | startSelecting(); |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | @Override |
|---|
| 679 | public void primitivesRemoved(PrimitivesRemovedEvent event) { |
|---|
| 680 | if (event.getPrimitives().contains(candidateNode) || event.getPrimitives().contains(targetWay)) { |
|---|
| 681 | updateCursorDependentObjectsIfNeeded(); |
|---|
| 682 | } |
|---|
| 683 | } |
|---|
| 684 | |
|---|
| 685 | @Override |
|---|
| 686 | public void primitivesAdded(PrimitivesAddedEvent event) { |
|---|
| 687 | // Do nothing |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | @Override |
|---|
| 691 | public void tagsChanged(TagsChangedEvent event) { |
|---|
| 692 | // Do nothing |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | @Override |
|---|
| 696 | public void nodeMoved(NodeMovedEvent event) { |
|---|
| 697 | // Do nothing |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | @Override |
|---|
| 701 | public void wayNodesChanged(WayNodesChangedEvent event) { |
|---|
| 702 | // Do nothing |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | @Override |
|---|
| 706 | public void relationMembersChanged(RelationMembersChangedEvent event) { |
|---|
| 707 | // Do nothing |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | @Override |
|---|
| 711 | public void otherDatasetChange(AbstractDatasetChangedEvent event) { |
|---|
| 712 | // Do nothing |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | @Override |
|---|
| 716 | public void dataChanged(DataChangedEvent event) { |
|---|
| 717 | // Do nothing |
|---|
| 718 | } |
|---|
| 719 | } |
|---|