1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.pair.properties;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType.UNDECIDED;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.beans.PropertyChangeListener;
|
---|
9 | import java.beans.PropertyChangeSupport;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Observable;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.command.Command;
|
---|
19 | import org.openstreetmap.josm.command.CoordinateConflictResolveCommand;
|
---|
20 | import org.openstreetmap.josm.command.DeletedStateConflictResolveCommand;
|
---|
21 | import org.openstreetmap.josm.command.PurgePrimitivesCommand;
|
---|
22 | import org.openstreetmap.josm.command.UndeletePrimitivesCommand;
|
---|
23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
24 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
25 | import org.openstreetmap.josm.data.osm.Node;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
27 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
28 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
29 | import org.openstreetmap.josm.data.osm.Way;
|
---|
30 | import org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType;
|
---|
31 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
32 | import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
|
---|
33 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
34 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * This is the model for resolving conflicts in the properties of the
|
---|
38 | * {@see OsmPrimitive}s. In particular, it represents conflicts in the coordiates of {@see Node}s and
|
---|
39 | * the deleted or visible state of {@see OsmPrimitive}s.
|
---|
40 | *
|
---|
41 | * This model is an {@see Observable}. It notifies registered {@see Observer}s whenever the
|
---|
42 | * internal state changes.
|
---|
43 | *
|
---|
44 | * This model also emits property changes for {@see #RESOLVED_COMPLETELY_PROP}. Property change
|
---|
45 | * listeners may register themselves using {@see #addPropertyChangeListener(PropertyChangeListener)}.
|
---|
46 | *
|
---|
47 | * @see Node#getCoor()
|
---|
48 | * @see OsmPrimitive#deleted
|
---|
49 | * @see OsmPrimitive#visible
|
---|
50 | *
|
---|
51 | */
|
---|
52 | public class PropertiesMergeModel extends Observable {
|
---|
53 |
|
---|
54 | static public final String RESOLVED_COMPLETELY_PROP = PropertiesMergeModel.class.getName() + ".resolvedCompletely";
|
---|
55 | static public final String DELETE_PRIMITIVE_PROP = PropertiesMergeModel.class.getName() + ".deletePrimitive";
|
---|
56 |
|
---|
57 | private OsmPrimitive my;
|
---|
58 |
|
---|
59 | private LatLon myCoords;
|
---|
60 | private LatLon theirCoords;
|
---|
61 | private MergeDecisionType coordMergeDecision;
|
---|
62 |
|
---|
63 | private boolean myDeletedState;
|
---|
64 | private boolean theirDeletedState;
|
---|
65 | private boolean myVisibleState;
|
---|
66 | private boolean theirVisibleState;
|
---|
67 | private MergeDecisionType deletedMergeDecision;
|
---|
68 | private MergeDecisionType visibleMergeDecision;
|
---|
69 | private final PropertyChangeSupport support;
|
---|
70 | private boolean resolvedCompletely;
|
---|
71 |
|
---|
72 | public void addPropertyChangeListener(PropertyChangeListener listener) {
|
---|
73 | support.addPropertyChangeListener(listener);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void removePropertyChangeListener(PropertyChangeListener listener) {
|
---|
77 | support.removePropertyChangeListener(listener);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void fireCompletelyResolved() {
|
---|
81 | boolean oldValue = resolvedCompletely;
|
---|
82 | resolvedCompletely = isResolvedCompletely();
|
---|
83 | support.firePropertyChange(RESOLVED_COMPLETELY_PROP, oldValue, resolvedCompletely);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public PropertiesMergeModel() {
|
---|
87 | coordMergeDecision = UNDECIDED;
|
---|
88 | deletedMergeDecision = UNDECIDED;
|
---|
89 | support = new PropertyChangeSupport(this);
|
---|
90 | resolvedCompletely = false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * replies true if there is a coordinate conflict and if this conflict is
|
---|
95 | * resolved
|
---|
96 | *
|
---|
97 | * @return true if there is a coordinate conflict and if this conflict is
|
---|
98 | * resolved; false, otherwise
|
---|
99 | */
|
---|
100 | public boolean isDecidedCoord() {
|
---|
101 | return ! coordMergeDecision.equals(UNDECIDED);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * replies true if there is a conflict in the deleted state and if this conflict is
|
---|
106 | * resolved
|
---|
107 | *
|
---|
108 | * @return true if there is a conflict in the deleted state and if this conflict is
|
---|
109 | * resolved; false, otherwise
|
---|
110 | */
|
---|
111 | public boolean isDecidedDeletedState() {
|
---|
112 | return ! deletedMergeDecision.equals(UNDECIDED);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * replies true if there is a conflict in the visible state and if this conflict is
|
---|
117 | * resolved
|
---|
118 | *
|
---|
119 | * @return true if there is a conflict in the visible state and if this conflict is
|
---|
120 | * resolved; false, otherwise
|
---|
121 | */
|
---|
122 | public boolean isDecidedVisibleState() {
|
---|
123 | return ! visibleMergeDecision.equals(UNDECIDED);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * replies true if the current decision for the coordinate conflict is <code>decision</code>
|
---|
128 | *
|
---|
129 | * @return true if the current decision for the coordinate conflict is <code>decision</code>;
|
---|
130 | * false, otherwise
|
---|
131 | */
|
---|
132 | public boolean isCoordMergeDecision(MergeDecisionType decision) {
|
---|
133 | return coordMergeDecision.equals(decision);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * replies true if the current decision for the deleted state conflict is <code>decision</code>
|
---|
138 | *
|
---|
139 | * @return true if the current decision for the deleted state conflict is <code>decision</code>;
|
---|
140 | * false, otherwise
|
---|
141 | */
|
---|
142 | public boolean isDeletedStateDecision(MergeDecisionType decision) {
|
---|
143 | return deletedMergeDecision.equals(decision);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * replies true if the current decision for the visible state conflict is <code>decision</code>
|
---|
148 | *
|
---|
149 | * @return true if the current decision for the visible state conflict is <code>decision</code>;
|
---|
150 | * false, otherwise
|
---|
151 | */
|
---|
152 | public boolean isVisibleStateDecision(MergeDecisionType decision) {
|
---|
153 | return visibleMergeDecision.equals(decision);
|
---|
154 | }
|
---|
155 | /**
|
---|
156 | * populates the model with the differences between my and their version
|
---|
157 | *
|
---|
158 | * @param my my version of the primitive
|
---|
159 | * @param their their version of the primitive
|
---|
160 | */
|
---|
161 | public void populate(OsmPrimitive my, OsmPrimitive their) {
|
---|
162 | this.my = my;
|
---|
163 | if (my instanceof Node) {
|
---|
164 | myCoords = ((Node)my).getCoor();
|
---|
165 | theirCoords = ((Node)their).getCoor();
|
---|
166 | } else {
|
---|
167 | myCoords = null;
|
---|
168 | theirCoords = null;
|
---|
169 | }
|
---|
170 |
|
---|
171 | myDeletedState = my.isDeleted();
|
---|
172 | theirDeletedState = their.isDeleted();
|
---|
173 |
|
---|
174 | myVisibleState = my.isVisible();
|
---|
175 | theirVisibleState = their.isVisible();
|
---|
176 |
|
---|
177 | coordMergeDecision = UNDECIDED;
|
---|
178 | deletedMergeDecision = UNDECIDED;
|
---|
179 | visibleMergeDecision = UNDECIDED;
|
---|
180 | setChanged();
|
---|
181 | notifyObservers();
|
---|
182 | fireCompletelyResolved();
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * replies the coordinates of my {@see OsmPrimitive}. null, if my primitive hasn't
|
---|
187 | * coordinates (i.e. because it is a {@see Way}).
|
---|
188 | *
|
---|
189 | * @return the coordinates of my {@see OsmPrimitive}. null, if my primitive hasn't
|
---|
190 | * coordinates (i.e. because it is a {@see Way}).
|
---|
191 | */
|
---|
192 | public LatLon getMyCoords() {
|
---|
193 | return myCoords;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * replies the coordinates of their {@see OsmPrimitive}. null, if their primitive hasn't
|
---|
198 | * coordinates (i.e. because it is a {@see Way}).
|
---|
199 | *
|
---|
200 | * @return the coordinates of my {@see OsmPrimitive}. null, if my primitive hasn't
|
---|
201 | * coordinates (i.e. because it is a {@see Way}).
|
---|
202 | */
|
---|
203 | public LatLon getTheirCoords() {
|
---|
204 | return theirCoords;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * replies the coordinates of the merged {@see OsmPrimitive}. null, if the current primitives
|
---|
209 | * have no coordinates or if the conflict is yet {@see MergeDecisionType#UNDECIDED}
|
---|
210 | *
|
---|
211 | * @return the coordinates of the merged {@see OsmPrimitive}. null, if the current primitives
|
---|
212 | * have no coordinates or if the conflict is yet {@see MergeDecisionType#UNDECIDED}
|
---|
213 | */
|
---|
214 | public LatLon getMergedCoords() {
|
---|
215 | switch(coordMergeDecision) {
|
---|
216 | case KEEP_MINE: return myCoords;
|
---|
217 | case KEEP_THEIR: return theirCoords;
|
---|
218 | case UNDECIDED: return null;
|
---|
219 | }
|
---|
220 | // should not happen
|
---|
221 | return null;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * decides a conflict between my and their coordinates
|
---|
226 | *
|
---|
227 | * @param decision the decision
|
---|
228 | */
|
---|
229 | public void decideCoordsConflict(MergeDecisionType decision) {
|
---|
230 | coordMergeDecision = decision;
|
---|
231 | setChanged();
|
---|
232 | notifyObservers();
|
---|
233 | fireCompletelyResolved();
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * replies my deleted state,
|
---|
238 | * @return
|
---|
239 | */
|
---|
240 | public Boolean getMyDeletedState() {
|
---|
241 | return myDeletedState;
|
---|
242 | }
|
---|
243 |
|
---|
244 | public Boolean getTheirDeletedState() {
|
---|
245 | return theirDeletedState;
|
---|
246 | }
|
---|
247 |
|
---|
248 | public Boolean getMergedDeletedState() {
|
---|
249 | switch(deletedMergeDecision) {
|
---|
250 | case KEEP_MINE: return myDeletedState;
|
---|
251 | case KEEP_THEIR: return theirDeletedState;
|
---|
252 | case UNDECIDED: return null;
|
---|
253 | }
|
---|
254 | // should not happen
|
---|
255 | return null;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * replies my visible state,
|
---|
260 | * @return my visible state
|
---|
261 | */
|
---|
262 | public Boolean getMyVisibleState() {
|
---|
263 | return myVisibleState;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * replies their visible state,
|
---|
268 | * @return their visible state
|
---|
269 | */
|
---|
270 | public Boolean getTheirVisibleState() {
|
---|
271 | return theirVisibleState;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * replies the merged visible state; null, if the merge decision is
|
---|
276 | * {@see MergeDecisionType#UNDECIDED}.
|
---|
277 | *
|
---|
278 | * @return the merged visible state
|
---|
279 | */
|
---|
280 | public Boolean getMergedVisibleState() {
|
---|
281 | switch(visibleMergeDecision) {
|
---|
282 | case KEEP_MINE: return myVisibleState;
|
---|
283 | case KEEP_THEIR: return theirVisibleState;
|
---|
284 | case UNDECIDED: return null;
|
---|
285 | }
|
---|
286 | // should not happen
|
---|
287 | return null;
|
---|
288 | }
|
---|
289 |
|
---|
290 | private boolean getMergedDeletedState(MergeDecisionType decision) {
|
---|
291 | switch (decision) {
|
---|
292 | case KEEP_MINE:
|
---|
293 | return myDeletedState;
|
---|
294 | case KEEP_THEIR:
|
---|
295 | return theirDeletedState;
|
---|
296 | default:
|
---|
297 | return false;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * decides the conflict between two deleted states
|
---|
303 | * @param decision the decision (must not be null)
|
---|
304 | *
|
---|
305 | * @throws IllegalArgumentException thrown, if decision is null
|
---|
306 | */
|
---|
307 | public void decideDeletedStateConflict(MergeDecisionType decision) throws IllegalArgumentException{
|
---|
308 | CheckParameterUtil.ensureParameterNotNull(decision, "decision");
|
---|
309 |
|
---|
310 | boolean oldMergedDeletedState = getMergedDeletedState(this.deletedMergeDecision);
|
---|
311 | boolean newMergedDeletedState = getMergedDeletedState(decision);
|
---|
312 |
|
---|
313 | this.deletedMergeDecision = decision;
|
---|
314 | setChanged();
|
---|
315 | notifyObservers();
|
---|
316 | fireCompletelyResolved();
|
---|
317 |
|
---|
318 | if (oldMergedDeletedState != newMergedDeletedState) {
|
---|
319 | support.firePropertyChange(DELETE_PRIMITIVE_PROP, oldMergedDeletedState, newMergedDeletedState);
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * decides the conflict between two visible states
|
---|
325 | * @param decision the decision (must not be null)
|
---|
326 | *
|
---|
327 | * @throws IllegalArgumentException thrown, if decision is null
|
---|
328 | */
|
---|
329 | public void decideVisibleStateConflict(MergeDecisionType decision) throws IllegalArgumentException {
|
---|
330 | CheckParameterUtil.ensureParameterNotNull(decision, "decision");
|
---|
331 | this.visibleMergeDecision = decision;
|
---|
332 | setChanged();
|
---|
333 | notifyObservers();
|
---|
334 | fireCompletelyResolved();
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * replies true if my and their primitive have a conflict between
|
---|
339 | * their coordinate values
|
---|
340 | *
|
---|
341 | * @return true if my and their primitive have a conflict between
|
---|
342 | * their coordinate values; false otherwise
|
---|
343 | */
|
---|
344 | public boolean hasCoordConflict() {
|
---|
345 | if (myCoords == null && theirCoords != null) return true;
|
---|
346 | if (myCoords != null && theirCoords == null) return true;
|
---|
347 | if (myCoords == null && theirCoords == null) return false;
|
---|
348 | return !myCoords.equalsEpsilon(theirCoords);
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * replies true if my and their primitive have a conflict between
|
---|
353 | * their deleted states
|
---|
354 | *
|
---|
355 | * @return true if my and their primitive have a conflict between
|
---|
356 | * their deleted states
|
---|
357 | */
|
---|
358 | public boolean hasDeletedStateConflict() {
|
---|
359 | return myDeletedState != theirDeletedState;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * replies true if my and their primitive have a conflict between
|
---|
364 | * their visible states
|
---|
365 | *
|
---|
366 | * @return true if my and their primitive have a conflict between
|
---|
367 | * their visible states
|
---|
368 | */
|
---|
369 | public boolean hasVisibleStateConflict() {
|
---|
370 | return myVisibleState != theirVisibleState;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * replies true if all conflict in this model are resolved
|
---|
375 | *
|
---|
376 | * @return true if all conflict in this model are resolved; false otherwise
|
---|
377 | */
|
---|
378 | public boolean isResolvedCompletely() {
|
---|
379 | boolean ret = true;
|
---|
380 | if (hasCoordConflict()) {
|
---|
381 | ret = ret && ! coordMergeDecision.equals(UNDECIDED);
|
---|
382 | }
|
---|
383 | if (hasDeletedStateConflict()) {
|
---|
384 | ret = ret && ! deletedMergeDecision.equals(UNDECIDED);
|
---|
385 | }
|
---|
386 | if (hasVisibleStateConflict()) {
|
---|
387 | ret = ret && ! visibleMergeDecision.equals(UNDECIDED);
|
---|
388 | }
|
---|
389 | return ret;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * builds the command(s) to apply the conflict resolutions to my primitive
|
---|
394 | *
|
---|
395 | * @param my my primitive
|
---|
396 | * @param their their primitive
|
---|
397 | * @return the list of commands
|
---|
398 | */
|
---|
399 | public List<Command> buildResolveCommand(OsmPrimitive my, OsmPrimitive their) throws OperationCancelledException{
|
---|
400 | ArrayList<Command> cmds = new ArrayList<Command>();
|
---|
401 | if (hasVisibleStateConflict() && isDecidedVisibleState()) {
|
---|
402 | if (isVisibleStateDecision(MergeDecisionType.KEEP_MINE)) {
|
---|
403 | try {
|
---|
404 | UndeletePrimitivesCommand cmd = createUndeletePrimitiveCommand(my);
|
---|
405 | if (cmd == null)
|
---|
406 | throw new OperationCancelledException();
|
---|
407 | cmds.add(cmd);
|
---|
408 | } catch(OsmTransferException e) {
|
---|
409 | handleExceptionWhileBuildingCommand(e);
|
---|
410 | throw new OperationCancelledException(e);
|
---|
411 | }
|
---|
412 | } else if (isVisibleStateDecision(MergeDecisionType.KEEP_THEIR)) {
|
---|
413 | cmds.add(new PurgePrimitivesCommand(my));
|
---|
414 | }
|
---|
415 | }
|
---|
416 | if (hasCoordConflict() && isDecidedCoord()) {
|
---|
417 | cmds.add(new CoordinateConflictResolveCommand((Node)my, (Node)their, coordMergeDecision));
|
---|
418 | }
|
---|
419 | if (hasDeletedStateConflict() && isDecidedDeletedState()) {
|
---|
420 | cmds.add(new DeletedStateConflictResolveCommand(my, their, deletedMergeDecision));
|
---|
421 | }
|
---|
422 | return cmds;
|
---|
423 | }
|
---|
424 |
|
---|
425 | public OsmPrimitive getMyPrimitive() {
|
---|
426 | return my;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | *
|
---|
431 | * @param id
|
---|
432 | */
|
---|
433 | protected void handleExceptionWhileBuildingCommand(Exception e) {
|
---|
434 | e.printStackTrace();
|
---|
435 | String msg = e.getMessage() != null ? e.getMessage() : e.toString();
|
---|
436 | msg = msg.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
---|
437 | JOptionPane.showMessageDialog(
|
---|
438 | Main.parent,
|
---|
439 | tr("<html>An error occurred while communicating with the server<br>"
|
---|
440 | + "Details: {0}</html>",
|
---|
441 | msg
|
---|
442 | ),
|
---|
443 | tr("Communication with server failed"),
|
---|
444 | JOptionPane.ERROR_MESSAGE
|
---|
445 | );
|
---|
446 | }
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * User has decided to keep his local version of a primitive which had been deleted
|
---|
450 | * on the server
|
---|
451 | *
|
---|
452 | * @param id the primitive id
|
---|
453 | */
|
---|
454 | protected UndeletePrimitivesCommand createUndeletePrimitiveCommand(OsmPrimitive my) throws OsmTransferException {
|
---|
455 | if (my instanceof Node)
|
---|
456 | return createUndeleteNodeCommand((Node)my);
|
---|
457 | else if (my instanceof Way)
|
---|
458 | return createUndeleteWayCommand((Way)my);
|
---|
459 | else if (my instanceof Relation)
|
---|
460 | return createUndeleteRelationCommand((Relation)my);
|
---|
461 | return null;
|
---|
462 | }
|
---|
463 | /**
|
---|
464 | * Undelete a node which is already deleted on the server. The API
|
---|
465 | * doesn't offer a call for "undeleting" a node. We therefore create
|
---|
466 | * a clone of the node which we flag as new. On the next upload the
|
---|
467 | * server will assign the node a new id.
|
---|
468 | *
|
---|
469 | * @param node the node to undelete
|
---|
470 | */
|
---|
471 | protected UndeletePrimitivesCommand createUndeleteNodeCommand(Node node) {
|
---|
472 | return new UndeletePrimitivesCommand(node);
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * displays a confirmation message. The user has to confirm that additional dependent
|
---|
477 | * nodes should be undeleted too.
|
---|
478 | *
|
---|
479 | * @param way the way
|
---|
480 | * @param dependent a list of dependent nodes which have to be undelete too
|
---|
481 | * @return true, if the user confirms; false, otherwise
|
---|
482 | */
|
---|
483 | protected boolean confirmUndeleteDependentPrimitives(Way way, ArrayList<OsmPrimitive> dependent) {
|
---|
484 | String [] options = {
|
---|
485 | tr("Yes, undelete them too"),
|
---|
486 | tr("No, cancel operation")
|
---|
487 | };
|
---|
488 | int ret = JOptionPane.showOptionDialog(
|
---|
489 | Main.parent,
|
---|
490 | "<html>" + trn("There is {0} additional node used by way {1}<br>"
|
---|
491 | + "which is deleted on the server."
|
---|
492 | + "<br><br>"
|
---|
493 | + "Do you want to undelete this node too?",
|
---|
494 | "There are {0} additional nodes used by way {1}<br>"
|
---|
495 | + "which are deleted on the server."
|
---|
496 | + "<br><br>"
|
---|
497 | + "Do you want to undelete these nodes too?",
|
---|
498 | dependent.size(), dependent.size(), way.getId())
|
---|
499 | + "</html>",
|
---|
500 | tr("Undelete additional nodes?"),
|
---|
501 | JOptionPane.YES_NO_OPTION,
|
---|
502 | JOptionPane.QUESTION_MESSAGE,
|
---|
503 | null,
|
---|
504 | options,
|
---|
505 | options[0]
|
---|
506 | );
|
---|
507 |
|
---|
508 | switch(ret) {
|
---|
509 | case JOptionPane.CLOSED_OPTION: return false;
|
---|
510 | case JOptionPane.YES_OPTION: return true;
|
---|
511 | case JOptionPane.NO_OPTION: return false;
|
---|
512 | }
|
---|
513 | return false;
|
---|
514 |
|
---|
515 | }
|
---|
516 |
|
---|
517 | protected boolean confirmUndeleteDependentPrimitives(Relation r, ArrayList<OsmPrimitive> dependent) {
|
---|
518 | String [] options = {
|
---|
519 | tr("Yes, undelete them too"),
|
---|
520 | tr("No, cancel operation")
|
---|
521 | };
|
---|
522 | int ret = JOptionPane.showOptionDialog(
|
---|
523 | Main.parent,
|
---|
524 | "<html>" + trn("There is {0} additional primitive referred to by relation {1}<br>"
|
---|
525 | + "which is deleted on the server."
|
---|
526 | + "<br><br>"
|
---|
527 | + "Do you want to undelete this too?",
|
---|
528 | "There are {0} additional primitives referred to by relation {1}<br>"
|
---|
529 | + "which are deleted on the server."
|
---|
530 | + "<br><br>"
|
---|
531 | + "Do you want to undelete these too?",
|
---|
532 | dependent.size(), dependent.size(), r.getId())
|
---|
533 | + "</html>",
|
---|
534 | tr("Undelete dependent primitives?"),
|
---|
535 | JOptionPane.YES_NO_OPTION,
|
---|
536 | JOptionPane.QUESTION_MESSAGE,
|
---|
537 | null,
|
---|
538 | options,
|
---|
539 | options[0]
|
---|
540 | );
|
---|
541 |
|
---|
542 | switch(ret) {
|
---|
543 | case JOptionPane.CLOSED_OPTION: return false;
|
---|
544 | case JOptionPane.YES_OPTION: return true;
|
---|
545 | case JOptionPane.NO_OPTION: return false;
|
---|
546 | }
|
---|
547 | return false;
|
---|
548 |
|
---|
549 | }
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * Creates the undelete command for a way which is already deleted on the server.
|
---|
553 | *
|
---|
554 | * This method also checks whether there are additional nodes referred to by
|
---|
555 | * this way which are deleted on the server too.
|
---|
556 | *
|
---|
557 | * @param way the way to undelete
|
---|
558 | * @return the undelete command
|
---|
559 | * @see #createUndeleteNodeCommand(Node)
|
---|
560 | */
|
---|
561 | protected UndeletePrimitivesCommand createUndeleteWayCommand(final Way way) throws OsmTransferException {
|
---|
562 |
|
---|
563 | HashMap<Long,OsmPrimitive> candidates = new HashMap<Long,OsmPrimitive>();
|
---|
564 | for (Node n : way.getNodes()) {
|
---|
565 | if (!n.isNew() && !candidates.values().contains(n)) {
|
---|
566 | candidates.put(n.getId(), n);
|
---|
567 | }
|
---|
568 | }
|
---|
569 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
570 | reader.append(candidates.values());
|
---|
571 | DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
572 |
|
---|
573 | ArrayList<OsmPrimitive> toDelete = new ArrayList<OsmPrimitive>();
|
---|
574 | for (OsmPrimitive their : ds.allPrimitives()) {
|
---|
575 | if (candidates.keySet().contains(their.getId()) && ! their.isVisible()) {
|
---|
576 | toDelete.add(candidates.get(their.getId()));
|
---|
577 | }
|
---|
578 | }
|
---|
579 | if (!toDelete.isEmpty()) {
|
---|
580 | if (! confirmUndeleteDependentPrimitives(way, toDelete))
|
---|
581 | // FIXME: throw exception ?
|
---|
582 | return null;
|
---|
583 | }
|
---|
584 | toDelete.add(way);
|
---|
585 | return new UndeletePrimitivesCommand(toDelete);
|
---|
586 | }
|
---|
587 |
|
---|
588 | /**
|
---|
589 | * Creates an undelete command for a relation which is already deleted on the server.
|
---|
590 | *
|
---|
591 | * This method checks whether there are additional primitives referred to by
|
---|
592 | * this relation which are already deleted on the server.
|
---|
593 | *
|
---|
594 | * @param r the relation
|
---|
595 | * @return the undelete command
|
---|
596 | * @see #createUndeleteNodeCommand(Node)
|
---|
597 | */
|
---|
598 | protected UndeletePrimitivesCommand createUndeleteRelationCommand(final Relation r) throws OsmTransferException {
|
---|
599 |
|
---|
600 | HashMap<Long,OsmPrimitive> candidates = new HashMap<Long, OsmPrimitive>();
|
---|
601 | for (RelationMember m : r.getMembers()) {
|
---|
602 | if (!m.getMember().isNew() && !candidates.values().contains(m.getMember())) {
|
---|
603 | candidates.put(m.getMember().getId(), m.getMember());
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|
607 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
608 | reader.append(candidates.values());
|
---|
609 | DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
610 |
|
---|
611 | ArrayList<OsmPrimitive> toDelete = new ArrayList<OsmPrimitive>();
|
---|
612 | for (OsmPrimitive their : ds.allPrimitives()) {
|
---|
613 | if (candidates.keySet().contains(their.getId()) && ! their.isVisible()) {
|
---|
614 | toDelete.add(candidates.get(their.getId()));
|
---|
615 | }
|
---|
616 | }
|
---|
617 | if (!toDelete.isEmpty()) {
|
---|
618 | if (! confirmUndeleteDependentPrimitives(r, toDelete))
|
---|
619 | // FIXME: throw exception ?
|
---|
620 | return null;
|
---|
621 | }
|
---|
622 | toDelete.add(r);
|
---|
623 | return new UndeletePrimitivesCommand(toDelete);
|
---|
624 | }
|
---|
625 |
|
---|
626 | }
|
---|