source: josm/trunk/src/org/openstreetmap/josm/command/ChangeRelationMemberRoleCommand.java

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

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

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