1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Objects;
|
---|
8 |
|
---|
9 | import javax.swing.Icon;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
15 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Command that changes the role of a relation member
|
---|
20 | *
|
---|
21 | * @author Teemu Koskinen <teemu.koskinen@mbnet.fi>
|
---|
22 | */
|
---|
23 | public class ChangeRelationMemberRoleCommand extends Command {
|
---|
24 |
|
---|
25 | // The relation to be changed
|
---|
26 | private final Relation relation;
|
---|
27 | // Position of the member
|
---|
28 | private final int position;
|
---|
29 | // The new role
|
---|
30 | private final String newRole;
|
---|
31 | // The old role
|
---|
32 | private String oldRole;
|
---|
33 | // Old value of modified
|
---|
34 | private Boolean oldModified;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code ChangeRelationMemberRoleCommand}.
|
---|
38 | * @param relation The relation to be changed
|
---|
39 | * @param position Member position
|
---|
40 | * @param newRole New role
|
---|
41 | */
|
---|
42 | public ChangeRelationMemberRoleCommand(Relation relation, int position, String newRole) {
|
---|
43 | this.relation = relation;
|
---|
44 | this.position = position;
|
---|
45 | this.newRole = newRole;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public boolean executeCommand() {
|
---|
50 | if (position < 0 || position >= relation.getMembersCount())
|
---|
51 | return false;
|
---|
52 |
|
---|
53 | oldRole = relation.getMember(position).getRole();
|
---|
54 | if (newRole.equals(oldRole)) return true;
|
---|
55 | relation.setMember(position, new RelationMember(newRole, relation.getMember(position).getMember()));
|
---|
56 |
|
---|
57 | oldModified = relation.isModified();
|
---|
58 | relation.setModified(true);
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void undoCommand() {
|
---|
64 | relation.setMember(position, new RelationMember(oldRole, relation.getMember(position).getMember()));
|
---|
65 | if (oldModified != null) {
|
---|
66 | relation.setModified(oldModified);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
72 | modified.add(relation);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public String getDescriptionText() {
|
---|
77 | return tr("Change relation member role for {0} {1}",
|
---|
78 | OsmPrimitiveType.from(relation),
|
---|
79 | relation.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public Icon getDescriptionIcon() {
|
---|
84 | return ImageProvider.get(relation.getDisplayType());
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public int hashCode() {
|
---|
89 | return Objects.hash(super.hashCode(), relation, position, newRole, oldRole, oldModified);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public boolean equals(Object obj) {
|
---|
94 | if (this == obj) return true;
|
---|
95 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
96 | if (!super.equals(obj)) return false;
|
---|
97 | ChangeRelationMemberRoleCommand that = (ChangeRelationMemberRoleCommand) obj;
|
---|
98 | return position == that.position &&
|
---|
99 | Objects.equals(relation, that.relation) &&
|
---|
100 | Objects.equals(newRole, that.newRole) &&
|
---|
101 | Objects.equals(oldRole, that.oldRole) &&
|
---|
102 | Objects.equals(oldModified, that.oldModified);
|
---|
103 | }
|
---|
104 | }
|
---|