| | 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 a node to be disconnected."), |
| | 49 | tr("Warning"), |
| | 50 | JOptionPane.WARNING_MESSAGE); |
| | 51 | return; |
| | 52 | } else if (applicableWays.isEmpty()) { |
| | 53 | JOptionPane.showMessageDialog(Main.parent, |
| | 54 | tr("The selected nodes do not share the same way."), |
| | 55 | tr("Warning"), |
| | 56 | JOptionPane.WARNING_MESSAGE); |
| | 57 | return; |
| | 58 | } else if (applicableWays.size() > 1) { |
| | 59 | JOptionPane.showMessageDialog(Main.parent, |
| | 60 | trn("There is more than one way using the node you selected. Please select the way also.", |
| | 61 | "There is more than one way using the nodes you selected. Please select the way also.", |
| | 62 | selectedNodes.size()), |
| | 63 | tr("Warning"), |
| | 64 | JOptionPane.WARNING_MESSAGE); |
| | 65 | return; |
| | 66 | } else if (applicableWays.get(0).getRealNodesCount() < selectedNodes.size() + 2) { |
| | 67 | // there is only one affected way, but removing the selected nodes would only leave it |
| | 68 | // with less than 2 nodes |
| | 69 | JOptionPane.showMessageDialog(Main.parent, |
| | 70 | trn("The affected way would disappear after removing the selected node.", |
| | 71 | "The affected way would disappear after removing the selected nodes.", |
| | 72 | selectedNodes.size()), |
| | 73 | tr("Warning"), |
| | 74 | JOptionPane.WARNING_MESSAGE); |
| | 75 | return; |
| | 76 | } |
| | 77 | |
| | 78 | // Finally, applicableWays contains only one perfect way |
| | 79 | Way selectedWay = applicableWays.get(0); |
| | 80 | List<Node> otherNodes = selectedWay.getNodes(); |
| | 81 | otherNodes.removeAll(selectedNodes); |
| | 82 | Main.main.undoRedo.add(new ChangeNodesCommand(selectedWay, otherNodes)); |
| | 83 | Main.map.repaint(); |
| | 84 | } |
| | 85 | |
| | 86 | // shamefully copied over from SplitWayAction |
| | 87 | private List<Way> getApplicableWays(List<Way> selectedWays, List<Node> selectedNodes) { |
| | 88 | if (selectedNodes.isEmpty()) |
| | 89 | return null; |
| | 90 | |
| | 91 | // Special case - one of the selected ways touches (not cross) way that we want to split |
| | 92 | if (selectedNodes.size() == 1) { |
| | 93 | Node n = selectedNodes.get(0); |
| | 94 | List<Way> referedWays = OsmPrimitive.getFilteredList(n.getReferrers(), Way.class); |
| | 95 | Way inTheMiddle = null; |
| | 96 | boolean foundSelected = false; |
| | 97 | for (Way w: referedWays) { |
| | 98 | if (selectedWays.contains(w)) { |
| | 99 | foundSelected = true; |
| | 100 | } |
| | 101 | if (w.getNode(0) != n && w.getNode(w.getNodesCount() - 1) != n) { |
| | 102 | if (inTheMiddle == null) { |
| | 103 | inTheMiddle = w; |
| | 104 | } else { |
| | 105 | inTheMiddle = null; |
| | 106 | break; |
| | 107 | } |
| | 108 | } |
| | 109 | } |
| | 110 | if (foundSelected && inTheMiddle != null) |
| | 111 | return Collections.singletonList(inTheMiddle); |
| | 112 | } |
| | 113 | |
| | 114 | // List of ways shared by all nodes |
| | 115 | List<Way> result = new ArrayList<Way>(OsmPrimitive.getFilteredList(selectedNodes.get(0).getReferrers(), Way.class)); |
| | 116 | for (int i=1; i<selectedNodes.size(); i++) { |
| | 117 | List<OsmPrimitive> ref = selectedNodes.get(i).getReferrers(); |
| | 118 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) { |
| | 119 | if (!ref.contains(it.next())) { |
| | 120 | it.remove(); |
| | 121 | } |
| | 122 | } |
| | 123 | } |
| | 124 | |
| | 125 | // Remove broken ways |
| | 126 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) { |
| | 127 | if (it.next().getNodesCount() <= 2) { |
| | 128 | it.remove(); |
| | 129 | } |
| | 130 | } |
| | 131 | |
| | 132 | if (selectedWays.isEmpty()) |
| | 133 | return result; |
| | 134 | else { |
| | 135 | // Return only selected ways |
| | 136 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) { |
| | 137 | if (!selectedWays.contains(it.next())) { |
| | 138 | it.remove(); |
| | 139 | } |
| | 140 | } |
| | 141 | return result; |
| | 142 | } |
| | 143 | } |
| | 144 | |
| | 145 | @Override |
| | 146 | protected void updateEnabledState() { |
| | 147 | if (getCurrentDataSet() == null) { |
| | 148 | setEnabled(false); |
| | 149 | } else { |
| | 150 | updateEnabledState(getCurrentDataSet().getSelected()); |
| | 151 | } |
| | 152 | } |
| | 153 | |
| | 154 | @Override |
| | 155 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
| | 156 | setEnabled(selection != null && !selection.isEmpty()); |
| | 157 | } |
| | 158 | } |