source: josm/trunk/src/org/openstreetmap/josm/command/ChangePropertyKeyCommand.java@ 6388

Last change on this file since 6388 was 6329, checked in by Don-vip, 11 years ago

fix #9217 - tagchecker: allow auto-fix for single test/alternative

  • Property svn:eol-style set to native
File size: 3.8 KB
RevLine 
[3669]1// License: GPL. See LICENSE file for details.
[4806]2package org.openstreetmap.josm.command;
[3669]3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.LinkedList;
11import java.util.List;
[4918]12import javax.swing.Icon;
[3669]13
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.validation.util.NameVisitor;
16import org.openstreetmap.josm.tools.ImageProvider;
17
18/**
[6329]19 * Command that replaces the key of one or several objects
[3669]20 *
21 */
22public class ChangePropertyKeyCommand extends Command {
23 /**
24 * All primitives, that are affected with this command.
25 */
26 private final List<OsmPrimitive> objects;
27 /**
28 * The key that is subject to change.
29 */
30 private final String key;
31 /**
32 * The mew key.
33 */
34 private final String newKey;
35
36 /**
[6329]37 * Constructs a new {@code ChangePropertyKeyCommand}.
[3669]38 *
[6329]39 * @param object the object subject to change replacement
40 * @param key The key to replace
41 * @param newKey the new value of the key
42 * @since 6329
43 */
44 public ChangePropertyKeyCommand(OsmPrimitive object, String key, String newKey) {
45 this(Collections.singleton(object), key, newKey);
46 }
47
48 /**
49 * Constructs a new {@code ChangePropertyKeyCommand}.
50 *
[3669]51 * @param objects all objects subject to change replacement
52 * @param key The key to replace
53 * @param newKey the new value of the key
54 */
55 public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
56 this.objects = new LinkedList<OsmPrimitive>(objects);
57 this.key = key;
58 this.newKey = newKey;
59 }
60
[3671]61 @Override
62 public boolean executeCommand() {
63 if (!super.executeCommand())
64 return false; // save old
[3669]65 for (OsmPrimitive osm : objects) {
[3671]66 if (osm.hasKeys()) {
[3669]67 osm.setModified(true);
68 String oldValue = osm.get(key);
69 osm.put(newKey, oldValue);
70 osm.remove(key);
71 }
72 }
73 return true;
74 }
75
[3671]76 @Override
77 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
[3669]78 modified.addAll(objects);
79 }
80
[3671]81 @Override
[4918]82 public String getDescriptionText() {
[3669]83 String text = tr( "Replace \"{0}\" by \"{1}\" for", key, newKey);
84 if (objects.size() == 1) {
85 NameVisitor v = new NameVisitor();
[6009]86 objects.iterator().next().accept(v);
[3669]87 text += " "+tr(v.className)+" "+v.name;
[3671]88 } else {
[3669]89 text += " "+objects.size()+" "+trn("object","objects",objects.size());
[3671]90 }
[4918]91 return text;
[3669]92 }
93
[3671]94 @Override
[4918]95 public Icon getDescriptionIcon() {
96 return ImageProvider.get("data", "key");
97 }
98
99 @Override
[3671]100 public Collection<PseudoCommand> getChildren() {
[3669]101 if (objects.size() == 1)
102 return null;
103 List<PseudoCommand> children = new ArrayList<PseudoCommand>();
104
105 final NameVisitor v = new NameVisitor();
106 for (final OsmPrimitive osm : objects) {
[6009]107 osm.accept(v);
[3669]108 children.add(new PseudoCommand() {
[3671]109 @Override
[4918]110 public String getDescriptionText() {
111 return v.name;
[3669]112 }
[3671]113 @Override
[4918]114 public Icon getDescriptionIcon() {
115 return v.icon;
116 }
117 @Override
[3671]118 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
[3669]119 return Collections.singleton(osm);
120 }
121 });
122 }
123 return children;
124 }
125}
Note: See TracBrowser for help on using the repository browser.