| | 1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others |
| | 2 | package org.openstreetmap.josm.actions; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
| | 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 6 | import static org.openstreetmap.josm.tools.I18n.trn; |
| | 7 | |
| | 8 | import java.awt.event.ActionEvent; |
| | 9 | import java.awt.event.KeyEvent; |
| | 10 | import java.util.ArrayList; |
| | 11 | import java.util.Collection; |
| | 12 | import java.util.Collections; |
| | 13 | import java.util.HashSet; |
| | 14 | import java.util.Iterator; |
| | 15 | import java.util.List; |
| | 16 | |
| | 17 | import javax.swing.JOptionPane; |
| | 18 | |
| | 19 | import org.openstreetmap.josm.Main; |
| | 20 | import org.openstreetmap.josm.command.ChangeNodesCommand; |
| | 21 | import org.openstreetmap.josm.command.Command; |
| | 22 | import org.openstreetmap.josm.data.osm.Node; |
| | 23 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 24 | import org.openstreetmap.josm.data.osm.Way; |
| | 25 | import org.openstreetmap.josm.tools.Shortcut; |
| | 26 | |
| | 27 | public class UnJoinNodeWayAction extends JosmAction { |
| | 28 | public UnJoinNodeWayAction() { |
| | 29 | super(tr("Disconnect Node from Way"), "unjoinnodeway", |
| | 30 | tr("Disconnect nodes from a way they currently belong to"), |
| | 31 | Shortcut.registerShortcut("tools:unjoinnodeway", |
| | 32 | tr("Tool: {0}", tr("Disconnect Node from Way")), KeyEvent.VK_J, Shortcut.ALT), true); |
| | 33 | putValue("help", ht("/Action/UnJoinNodeWay")); |
| | 34 | } |
| | 35 | |
| | 36 | @Override |
| | 37 | public void actionPerformed(ActionEvent e) { |
| | 38 | |
| | 39 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected(); |
| | 40 | |
| | 41 | List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class); |
| | 42 | List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class); |
| | 43 | List<Way> applicableWays = getApplicableWays(selectedWays, selectedNodes); |
| | 44 | |
| | 45 | if (applicableWays == null) { |
| | 46 | JOptionPane.showMessageDialog( |
| | 47 | Main.parent, |
| | 48 | tr("Select at least one node to be disconnected."), |
| | 49 | tr("Warning"), |
| | 50 | JOptionPane.WARNING_MESSAGE); |
| | 51 | return; |
| | 52 | } else if (applicableWays.isEmpty()) { |
| | 53 | JOptionPane.showMessageDialog(Main.parent, |
| | 54 | trn("Selected node cannot be disconnected from anything.", |
| | 55 | "Selected nodes cannot be disconnected from anything.", |
| | 56 | selectedNodes.size()), |
| | 57 | tr("Warning"), |
| | 58 | JOptionPane.WARNING_MESSAGE); |
| | 59 | return; |
| | 60 | } else if (applicableWays.size() > 1) { |
| | 61 | JOptionPane.showMessageDialog(Main.parent, |
| | 62 | trn("There is more than one way using the node you selected. Please select the way also.", |
| | 63 | "There is more than one way using the nodes you selected. Please select the way also.", |
| | 64 | selectedNodes.size()), |
| | 65 | tr("Warning"), |
| | 66 | JOptionPane.WARNING_MESSAGE); |
| | 67 | return; |
| | 68 | } else if (applicableWays.get(0).getRealNodesCount() < selectedNodes.size() + 2) { |
| | 69 | // there is only one affected way, but removing the selected nodes would only leave it |
| | 70 | // with less than 2 nodes |
| | 71 | JOptionPane.showMessageDialog(Main.parent, |
| | 72 | trn("The affected way would disappear after disconnecting the selected node.", |
| | 73 | "The affected way would disappear after disconnecting the selected nodes.", |
| | 74 | selectedNodes.size()), |
| | 75 | tr("Warning"), |
| | 76 | JOptionPane.WARNING_MESSAGE); |
| | 77 | return; |
| | 78 | } |
| | 79 | |
| | 80 | |
| | 81 | // Finally, applicableWays contains only one perfect way |
| | 82 | Way selectedWay = applicableWays.get(0); |
| | 83 | |
| | 84 | // I'm sure there's a better way to handle this |
| | 85 | List<Node> orgNodes = selectedWay.getNodes(); |
| | 86 | selectedWay.removeNodes(new HashSet<Node>(selectedNodes)); |
| | 87 | List<Node> newNodes = selectedWay.getNodes(); |
| | 88 | selectedWay.setNodes(orgNodes); |
| | 89 | Main.main.undoRedo.add(new ChangeNodesCommand(selectedWay, newNodes)); |
| | 90 | Main.map.repaint(); |
| | 91 | } |
| | 92 | |
| | 93 | // Find ways to which the disconnect can be applied. This is the list of ways with more |
| | 94 | // than two nodes which pass through all the given nodes, intersected with the selected ways (if any) |
| | 95 | private List<Way> getApplicableWays(List<Way> selectedWays, List<Node> selectedNodes) { |
| | 96 | if (selectedNodes.isEmpty()) |
| | 97 | return null; |
| | 98 | |
| | 99 | // List of ways shared by all nodes |
| | 100 | List<Way> result = new ArrayList<Way>(OsmPrimitive.getFilteredList(selectedNodes.get(0).getReferrers(), Way.class)); |
| | 101 | for (int i=1; i<selectedNodes.size(); i++) { |
| | 102 | List<OsmPrimitive> ref = selectedNodes.get(i).getReferrers(); |
| | 103 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) { |
| | 104 | if (!ref.contains(it.next())) { |
| | 105 | it.remove(); |
| | 106 | } |
| | 107 | } |
| | 108 | } |
| | 109 | |
| | 110 | // Remove broken ways |
| | 111 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) { |
| | 112 | if (it.next().getNodesCount() <= 2) { |
| | 113 | it.remove(); |
| | 114 | } |
| | 115 | } |
| | 116 | |
| | 117 | if (selectedWays.isEmpty()) |
| | 118 | return result; |
| | 119 | else { |
| | 120 | // Return only selected ways |
| | 121 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) { |
| | 122 | if (!selectedWays.contains(it.next())) { |
| | 123 | it.remove(); |
| | 124 | } |
| | 125 | } |
| | 126 | return result; |
| | 127 | } |
| | 128 | } |
| | 129 | |
| | 130 | @Override |
| | 131 | protected void updateEnabledState() { |
| | 132 | if (getCurrentDataSet() == null) { |
| | 133 | setEnabled(false); |
| | 134 | } else { |
| | 135 | updateEnabledState(getCurrentDataSet().getSelected()); |
| | 136 | } |
| | 137 | } |
| | 138 | |
| | 139 | @Override |
| | 140 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
| | 141 | setEnabled(selection != null && !selection.isEmpty()); |
| | 142 | } |
| | 143 | } |