1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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.data.osm.RelationMember;
|
---|
17 | import org.openstreetmap.josm.gui.PrimitiveNameFormatter;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Command that adds a relation to an OSM object
|
---|
22 | *
|
---|
23 | * @author daveh
|
---|
24 | */
|
---|
25 | public class RemoveRelationMemberCommand extends Command {
|
---|
26 |
|
---|
27 | // container object in which to replace a sub object
|
---|
28 | private final Relation relation;
|
---|
29 | // the sub-object to be replaced
|
---|
30 | private final RelationMember member;
|
---|
31 | // its replacement
|
---|
32 | private int location = -1;
|
---|
33 |
|
---|
34 | public RemoveRelationMemberCommand(Relation _relation, RelationMember _member) {
|
---|
35 | this.relation = _relation;
|
---|
36 | this.member = _member;
|
---|
37 | }
|
---|
38 | public RemoveRelationMemberCommand(Relation _relation, RelationMember _member, int _location) {
|
---|
39 | this.relation = _relation;
|
---|
40 | this.member = _member;
|
---|
41 | location = _location;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override public boolean executeCommand() {
|
---|
45 | super.executeCommand();
|
---|
46 | int removed_at = relation.members.indexOf(member);
|
---|
47 | relation.members.remove(member);
|
---|
48 | if ((location != -1) && (removed_at != location)) {
|
---|
49 | relation.members.add(removed_at, member);
|
---|
50 | Main.debug("error removing relation member");
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 | relation.modified = true;
|
---|
54 | return true;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override public void undoCommand() {
|
---|
58 | super.undoCommand();
|
---|
59 | relation.members.add(member);
|
---|
60 | relation.modified = this.getOrig(relation).modified;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {}
|
---|
64 |
|
---|
65 | @Override public MutableTreeNode description() {
|
---|
66 | return new DefaultMutableTreeNode(
|
---|
67 | new JLabel(
|
---|
68 | tr("Remove relation member {0} {1}",
|
---|
69 | OsmPrimitiveType.from(relation).getLocalizedDisplayNameSingular(),
|
---|
70 | new PrimitiveNameFormatter().getName(relation)
|
---|
71 | ),
|
---|
72 | ImageProvider.get(OsmPrimitiveType.from(relation)),
|
---|
73 | JLabel.HORIZONTAL
|
---|
74 | )
|
---|
75 | );
|
---|
76 | }
|
---|
77 | }
|
---|