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.JLabel;
|
---|
9 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
10 | import javax.swing.tree.MutableTreeNode;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
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) {
|
---|
36 | super();
|
---|
37 | this.relation = relation;
|
---|
38 | this.position = position;
|
---|
39 | this.newRole = newRole;
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override public boolean executeCommand() {
|
---|
43 | if (position < 0 || position >= relation.members.size()) {
|
---|
44 | Main.debug("error changing the role");
|
---|
45 | return false;
|
---|
46 | }
|
---|
47 |
|
---|
48 | oldRole = relation.members.get(position).role;
|
---|
49 | relation.members.get(position).role = newRole;
|
---|
50 |
|
---|
51 | oldModified = relation.modified;
|
---|
52 | relation.modified = true;
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override public void undoCommand() {
|
---|
57 | relation.members.get(position).role = oldRole;
|
---|
58 | relation.modified = oldModified;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
62 | modified.add(relation);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override public MutableTreeNode description() {
|
---|
66 | NameVisitor v = new NameVisitor();
|
---|
67 | relation.visit(v);
|
---|
68 | return new DefaultMutableTreeNode(new JLabel(tr("ChangeRelationMemberRole {0} {1}", tr(v.className), v.name), v.icon, JLabel.HORIZONTAL));
|
---|
69 | }
|
---|
70 | }
|
---|