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

Last change on this file since 1857 was 1814, checked in by Gubaer, 15 years ago

removed dependencies to Main.ds, removed Main.ds
removed AddVisitor, NameVisitor, DeleteVisitor - unnecessary double dispatching for these simple cases

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