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

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

see #13036 - see #15229 - see #15182 - make Commands depends only on a DataSet, not a Layer. This removes a lot of GUI dependencies

  • Property svn:eol-style set to native
File size: 3.4 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.data.osm.DataSet;
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 = new ConflictCollection();
27
28 /**
29 * Constructs a new {@code ConflictResolveCommand} in the context of the current edit layer, if any.
30 */
31 public ConflictResolveCommand() {
32 // Do nothing
33 }
34
35 /**
36 * Constructs a new {@code ConflictResolveCommand} in the context of a given data layer.
37 * @param layer the data layer. Must not be null.
38 * @deprecated to be removed end of 2017. Use {@link #ConflictResolveCommand(DataSet)} instead
39 */
40 @Deprecated
41 public ConflictResolveCommand(OsmDataLayer layer) {
42 super(layer);
43 }
44
45 /**
46 * Constructs a new {@code ConflictResolveCommand} in the context of a given data set.
47 * @param ds the data set. Must not be null.
48 */
49 public ConflictResolveCommand(DataSet ds) {
50 super(ds);
51 }
52
53 /**
54 * remembers a conflict in the internal list of remembered conflicts
55 *
56 * @param c the remembered conflict
57 */
58 protected void rememberConflict(Conflict<?> c) {
59 if (!resolvedConflicts.hasConflictForMy(c.getMy())) {
60 resolvedConflicts.add(c);
61 }
62 }
63
64 /**
65 * reconstitutes all remembered conflicts. Add the remembered conflicts to the
66 * set of conflicts of the {@link DataSet} this command was applied to.
67 *
68 */
69 protected void reconstituteConflicts() {
70 DataSet ds = getAffectedDataSet();
71 for (Conflict<?> c : resolvedConflicts) {
72 if (!ds.getConflicts().hasConflictForMy(c.getMy())) {
73 ds.getConflicts().add(c);
74 }
75 }
76 }
77
78 @Override
79 public void undoCommand() {
80 super.undoCommand();
81
82 DataSet ds = getAffectedDataSet();
83 if (!Main.main.containsDataSet(ds)) {
84 Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
85 this.toString(),
86 ds.getName()
87 ));
88 return;
89 }
90
91 Main.main.setEditDataSet(ds);
92 reconstituteConflicts();
93 }
94
95 @Override
96 public int hashCode() {
97 return Objects.hash(super.hashCode(), resolvedConflicts);
98 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) return true;
103 if (obj == null || getClass() != obj.getClass()) return false;
104 if (!super.equals(obj)) return false;
105 ConflictResolveCommand that = (ConflictResolveCommand) obj;
106 return Objects.equals(resolvedConflicts, that.resolvedConflicts);
107 }
108}
Note: See TracBrowser for help on using the repository browser.