source: josm/trunk/src/org/openstreetmap/josm/actions/MergeNodesAction.java@ 1180

Last change on this file since 1180 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

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