| 1 | package org.openstreetmap.josm.actions.mapmode;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.event.ActionEvent;
|
|---|
| 4 | import java.awt.event.KeyEvent;
|
|---|
| 5 | import java.awt.event.MouseEvent;
|
|---|
| 6 | import java.util.ArrayList;
|
|---|
| 7 | import java.util.Arrays;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.Collections;
|
|---|
| 10 | import java.util.HashSet;
|
|---|
| 11 | import java.util.Map.Entry;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.JOptionPane;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.command.ChangeCommand;
|
|---|
| 17 | import org.openstreetmap.josm.command.Command;
|
|---|
| 18 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 19 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.Segment;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
|---|
| 25 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 26 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * An action that enables the user to delete nodes and other objects.
|
|---|
| 30 | *
|
|---|
| 31 | * The user can click on an object, which get deleted if possible. When Ctrl is
|
|---|
| 32 | * pressed when releasing the button, the objects and all its references are
|
|---|
| 33 | * deleted. The exact definition of "all its references" are in
|
|---|
| 34 | * @see #deleteWithReferences(OsmPrimitive)
|
|---|
| 35 | *
|
|---|
| 36 | * Pressing Alt will select the way instead of a segment, as usual.
|
|---|
| 37 | *
|
|---|
| 38 | * If the user did not press Ctrl and the object has any references, the user
|
|---|
| 39 | * is informed and nothing is deleted.
|
|---|
| 40 | *
|
|---|
| 41 | * If the user enters the mapmode and any object is selected, all selected
|
|---|
| 42 | * objects that can be deleted will.
|
|---|
| 43 | *
|
|---|
| 44 | * @author imi
|
|---|
| 45 | */
|
|---|
| 46 | public class DeleteAction extends MapMode {
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Construct a new DeleteAction. Mnemonic is the delete - key.
|
|---|
| 50 | * @param mapFrame The frame this action belongs to.
|
|---|
| 51 | */
|
|---|
| 52 | public DeleteAction(MapFrame mapFrame) {
|
|---|
| 53 | super("Delete",
|
|---|
| 54 | "delete",
|
|---|
| 55 | "Delete nodes, streets or segments.",
|
|---|
| 56 | "D",
|
|---|
| 57 | KeyEvent.VK_D,
|
|---|
| 58 | mapFrame,
|
|---|
| 59 | ImageProvider.getCursor("normal", "delete"));
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | @Override public void enterMode() {
|
|---|
| 63 | super.enterMode();
|
|---|
| 64 | Main.map.mapView.addMouseListener(this);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | @Override public void exitMode() {
|
|---|
| 68 | super.exitMode();
|
|---|
| 69 | Main.map.mapView.removeMouseListener(this);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | @Override public void actionPerformed(ActionEvent e) {
|
|---|
| 74 | super.actionPerformed(e);
|
|---|
| 75 | boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
|---|
| 76 | if (ctrl)
|
|---|
| 77 | deleteWithReferences(Main.ds.getSelected());
|
|---|
| 78 | else
|
|---|
| 79 | delete(Main.ds.getSelected(), false);
|
|---|
| 80 | Main.map.repaint();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * If user clicked with the left button, delete the nearest object.
|
|---|
| 85 | * position.
|
|---|
| 86 | */
|
|---|
| 87 | @Override public void mouseClicked(MouseEvent e) {
|
|---|
| 88 | if (e.getButton() != MouseEvent.BUTTON1)
|
|---|
| 89 | return;
|
|---|
| 90 |
|
|---|
| 91 | OsmPrimitive sel = Main.map.mapView.getNearest(e.getPoint(), (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0);
|
|---|
| 92 | if (sel == null)
|
|---|
| 93 | return;
|
|---|
| 94 |
|
|---|
| 95 | if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0)
|
|---|
| 96 | deleteWithReferences(Collections.singleton(sel));
|
|---|
| 97 | else
|
|---|
| 98 | delete(Collections.singleton(sel), true);
|
|---|
| 99 |
|
|---|
| 100 | Main.map.mapView.repaint();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Delete the primitives and everything they references.
|
|---|
| 105 | *
|
|---|
| 106 | * If a node is deleted, the node and all segments, ways and areas
|
|---|
| 107 | * the node is part of are deleted as well.
|
|---|
| 108 | *
|
|---|
| 109 | * If a segment is deleted, all ways the segment is part of
|
|---|
| 110 | * are deleted as well. No nodes are deleted.
|
|---|
| 111 | *
|
|---|
| 112 | * If a way is deleted, only the way and no segments or nodes are
|
|---|
| 113 | * deleted.
|
|---|
| 114 | *
|
|---|
| 115 | * If an area is deleted, only the area gets deleted.
|
|---|
| 116 | *
|
|---|
| 117 | * @param selection The list of all object to be deleted.
|
|---|
| 118 | */
|
|---|
| 119 | private void deleteWithReferences(Collection<OsmPrimitive> selection) {
|
|---|
| 120 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds);
|
|---|
| 121 | for (OsmPrimitive osm : selection)
|
|---|
| 122 | osm.visit(v);
|
|---|
| 123 | v.data.addAll(selection);
|
|---|
| 124 | if (!v.data.isEmpty())
|
|---|
| 125 | Main.main.editLayer().add(new DeleteCommand(v.data));
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Try to delete all given primitives. If a primitive is
|
|---|
| 130 | * used somewhere and that "somewhere" is not going to be deleted,
|
|---|
| 131 | * inform the user and do not delete.
|
|---|
| 132 | *
|
|---|
| 133 | * If deleting a node which is part of exactly two segments, and both segments
|
|---|
| 134 | * have no conflicting keys, join them and remove the node.
|
|---|
| 135 | *
|
|---|
| 136 | * @param selection The objects to delete.
|
|---|
| 137 | * @param msgBox Whether a message box for errors should be shown
|
|---|
| 138 | */
|
|---|
| 139 | private void delete(Collection<OsmPrimitive> selection, boolean msgBox) {
|
|---|
| 140 | Collection<OsmPrimitive> del = new HashSet<OsmPrimitive>();
|
|---|
| 141 | for (OsmPrimitive osm : selection) {
|
|---|
| 142 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds);
|
|---|
| 143 | osm.visit(v);
|
|---|
| 144 | if (!selection.containsAll(v.data)) {
|
|---|
| 145 | if (osm instanceof Node) {
|
|---|
| 146 | String reason = deleteNodeAndJoinSegment((Node)osm);
|
|---|
| 147 | if (reason != null && msgBox) {
|
|---|
| 148 | JOptionPane.showMessageDialog(Main.parent, "Cannot delete node. "+reason);
|
|---|
| 149 | return;
|
|---|
| 150 | }
|
|---|
| 151 | } else if (msgBox) {
|
|---|
| 152 | JOptionPane.showMessageDialog(Main.parent, "This object is in use.");
|
|---|
| 153 | return;
|
|---|
| 154 | }
|
|---|
| 155 | } else {
|
|---|
| 156 | del.addAll(v.data);
|
|---|
| 157 | del.add(osm);
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | if (!del.isEmpty())
|
|---|
| 161 | Main.main.editLayer().add(new DeleteCommand(del));
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | private String deleteNodeAndJoinSegment(Node n) {
|
|---|
| 165 | ArrayList<Segment> segs = new ArrayList<Segment>(2);
|
|---|
| 166 | for (Segment s : Main.ds.segments) {
|
|---|
| 167 | if (!s.deleted && (s.from == n || s.to == n)) {
|
|---|
| 168 | if (segs.size() > 1)
|
|---|
| 169 | return "Used by more than two segments.";
|
|---|
| 170 | segs.add(s);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 | if (segs.size() != 2)
|
|---|
| 174 | return "Used by only one segment.";
|
|---|
| 175 | Segment seg1 = segs.get(0);
|
|---|
| 176 | Segment seg2 = segs.get(1);
|
|---|
| 177 | if (seg1.from == seg2.to) {
|
|---|
| 178 | Segment s = seg1;
|
|---|
| 179 | seg1 = seg2;
|
|---|
| 180 | seg2 = s;
|
|---|
| 181 | }
|
|---|
| 182 | for (Way w : Main.ds.ways)
|
|---|
| 183 | if (!w.deleted && (w.segments.contains(seg1) || w.segments.contains(seg2)))
|
|---|
| 184 | return "Used in a way.";
|
|---|
| 185 | if (seg1.from == seg2.from || seg1.to == seg2.to)
|
|---|
| 186 | return "Wrong direction of segments.";
|
|---|
| 187 | for (Entry<String, String> e : seg1.entrySet())
|
|---|
| 188 | if (seg2.keySet().contains(e.getKey()) && !seg2.get(e.getKey()).equals(e.getValue()))
|
|---|
| 189 | return "Conflicting keys";
|
|---|
| 190 | Segment s = new Segment(seg1);
|
|---|
| 191 | s.to = seg2.to;
|
|---|
| 192 | if (s.keys == null)
|
|---|
| 193 | s.keys = seg2.keys;
|
|---|
| 194 | else if (seg2.keys != null)
|
|---|
| 195 | s.keys.putAll(seg2.keys);
|
|---|
| 196 | Command[] cmds = new Command[]{
|
|---|
| 197 | new ChangeCommand(seg1, s),
|
|---|
| 198 | new DeleteCommand(Arrays.asList(new OsmPrimitive[]{n, seg2}))};
|
|---|
| 199 | Main.main.editLayer().add(new SequenceCommand("Delete Node", Arrays.asList(cmds)));
|
|---|
| 200 | return null;
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|