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

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

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

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.command;
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;
12import javax.swing.Icon;
13
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.validation.util.NameVisitor;
16import org.openstreetmap.josm.tools.ImageProvider;
17
18/**
19 * Command that replaces the key of one or several objects
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 /**
37 * Constructs a new {@code ChangePropertyKeyCommand}.
38 *
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 *
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
61 @Override
62 public boolean executeCommand() {
63 if (!super.executeCommand())
64 return false; // save old
65 for (OsmPrimitive osm : objects) {
66 if (osm.hasKeys()) {
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
76 @Override
77 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
78 modified.addAll(objects);
79 }
80
81 @Override
82 public String getDescriptionText() {
83 String text = tr( "Replace \"{0}\" by \"{1}\" for", key, newKey);
84 if (objects.size() == 1) {
85 NameVisitor v = new NameVisitor();
86 objects.iterator().next().accept(v);
87 text += " "+tr(v.className)+" "+v.name;
88 } else {
89 text += " "+objects.size()+" "+trn("object","objects",objects.size());
90 }
91 return text;
92 }
93
94 @Override
95 public Icon getDescriptionIcon() {
96 return ImageProvider.get("data", "key");
97 }
98
99 @Override
100 public Collection<PseudoCommand> getChildren() {
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) {
107 osm.accept(v);
108 children.add(new PseudoCommand() {
109 @Override
110 public String getDescriptionText() {
111 return v.name;
112 }
113 @Override
114 public Icon getDescriptionIcon() {
115 return v.icon;
116 }
117 @Override
118 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
119 return Collections.singleton(osm);
120 }
121 });
122 }
123 return children;
124 }
125}
Note: See TracBrowser for help on using the repository browser.