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 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
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.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.Map;
|
---|
15 | import java.util.Set;
|
---|
16 | import java.util.TreeMap;
|
---|
17 | import java.util.TreeSet;
|
---|
18 | import java.util.Map.Entry;
|
---|
19 |
|
---|
20 | import javax.swing.Box;
|
---|
21 | import javax.swing.JComboBox;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JOptionPane;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
28 | import org.openstreetmap.josm.command.Command;
|
---|
29 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
30 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
31 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
32 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
33 | import org.openstreetmap.josm.data.osm.Node;
|
---|
34 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
35 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
36 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
37 | import org.openstreetmap.josm.data.osm.TigerUtils;
|
---|
38 | import org.openstreetmap.josm.data.osm.Way;
|
---|
39 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
40 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
41 | import org.openstreetmap.josm.tools.GBC;
|
---|
42 | import org.openstreetmap.josm.tools.Pair;
|
---|
43 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Merge two or more nodes into one node.
|
---|
48 | * (based on Combine ways)
|
---|
49 | *
|
---|
50 | * @author Matthew Newton
|
---|
51 | *
|
---|
52 | */
|
---|
53 | public class MergeNodesAction extends JosmAction implements SelectionChangedListener {
|
---|
54 |
|
---|
55 | public MergeNodesAction() {
|
---|
56 | super(tr("Merge Nodes"), "mergenodes", tr("Merge nodes into the oldest one."),
|
---|
57 | Shortcut.registerShortcut("tools:mergenodes", tr("Tool: {0}", tr("Merge Nodes")), KeyEvent.VK_M, Shortcut.GROUP_EDIT), true);
|
---|
58 | DataSet.selListeners.add(this);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void actionPerformed(ActionEvent event) {
|
---|
62 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
63 | LinkedList<Node> selectedNodes = new LinkedList<Node>();
|
---|
64 |
|
---|
65 | // the selection check should stop this procedure starting if
|
---|
66 | // nothing but node are selected - otherwise we don't care
|
---|
67 | // anyway as long as we have at least two nodes
|
---|
68 | for (OsmPrimitive osm : selection)
|
---|
69 | if (osm instanceof Node)
|
---|
70 | selectedNodes.add((Node)osm);
|
---|
71 |
|
---|
72 | if (selectedNodes.size() < 2) {
|
---|
73 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least two nodes to merge."));
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // Find which node to merge into (i.e. which one will be left)
|
---|
78 | // - this should be combined from two things:
|
---|
79 | // 1. It will be the first node in the list that has a
|
---|
80 | // positive ID number, OR the first node.
|
---|
81 | // 2. It will be at the position of the first node in the
|
---|
82 | // list.
|
---|
83 | //
|
---|
84 | // *However* - there is the problem that the selection list is
|
---|
85 | // _not_ in the order that the nodes were clicked on, meaning
|
---|
86 | // that the user doesn't know which node will be chosen (so
|
---|
87 | // (2) is not implemented yet.) :-(
|
---|
88 | Node useNode = null;
|
---|
89 | for (Node n: selectedNodes) {
|
---|
90 | if (n.id > 0) {
|
---|
91 | useNode = n;
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | if (useNode == null)
|
---|
96 | useNode = selectedNodes.iterator().next();
|
---|
97 |
|
---|
98 | mergeNodes(selectedNodes, useNode);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * really do the merging - returns the node that is left
|
---|
103 | */
|
---|
104 | public static Node mergeNodes(LinkedList<Node> allNodes, Node dest) {
|
---|
105 | Node newNode = new Node(dest);
|
---|
106 |
|
---|
107 | // Check whether all ways have identical relationship membership. More
|
---|
108 | // specifically: If one of the selected ways is a member of relation X
|
---|
109 | // in role Y, then all selected ways must be members of X in role Y.
|
---|
110 |
|
---|
111 | // FIXME: In a later revision, we should display some sort of conflict
|
---|
112 | // dialog like we do for tags, to let the user choose which relations
|
---|
113 | // should be kept.
|
---|
114 |
|
---|
115 | // Step 1, iterate over all relations and figure out which of our
|
---|
116 | // selected ways are members of a relation.
|
---|
117 | HashMap<Pair<Relation,String>, HashSet<Node>> backlinks =
|
---|
118 | new HashMap<Pair<Relation,String>, HashSet<Node>>();
|
---|
119 | HashSet<Relation> relationsUsingNodes = new HashSet<Relation>();
|
---|
120 | for (Relation r : Main.ds.relations) {
|
---|
121 | if (r.deleted || r.incomplete) continue;
|
---|
122 | for (RelationMember rm : r.members) {
|
---|
123 | if (rm.member instanceof Node) {
|
---|
124 | for (Node n : allNodes) {
|
---|
125 | if (rm.member == n) {
|
---|
126 | Pair<Relation,String> pair = new Pair<Relation,String>(r, rm.role);
|
---|
127 | HashSet<Node> nodelinks = new HashSet<Node>();
|
---|
128 | if (backlinks.containsKey(pair)) {
|
---|
129 | nodelinks = backlinks.get(pair);
|
---|
130 | } else {
|
---|
131 | nodelinks = new HashSet<Node>();
|
---|
132 | backlinks.put(pair, nodelinks);
|
---|
133 | }
|
---|
134 | nodelinks.add(n);
|
---|
135 |
|
---|
136 | // this is just a cache for later use
|
---|
137 | relationsUsingNodes.add(r);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | // Complain to the user if the ways don't have equal memberships.
|
---|
145 | for (HashSet<Node> nodelinks : backlinks.values()) {
|
---|
146 | if (!nodelinks.containsAll(allNodes)) {
|
---|
147 | int option = new ExtendedDialog(Main.parent,
|
---|
148 | tr("Merge nodes with different memberships?"),
|
---|
149 | tr("The selected nodes have differing relation memberships. "
|
---|
150 | + "Do you still want to merge them?"),
|
---|
151 | new String[] {tr("Merge Anyway"), tr("Cancel")},
|
---|
152 | new String[] {"mergenodes.png", "cancel.png"}).getValue();
|
---|
153 | if (option == 1) break;
|
---|
154 | return null;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | // collect properties for later conflict resolving
|
---|
159 | Map<String, Set<String>> props = new TreeMap<String, Set<String>>();
|
---|
160 | for (Node n : allNodes) {
|
---|
161 | for (Entry<String,String> e : n.entrySet()) {
|
---|
162 | if (!props.containsKey(e.getKey()))
|
---|
163 | props.put(e.getKey(), new TreeSet<String>());
|
---|
164 | props.get(e.getKey()).add(e.getValue());
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | // display conflict dialog
|
---|
169 | Map<String, JComboBox> components = new HashMap<String, JComboBox>();
|
---|
170 | JPanel p = new JPanel(new GridBagLayout());
|
---|
171 | for (Entry<String, Set<String>> e : props.entrySet()) {
|
---|
172 | if (TigerUtils.isTigerTag(e.getKey())) {
|
---|
173 | String combined = TigerUtils.combineTags(e.getKey(), e.getValue());
|
---|
174 | newNode.put(e.getKey(), combined);
|
---|
175 | } else if (e.getValue().size() > 1) {
|
---|
176 | JComboBox c = new JComboBox(e.getValue().toArray());
|
---|
177 | c.setEditable(true);
|
---|
178 | p.add(new JLabel(e.getKey()), GBC.std());
|
---|
179 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
180 | p.add(c, GBC.eol());
|
---|
181 | components.put(e.getKey(), c);
|
---|
182 | } else {
|
---|
183 | newNode.put(e.getKey(), e.getValue().iterator().next());
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (!components.isEmpty()) {
|
---|
188 | int answer = new ExtendedDialog(Main.parent,
|
---|
189 | tr("Enter values for all conflicts."),
|
---|
190 | p,
|
---|
191 | new String[] {tr("Solve Conflicts"), tr("Cancel")},
|
---|
192 | new String[] {"dialogs/conflict.png", "cancel.png"}).getValue();
|
---|
193 | if (answer != 1)
|
---|
194 | return null;
|
---|
195 | for (Entry<String, JComboBox> e : components.entrySet())
|
---|
196 | newNode.put(e.getKey(), e.getValue().getEditor().getItem().toString());
|
---|
197 | }
|
---|
198 |
|
---|
199 | LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
200 | cmds.add(new ChangeCommand(dest, newNode));
|
---|
201 |
|
---|
202 | Collection<OsmPrimitive> del = new HashSet<OsmPrimitive>();
|
---|
203 |
|
---|
204 | for (Way w : Main.ds.ways) {
|
---|
205 | if (w.deleted || w.incomplete || w.nodes.size() < 1) continue;
|
---|
206 | boolean modify = false;
|
---|
207 | for (Node sn : allNodes) {
|
---|
208 | if (sn == dest) continue;
|
---|
209 | if (w.nodes.contains(sn)) {
|
---|
210 | modify = true;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | if (!modify) continue;
|
---|
214 | // OK - this way contains one or more nodes to change
|
---|
215 | ArrayList<Node> nn = new ArrayList<Node>();
|
---|
216 | Node lastNode = null;
|
---|
217 | for (int i = 0; i < w.nodes.size(); i++) {
|
---|
218 | Node pushNode = w.nodes.get(i);
|
---|
219 | if (allNodes.contains(pushNode)) {
|
---|
220 | pushNode = dest;
|
---|
221 | }
|
---|
222 | if (pushNode != lastNode) {
|
---|
223 | nn.add(pushNode);
|
---|
224 | }
|
---|
225 | lastNode = pushNode;
|
---|
226 | }
|
---|
227 | if (nn.size() < 2) {
|
---|
228 | CollectBackReferencesVisitor backRefs =
|
---|
229 | new CollectBackReferencesVisitor(Main.ds, false);
|
---|
230 | w.visit(backRefs);
|
---|
231 | if (!backRefs.data.isEmpty()) {
|
---|
232 | JOptionPane.showMessageDialog(Main.parent,
|
---|
233 | tr("Cannot merge nodes: " +
|
---|
234 | "Would have to delete a way that is still used."));
|
---|
235 | return null;
|
---|
236 | }
|
---|
237 | del.add(w);
|
---|
238 | } else {
|
---|
239 | Way newWay = new Way(w);
|
---|
240 | newWay.nodes.clear();
|
---|
241 | newWay.nodes.addAll(nn);
|
---|
242 | cmds.add(new ChangeCommand(w, newWay));
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | // delete any merged nodes
|
---|
247 | del.addAll(allNodes);
|
---|
248 | del.remove(dest);
|
---|
249 | if (!del.isEmpty()) cmds.add(new DeleteCommand(del));
|
---|
250 |
|
---|
251 | // modify all relations containing the now-deleted nodes
|
---|
252 | for (Relation r : relationsUsingNodes) {
|
---|
253 | Relation newRel = new Relation(r);
|
---|
254 | newRel.members.clear();
|
---|
255 | HashSet<String> rolesToReAdd = new HashSet<String>();
|
---|
256 | for (RelationMember rm : r.members) {
|
---|
257 | // Don't copy the member if it points to one of our nodes,
|
---|
258 | // just keep a note to re-add it later on.
|
---|
259 | if (allNodes.contains(rm.member)) {
|
---|
260 | rolesToReAdd.add(rm.role);
|
---|
261 | } else {
|
---|
262 | newRel.members.add(rm);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | for (String role : rolesToReAdd) {
|
---|
266 | newRel.members.add(new RelationMember(role, dest));
|
---|
267 | }
|
---|
268 | cmds.add(new ChangeCommand(r, newRel));
|
---|
269 | }
|
---|
270 |
|
---|
271 | Main.main.undoRedo.add(new SequenceCommand(tr("Merge {0} nodes", allNodes.size()), cmds));
|
---|
272 | Main.ds.setSelected(dest);
|
---|
273 |
|
---|
274 | return dest;
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Enable the "Merge Nodes" menu option if more then one node is selected
|
---|
280 | */
|
---|
281 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
282 | boolean ok = true;
|
---|
283 | if (newSelection.size() < 2) {
|
---|
284 | setEnabled(false);
|
---|
285 | return;
|
---|
286 | }
|
---|
287 | for (OsmPrimitive osm : newSelection) {
|
---|
288 | if (!(osm instanceof Node)) {
|
---|
289 | ok = false;
|
---|
290 | break;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | setEnabled(ok);
|
---|
294 | }
|
---|
295 | }
|
---|