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.OsmPrimitiveType;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.gui.PrimitiveNameFormatter;
|
---|
17 | import 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 | */
|
---|
24 | public class ChangeRelationMemberRoleCommand extends Command {
|
---|
25 |
|
---|
26 | // The relation to be changed
|
---|
27 | private final Relation relation;
|
---|
28 | // Position of the member
|
---|
29 | private int position = -1;
|
---|
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 | public ChangeRelationMemberRoleCommand(Relation relation, int position, String newRole) {
|
---|
38 | super();
|
---|
39 | this.relation = relation;
|
---|
40 | this.position = position;
|
---|
41 | this.newRole = newRole;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override public boolean executeCommand() {
|
---|
45 | if (position < 0 || position >= relation.getMembersCount()) {
|
---|
46 | Main.debug("error changing the role");
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 |
|
---|
50 | oldRole = relation.getMember(position).role;
|
---|
51 | relation.getMember(position).role = newRole;
|
---|
52 |
|
---|
53 | oldModified = relation.modified;
|
---|
54 | relation.modified = true;
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override public void undoCommand() {
|
---|
59 | relation.getMember(position).role = oldRole;
|
---|
60 | relation.modified = oldModified;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
64 | modified.add(relation);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override public MutableTreeNode description() {
|
---|
68 | return new DefaultMutableTreeNode(
|
---|
69 | new JLabel(
|
---|
70 | tr("Change relation member role for {0} {1}",
|
---|
71 | OsmPrimitiveType.from(relation),
|
---|
72 | new PrimitiveNameFormatter().getName(relation)
|
---|
73 | ),
|
---|
74 | ImageProvider.get(OsmPrimitiveType.from(relation)),
|
---|
75 | JLabel.HORIZONTAL)
|
---|
76 | );
|
---|
77 | }
|
---|
78 | }
|
---|