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

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