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 |
|
---|
8 | import javax.swing.Icon;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
14 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Command that changes the role of a relation member
|
---|
19 | *
|
---|
20 | * @author Teemu Koskinen <teemu.koskinen@mbnet.fi>
|
---|
21 | */
|
---|
22 | public class ChangeRelationMemberRoleCommand extends Command {
|
---|
23 |
|
---|
24 | // The relation to be changed
|
---|
25 | private final Relation relation;
|
---|
26 | // Position of the member
|
---|
27 | private int position = -1;
|
---|
28 | // The new role
|
---|
29 | private final String newRole;
|
---|
30 | // The old role
|
---|
31 | private String oldRole;
|
---|
32 | // Old value of modified
|
---|
33 | private Boolean oldModified;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructs a new {@code ChangeRelationMemberRoleCommand}.
|
---|
37 | * @param relation The relation to be changed
|
---|
38 | * @param position Member position
|
---|
39 | * @param newRole New role
|
---|
40 | */
|
---|
41 | public ChangeRelationMemberRoleCommand(Relation relation, int position, String newRole) {
|
---|
42 | this.relation = relation;
|
---|
43 | this.position = position;
|
---|
44 | this.newRole = newRole;
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public boolean executeCommand() {
|
---|
49 | if (position < 0 || position >= relation.getMembersCount())
|
---|
50 | return false;
|
---|
51 |
|
---|
52 | oldRole = relation.getMember(position).getRole();
|
---|
53 | if (newRole.equals(oldRole)) return true;
|
---|
54 | relation.setMember(position, new RelationMember(newRole, relation.getMember(position).getMember()));
|
---|
55 |
|
---|
56 | oldModified = relation.isModified();
|
---|
57 | relation.setModified(true);
|
---|
58 | return true;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void undoCommand() {
|
---|
63 | relation.setMember(position, new RelationMember(oldRole, relation.getMember(position).getMember()));
|
---|
64 | relation.setModified(oldModified);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
69 | modified.add(relation);
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public String getDescriptionText() {
|
---|
74 | return tr("Change relation member role for {0} {1}",
|
---|
75 | OsmPrimitiveType.from(relation),
|
---|
76 | relation.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public Icon getDescriptionIcon() {
|
---|
81 | return ImageProvider.get(relation.getDisplayType());
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public int hashCode() {
|
---|
86 | final int prime = 31;
|
---|
87 | int result = super.hashCode();
|
---|
88 | result = prime * result + ((newRole == null) ? 0 : newRole.hashCode());
|
---|
89 | result = prime * result + ((oldModified == null) ? 0 : oldModified.hashCode());
|
---|
90 | result = prime * result + ((oldRole == null) ? 0 : oldRole.hashCode());
|
---|
91 | result = prime * result + position;
|
---|
92 | result = prime * result + ((relation == null) ? 0 : relation.hashCode());
|
---|
93 | return result;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public boolean equals(Object obj) {
|
---|
98 | if (this == obj)
|
---|
99 | return true;
|
---|
100 | if (!super.equals(obj))
|
---|
101 | return false;
|
---|
102 | if (getClass() != obj.getClass())
|
---|
103 | return false;
|
---|
104 | ChangeRelationMemberRoleCommand other = (ChangeRelationMemberRoleCommand) obj;
|
---|
105 | if (newRole == null) {
|
---|
106 | if (other.newRole != null)
|
---|
107 | return false;
|
---|
108 | } else if (!newRole.equals(other.newRole))
|
---|
109 | return false;
|
---|
110 | if (oldModified == null) {
|
---|
111 | if (other.oldModified != null)
|
---|
112 | return false;
|
---|
113 | } else if (!oldModified.equals(other.oldModified))
|
---|
114 | return false;
|
---|
115 | if (oldRole == null) {
|
---|
116 | if (other.oldRole != null)
|
---|
117 | return false;
|
---|
118 | } else if (!oldRole.equals(other.oldRole))
|
---|
119 | return false;
|
---|
120 | if (position != other.position)
|
---|
121 | return false;
|
---|
122 | if (relation == null) {
|
---|
123 | if (other.relation != null)
|
---|
124 | return false;
|
---|
125 | } else if (!relation.equals(other.relation))
|
---|
126 | return false;
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 | }
|
---|