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

Last change on this file since 4894 was 4773, checked in by bastiK, 12 years ago

applied #6883 - property toggle dialog: possibility to select and perform actions on several entries at once. (patch by joshdoe)

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.AbstractMap;
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.JLabel;
18
19import org.openstreetmap.josm.Main;
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 */
31public 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 properties 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 AbstractMap<String, String> tags;
42
43 /**
44 * Creates a command to change multiple properties of multiple objects
45 *
46 * @param objects the objects to modify
47 * @param tags the properties to set
48 */
49 public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, AbstractMap<String, String> tags) {
50 super();
51 this.objects = new LinkedList<OsmPrimitive>();
52 this.tags = tags;
53 init(objects);
54 }
55
56 /**
57 * Creates a command to change one property of multiple objects
58 *
59 * @param objects the objects to modify
60 * @param key the key of the property 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.objects = new LinkedList<OsmPrimitive>();
65 this.tags = new HashMap<String, String>(1);
66 this.tags.put(key, value);
67 init(objects);
68 }
69
70 /**
71 * Creates a command to change on property of one object
72 *
73 * @param object the object to modify
74 * @param key the key of the property to set
75 * @param value the value of the key to set
76 */
77 public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
78 this(Arrays.asList(object), key, value);
79 }
80
81 /**
82 * Initialize the instance by finding what objects will be modified
83 *
84 * @param objects the objects to (possibly) modify
85 */
86 private void init(Collection<? extends OsmPrimitive> objects) {
87 // determine what objects will be modified
88 for (OsmPrimitive osm : objects) {
89 boolean modified = false;
90
91 // loop over all tags
92 for (Map.Entry<String, String> tag : this.tags.entrySet()) {
93 String oldVal = osm.get(tag.getKey());
94 String newVal = tag.getValue();
95
96 if (newVal == null || newVal.isEmpty()) {
97 if (oldVal != null)
98 // new value is null and tag exists (will delete tag)
99 modified = true;
100 }
101 else if (oldVal == null || !newVal.equals(oldVal))
102 // new value is not null and is different from current value
103 modified = true;
104 }
105 if (modified)
106 this.objects.add(osm);
107 }
108 }
109
110 @Override public boolean executeCommand() {
111 Main.main.getCurrentDataSet().beginUpdate();
112 try {
113 super.executeCommand(); // save old
114
115 for (OsmPrimitive osm : objects) {
116 boolean modified = false;
117
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 modified = true;
127 }
128 }
129 else if (oldVal == null || !newVal.equals(oldVal))
130 osm.put(tag.getKey(), newVal);
131 modified = true;
132 }
133 if (modified)
134 osm.setModified(true);
135 }
136 return true;
137 }
138 finally {
139 Main.main.getCurrentDataSet().endUpdate();
140 }
141 }
142
143 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
144 modified.addAll(objects);
145 }
146
147 @Override public JLabel getDescription() {
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 new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL);
190 }
191
192 @Override public Collection<PseudoCommand> getChildren() {
193 if (objects.size() == 1)
194 return null;
195 List<PseudoCommand> children = new ArrayList<PseudoCommand>();
196 for (final OsmPrimitive osm : objects) {
197 children.add(new PseudoCommand() {
198 @Override public JLabel getDescription() {
199 return new JLabel(
200 osm.getDisplayName(DefaultNameFormatter.getInstance()),
201 ImageProvider.get(OsmPrimitiveType.from(osm)),
202 JLabel.HORIZONTAL);
203
204 }
205 @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
206 return Collections.singleton(osm);
207 }
208
209 });
210 }
211 return children;
212 }
213}
Note: See TracBrowser for help on using the repository browser.