source: josm/trunk/src/org/openstreetmap/josm/command/ChangePropertyCommand.java@ 8945

Last change on this file since 8945 was 8945, checked in by Don-vip, 10 years ago

fix #12008 - do not create empty commands from tagging preset dialog + add robustness

  • Property svn:eol-style set to native
File size: 10.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashMap;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.Map;
16
17import javax.swing.Icon;
18
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
22import org.openstreetmap.josm.gui.DefaultNameFormatter;
23import 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 * @since 24
31 */
32public class ChangePropertyCommand extends Command {
33 /**
34 * All primitives that are affected with this command.
35 */
36 private final List<OsmPrimitive> objects = new LinkedList<>();
37
38 /**
39 * Key and value pairs. If value is <code>null</code>, delete all key references with the given
40 * key. Otherwise, change the tags of all objects to the given value or create keys of
41 * those objects that do not have the key yet.
42 */
43 private final Map<String, String> tags;
44
45 /**
46 * Creates a command to change multiple tags of multiple objects
47 *
48 * @param objects the objects to modify
49 * @param tags the tags to set
50 */
51 public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, Map<String, String> tags) {
52 this.tags = tags;
53 init(objects);
54 }
55
56 /**
57 * Creates a command to change one tag of multiple objects
58 *
59 * @param objects the objects to modify
60 * @param key the key of the tag to set
61 * @param value the value of the key to set
62 */
63 public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, String key, String value) {
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
109 public boolean executeCommand() {
110 if (objects.isEmpty())
111 return true;
112 final DataSet dataSet = objects.get(0).getDataSet();
113 dataSet.beginUpdate();
114 try {
115 super.executeCommand(); // save old
116
117 for (OsmPrimitive osm : objects) {
118 // loop over all tags
119 for (Map.Entry<String, String> tag : this.tags.entrySet()) {
120 String oldVal = osm.get(tag.getKey());
121 String newVal = tag.getValue();
122
123 if (newVal == null || newVal.isEmpty()) {
124 if (oldVal != null)
125 osm.remove(tag.getKey());
126 } else if (oldVal == null || !newVal.equals(oldVal))
127 osm.put(tag.getKey(), newVal);
128 }
129 // init() only keeps modified primitives. Therefore the modified
130 // bit can be set without further checks.
131 osm.setModified(true);
132 }
133 return true;
134 } finally {
135 dataSet.endUpdate();
136 }
137 }
138
139 @Override
140 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
141 modified.addAll(objects);
142 }
143
144 @Override
145 public String getDescriptionText() {
146 String text;
147 if (objects.size() == 1 && tags.size() == 1) {
148 OsmPrimitive primitive = objects.get(0);
149 String msg = "";
150 Map.Entry<String, String> entry = tags.entrySet().iterator().next();
151 if (entry.getValue() == null) {
152 switch(OsmPrimitiveType.from(primitive)) {
153 case NODE: msg = marktr("Remove \"{0}\" for node ''{1}''"); break;
154 case WAY: msg = marktr("Remove \"{0}\" for way ''{1}''"); break;
155 case RELATION: msg = marktr("Remove \"{0}\" for relation ''{1}''"); break;
156 }
157 text = tr(msg, entry.getKey(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
158 } else {
159 switch(OsmPrimitiveType.from(primitive)) {
160 case NODE: msg = marktr("Set {0}={1} for node ''{2}''"); break;
161 case WAY: msg = marktr("Set {0}={1} for way ''{2}''"); break;
162 case RELATION: msg = marktr("Set {0}={1} for relation ''{2}''"); break;
163 }
164 text = tr(msg, entry.getKey(), entry.getValue(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
165 }
166 } else if (objects.size() > 1 && tags.size() == 1) {
167 Map.Entry<String, String> entry = tags.entrySet().iterator().next();
168 if (entry.getValue() == null) {
169 /* I18n: plural form for objects, but value < 2 not possible! */
170 text = trn("Remove \"{0}\" for {1} object", "Remove \"{0}\" for {1} objects", objects.size(), entry.getKey(), objects.size());
171 } else {
172 /* I18n: plural form for objects, but value < 2 not possible! */
173 text = trn("Set {0}={1} for {2} object", "Set {0}={1} for {2} objects",
174 objects.size(), entry.getKey(), entry.getValue(), objects.size());
175 }
176 } else {
177 boolean allnull = true;
178 for (Map.Entry<String, String> tag : this.tags.entrySet()) {
179 if (tag.getValue() != null) {
180 allnull = false;
181 break;
182 }
183 }
184
185 if (allnull) {
186 /* I18n: plural form detected for objects only (but value < 2 not possible!), try to do your best for tags */
187 text = trn("Deleted {0} tags for {1} object", "Deleted {0} tags for {1} objects", objects.size(), tags.size(), objects.size());
188 } else {
189 /* I18n: plural form detected for objects only (but value < 2 not possible!), try to do your best for tags */
190 text = trn("Set {0} tags for {1} object", "Set {0} tags for {1} objects", objects.size(), tags.size(), objects.size());
191 }
192 }
193 return text;
194 }
195
196 @Override
197 public Icon getDescriptionIcon() {
198 return ImageProvider.get("data", "key");
199 }
200
201 @Override
202 public Collection<PseudoCommand> getChildren() {
203 if (objects.size() == 1)
204 return null;
205 List<PseudoCommand> children = new ArrayList<>();
206 for (final OsmPrimitive osm : objects) {
207 children.add(new PseudoCommand() {
208 @Override public String getDescriptionText() {
209 return osm.getDisplayName(DefaultNameFormatter.getInstance());
210 }
211
212 @Override public Icon getDescriptionIcon() {
213 return ImageProvider.get(osm.getDisplayType());
214 }
215
216 @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
217 return Collections.singleton(osm);
218 }
219
220 });
221 }
222 return children;
223 }
224
225 /**
226 * Returns the number of objects that will effectively be modified, before the command is executed.
227 * @return the number of objects that will effectively be modified (can be 0)
228 * @see Command#getParticipatingPrimitives()
229 * @since 8945
230 */
231 public final int getObjectsNumber() {
232 return objects.size();
233 }
234
235 /**
236 * Returns the tags to set (key/value pairs).
237 * @return the tags to set (key/value pairs)
238 */
239 public Map<String, String> getTags() {
240 return Collections.unmodifiableMap(tags);
241 }
242
243 @Override
244 public int hashCode() {
245 final int prime = 31;
246 int result = super.hashCode();
247 result = prime * result + ((objects == null) ? 0 : objects.hashCode());
248 result = prime * result + ((tags == null) ? 0 : tags.hashCode());
249 return result;
250 }
251
252 @Override
253 public boolean equals(Object obj) {
254 if (this == obj)
255 return true;
256 if (!super.equals(obj))
257 return false;
258 if (getClass() != obj.getClass())
259 return false;
260 ChangePropertyCommand other = (ChangePropertyCommand) obj;
261 if (objects == null) {
262 if (other.objects != null)
263 return false;
264 } else if (!objects.equals(other.objects))
265 return false;
266 if (tags == null) {
267 if (other.tags != null)
268 return false;
269 } else if (!tags.equals(other.tags))
270 return false;
271 return true;
272 }
273}
Note: See TracBrowser for help on using the repository browser.