source: josm/trunk/src/org/openstreetmap/josm/command/conflict/ConflictResolveCommand.java@ 12630

Last change on this file since 12630 was 12630, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command.conflict;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Objects;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.command.Command;
10import org.openstreetmap.josm.data.conflict.Conflict;
11import org.openstreetmap.josm.data.conflict.ConflictCollection;
12import org.openstreetmap.josm.gui.MainApplication;
13import org.openstreetmap.josm.gui.layer.OsmDataLayer;
14import org.openstreetmap.josm.tools.Logging;
15
16/**
17 * This is the common base class for {@link Command}s which manipulate {@link Conflict}s in
18 * addition to {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s.
19 *
20 * A ConflictResolverCommand can remember a collection of conflicts it resolves. Upon undoing
21 * it reconstitutes them.
22 *
23 */
24public abstract class ConflictResolveCommand extends Command {
25 /** the list of resolved conflicts */
26 private final ConflictCollection resolvedConflicts;
27
28 /**
29 * Constructs a new {@code ConflictResolveCommand} in the context of the current edit layer, if any.
30 */
31 public ConflictResolveCommand() {
32 super();
33 resolvedConflicts = new ConflictCollection();
34 }
35
36 /**
37 * Constructs a new {@code ConflictResolveCommand} in the context of a given data layer.
38 * @param layer the data layer. Must not be null.
39 */
40 public ConflictResolveCommand(OsmDataLayer layer) {
41 super(layer);
42 resolvedConflicts = new ConflictCollection();
43 }
44
45 /**
46 * remembers a conflict in the internal list of remembered conflicts
47 *
48 * @param c the remembered conflict
49 */
50 protected void rememberConflict(Conflict<?> c) {
51 if (!resolvedConflicts.hasConflictForMy(c.getMy())) {
52 resolvedConflicts.add(c);
53 }
54 }
55
56 /**
57 * reconstitutes all remembered conflicts. Add the remembered conflicts to the
58 * set of conflicts of the {@link OsmDataLayer} this command was applied to.
59 *
60 */
61 protected void reconstituteConflicts() {
62 OsmDataLayer editLayer = getLayer();
63 for (Conflict<?> c : resolvedConflicts) {
64 if (!editLayer.getConflicts().hasConflictForMy(c.getMy())) {
65 editLayer.getConflicts().add(c);
66 }
67 }
68 }
69
70 @Override
71 public void undoCommand() {
72 super.undoCommand();
73
74 if (MainApplication.isDisplayingMapView()) {
75 if (!Main.getLayerManager().containsLayer(getLayer())) {
76 Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
77 this.toString(),
78 getLayer().toString()
79 ));
80 return;
81 }
82
83 Main.getLayerManager().setActiveLayer(getLayer());
84 }
85 reconstituteConflicts();
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(super.hashCode(), resolvedConflicts);
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj) return true;
96 if (obj == null || getClass() != obj.getClass()) return false;
97 if (!super.equals(obj)) return false;
98 ConflictResolveCommand that = (ConflictResolveCommand) obj;
99 return Objects.equals(resolvedConflicts, that.resolvedConflicts);
100 }
101}
Note: See TracBrowser for help on using the repository browser.