[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 | *
|
---|
| 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 | public ChangeRelationMemberRoleCommand(Relation relation, int position, String newRole) {
|
---|
[1750] | 36 | super();
|
---|
[1617] | 37 | this.relation = relation;
|
---|
| 38 | this.position = position;
|
---|
| 39 | this.newRole = newRole;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override public boolean executeCommand() {
|
---|
[5077] | 43 | if (position < 0 || position >= relation.getMembersCount())
|
---|
[1617] | 44 | return false;
|
---|
| 45 |
|
---|
[1930] | 46 | oldRole = relation.getMember(position).getRole();
|
---|
[2683] | 47 | if (newRole.equals(oldRole)) return true;
|
---|
[2611] | 48 | relation.setMember(position, new RelationMember(newRole, relation.getMember(position).getMember()));
|
---|
[1617] | 49 |
|
---|
[2025] | 50 | oldModified = relation.isModified();
|
---|
| 51 | relation.setModified(true);
|
---|
[1617] | 52 | return true;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | @Override public void undoCommand() {
|
---|
[2683] | 56 | relation.setMember(position, new RelationMember(oldRole, relation.getMember(position).getMember()));
|
---|
[2025] | 57 | relation.setModified(oldModified);
|
---|
[1617] | 58 | }
|
---|
| 59 |
|
---|
| 60 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
| 61 | modified.add(relation);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[4918] | 64 | @Override
|
---|
| 65 | public String getDescriptionText() {
|
---|
| 66 | return tr("Change relation member role for {0} {1}",
|
---|
| 67 | OsmPrimitiveType.from(relation),
|
---|
| 68 | relation.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
[1617] | 69 | }
|
---|
[4918] | 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public Icon getDescriptionIcon() {
|
---|
[5077] | 73 | return ImageProvider.get(relation.getDisplayType());
|
---|
[4918] | 74 | }
|
---|
[1617] | 75 | }
|
---|