[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[858] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2410] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[858] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[2842] | 6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[858] | 7 |
|
---|
[9292] | 8 | import java.awt.GridBagLayout;
|
---|
[858] | 9 | import java.awt.event.ActionEvent;
|
---|
| 10 | import java.awt.event.KeyEvent;
|
---|
| 11 | import java.util.ArrayList;
|
---|
| 12 | import java.util.Collection;
|
---|
[1429] | 13 | import java.util.Collections;
|
---|
[7152] | 14 | import java.util.HashMap;
|
---|
[858] | 15 | import java.util.HashSet;
|
---|
| 16 | import java.util.LinkedList;
|
---|
| 17 | import java.util.List;
|
---|
[7152] | 18 | import java.util.Map;
|
---|
[4487] | 19 | import java.util.Set;
|
---|
[858] | 20 |
|
---|
[9292] | 21 | import javax.swing.AbstractButton;
|
---|
| 22 | import javax.swing.ButtonGroup;
|
---|
| 23 | import javax.swing.JLabel;
|
---|
[858] | 24 | import javax.swing.JOptionPane;
|
---|
[1429] | 25 | import javax.swing.JPanel;
|
---|
[9292] | 26 | import javax.swing.JToggleButton;
|
---|
[858] | 27 |
|
---|
| 28 | import org.openstreetmap.josm.Main;
|
---|
| 29 | import org.openstreetmap.josm.command.AddCommand;
|
---|
| 30 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
[6959] | 31 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
---|
[858] | 32 | import org.openstreetmap.josm.command.Command;
|
---|
| 33 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
| 34 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 35 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 36 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 37 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 38 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[9387] | 39 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
[9292] | 40 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[1429] | 41 | import org.openstreetmap.josm.gui.MapView;
|
---|
[6130] | 42 | import org.openstreetmap.josm.gui.Notification;
|
---|
[9292] | 43 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 44 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[1084] | 45 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[9292] | 46 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
| 47 | import org.openstreetmap.josm.tools.Utils;
|
---|
[858] | 48 |
|
---|
| 49 | /**
|
---|
[1024] | 50 | * Duplicate nodes that are used by multiple ways.
|
---|
[858] | 51 | *
|
---|
| 52 | * Resulting nodes are identical, up to their position.
|
---|
| 53 | *
|
---|
| 54 | * This is the opposite of the MergeNodesAction.
|
---|
[1677] | 55 | *
|
---|
[1429] | 56 | * If a single node is selected, it will copy that node and remove all tags from the old one
|
---|
[858] | 57 | */
|
---|
[1820] | 58 | public class UnGlueAction extends JosmAction {
|
---|
[858] | 59 |
|
---|
[8308] | 60 | private transient Node selectedNode;
|
---|
| 61 | private transient Way selectedWay;
|
---|
| 62 | private transient Set<Node> selectedNodes;
|
---|
[858] | 63 |
|
---|
[1169] | 64 | /**
|
---|
| 65 | * Create a new UnGlueAction.
|
---|
| 66 | */
|
---|
| 67 | public UnGlueAction() {
|
---|
| 68 | super(tr("UnGlue Ways"), "unglueways", tr("Duplicate nodes that are used by multiple ways."),
|
---|
[4982] | 69 | Shortcut.registerShortcut("tools:unglue", tr("Tool: {0}", tr("UnGlue Ways")), KeyEvent.VK_G, Shortcut.DIRECT), true);
|
---|
[2323] | 70 | putValue("help", ht("/Action/UnGlue"));
|
---|
[1169] | 71 | }
|
---|
[858] | 72 |
|
---|
[1169] | 73 | /**
|
---|
| 74 | * Called when the action is executed.
|
---|
| 75 | *
|
---|
| 76 | * This method does some checking on the selection and calls the matching unGlueWay method.
|
---|
| 77 | */
|
---|
[4458] | 78 | @Override
|
---|
[1169] | 79 | public void actionPerformed(ActionEvent e) {
|
---|
[9387] | 80 | try {
|
---|
| 81 | unglue(e);
|
---|
| 82 | } catch (UserCancelException ignore) {
|
---|
[10420] | 83 | Main.trace(ignore);
|
---|
[9387] | 84 | } finally {
|
---|
| 85 | cleanup();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[858] | 88 |
|
---|
[9387] | 89 | protected void unglue(ActionEvent e) throws UserCancelException {
|
---|
| 90 |
|
---|
[10382] | 91 | Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
|
---|
[1677] | 92 |
|
---|
[1429] | 93 | String errMsg = null;
|
---|
[6130] | 94 | int errorTime = Notification.TIME_DEFAULT;
|
---|
[9291] | 95 | if (checkSelectionOneNodeAtMostOneWay(selection)) {
|
---|
[9387] | 96 | checkAndConfirmOutlyingUnglue();
|
---|
[1169] | 97 | int count = 0;
|
---|
[12031] | 98 | for (Way w : selectedNode.getParentWays()) {
|
---|
[2120] | 99 | if (!w.isUsable() || w.getNodesCount() < 1) {
|
---|
[1814] | 100 | continue;
|
---|
| 101 | }
|
---|
[1169] | 102 | count++;
|
---|
| 103 | }
|
---|
| 104 | if (count < 2) {
|
---|
[6959] | 105 | boolean selfCrossing = false;
|
---|
| 106 | if (count == 1) {
|
---|
| 107 | // First try unglue self-crossing way
|
---|
| 108 | selfCrossing = unglueSelfCrossingWay();
|
---|
| 109 | }
|
---|
[1429] | 110 | // If there aren't enough ways, maybe the user wanted to unglue the nodes
|
---|
| 111 | // (= copy tags to a new node)
|
---|
[6959] | 112 | if (!selfCrossing)
|
---|
| 113 | if (checkForUnglueNode(selection)) {
|
---|
[9291] | 114 | unglueOneNodeAtMostOneWay(e);
|
---|
[6959] | 115 | } else {
|
---|
| 116 | errorTime = Notification.TIME_SHORT;
|
---|
| 117 | errMsg = tr("This node is not glued to anything else.");
|
---|
| 118 | }
|
---|
[1169] | 119 | } else {
|
---|
| 120 | // and then do the work.
|
---|
| 121 | unglueWays();
|
---|
| 122 | }
|
---|
[9291] | 123 | } else if (checkSelectionOneWayAnyNodes(selection)) {
|
---|
[9387] | 124 | checkAndConfirmOutlyingUnglue();
|
---|
[7005] | 125 | Set<Node> tmpNodes = new HashSet<>();
|
---|
[1169] | 126 | for (Node n : selectedNodes) {
|
---|
| 127 | int count = 0;
|
---|
[12031] | 128 | for (Way w : n.getParentWays()) {
|
---|
[2748] | 129 | if (!w.isUsable()) {
|
---|
[1814] | 130 | continue;
|
---|
[2748] | 131 | }
|
---|
[1169] | 132 | count++;
|
---|
| 133 | }
|
---|
| 134 | if (count >= 2) {
|
---|
| 135 | tmpNodes.add(n);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
[8318] | 138 | if (tmpNodes.isEmpty()) {
|
---|
[1169] | 139 | if (selection.size() > 1) {
|
---|
[10378] | 140 | errMsg = tr("None of these nodes are glued to anything else.");
|
---|
[1169] | 141 | } else {
|
---|
[2842] | 142 | errMsg = tr("None of this way''s nodes are glued to anything else.");
|
---|
[1169] | 143 | }
|
---|
| 144 | } else {
|
---|
| 145 | // and then do the work.
|
---|
| 146 | selectedNodes = tmpNodes;
|
---|
[9291] | 147 | unglueOneWayAnyNodes();
|
---|
[1169] | 148 | }
|
---|
| 149 | } else {
|
---|
[6130] | 150 | errorTime = Notification.TIME_VERY_LONG;
|
---|
[1429] | 151 | errMsg =
|
---|
[8846] | 152 | tr("The current selection cannot be used for unglueing.")+'\n'+
|
---|
| 153 | '\n'+
|
---|
| 154 | tr("Select either:")+'\n'+
|
---|
| 155 | tr("* One tagged node, or")+'\n'+
|
---|
| 156 | tr("* One node that is used by more than one way, or")+'\n'+
|
---|
| 157 | tr("* One node that is used by more than one way and one of those ways, or")+'\n'+
|
---|
| 158 | tr("* One way that has one or more nodes that are used by more than one way, or")+'\n'+
|
---|
| 159 | tr("* One way and one or more of its nodes that are used by more than one way.")+'\n'+
|
---|
| 160 | '\n'+
|
---|
[1169] | 161 | tr("Note: If a way is selected, this way will get fresh copies of the unglued\n"+
|
---|
[1814] | 162 | "nodes and the new nodes will be selected. Otherwise, all ways will get their\n"+
|
---|
| 163 | "own copy and all nodes will be selected.");
|
---|
[1169] | 164 | }
|
---|
[1677] | 165 |
|
---|
[8510] | 166 | if (errMsg != null) {
|
---|
[6130] | 167 | new Notification(
|
---|
| 168 | errMsg)
|
---|
| 169 | .setIcon(JOptionPane.ERROR_MESSAGE)
|
---|
| 170 | .setDuration(errorTime)
|
---|
| 171 | .show();
|
---|
[1814] | 172 | }
|
---|
[9387] | 173 | }
|
---|
[1677] | 174 |
|
---|
[9387] | 175 | private void cleanup() {
|
---|
[1169] | 176 | selectedNode = null;
|
---|
| 177 | selectedWay = null;
|
---|
| 178 | selectedNodes = null;
|
---|
| 179 | }
|
---|
[1677] | 180 |
|
---|
[1429] | 181 | /**
|
---|
[9292] | 182 | * Provides toggle buttons to allow the user choose the existing node, the new nodes, or all of them.
|
---|
| 183 | */
|
---|
| 184 | private static class ExistingBothNewChoice {
|
---|
| 185 | final AbstractButton oldNode = new JToggleButton(tr("Existing node"), ImageProvider.get("dialogs/conflict/tagkeeptheir"));
|
---|
| 186 | final AbstractButton bothNodes = new JToggleButton(tr("Both nodes"), ImageProvider.get("dialogs/conflict/tagundecide"));
|
---|
| 187 | final AbstractButton newNode = new JToggleButton(tr("New node"), ImageProvider.get("dialogs/conflict/tagkeepmine"));
|
---|
| 188 |
|
---|
| 189 | ExistingBothNewChoice(final boolean preselectNew) {
|
---|
| 190 | final ButtonGroup tagsGroup = new ButtonGroup();
|
---|
| 191 | tagsGroup.add(oldNode);
|
---|
| 192 | tagsGroup.add(bothNodes);
|
---|
| 193 | tagsGroup.add(newNode);
|
---|
| 194 | tagsGroup.setSelected((preselectNew ? newNode : oldNode).getModel(), true);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /**
|
---|
| 199 | * A dialog allowing the user decide whether the tags/memberships of the existing node should afterwards be at
|
---|
| 200 | * the existing node, the new nodes, or all of them.
|
---|
| 201 | */
|
---|
| 202 | static final class PropertiesMembershipDialog extends ExtendedDialog {
|
---|
| 203 |
|
---|
[10254] | 204 | final transient ExistingBothNewChoice tags;
|
---|
| 205 | final transient ExistingBothNewChoice memberships;
|
---|
[9292] | 206 |
|
---|
| 207 | private PropertiesMembershipDialog(boolean preselectNew, boolean queryTags, boolean queryMemberships) {
|
---|
| 208 | super(Main.parent, tr("Tags / Memberships"), new String[]{tr("Unglue"), tr("Cancel")});
|
---|
| 209 | setButtonIcons(new String[]{"unglueways", "cancel"});
|
---|
| 210 |
|
---|
| 211 | final JPanel content = new JPanel(new GridBagLayout());
|
---|
| 212 |
|
---|
| 213 | if (queryTags) {
|
---|
| 214 | content.add(new JLabel(tr("Where should the tags of the node be put?")), GBC.std(1, 1).span(3).insets(0, 20, 0, 0));
|
---|
| 215 | tags = new ExistingBothNewChoice(preselectNew);
|
---|
| 216 | content.add(tags.oldNode, GBC.std(1, 2));
|
---|
| 217 | content.add(tags.bothNodes, GBC.std(2, 2));
|
---|
| 218 | content.add(tags.newNode, GBC.std(3, 2));
|
---|
| 219 | } else {
|
---|
| 220 | tags = null;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | if (queryMemberships) {
|
---|
| 224 | content.add(new JLabel(tr("Where should the memberships of this node be put?")), GBC.std(1, 3).span(3).insets(0, 20, 0, 0));
|
---|
| 225 | memberships = new ExistingBothNewChoice(preselectNew);
|
---|
| 226 | content.add(memberships.oldNode, GBC.std(1, 4));
|
---|
| 227 | content.add(memberships.bothNodes, GBC.std(2, 4));
|
---|
| 228 | content.add(memberships.newNode, GBC.std(3, 4));
|
---|
| 229 | } else {
|
---|
| 230 | memberships = null;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | setContent(content);
|
---|
| 234 | setResizable(false);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[10657] | 237 | static PropertiesMembershipDialog showIfNecessary(Collection<Node> selectedNodes, boolean preselectNew) throws UserCancelException {
|
---|
[9292] | 238 | final boolean tagged = isTagged(selectedNodes);
|
---|
| 239 | final boolean usedInRelations = isUsedInRelations(selectedNodes);
|
---|
| 240 | if (tagged || usedInRelations) {
|
---|
| 241 | final PropertiesMembershipDialog dialog = new PropertiesMembershipDialog(preselectNew, tagged, usedInRelations);
|
---|
| 242 | dialog.showDialog();
|
---|
| 243 | if (dialog.getValue() != 1) {
|
---|
| 244 | throw new UserCancelException();
|
---|
| 245 | }
|
---|
| 246 | return dialog;
|
---|
| 247 | }
|
---|
| 248 | return null;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[10657] | 251 | private static boolean isTagged(final Collection<Node> existingNodes) {
|
---|
[10717] | 252 | return existingNodes.stream().anyMatch(Node::hasKeys);
|
---|
[9292] | 253 | }
|
---|
| 254 |
|
---|
[10657] | 255 | private static boolean isUsedInRelations(final Collection<Node> existingNodes) {
|
---|
| 256 | return existingNodes.stream().anyMatch(
|
---|
[10716] | 257 | selectedNode -> selectedNode.getReferrers().stream().anyMatch(Relation.class::isInstance));
|
---|
[9292] | 258 | }
|
---|
| 259 |
|
---|
| 260 | void update(final Node existingNode, final List<Node> newNodes, final Collection<Command> cmds) {
|
---|
| 261 | updateMemberships(existingNode, newNodes, cmds);
|
---|
| 262 | updateProperties(existingNode, newNodes, cmds);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | private void updateProperties(final Node existingNode, final Iterable<Node> newNodes, final Collection<Command> cmds) {
|
---|
| 266 | if (tags != null && tags.newNode.isSelected()) {
|
---|
| 267 | final Node newSelectedNode = new Node(existingNode);
|
---|
| 268 | newSelectedNode.removeAll();
|
---|
| 269 | cmds.add(new ChangeCommand(existingNode, newSelectedNode));
|
---|
| 270 | } else if (tags != null && tags.oldNode.isSelected()) {
|
---|
| 271 | for (Node newNode : newNodes) {
|
---|
| 272 | newNode.removeAll();
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | private void updateMemberships(final Node existingNode, final List<Node> newNodes, final Collection<Command> cmds) {
|
---|
| 278 | if (memberships != null && memberships.bothNodes.isSelected()) {
|
---|
| 279 | fixRelations(existingNode, cmds, newNodes, false);
|
---|
| 280 | } else if (memberships != null && memberships.newNode.isSelected()) {
|
---|
| 281 | fixRelations(existingNode, cmds, newNodes, true);
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | /**
|
---|
[6130] | 287 | * Assumes there is one tagged Node stored in selectedNode that it will try to unglue.
|
---|
| 288 | * (i.e. copy node and remove all tags from the old one. Relations will not be removed)
|
---|
[9230] | 289 | * @param e event that trigerred the action
|
---|
[1429] | 290 | */
|
---|
[9291] | 291 | private void unglueOneNodeAtMostOneWay(ActionEvent e) {
|
---|
[9292] | 292 | final PropertiesMembershipDialog dialog;
|
---|
| 293 | try {
|
---|
| 294 | dialog = PropertiesMembershipDialog.showIfNecessary(Collections.singleton(selectedNode), true);
|
---|
[10420] | 295 | } catch (UserCancelException ex) {
|
---|
| 296 | Main.trace(ex);
|
---|
[9292] | 297 | return;
|
---|
| 298 | }
|
---|
[1677] | 299 |
|
---|
[9292] | 300 | final Node n = new Node(selectedNode, true);
|
---|
[1677] | 301 |
|
---|
[10420] | 302 | List<Command> cmds = new LinkedList<>();
|
---|
[9292] | 303 | cmds.add(new AddCommand(n));
|
---|
| 304 | if (dialog != null) {
|
---|
| 305 | dialog.update(selectedNode, Collections.singletonList(n), cmds);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[1429] | 308 | // If this wasn't called from menu, place it where the cursor is/was
|
---|
[8510] | 309 | if (e.getSource() instanceof JPanel) {
|
---|
[1429] | 310 | MapView mv = Main.map.mapView;
|
---|
[1722] | 311 | n.setCoor(mv.getLatLon(mv.lastMEvent.getX(), mv.lastMEvent.getY()));
|
---|
[1429] | 312 | }
|
---|
[1677] | 313 |
|
---|
[1429] | 314 | Main.main.undoRedo.add(new SequenceCommand(tr("Unglued Node"), cmds));
|
---|
[10382] | 315 | getLayerManager().getEditDataSet().setSelected(n);
|
---|
[1429] | 316 | Main.map.mapView.repaint();
|
---|
| 317 | }
|
---|
[1677] | 318 |
|
---|
[1169] | 319 | /**
|
---|
[1429] | 320 | * Checks if selection is suitable for ungluing. This is the case when there's a single,
|
---|
| 321 | * tagged node selected that's part of at least one way (ungluing an unconnected node does
|
---|
| 322 | * not make sense. Due to the call order in actionPerformed, this is only called when the
|
---|
[1677] | 323 | * node is only part of one or less ways.
|
---|
| 324 | *
|
---|
[5909] | 325 | * @param selection The selection to check against
|
---|
| 326 | * @return {@code true} if selection is suitable
|
---|
[1429] | 327 | */
|
---|
| 328 | private boolean checkForUnglueNode(Collection<? extends OsmPrimitive> selection) {
|
---|
[2381] | 329 | if (selection.size() != 1)
|
---|
[1429] | 330 | return false;
|
---|
| 331 | OsmPrimitive n = (OsmPrimitive) selection.toArray()[0];
|
---|
[2381] | 332 | if (!(n instanceof Node))
|
---|
[1429] | 333 | return false;
|
---|
[12031] | 334 | if (((Node) n).getParentWays().isEmpty())
|
---|
[1429] | 335 | return false;
|
---|
[1677] | 336 |
|
---|
[2381] | 337 | selectedNode = (Node) n;
|
---|
| 338 | return selectedNode.isTagged();
|
---|
[1429] | 339 | }
|
---|
| 340 |
|
---|
| 341 | /**
|
---|
[1169] | 342 | * Checks if the selection consists of something we can work with.
|
---|
[3538] | 343 | * Checks only if the number and type of items selected looks good.
|
---|
[1169] | 344 | *
|
---|
[9230] | 345 | * If this method returns "true", selectedNode and selectedWay will be set.
|
---|
[1169] | 346 | *
|
---|
| 347 | * Returns true if either one node is selected or one node and one
|
---|
| 348 | * way are selected and the node is part of the way.
|
---|
| 349 | *
|
---|
[9230] | 350 | * The way will be put into the object variable "selectedWay", the node into "selectedNode".
|
---|
| 351 | * @param selection selected primitives
|
---|
[8931] | 352 | * @return true if either one node is selected or one node and one way are selected and the node is part of the way
|
---|
[1169] | 353 | */
|
---|
[9291] | 354 | private boolean checkSelectionOneNodeAtMostOneWay(Collection<? extends OsmPrimitive> selection) {
|
---|
[1023] | 355 |
|
---|
[1169] | 356 | int size = selection.size();
|
---|
| 357 | if (size < 1 || size > 2)
|
---|
| 358 | return false;
|
---|
[1023] | 359 |
|
---|
[1169] | 360 | selectedNode = null;
|
---|
| 361 | selectedWay = null;
|
---|
[1023] | 362 |
|
---|
[1169] | 363 | for (OsmPrimitive p : selection) {
|
---|
| 364 | if (p instanceof Node) {
|
---|
| 365 | selectedNode = (Node) p;
|
---|
| 366 | if (size == 1 || selectedWay != null)
|
---|
[1912] | 367 | return size == 1 || selectedWay.containsNode(selectedNode);
|
---|
[1169] | 368 | } else if (p instanceof Way) {
|
---|
| 369 | selectedWay = (Way) p;
|
---|
| 370 | if (size == 2 && selectedNode != null)
|
---|
[1912] | 371 | return selectedWay.containsNode(selectedNode);
|
---|
[1169] | 372 | }
|
---|
| 373 | }
|
---|
[1023] | 374 |
|
---|
[1169] | 375 | return false;
|
---|
| 376 | }
|
---|
[858] | 377 |
|
---|
[1169] | 378 | /**
|
---|
| 379 | * Checks if the selection consists of something we can work with.
|
---|
[3538] | 380 | * Checks only if the number and type of items selected looks good.
|
---|
[1169] | 381 | *
|
---|
[9230] | 382 | * Returns true if one way and any number of nodes that are part of that way are selected.
|
---|
| 383 | * Note: "any" can be none, then all nodes of the way are used.
|
---|
[1169] | 384 | *
|
---|
[9230] | 385 | * The way will be put into the object variable "selectedWay", the nodes into "selectedNodes".
|
---|
| 386 | * @param selection selected primitives
|
---|
[8931] | 387 | * @return true if one way and any number of nodes that are part of that way are selected
|
---|
[1169] | 388 | */
|
---|
[9291] | 389 | private boolean checkSelectionOneWayAnyNodes(Collection<? extends OsmPrimitive> selection) {
|
---|
[8318] | 390 | if (selection.isEmpty())
|
---|
[1169] | 391 | return false;
|
---|
[1024] | 392 |
|
---|
[1169] | 393 | selectedWay = null;
|
---|
| 394 | for (OsmPrimitive p : selection) {
|
---|
| 395 | if (p instanceof Way) {
|
---|
[1814] | 396 | if (selectedWay != null)
|
---|
[1169] | 397 | return false;
|
---|
| 398 | selectedWay = (Way) p;
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
[1814] | 401 | if (selectedWay == null)
|
---|
[1169] | 402 | return false;
|
---|
[1024] | 403 |
|
---|
[7005] | 404 | selectedNodes = new HashSet<>();
|
---|
[1169] | 405 | for (OsmPrimitive p : selection) {
|
---|
| 406 | if (p instanceof Node) {
|
---|
| 407 | Node n = (Node) p;
|
---|
[1912] | 408 | if (!selectedWay.containsNode(n))
|
---|
[1169] | 409 | return false;
|
---|
| 410 | selectedNodes.add(n);
|
---|
| 411 | }
|
---|
| 412 | }
|
---|
[1024] | 413 |
|
---|
[8318] | 414 | if (selectedNodes.isEmpty()) {
|
---|
[1862] | 415 | selectedNodes.addAll(selectedWay.getNodes());
|
---|
[1169] | 416 | }
|
---|
[1024] | 417 |
|
---|
[1169] | 418 | return true;
|
---|
| 419 | }
|
---|
[1024] | 420 |
|
---|
[1169] | 421 | /**
|
---|
| 422 | * dupe the given node of the given way
|
---|
| 423 | *
|
---|
[9230] | 424 | * assume that originalNode is in the way
|
---|
[6830] | 425 | * <ul>
|
---|
| 426 | * <li>the new node will be put into the parameter newNodes.</li>
|
---|
| 427 | * <li>the add-node command will be put into the parameter cmds.</li>
|
---|
| 428 | * <li>the changed way will be returned and must be put into cmds by the caller!</li>
|
---|
| 429 | * </ul>
|
---|
[9230] | 430 | * @param originalNode original node to duplicate
|
---|
| 431 | * @param w parent way
|
---|
| 432 | * @param cmds List of commands that will contain the new "add node" command
|
---|
| 433 | * @param newNodes List of nodes that will contain the new node
|
---|
| 434 | * @return new way The modified way. Change command mus be handled by the caller
|
---|
[1169] | 435 | */
|
---|
[8870] | 436 | private static Way modifyWay(Node originalNode, Way w, List<Command> cmds, List<Node> newNodes) {
|
---|
[3241] | 437 | // clone the node for the way
|
---|
| 438 | Node newNode = new Node(originalNode, true /* clear OSM ID */);
|
---|
| 439 | newNodes.add(newNode);
|
---|
| 440 | cmds.add(new AddCommand(newNode));
|
---|
| 441 |
|
---|
[7005] | 442 | List<Node> nn = new ArrayList<>();
|
---|
[1862] | 443 | for (Node pushNode : w.getNodes()) {
|
---|
[1169] | 444 | if (originalNode == pushNode) {
|
---|
[3241] | 445 | pushNode = newNode;
|
---|
[1169] | 446 | }
|
---|
| 447 | nn.add(pushNode);
|
---|
| 448 | }
|
---|
| 449 | Way newWay = new Way(w);
|
---|
[1862] | 450 | newWay.setNodes(nn);
|
---|
[858] | 451 |
|
---|
[1169] | 452 | return newWay;
|
---|
| 453 | }
|
---|
[1023] | 454 |
|
---|
[1169] | 455 | /**
|
---|
| 456 | * put all newNodes into the same relation(s) that originalNode is in
|
---|
[9230] | 457 | * @param originalNode original node to duplicate
|
---|
| 458 | * @param cmds List of commands that will contain the new "change relation" commands
|
---|
| 459 | * @param newNodes List of nodes that contain the new node
|
---|
[9315] | 460 | * @param removeOldMember whether the membership of the "old node" should be removed
|
---|
[1169] | 461 | */
|
---|
[9292] | 462 | private static void fixRelations(Node originalNode, Collection<Command> cmds, List<Node> newNodes, boolean removeOldMember) {
|
---|
[1169] | 463 | // modify all relations containing the node
|
---|
[2412] | 464 | for (Relation r : OsmPrimitive.getFilteredList(originalNode.getReferrers(), Relation.class)) {
|
---|
[2748] | 465 | if (r.isDeleted()) {
|
---|
[1814] | 466 | continue;
|
---|
[2748] | 467 | }
|
---|
[7152] | 468 | Relation newRel = null;
|
---|
[8338] | 469 | Map<String, Integer> rolesToReAdd = null; // <role name, index>
|
---|
[7152] | 470 | int i = 0;
|
---|
[1925] | 471 | for (RelationMember rm : r.getMembers()) {
|
---|
[6981] | 472 | if (rm.isNode() && rm.getMember() == originalNode) {
|
---|
| 473 | if (newRel == null) {
|
---|
| 474 | newRel = new Relation(r);
|
---|
[7152] | 475 | rolesToReAdd = new HashMap<>();
|
---|
[1169] | 476 | }
|
---|
[9230] | 477 | if (rolesToReAdd != null) {
|
---|
| 478 | rolesToReAdd.put(rm.getRole(), i);
|
---|
| 479 | }
|
---|
[1169] | 480 | }
|
---|
[7152] | 481 | i++;
|
---|
[1169] | 482 | }
|
---|
| 483 | if (newRel != null) {
|
---|
[9230] | 484 | if (rolesToReAdd != null) {
|
---|
[9292] | 485 | for (Map.Entry<String, Integer> role : rolesToReAdd.entrySet()) {
|
---|
| 486 | for (Node n : newNodes) {
|
---|
[9230] | 487 | newRel.addMember(role.getValue() + 1, new RelationMember(role.getKey(), n));
|
---|
| 488 | }
|
---|
[9292] | 489 | if (removeOldMember) {
|
---|
| 490 | newRel.removeMember(role.getValue());
|
---|
| 491 | }
|
---|
[1169] | 492 | }
|
---|
| 493 | }
|
---|
| 494 | cmds.add(new ChangeCommand(r, newRel));
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 | }
|
---|
[858] | 498 |
|
---|
[1169] | 499 | /**
|
---|
| 500 | * dupe a single node into as many nodes as there are ways using it, OR
|
---|
| 501 | *
|
---|
| 502 | * dupe a single node once, and put the copy on the selected way
|
---|
| 503 | */
|
---|
| 504 | private void unglueWays() {
|
---|
[9292] | 505 | final PropertiesMembershipDialog dialog;
|
---|
| 506 | try {
|
---|
| 507 | dialog = PropertiesMembershipDialog.showIfNecessary(Collections.singleton(selectedNode), false);
|
---|
| 508 | } catch (UserCancelException e) {
|
---|
[10420] | 509 | Main.trace(e);
|
---|
[9292] | 510 | return;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[10420] | 513 | List<Command> cmds = new LinkedList<>();
|
---|
| 514 | List<Node> newNodes = new LinkedList<>();
|
---|
[1169] | 515 | if (selectedWay == null) {
|
---|
[3538] | 516 | Way wayWithSelectedNode = null;
|
---|
[7005] | 517 | LinkedList<Way> parentWays = new LinkedList<>();
|
---|
[3538] | 518 | for (OsmPrimitive osm : selectedNode.getReferrers()) {
|
---|
| 519 | if (osm.isUsable() && osm instanceof Way) {
|
---|
| 520 | Way w = (Way) osm;
|
---|
| 521 | if (wayWithSelectedNode == null && !w.isFirstLastNode(selectedNode)) {
|
---|
| 522 | wayWithSelectedNode = w;
|
---|
| 523 | } else {
|
---|
| 524 | parentWays.add(w);
|
---|
| 525 | }
|
---|
[1814] | 526 | }
|
---|
[1169] | 527 | }
|
---|
[3538] | 528 | if (wayWithSelectedNode == null) {
|
---|
[6289] | 529 | parentWays.removeFirst();
|
---|
[3538] | 530 | }
|
---|
| 531 | for (Way w : parentWays) {
|
---|
| 532 | cmds.add(new ChangeCommand(w, modifyWay(selectedNode, w, cmds, newNodes)));
|
---|
| 533 | }
|
---|
[9387] | 534 | notifyWayPartOfRelation(parentWays);
|
---|
[1169] | 535 | } else {
|
---|
| 536 | cmds.add(new ChangeCommand(selectedWay, modifyWay(selectedNode, selectedWay, cmds, newNodes)));
|
---|
[9387] | 537 | notifyWayPartOfRelation(Collections.singleton(selectedWay));
|
---|
[1169] | 538 | }
|
---|
[1024] | 539 |
|
---|
[9292] | 540 | if (dialog != null) {
|
---|
| 541 | dialog.update(selectedNode, newNodes, cmds);
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[6959] | 544 | execCommands(cmds, newNodes);
|
---|
| 545 | }
|
---|
[1024] | 546 |
|
---|
[6959] | 547 | /**
|
---|
| 548 | * Add commands to undo-redo system.
|
---|
| 549 | * @param cmds Commands to execute
|
---|
| 550 | * @param newNodes New created nodes by this set of command
|
---|
| 551 | */
|
---|
[10448] | 552 | private void execCommands(List<Command> cmds, List<Node> newNodes) {
|
---|
[6507] | 553 | Main.main.undoRedo.add(new SequenceCommand(/* for correct i18n of plural forms - see #9110 */
|
---|
[10181] | 554 | trn("Dupe into {0} node", "Dupe into {0} nodes", newNodes.size() + 1L, newNodes.size() + 1L), cmds));
|
---|
[3538] | 555 | // select one of the new nodes
|
---|
[10448] | 556 | getLayerManager().getEditDataSet().setSelected(newNodes.get(0));
|
---|
[1169] | 557 | }
|
---|
[858] | 558 |
|
---|
[1169] | 559 | /**
|
---|
[6959] | 560 | * Duplicates a node used several times by the same way. See #9896.
|
---|
| 561 | * @return true if action is OK false if there is nothing to do
|
---|
| 562 | */
|
---|
| 563 | private boolean unglueSelfCrossingWay() {
|
---|
| 564 | // According to previous check, only one valid way through that node
|
---|
| 565 | Way way = null;
|
---|
[12031] | 566 | for (Way w: selectedNode.getParentWays()) {
|
---|
[6959] | 567 | if (w.isUsable() && w.getNodesCount() >= 1) {
|
---|
| 568 | way = w;
|
---|
| 569 | }
|
---|
[8513] | 570 | }
|
---|
[9087] | 571 | if (way == null) {
|
---|
| 572 | return false;
|
---|
| 573 | }
|
---|
[10420] | 574 | List<Command> cmds = new LinkedList<>();
|
---|
[6959] | 575 | List<Node> oldNodes = way.getNodes();
|
---|
[8338] | 576 | List<Node> newNodes = new ArrayList<>(oldNodes.size());
|
---|
| 577 | List<Node> addNodes = new ArrayList<>();
|
---|
[6959] | 578 | boolean seen = false;
|
---|
| 579 | for (Node n: oldNodes) {
|
---|
| 580 | if (n == selectedNode) {
|
---|
| 581 | if (seen) {
|
---|
| 582 | Node newNode = new Node(n, true /* clear OSM ID */);
|
---|
| 583 | newNodes.add(newNode);
|
---|
| 584 | cmds.add(new AddCommand(newNode));
|
---|
| 585 | newNodes.add(newNode);
|
---|
| 586 | addNodes.add(newNode);
|
---|
| 587 | } else {
|
---|
| 588 | newNodes.add(n);
|
---|
| 589 | seen = true;
|
---|
| 590 | }
|
---|
| 591 | } else {
|
---|
| 592 | newNodes.add(n);
|
---|
| 593 | }
|
---|
| 594 | }
|
---|
| 595 | if (addNodes.isEmpty()) {
|
---|
| 596 | // selectedNode doesn't need unglue
|
---|
| 597 | return false;
|
---|
| 598 | }
|
---|
| 599 | cmds.add(new ChangeNodesCommand(way, newNodes));
|
---|
[9387] | 600 | notifyWayPartOfRelation(Collections.singleton(way));
|
---|
[9292] | 601 | try {
|
---|
| 602 | final PropertiesMembershipDialog dialog = PropertiesMembershipDialog.showIfNecessary(Collections.singleton(selectedNode), false);
|
---|
| 603 | if (dialog != null) {
|
---|
| 604 | dialog.update(selectedNode, addNodes, cmds);
|
---|
| 605 | }
|
---|
| 606 | execCommands(cmds, addNodes);
|
---|
| 607 | return true;
|
---|
| 608 | } catch (UserCancelException ignore) {
|
---|
[10420] | 609 | Main.trace(ignore);
|
---|
[9292] | 610 | }
|
---|
| 611 | return false;
|
---|
| 612 | }
|
---|
[6959] | 613 |
|
---|
| 614 | /**
|
---|
[1169] | 615 | * dupe all nodes that are selected, and put the copies on the selected way
|
---|
| 616 | *
|
---|
| 617 | */
|
---|
[9291] | 618 | private void unglueOneWayAnyNodes() {
|
---|
[1169] | 619 | Way tmpWay = selectedWay;
|
---|
[1024] | 620 |
|
---|
[9292] | 621 | final PropertiesMembershipDialog dialog;
|
---|
| 622 | try {
|
---|
| 623 | dialog = PropertiesMembershipDialog.showIfNecessary(selectedNodes, false);
|
---|
| 624 | } catch (UserCancelException e) {
|
---|
[10420] | 625 | Main.trace(e);
|
---|
[9292] | 626 | return;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
[10420] | 629 | List<Command> cmds = new LinkedList<>();
|
---|
| 630 | List<Node> allNewNodes = new LinkedList<>();
|
---|
[1169] | 631 | for (Node n : selectedNodes) {
|
---|
[7005] | 632 | List<Node> newNodes = new LinkedList<>();
|
---|
[1169] | 633 | tmpWay = modifyWay(n, tmpWay, cmds, newNodes);
|
---|
[9292] | 634 | if (dialog != null) {
|
---|
| 635 | dialog.update(n, newNodes, cmds);
|
---|
| 636 | }
|
---|
[1169] | 637 | allNewNodes.addAll(newNodes);
|
---|
| 638 | }
|
---|
| 639 | cmds.add(new ChangeCommand(selectedWay, tmpWay)); // only one changeCommand for a way, else garbage will happen
|
---|
[9387] | 640 | notifyWayPartOfRelation(Collections.singleton(selectedWay));
|
---|
[1024] | 641 |
|
---|
[2842] | 642 | Main.main.undoRedo.add(new SequenceCommand(
|
---|
[8509] | 643 | trn("Dupe {0} node into {1} nodes", "Dupe {0} nodes into {1} nodes",
|
---|
| 644 | selectedNodes.size(), selectedNodes.size(), selectedNodes.size()+allNewNodes.size()), cmds));
|
---|
[10382] | 645 | getLayerManager().getEditDataSet().setSelected(allNewNodes);
|
---|
[1169] | 646 | }
|
---|
[1820] | 647 |
|
---|
| 648 | @Override
|
---|
| 649 | protected void updateEnabledState() {
|
---|
[10409] | 650 | updateEnabledStateOnCurrentSelection();
|
---|
[1820] | 651 | }
|
---|
[2256] | 652 |
|
---|
| 653 | @Override
|
---|
| 654 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 655 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 656 | }
|
---|
[4458] | 657 |
|
---|
[9387] | 658 | protected void checkAndConfirmOutlyingUnglue() throws UserCancelException {
|
---|
[7005] | 659 | List<OsmPrimitive> primitives = new ArrayList<>(2 + (selectedNodes == null ? 0 : selectedNodes.size()));
|
---|
[4458] | 660 | if (selectedNodes != null)
|
---|
| 661 | primitives.addAll(selectedNodes);
|
---|
| 662 | if (selectedNode != null)
|
---|
| 663 | primitives.add(selectedNode);
|
---|
[9387] | 664 | final boolean ok = Command.checkAndConfirmOutlyingOperation("unglue",
|
---|
[4458] | 665 | tr("Unglue confirmation"),
|
---|
| 666 | tr("You are about to unglue nodes outside of the area you have downloaded."
|
---|
| 667 | + "<br>"
|
---|
| 668 | + "This can cause problems because other objects (that you do not see) might use them."
|
---|
| 669 | + "<br>"
|
---|
| 670 | + "Do you really want to unglue?"),
|
---|
| 671 | tr("You are about to unglue incomplete objects."
|
---|
| 672 | + "<br>"
|
---|
| 673 | + "This will cause problems because you don''t see the real object."
|
---|
| 674 | + "<br>" + "Do you really want to unglue?"),
|
---|
[6639] | 675 | primitives, null);
|
---|
[9387] | 676 | if (!ok) {
|
---|
| 677 | throw new UserCancelException();
|
---|
| 678 | }
|
---|
[4458] | 679 | }
|
---|
[9387] | 680 |
|
---|
| 681 | protected void notifyWayPartOfRelation(final Iterable<Way> ways) {
|
---|
| 682 | final Set<String> affectedRelations = new HashSet<>();
|
---|
| 683 | for (Way way : ways) {
|
---|
| 684 | for (OsmPrimitive ref : way.getReferrers()) {
|
---|
| 685 | if (ref instanceof Relation && ref.isUsable()) {
|
---|
[9970] | 686 | affectedRelations.add(ref.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
[9387] | 687 | }
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
[9470] | 690 | if (affectedRelations.isEmpty()) {
|
---|
| 691 | return;
|
---|
| 692 | }
|
---|
| 693 |
|
---|
[9387] | 694 | final String msg1 = trn("Unglueing affected {0} relation: {1}", "Unglueing affected {0} relations: {1}",
|
---|
| 695 | affectedRelations.size(), affectedRelations.size(), Utils.joinAsHtmlUnorderedList(affectedRelations));
|
---|
| 696 | final String msg2 = trn("Ensure that the relation has not been broken!", "Ensure that the relations have not been broken!",
|
---|
| 697 | affectedRelations.size());
|
---|
| 698 | new Notification("<html>" + msg1 + msg2).setIcon(JOptionPane.WARNING_MESSAGE).show();
|
---|
| 699 | }
|
---|
[858] | 700 | }
|
---|