1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import javax.swing.Icon;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
22 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Command that manipulate the key/value structure of several objects. Manages deletion,
|
---|
27 | * adding and modify of values and keys.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | public class ChangePropertyCommand extends Command {
|
---|
32 | /**
|
---|
33 | * All primitives that are affected with this command.
|
---|
34 | */
|
---|
35 | private final List<OsmPrimitive> objects;
|
---|
36 | /**
|
---|
37 | * Key and value pairs. If value is <code>null</code>, delete all key references with the given
|
---|
38 | * key. Otherwise, change the tags of all objects to the given value or create keys of
|
---|
39 | * those objects that do not have the key yet.
|
---|
40 | */
|
---|
41 | private final Map<String, String> tags;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates a command to change multiple tags of multiple objects
|
---|
45 | *
|
---|
46 | * @param objects the objects to modify
|
---|
47 | * @param tags the tags to set
|
---|
48 | */
|
---|
49 | public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, Map<String, String> tags) {
|
---|
50 | this.objects = new LinkedList<>();
|
---|
51 | this.tags = tags;
|
---|
52 | init(objects);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Creates a command to change one tag of multiple objects
|
---|
57 | *
|
---|
58 | * @param objects the objects to modify
|
---|
59 | * @param key the key of the tag to set
|
---|
60 | * @param value the value of the key to set
|
---|
61 | */
|
---|
62 | public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, String key, String value) {
|
---|
63 | this.objects = new LinkedList<>();
|
---|
64 | this.tags = new HashMap<>(1);
|
---|
65 | this.tags.put(key, value);
|
---|
66 | init(objects);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Creates a command to change one tag of one object
|
---|
71 | *
|
---|
72 | * @param object the object to modify
|
---|
73 | * @param key the key of the tag to set
|
---|
74 | * @param value the value of the key to set
|
---|
75 | */
|
---|
76 | public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
|
---|
77 | this(Arrays.asList(object), key, value);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Initialize the instance by finding what objects will be modified
|
---|
82 | *
|
---|
83 | * @param objects the objects to (possibly) modify
|
---|
84 | */
|
---|
85 | private void init(Collection<? extends OsmPrimitive> objects) {
|
---|
86 | // determine what objects will be modified
|
---|
87 | for (OsmPrimitive osm : objects) {
|
---|
88 | boolean modified = false;
|
---|
89 |
|
---|
90 | // loop over all tags
|
---|
91 | for (Map.Entry<String, String> tag : this.tags.entrySet()) {
|
---|
92 | String oldVal = osm.get(tag.getKey());
|
---|
93 | String newVal = tag.getValue();
|
---|
94 |
|
---|
95 | if (newVal == null || newVal.isEmpty()) {
|
---|
96 | if (oldVal != null)
|
---|
97 | // new value is null and tag exists (will delete tag)
|
---|
98 | modified = true;
|
---|
99 | } else if (oldVal == null || !newVal.equals(oldVal))
|
---|
100 | // new value is not null and is different from current value
|
---|
101 | modified = true;
|
---|
102 | }
|
---|
103 | if (modified)
|
---|
104 | this.objects.add(osm);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override public boolean executeCommand() {
|
---|
109 | Main.main.getCurrentDataSet().beginUpdate();
|
---|
110 | try {
|
---|
111 | super.executeCommand(); // save old
|
---|
112 |
|
---|
113 | for (OsmPrimitive osm : objects) {
|
---|
114 | // loop over all tags
|
---|
115 | for (Map.Entry<String, String> tag : this.tags.entrySet()) {
|
---|
116 | String oldVal = osm.get(tag.getKey());
|
---|
117 | String newVal = tag.getValue();
|
---|
118 |
|
---|
119 | if (newVal == null || newVal.isEmpty()) {
|
---|
120 | if (oldVal != null)
|
---|
121 | osm.remove(tag.getKey());
|
---|
122 | } else if (oldVal == null || !newVal.equals(oldVal))
|
---|
123 | osm.put(tag.getKey(), newVal);
|
---|
124 | }
|
---|
125 | // init() only keeps modified primitives. Therefore the modified
|
---|
126 | // bit can be set without further checks.
|
---|
127 | osm.setModified(true);
|
---|
128 | }
|
---|
129 | return true;
|
---|
130 | } finally {
|
---|
131 | Main.main.getCurrentDataSet().endUpdate();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
136 | modified.addAll(objects);
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public String getDescriptionText() {
|
---|
141 | String text;
|
---|
142 | if (objects.size() == 1 && tags.size() == 1) {
|
---|
143 | OsmPrimitive primitive = objects.iterator().next();
|
---|
144 | String msg = "";
|
---|
145 | Map.Entry<String, String> entry = tags.entrySet().iterator().next();
|
---|
146 | if (entry.getValue() == null) {
|
---|
147 | switch(OsmPrimitiveType.from(primitive)) {
|
---|
148 | case NODE: msg = marktr("Remove \"{0}\" for node ''{1}''"); break;
|
---|
149 | case WAY: msg = marktr("Remove \"{0}\" for way ''{1}''"); break;
|
---|
150 | case RELATION: msg = marktr("Remove \"{0}\" for relation ''{1}''"); break;
|
---|
151 | }
|
---|
152 | text = tr(msg, entry.getKey(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
153 | } else {
|
---|
154 | switch(OsmPrimitiveType.from(primitive)) {
|
---|
155 | case NODE: msg = marktr("Set {0}={1} for node ''{2}''"); break;
|
---|
156 | case WAY: msg = marktr("Set {0}={1} for way ''{2}''"); break;
|
---|
157 | case RELATION: msg = marktr("Set {0}={1} for relation ''{2}''"); break;
|
---|
158 | }
|
---|
159 | text = tr(msg, entry.getKey(), entry.getValue(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
160 | }
|
---|
161 | } else if (objects.size() > 1 && tags.size() == 1) {
|
---|
162 | Map.Entry<String, String> entry = tags.entrySet().iterator().next();
|
---|
163 | if (entry.getValue() == null) {
|
---|
164 | /* I18n: plural form for objects, but value < 2 not possible! */
|
---|
165 | text = trn("Remove \"{0}\" for {1} object", "Remove \"{0}\" for {1} objects", objects.size(), entry.getKey(), objects.size());
|
---|
166 | } else {
|
---|
167 | /* I18n: plural form for objects, but value < 2 not possible! */
|
---|
168 | text = trn("Set {0}={1} for {2} object", "Set {0}={1} for {2} objects", objects.size(), entry.getKey(), entry.getValue(), objects.size());
|
---|
169 | }
|
---|
170 | } else {
|
---|
171 | boolean allnull = true;
|
---|
172 | for (Map.Entry<String, String> tag : this.tags.entrySet()) {
|
---|
173 | if (tag.getValue() != null) {
|
---|
174 | allnull = false;
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (allnull) {
|
---|
180 | /* I18n: plural form detected for objects only (but value < 2 not possible!), try to do your best for tags */
|
---|
181 | text = trn("Deleted {0} tags for {1} object", "Deleted {0} tags for {1} objects", objects.size(), tags.size(), objects.size());
|
---|
182 | } else {
|
---|
183 | /* I18n: plural form detected for objects only (but value < 2 not possible!), try to do your best for tags */
|
---|
184 | text = trn("Set {0} tags for {1} object", "Set {0} tags for {1} objects", objects.size(), tags.size(), objects.size());
|
---|
185 | }
|
---|
186 | }
|
---|
187 | return text;
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | public Icon getDescriptionIcon() {
|
---|
192 | return ImageProvider.get("data", "key");
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override public Collection<PseudoCommand> getChildren() {
|
---|
196 | if (objects.size() == 1)
|
---|
197 | return null;
|
---|
198 | List<PseudoCommand> children = new ArrayList<>();
|
---|
199 | for (final OsmPrimitive osm : objects) {
|
---|
200 | children.add(new PseudoCommand() {
|
---|
201 | @Override public String getDescriptionText() {
|
---|
202 | return osm.getDisplayName(DefaultNameFormatter.getInstance());
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override public Icon getDescriptionIcon() {
|
---|
206 | return ImageProvider.get(osm.getDisplayType());
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
210 | return Collections.singleton(osm);
|
---|
211 | }
|
---|
212 |
|
---|
213 | });
|
---|
214 | }
|
---|
215 | return children;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Returns the tags to set (key/value pairs).
|
---|
220 | * @return the tags to set (key/value pairs)
|
---|
221 | */
|
---|
222 | public Map<String, String> getTags() {
|
---|
223 | return Collections.unmodifiableMap(tags);
|
---|
224 | }
|
---|
225 |
|
---|
226 | @Override
|
---|
227 | public int hashCode() {
|
---|
228 | final int prime = 31;
|
---|
229 | int result = super.hashCode();
|
---|
230 | result = prime * result + ((objects == null) ? 0 : objects.hashCode());
|
---|
231 | result = prime * result + ((tags == null) ? 0 : tags.hashCode());
|
---|
232 | return result;
|
---|
233 | }
|
---|
234 |
|
---|
235 | @Override
|
---|
236 | public boolean equals(Object obj) {
|
---|
237 | if (this == obj)
|
---|
238 | return true;
|
---|
239 | if (!super.equals(obj))
|
---|
240 | return false;
|
---|
241 | if (getClass() != obj.getClass())
|
---|
242 | return false;
|
---|
243 | ChangePropertyCommand other = (ChangePropertyCommand) obj;
|
---|
244 | if (objects == null) {
|
---|
245 | if (other.objects != null)
|
---|
246 | return false;
|
---|
247 | } else if (!objects.equals(other.objects))
|
---|
248 | return false;
|
---|
249 | if (tags == null) {
|
---|
250 | if (other.tags != null)
|
---|
251 | return false;
|
---|
252 | } else if (!tags.equals(other.tags))
|
---|
253 | return false;
|
---|
254 | return true;
|
---|
255 | }
|
---|
256 | }
|
---|