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

Last change on this file since 12718 was 12718, checked in by Don-vip, 3 weeks 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.5 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.Collection;
7import java.util.List;
8import java.util.Objects;
9
10import javax.swing.Icon;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.conflict.Conflict;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.RelationMember;
18import org.openstreetmap.josm.tools.ImageProvider;
19import org.openstreetmap.josm.tools.Logging;
20
21/**
22 * Represents the resolution of conflicts in the member list of two {@link Relation}s.
23 * @since 1631
24 */
25public class RelationMemberConflictResolverCommand extends ConflictResolveCommand {
26    /** the conflict to resolve */
27    private final Conflict<Relation> conflict;
28    /** the list of merged nodes. This becomes the list of news of my way after the command is executed */
29    private final List<RelationMember> mergedMembers;
30
31    /**
32     * Constructs a new {@code RelationMemberConflictResolverCommand}.
33     * @param conflict the conflict to resolve
34     * @param mergedMembers the list of merged relation members
35     */
36    @SuppressWarnings("unchecked")
37    public RelationMemberConflictResolverCommand(Conflict<? extends OsmPrimitive> conflict, List<RelationMember> mergedMembers) {
38        this.conflict = (Conflict<Relation>) conflict;
39        this.mergedMembers = mergedMembers;
40    }
41
42    @Override
43    public String getDescriptionText() {
44        return tr("Resolve conflicts in member list of relation {0}", conflict.getMy().getId());
45    }
46
47    @Override
48    public Icon getDescriptionIcon() {
49        return ImageProvider.get("data", "object");
50    }
51
52    @Override
53    public boolean executeCommand() {
54        // remember the current state of 'my' way
55        //
56        super.executeCommand();
57
58        // replace the list of members of 'my' relation by the list of merged members
59        //
60        conflict.getMy().setMembers(mergedMembers);
61
62        return true;
63    }
64
65    @Override
66    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
67            Collection<OsmPrimitive> added) {
68        modified.add(conflict.getMy());
69    }
70
71    @Override
72    public void undoCommand() {
73        DataSet editDs = getAffectedDataSet();
74        if (!Main.main.containsDataSet(editDs)) {
75            Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
76                    this.toString(),
77                    editDs.getName()
78            ));
79            return;
80        }
81
82        Main.main.setEditDataSet(editDs);
83
84        // restore the former state
85        //
86        super.undoCommand();
87
88        // restore a conflict if necessary
89        //
90        if (!editDs.getConflicts().hasConflictForMy(conflict.getMy())) {
91            editDs.getConflicts().add(conflict);
92        }
93    }
94
95    @Override
96    public int hashCode() {
97        return Objects.hash(super.hashCode(), conflict, mergedMembers);
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        RelationMemberConflictResolverCommand that = (RelationMemberConflictResolverCommand) obj;
106        return Objects.equals(conflict, that.conflict) &&
107               Objects.equals(mergedMembers, that.mergedMembers);
108    }
109}
Note: See TracBrowser for help on using the repository browser.