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

Last change on this file since 6499 was 6380, checked in by Don-vip, 12 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 7.9 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[626]2package org.openstreetmap.josm.command;
3
[1990]4import static org.openstreetmap.josm.tools.I18n.marktr;
[626]5import static org.openstreetmap.josm.tools.I18n.tr;
6
[4773]7import java.util.AbstractMap;
[3262]8import java.util.ArrayList;
[4302]9import java.util.Arrays;
[626]10import java.util.Collection;
[3262]11import java.util.Collections;
[4773]12import java.util.HashMap;
[626]13import java.util.LinkedList;
14import java.util.List;
[4773]15import java.util.Map;
[5077]16
[4918]17import javax.swing.Icon;
[626]18
[3910]19import org.openstreetmap.josm.Main;
[626]20import org.openstreetmap.josm.data.osm.OsmPrimitive;
[1814]21import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
[1990]22import org.openstreetmap.josm.gui.DefaultNameFormatter;
[626]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.
[1169]28 *
[626]29 * @author imi
30 */
31public class ChangePropertyCommand extends Command {
[1169]32 /**
33 * All primitives that are affected with this command.
34 */
35 private final List<OsmPrimitive> objects;
36 /**
[4773]37 * Key and value pairs. If value is <code>null</code>, delete all key references with the given
[6324]38 * key. Otherwise, change the tags of all objects to the given value or create keys of
[1169]39 * those objects that do not have the key yet.
40 */
[4773]41 private final AbstractMap<String, String> tags;
[628]42
[4773]43 /**
[6324]44 * Creates a command to change multiple tags of multiple objects
[4773]45 *
46 * @param objects the objects to modify
[6324]47 * @param tags the tags to set
[4773]48 */
49 public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, AbstractMap<String, String> tags) {
[1750]50 super();
[1169]51 this.objects = new LinkedList<OsmPrimitive>();
[4773]52 this.tags = tags;
53 init(objects);
[1169]54 }
[626]55
[4773]56 /**
[6324]57 * Creates a command to change one tag of multiple objects
[4773]58 *
59 * @param objects the objects to modify
[6324]60 * @param key the key of the tag to set
[4773]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 /**
[6324]71 * Creates a command to change one tag of one object
[4773]72 *
73 * @param object the object to modify
[6324]74 * @param key the key of the tag to set
[4773]75 * @param value the value of the key to set
76 */
[1169]77 public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
[4302]78 this(Arrays.asList(object), key, value);
[1169]79 }
[626]80
[4773]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
[1169]110 @Override public boolean executeCommand() {
[3910]111 Main.main.getCurrentDataSet().beginUpdate();
112 try {
113 super.executeCommand(); // save old
[4773]114
115 for (OsmPrimitive osm : objects) {
[5143]116 // loop over all tags
[4773]117 for (Map.Entry<String, String> tag : this.tags.entrySet()) {
118 String oldVal = osm.get(tag.getKey());
119 String newVal = tag.getValue();
120
121 if (newVal == null || newVal.isEmpty()) {
[5143]122 if (oldVal != null)
[4773]123 osm.remove(tag.getKey());
124 }
125 else if (oldVal == null || !newVal.equals(oldVal))
126 osm.put(tag.getKey(), newVal);
[3910]127 }
[5143]128 // init() only keeps modified primitives. Therefore the modified
129 // bit can be set without further checks.
130 osm.setModified(true);
[1169]131 }
[3910]132 return true;
[1169]133 }
[3910]134 finally {
135 Main.main.getCurrentDataSet().endUpdate();
136 }
[626]137 }
[1169]138
139 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
140 modified.addAll(objects);
141 }
142
[4918]143 @Override
144 public String getDescriptionText() {
[1182]145 String text;
[4773]146 if (objects.size() == 1 && tags.size() == 1) {
[1814]147 OsmPrimitive primitive = objects.iterator().next();
[1989]148 String msg = "";
[4773]149 Map.Entry<String, String> entry = tags.entrySet().iterator().next();
150 if (entry.getValue() == null) {
[1989]151 switch(OsmPrimitiveType.from(primitive)) {
[2990]152 case NODE: msg = marktr("Remove \"{0}\" for node ''{1}''"); break;
153 case WAY: msg = marktr("Remove \"{0}\" for way ''{1}''"); break;
154 case RELATION: msg = marktr("Remove \"{0}\" for relation ''{1}''"); break;
[1989]155 }
[4773]156 text = tr(msg, entry.getKey(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
[1989]157 } else {
158 switch(OsmPrimitiveType.from(primitive)) {
[2990]159 case NODE: msg = marktr("Set {0}={1} for node ''{2}''"); break;
160 case WAY: msg = marktr("Set {0}={1} for way ''{2}''"); break;
161 case RELATION: msg = marktr("Set {0}={1} for relation ''{2}''"); break;
[1989]162 }
[4773]163 text = tr(msg, entry.getKey(), entry.getValue(), primitive.getDisplayName(DefaultNameFormatter.getInstance()));
[1989]164 }
[4773]165 } else if (objects.size() > 1 && tags.size() == 1) {
166 Map.Entry<String, String> entry = tags.entrySet().iterator().next();
167 if (entry.getValue() == null)
168 text = tr("Remove \"{0}\" for {1} objects", entry.getKey(), objects.size());
169 else
170 text = tr("Set {0}={1} for {2} objects", entry.getKey(), entry.getValue(), objects.size());
[1182]171 }
[4773]172 else {
173 boolean allnull = true;
174 for (Map.Entry<String, String> tag : this.tags.entrySet()) {
175 if (tag.getValue() != null) {
176 allnull = false;
177 break;
178 }
179 }
[5077]180
[4773]181 if (allnull) {
[6324]182 text = tr("Deleted {0} tags for {1} objects", tags.size(), objects.size());
[4773]183 } else
[6324]184 text = tr("Set {0} tags for {1} objects", tags.size(), objects.size());
[4773]185 }
[4918]186 return text;
[3262]187 }
188
[4918]189 @Override
190 public Icon getDescriptionIcon() {
191 return ImageProvider.get("data", "key");
192 }
193
[3262]194 @Override public Collection<PseudoCommand> getChildren() {
[1169]195 if (objects.size() == 1)
[3262]196 return null;
197 List<PseudoCommand> children = new ArrayList<PseudoCommand>();
198 for (final OsmPrimitive osm : objects) {
199 children.add(new PseudoCommand() {
[4918]200 @Override public String getDescriptionText() {
201 return osm.getDisplayName(DefaultNameFormatter.getInstance());
202 }
[3262]203
[4918]204 @Override public Icon getDescriptionIcon() {
[5077]205 return ImageProvider.get(osm.getDisplayType());
[3262]206 }
[4918]207
[3262]208 @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
209 return Collections.singleton(osm);
210 }
211
212 });
[1169]213 }
[3262]214 return children;
[1169]215 }
[626]216}
Note: See TracBrowser for help on using the repository browser.