source: josm/trunk/src/org/openstreetmap/josm/command/PurgePrimitivesCommand.java@ 1898

Last change on this file since 1898 was 1898, checked in by jttt, 15 years ago

Way refactoring - rewritten another parts of code to the new nodes api

File size: 9.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.List;
9import java.util.logging.Logger;
10
11import javax.swing.JLabel;
12import javax.swing.tree.DefaultMutableTreeNode;
13import javax.swing.tree.MutableTreeNode;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.conflict.ConflictCollection;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Relation;
21import org.openstreetmap.josm.data.osm.RelationMember;
22import org.openstreetmap.josm.data.osm.Way;
23import org.openstreetmap.josm.tools.ImageProvider;
24
25/**
26 * Physically removes an {@see OsmPrimitive} from the dataset of the edit
27 * layer and disconnects any references from {@see Way}s or {@see Relation}s
28 * to this primitive.
29 *
30 * This command is necessary if a local {@see OsmPrimitive} has been deleted on
31 * the server by another user and if the local user decides to delete his version
32 * too. If he only deleted it "logically" JOSM would try to delete it on the server
33 * which would result in an non resolvable conflict.
34 *
35 */
36public class PurgePrimitivesCommand extends ConflictResolveCommand{
37
38 static private final Logger logger = Logger.getLogger(PurgePrimitivesCommand.class.getName());
39
40 /**
41 * Represents a pair of {@see OsmPrimitive} where the parent referrs to
42 * the child, either because a {@see Way} includes a {@see Node} or
43 * because a {@see Relation} refers to any other {@see OsmPrimitive}
44 * via a relation member.
45 *
46 */
47 static class OsmParentChildPair {
48 private OsmPrimitive parent;
49 private OsmPrimitive child;
50
51
52 public OsmParentChildPair(OsmPrimitive parent, OsmPrimitive child) {
53 this.parent = parent;
54 this.child = child;
55 }
56
57 public OsmPrimitive getParent() {
58 return parent;
59 }
60
61 public OsmPrimitive getChild() {
62 return child;
63 }
64
65 @Override
66 public int hashCode() {
67 final int prime = 31;
68 int result = 1;
69 result = prime * result + ((child == null) ? 0 : child.hashCode());
70 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
71 return result;
72 }
73
74 @Override
75 public boolean equals(Object obj) {
76 if (this == obj)
77 return true;
78 if (obj == null)
79 return false;
80 if (getClass() != obj.getClass())
81 return false;
82 OsmParentChildPair other = (OsmParentChildPair) obj;
83 if (child == null) {
84 if (other.child != null)
85 return false;
86 } else if (child != other.child)
87 return false;
88 if (parent == null) {
89 if (other.parent != null)
90 return false;
91 } else if (parent != other.parent)
92 return false;
93 return true;
94 }
95 }
96
97 /**
98 * creates a list of all {@see OsmParentChildPair}s for a given {@see OsmPrimitive}
99 * as child and given set of parents. We don't use {@see CollectBackReferencesVisitor}
100 * because it seems quite inefficient.
101 *
102 * @param parents the set of potential parents
103 * @param child the child
104 * @return the list of {@see OsmParentChildPair}
105 */
106 protected List<OsmParentChildPair> getParentChildPairs(List<OsmPrimitive> parents, OsmPrimitive child) {
107 ArrayList<OsmParentChildPair> pairs = new ArrayList<OsmParentChildPair>();
108 for (OsmPrimitive parent : parents) {
109 if (parent instanceof Way) {
110 Way w = (Way)parent;
111 for (OsmPrimitive node : w.getNodes()) {
112 if (node == child) {
113 OsmParentChildPair pair = new OsmParentChildPair(parent, node);
114 if (! pairs.contains(pair)) {
115 pairs.add(pair);
116 }
117 }
118 }
119 } else if (parent instanceof Relation) {
120 Relation r = (Relation)parent;
121 for (RelationMember member : r.members) {
122 if (member.member == child) {
123 OsmParentChildPair pair = new OsmParentChildPair(parent, member.member);
124 if (! pairs.contains(pair)) {
125 pairs.add(pair);
126 }
127 }
128 }
129 }
130 }
131 return pairs;
132 }
133
134 /** the primitive to purge */
135 private OsmPrimitive primitive;
136
137 /** the set of primitives to purge as consequence of purging
138 * {@see #primitive}, including {@see #primitive}
139 */
140 private ArrayList<OsmPrimitive> purgedPrimitives;
141
142 /** the set of {@see OsmParentChildPair}. We keep a reference
143 * to this set for the {@see #fillModifiedData(Collection, Collection, Collection)} operation
144 */
145 private ArrayList<OsmParentChildPair> pairs;
146
147
148 /**
149 * constructor
150 * @param node the node to undelete
151 */
152 public PurgePrimitivesCommand(OsmPrimitive primitive) {
153 this.primitive = primitive;
154 purgedPrimitives = new ArrayList<OsmPrimitive>();
155 pairs = new ArrayList<OsmParentChildPair>();
156 }
157
158 @Override
159 public MutableTreeNode description() {
160 return new DefaultMutableTreeNode(
161 new JLabel(
162 tr("Purging 1 primitive"),
163 ImageProvider.get("data", "object"),
164 JLabel.HORIZONTAL
165 )
166 );
167 }
168
169 /**
170 * Purges an {@see OsmPrimitive} <code>toPurge</code> from a {@see DataSet}.
171 *
172 * @param toPurge the primitive to purge
173 * @param ds the dataset to purge from
174 * @param hive the hive of {@see OsmPrimitive}s we remember other {@see OsmPrimitive}
175 * we have to purge because we purge <code>toPurge</code>.
176 *
177 */
178 protected void purge(OsmPrimitive toPurge, DataSet ds, ArrayList<OsmPrimitive> hive) {
179 ArrayList<OsmPrimitive> parents = new ArrayList<OsmPrimitive>();
180 parents.addAll(getLayer().data.ways);
181 parents.addAll(getLayer().data.relations);
182 List<OsmParentChildPair> pairs = getParentChildPairs(parents, primitive);
183 hive.remove(toPurge);
184 for (OsmParentChildPair pair: pairs) {
185 if (pair.getParent() instanceof Way) {
186 Way w = (Way)pair.getParent();
187 System.out.println(tr("removing reference from way {0}",w.id));
188 w.nodes.remove(primitive);
189 // if a way ends up with less than two node we
190 // remember it on the "hive"
191 //
192 if (w.getNodesCount() < 2) {
193 System.out.println(tr("Warning: Purging way {0} because number of nodes dropped below 2. Current is {1}",
194 w.id,w.getNodesCount()));
195 if (!hive.contains(w)) {
196 hive.add(w);
197 }
198 }
199 } else if (pair.getParent() instanceof Relation) {
200 Relation r = (Relation)pair.getParent();
201 System.out.println(tr("removing reference from relation {0}",r.id));
202 r.removeMembersFor(primitive);
203 }
204 }
205 }
206
207 @Override
208 public boolean executeCommand() {
209 ArrayList<OsmPrimitive> hive = new ArrayList<OsmPrimitive>();
210
211 // iteratively purge the primitive and all primitives
212 // which violate invariants after they loose a reference to
213 // the primitive (i.e. ways which end up with less than two
214 // nodes)
215 hive.add(primitive);
216 while(! hive.isEmpty()) {
217 OsmPrimitive toPurge = hive.get(0);
218 purge(toPurge, getLayer().data, hive);
219 if (toPurge instanceof Node) {
220 getLayer().data.nodes.remove(toPurge);
221 } else if (primitive instanceof Way) {
222 getLayer().data.ways.remove(toPurge);
223 } else if (primitive instanceof Relation) {
224 getLayer().data.relations.remove(toPurge);
225 }
226 purgedPrimitives.add(toPurge);
227 ConflictCollection conflicts = getLayer().getConflicts();
228 if (conflicts.hasConflictForMy(toPurge)) {
229 rememberConflict(conflicts.getConflictForMy(toPurge));
230 conflicts.remove(toPurge);
231 }
232 }
233 return super.executeCommand();
234 }
235
236 @Override
237 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
238 Collection<OsmPrimitive> added) {
239 for (OsmParentChildPair pair : pairs) {
240 modified.add(pair.getParent());
241 }
242 // we don't need pairs anymore
243 pairs = null;
244 }
245
246 @Override
247 public void undoCommand() {
248 if (! Main.map.mapView.hasLayer(getLayer())) {
249 logger.warning(tr("Can't undo command ''{0}'' because layer ''{1}'' is not present anymore",
250 this.toString(),
251 getLayer().toString()
252 ));
253 return;
254 }
255 Main.map.mapView.setActiveLayer(getLayer());
256
257 // restore purged primitives
258 //
259 for (OsmPrimitive purged : purgedPrimitives) {
260 getLayer().data.addPrimitive(purged);
261 }
262 reconstituteConflicts();
263 // will restore the former references to the purged nodes
264 //
265 super.undoCommand();
266 }
267}
Note: See TracBrowser for help on using the repository browser.