[1617] | 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 |
|
---|
[4918] | 8 | import javax.swing.Icon;
|
---|
[5077] | 9 |
|
---|
[1617] | 10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[1814] | 11 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[1617] | 12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
[2611] | 13 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
[1990] | 14 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
[1814] | 15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[1617] | 16 |
|
---|
| 17 | /**
|
---|
| 18 | * Command that changes the role of a relation member
|
---|
| 19 | *
|
---|
[6830] | 20 | * @author Teemu Koskinen <teemu.koskinen@mbnet.fi>
|
---|
[1617] | 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;
|
---|
[6296] | 32 | // Old value of modified
|
---|
[1617] | 33 | private Boolean oldModified;
|
---|
| 34 |
|
---|
[6881] | 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 | */
|
---|
[1617] | 41 | public ChangeRelationMemberRoleCommand(Relation relation, int position, String newRole) {
|
---|
| 42 | this.relation = relation;
|
---|
| 43 | this.position = position;
|
---|
| 44 | this.newRole = newRole;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[6881] | 47 | @Override
|
---|
| 48 | public boolean executeCommand() {
|
---|
[5077] | 49 | if (position < 0 || position >= relation.getMembersCount())
|
---|
[1617] | 50 | return false;
|
---|
| 51 |
|
---|
[1930] | 52 | oldRole = relation.getMember(position).getRole();
|
---|
[2683] | 53 | if (newRole.equals(oldRole)) return true;
|
---|
[2611] | 54 | relation.setMember(position, new RelationMember(newRole, relation.getMember(position).getMember()));
|
---|
[1617] | 55 |
|
---|
[2025] | 56 | oldModified = relation.isModified();
|
---|
| 57 | relation.setModified(true);
|
---|
[1617] | 58 | return true;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[6881] | 61 | @Override
|
---|
| 62 | public void undoCommand() {
|
---|
[2683] | 63 | relation.setMember(position, new RelationMember(oldRole, relation.getMember(position).getMember()));
|
---|
[2025] | 64 | relation.setModified(oldModified);
|
---|
[1617] | 65 | }
|
---|
| 66 |
|
---|
[6881] | 67 | @Override
|
---|
| 68 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[1617] | 69 | modified.add(relation);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[4918] | 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()));
|
---|
[1617] | 77 | }
|
---|
[4918] | 78 |
|
---|
| 79 | @Override
|
---|
| 80 | public Icon getDescriptionIcon() {
|
---|
[5077] | 81 | return ImageProvider.get(relation.getDisplayType());
|
---|
[4918] | 82 | }
|
---|
[8456] | 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 | }
|
---|
[1617] | 129 | }
|
---|