1 | package org.openstreetmap.josm.command;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Iterator;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.Key;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Command that manipulate the key/value structure of several objects. Manages deletion,
|
---|
15 | * adding and modify of values and keys.
|
---|
16 | *
|
---|
17 | * @author imi
|
---|
18 | */
|
---|
19 | public class ChangeKeyValueCommand implements Command {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * All primitives, that are affected with this command.
|
---|
23 | */
|
---|
24 | private final List<OsmPrimitive> objects;
|
---|
25 | /**
|
---|
26 | * The key that is subject to change.
|
---|
27 | */
|
---|
28 | private final Key key;
|
---|
29 | /**
|
---|
30 | * The key value. If it is <code>null</code>, delete all key references with the given
|
---|
31 | * key. Else, change the properties of all objects to the given value or create keys of
|
---|
32 | * those objects that do not have the key yet.
|
---|
33 | */
|
---|
34 | private final String value;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * These are the old values of the objects to do a proper undo.
|
---|
38 | */
|
---|
39 | private List<Map<Key, String>> oldProperties;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * These are the old modified states of the data.
|
---|
43 | */
|
---|
44 | private List<Boolean> oldModified = new LinkedList<Boolean>();
|
---|
45 |
|
---|
46 | public ChangeKeyValueCommand(Collection<OsmPrimitive> objects, Key key, String value) {
|
---|
47 | this.objects = new LinkedList<OsmPrimitive>(objects);
|
---|
48 | this.key = key;
|
---|
49 | this.value = value;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void executeCommand() {
|
---|
53 | // save old
|
---|
54 | oldProperties = new LinkedList<Map<Key, String>>();
|
---|
55 | for (OsmPrimitive osm : objects) {
|
---|
56 | oldProperties.add(osm.keys == null ? null : new HashMap<Key, String>(osm.keys));
|
---|
57 | oldModified.add(osm.modified);
|
---|
58 | osm.modified = true;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (value == null) {
|
---|
62 | for (OsmPrimitive osm : objects) {
|
---|
63 | if (osm.keys != null) {
|
---|
64 | osm.keys.remove(key);
|
---|
65 | if (osm.keys.isEmpty())
|
---|
66 | osm.keys = null;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | } else {
|
---|
70 | for (OsmPrimitive osm : objects) {
|
---|
71 | if (osm.keys == null)
|
---|
72 | osm.keys = new HashMap<Key, String>();
|
---|
73 | osm.keys.put(key, value);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void undoCommand() {
|
---|
79 | Iterator<Map<Key, String>> it = oldProperties.iterator();
|
---|
80 | Iterator<Boolean> itMod = oldModified.iterator();
|
---|
81 | for (OsmPrimitive osm : objects) {
|
---|
82 | osm.keys = it.next();
|
---|
83 | osm.modified = itMod.next();
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
88 | modified.addAll(objects);
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|