1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.Comparator;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.Iterator;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Set;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 |
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.command.AddCommand;
|
---|
25 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
26 | import org.openstreetmap.josm.command.Command;
|
---|
27 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
28 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
29 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
30 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
31 | import org.openstreetmap.josm.data.osm.Node;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
33 | import org.openstreetmap.josm.data.osm.Way;
|
---|
34 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
35 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Splits a way into multiple ways (all identical except for their node list).
|
---|
39 | *
|
---|
40 | * Ways are just split at the selected nodes. The nodes remain in their
|
---|
41 | * original order. Selected nodes at the end of a way are ignored.
|
---|
42 | */
|
---|
43 |
|
---|
44 | public class SplitWayAction extends JosmAction implements SelectionChangedListener {
|
---|
45 |
|
---|
46 | private Way selectedWay;
|
---|
47 | private List<Node> selectedNodes;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Create a new SplitWayAction.
|
---|
51 | */
|
---|
52 | public SplitWayAction() {
|
---|
53 | super(tr("Split Way"), "splitway", tr("Split a way at the selected node."), KeyEvent.VK_P, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK, true);
|
---|
54 | DataSet.selListeners.add(this);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Called when the action is executed.
|
---|
59 | *
|
---|
60 | * This method performs an expensive check whether the selection clearly defines one
|
---|
61 | * of the split actions outlined above, and if yes, calls the splitWay method.
|
---|
62 | */
|
---|
63 | public void actionPerformed(ActionEvent e) {
|
---|
64 |
|
---|
65 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
66 |
|
---|
67 | if (!checkSelection(selection)) {
|
---|
68 | JOptionPane.showMessageDialog(Main.parent, tr("The current selection cannot be used for splitting."));
|
---|
69 | return;
|
---|
70 | }
|
---|
71 |
|
---|
72 | selectedWay = null;
|
---|
73 | selectedNodes = null;
|
---|
74 |
|
---|
75 | Visitor splitVisitor = new Visitor(){
|
---|
76 | public void visit(Node n) {
|
---|
77 | if (selectedNodes == null)
|
---|
78 | selectedNodes = new LinkedList<Node>();
|
---|
79 | selectedNodes.add(n);
|
---|
80 | }
|
---|
81 | public void visit(Way w) {
|
---|
82 | selectedWay = w;
|
---|
83 | }
|
---|
84 | public void visit(Relation e) {
|
---|
85 | // enties are not considered
|
---|
86 | }
|
---|
87 | };
|
---|
88 |
|
---|
89 | for (OsmPrimitive p : selection)
|
---|
90 | p.visit(splitVisitor);
|
---|
91 |
|
---|
92 | // If only nodes are selected, try to guess which way to split. This works if there
|
---|
93 | // is exactly one way that all nodes are part of.
|
---|
94 | if (selectedWay == null && selectedNodes != null) {
|
---|
95 | HashMap<Way, Integer> wayOccurenceCounter = new HashMap<Way, Integer>();
|
---|
96 | for (Node n : selectedNodes) {
|
---|
97 | for (Way w : Main.ds.ways) {
|
---|
98 | for (Node wn : w.nodes) {
|
---|
99 | if (n.equals(wn)) {
|
---|
100 | Integer old = wayOccurenceCounter.get(w);
|
---|
101 | wayOccurenceCounter.put(w, (old == null) ? 1 : old+1);
|
---|
102 | break;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | if (wayOccurenceCounter.isEmpty()) {
|
---|
108 | JOptionPane.showMessageDialog(Main.parent,
|
---|
109 | trn("The selected node is not part of any way.",
|
---|
110 | "The selected nodes are not part of any way.", selectedNodes.size()));
|
---|
111 | return;
|
---|
112 | }
|
---|
113 |
|
---|
114 | for (Entry<Way, Integer> entry : wayOccurenceCounter.entrySet()) {
|
---|
115 | if (entry.getValue().equals(selectedNodes.size())) {
|
---|
116 | if (selectedWay != null) {
|
---|
117 | JOptionPane.showMessageDialog(Main.parent, tr("There is more than one way using the node(s) you selected. Please select the way also."));
|
---|
118 | return;
|
---|
119 | }
|
---|
120 | selectedWay = entry.getKey();
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (selectedWay == null) {
|
---|
125 | JOptionPane.showMessageDialog(Main.parent, tr("The selected nodes do not share the same way."));
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // If a way and nodes are selected, verify that the nodes are part of the way.
|
---|
130 | } else if (selectedWay != null && selectedNodes != null) {
|
---|
131 |
|
---|
132 | HashSet<Node> nds = new HashSet<Node>(selectedNodes);
|
---|
133 | for (Node n : selectedWay.nodes) {
|
---|
134 | nds.remove(n);
|
---|
135 | }
|
---|
136 | if (!nds.isEmpty()) {
|
---|
137 | JOptionPane.showMessageDialog(Main.parent,
|
---|
138 | trn("The selected way does not contain the selected node.",
|
---|
139 | "The selected way does not contain all the selected nodes.", selectedNodes.size()));
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | // and then do the work.
|
---|
145 | splitWay();
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Checks if the selection consists of something we can work with.
|
---|
150 | * Checks only if the number and type of items selected looks good;
|
---|
151 | * does not check whether the selected items are really a valid
|
---|
152 | * input for splitting (this would be too expensive to be carried
|
---|
153 | * out from the selectionChanged listener).
|
---|
154 | */
|
---|
155 | private boolean checkSelection(Collection<? extends OsmPrimitive> selection) {
|
---|
156 | boolean way = false;
|
---|
157 | boolean node = false;
|
---|
158 | for (OsmPrimitive p : selection) {
|
---|
159 | if (p instanceof Way && !way) {
|
---|
160 | way = true;
|
---|
161 | } else if (p instanceof Node) {
|
---|
162 | node = true;
|
---|
163 | } else {
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | return node;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Split a way into two or more parts, starting at a selected node.
|
---|
172 | *
|
---|
173 | * FIXME: what do the following "arguments" refer to?
|
---|
174 | * @param way the way to split
|
---|
175 | * @param nodes the node(s) to split the way at; must be part of the way.
|
---|
176 | */
|
---|
177 | private void splitWay() {
|
---|
178 | // We take our way's list of nodes and copy them to a way chunk (a
|
---|
179 | // list of nodes). Whenever we stumble upon a selected node, we start
|
---|
180 | // a new way chunk.
|
---|
181 |
|
---|
182 | Set<Node> nodeSet = new HashSet<Node>(selectedNodes);
|
---|
183 | List<List<Node>> wayChunks = new LinkedList<List<Node>>();
|
---|
184 | List<Node> currentWayChunk = new ArrayList<Node>();
|
---|
185 | wayChunks.add(currentWayChunk);
|
---|
186 |
|
---|
187 | Iterator<Node> it = selectedWay.nodes.iterator();
|
---|
188 | while (it.hasNext()) {
|
---|
189 | Node currentNode = it.next();
|
---|
190 | boolean atEndOfWay = currentWayChunk.isEmpty() || !it.hasNext();
|
---|
191 | currentWayChunk.add(currentNode);
|
---|
192 | if (nodeSet.contains(currentNode) && !atEndOfWay) {
|
---|
193 | currentWayChunk = new ArrayList<Node>();
|
---|
194 | currentWayChunk.add(currentNode);
|
---|
195 | wayChunks.add(currentWayChunk);
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (wayChunks.size() < 2) {
|
---|
200 | JOptionPane.showMessageDialog(Main.parent, tr("The way cannot be split at the selected nodes. (Hint: Select nodes in the middle of the way.)"));
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | // build a list of commands, and also a new selection list
|
---|
205 | Collection<Command> commandList = new ArrayList<Command>(wayChunks.size());
|
---|
206 | Collection<Way> newSelection = new ArrayList<Way>(wayChunks.size());
|
---|
207 |
|
---|
208 | Iterator<List<Node>> chunkIt = wayChunks.iterator();
|
---|
209 |
|
---|
210 | // First, change the original way
|
---|
211 | Way changedWay = new Way(selectedWay);
|
---|
212 | changedWay.nodes.clear();
|
---|
213 | changedWay.nodes.addAll(chunkIt.next());
|
---|
214 | commandList.add(new ChangeCommand(selectedWay, changedWay));
|
---|
215 | newSelection.add(selectedWay);
|
---|
216 |
|
---|
217 | // Second, create new ways
|
---|
218 | while (chunkIt.hasNext()) {
|
---|
219 | Way wayToAdd = new Way();
|
---|
220 | if (selectedWay.keys != null)
|
---|
221 | wayToAdd.keys = new HashMap<String, String>(selectedWay.keys);
|
---|
222 | wayToAdd.nodes.addAll(chunkIt.next());
|
---|
223 | commandList.add(new AddCommand(wayToAdd));
|
---|
224 | newSelection.add(wayToAdd);
|
---|
225 | }
|
---|
226 |
|
---|
227 | NameVisitor v = new NameVisitor();
|
---|
228 | v.visit(selectedWay);
|
---|
229 | Main.main.undoRedo.add(
|
---|
230 | new SequenceCommand(tr("Split way {0} into {1} parts",
|
---|
231 | v.name, wayChunks.size()),
|
---|
232 | commandList));
|
---|
233 | Main.ds.setSelected(newSelection);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Enable the "split way" menu option if the selection looks like we could use it.
|
---|
238 | */
|
---|
239 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
240 | setEnabled(checkSelection(newSelection));
|
---|
241 | }
|
---|
242 | }
|
---|