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.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.AbstractMap;
|
---|
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 | import javax.swing.Icon;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
21 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Command that manipulate the key/value structure of several objects. Manages deletion,
|
---|
26 | * adding and modify of values and keys.
|
---|
27 | *
|
---|
28 | * @author imi
|
---|
29 | */
|
---|
30 | public class ChangePropertyCommand extends Command {
|
---|
31 | /**
|
---|
32 | * All primitives that are affected with this command.
|
---|
33 | */
|
---|
34 | private final List<OsmPrimitive> objects;
|
---|
35 | /**
|
---|
36 | * Key and value pairs. If value is <code>null</code>, delete all key references with the given
|
---|
37 | * key. Otherwise, change the properties of all objects to the given value or create keys of
|
---|
38 | * those objects that do not have the key yet.
|
---|
39 | */
|
---|
40 | private final AbstractMap<String, String> tags;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Creates a command to change multiple properties of multiple objects
|
---|
44 | *
|
---|
45 | * @param objects the objects to modify
|
---|
46 | * @param tags the properties to set
|
---|
47 | */
|
---|
48 | public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, AbstractMap<String, String> tags) {
|
---|
49 | super();
|
---|
50 | this.objects = new LinkedList<OsmPrimitive>();
|
---|
51 | this.tags = tags;
|
---|
52 | init(objects);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Creates a command to change one property of multiple objects
|
---|
57 | *
|
---|
58 | * @param objects the objects to modify
|
---|
59 | * @param key the key of the property 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<OsmPrimitive>();
|
---|
64 | this.tags = new HashMap<String, String>(1);
|
---|
65 | this.tags.put(key, value);
|
---|
66 | init(objects);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Creates a command to change on property of one object
|
---|
71 | *
|
---|
72 | * @param object the object to modify
|
---|
73 | * @param key the key of the property 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 | }
|
---|
100 | else if (oldVal == null || !newVal.equals(oldVal))
|
---|
101 | // new value is not null and is different from current value
|
---|
102 | modified = true;
|
---|
103 | }
|
---|
104 | if (modified)
|
---|
105 | this.objects.add(osm);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override public boolean executeCommand() {
|
---|
110 | Main.main.getCurrentDataSet().beginUpdate();
|
---|
111 | try {
|
---|
112 | super.executeCommand(); // save old
|
---|
113 |
|
---|
114 | for (OsmPrimitive osm : objects) {
|
---|
115 | boolean modified = false;
|
---|
116 |
|
---|
117 | // loop over all tags
|
---|
118 | for (Map.Entry<String, String> tag : this.tags.entrySet()) {
|
---|
119 | String oldVal = osm.get(tag.getKey());
|
---|
120 | String newVal = tag.getValue();
|
---|
121 |
|
---|
122 | if (newVal == null || newVal.isEmpty()) {
|
---|
123 | if (oldVal != null) {
|
---|
124 | osm.remove(tag.getKey());
|
---|
125 | modified = true;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | else if (oldVal == null || !newVal.equals(oldVal))
|
---|
129 | osm.put(tag.getKey(), newVal);
|
---|
130 | modified = true;
|
---|
131 | }
|
---|
132 | if (modified)
|
---|
133 | osm.setModified(true);
|
---|
134 | }
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 | finally {
|
---|
138 | Main.main.getCurrentDataSet().endUpdate();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
143 | modified.addAll(objects);
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public String getDescriptionText() {
|
---|
148 | String text;
|
---|
149 | if (objects.size() == 1 && tags.size() == 1) {
|
---|
150 | OsmPrimitive primitive = objects.iterator().next();
|
---|
151 | String msg = "";
|
---|
152 | Map.Entry<String, String> entry = tags.entrySet().iterator().next();
|
---|
153 | if (entry.getValue() == null) {
|
---|
154 | switch(OsmPrimitiveType.from(primitive)) {
|
---|
155 | case NODE: msg = marktr("Remove \"{0}\" for node ''{1}''"); break;
|
---|
156 | case WAY: msg = marktr("Remove \"{0}\" for way ''{1}''"); break;
|
---|
157 | case RELATION: msg = marktr("Remove \"{0}\" for relation ''{1}''"); break;
|
---|
158 | }
|
---|
159 | text = tr(msg, entry.getKey(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
160 | } else {
|
---|
161 | switch(OsmPrimitiveType.from(primitive)) {
|
---|
162 | case NODE: msg = marktr("Set {0}={1} for node ''{2}''"); break;
|
---|
163 | case WAY: msg = marktr("Set {0}={1} for way ''{2}''"); break;
|
---|
164 | case RELATION: msg = marktr("Set {0}={1} for relation ''{2}''"); break;
|
---|
165 | }
|
---|
166 | text = tr(msg, entry.getKey(), entry.getValue(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
167 | }
|
---|
168 | } else if (objects.size() > 1 && tags.size() == 1) {
|
---|
169 | Map.Entry<String, String> entry = tags.entrySet().iterator().next();
|
---|
170 | if (entry.getValue() == null)
|
---|
171 | text = tr("Remove \"{0}\" for {1} objects", entry.getKey(), objects.size());
|
---|
172 | else
|
---|
173 | text = tr("Set {0}={1} for {2} objects", entry.getKey(), entry.getValue(), objects.size());
|
---|
174 | }
|
---|
175 | else {
|
---|
176 | boolean allnull = true;
|
---|
177 | for (Map.Entry<String, String> tag : this.tags.entrySet()) {
|
---|
178 | if (tag.getValue() != null) {
|
---|
179 | allnull = false;
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (allnull) {
|
---|
185 | text = tr("Deleted {0} properties for {1} objects", tags.size(), objects.size());
|
---|
186 | } else
|
---|
187 | text = tr("Set {0} properties for {1} objects", tags.size(), objects.size());
|
---|
188 | }
|
---|
189 | return text;
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public Icon getDescriptionIcon() {
|
---|
194 | return ImageProvider.get("data", "key");
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Override public Collection<PseudoCommand> getChildren() {
|
---|
198 | if (objects.size() == 1)
|
---|
199 | return null;
|
---|
200 | List<PseudoCommand> children = new ArrayList<PseudoCommand>();
|
---|
201 | for (final OsmPrimitive osm : objects) {
|
---|
202 | children.add(new PseudoCommand() {
|
---|
203 | @Override public String getDescriptionText() {
|
---|
204 | return osm.getDisplayName(DefaultNameFormatter.getInstance());
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override public Icon getDescriptionIcon() {
|
---|
208 | return ImageProvider.get(OsmPrimitiveType.from(osm));
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
212 | return Collections.singleton(osm);
|
---|
213 | }
|
---|
214 |
|
---|
215 | });
|
---|
216 | }
|
---|
217 | return children;
|
---|
218 | }
|
---|
219 | }
|
---|