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

Last change on this file since 11995 was 11608, checked in by Don-vip, 7 years ago

fix #14402 - add blacklist for leisure area values to avoid false positives - improve globally the detection of keys/tags

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
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 java.util.Objects;
13
14import javax.swing.Icon;
15
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.validation.util.NameVisitor;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * Command that replaces the key of one or several objects
22 * @since 3669
23 */
24public class ChangePropertyKeyCommand extends Command {
25 static final class SinglePrimitivePseudoCommand implements PseudoCommand {
26 private final String name;
27 private final OsmPrimitive osm;
28 private final Icon icon;
29
30 SinglePrimitivePseudoCommand(String name, OsmPrimitive osm, Icon icon) {
31 this.name = name;
32 this.osm = osm;
33 this.icon = icon;
34 }
35
36 @Override
37 public String getDescriptionText() {
38 return name;
39 }
40
41 @Override
42 public Icon getDescriptionIcon() {
43 return icon;
44 }
45
46 @Override
47 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
48 return Collections.singleton(osm);
49 }
50 }
51
52 /**
53 * All primitives, that are affected with this command.
54 */
55 private final List<? extends OsmPrimitive> objects;
56 /**
57 * The key that is subject to change.
58 */
59 private final String key;
60 /**
61 * The mew key.
62 */
63 private final String newKey;
64
65 /**
66 * Constructs a new {@code ChangePropertyKeyCommand}.
67 *
68 * @param object the object subject to change replacement
69 * @param key The key to replace
70 * @param newKey the new value of the key
71 * @since 6329
72 */
73 public ChangePropertyKeyCommand(OsmPrimitive object, String key, String newKey) {
74 this(Collections.singleton(object), key, newKey);
75 }
76
77 /**
78 * Constructs a new {@code ChangePropertyKeyCommand}.
79 *
80 * @param objects all objects subject to change replacement
81 * @param key The key to replace
82 * @param newKey the new value of the key
83 */
84 public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
85 this.objects = new LinkedList<>(objects);
86 this.key = key;
87 this.newKey = newKey;
88 }
89
90 @Override
91 public boolean executeCommand() {
92 if (!super.executeCommand())
93 return false; // save old
94 for (OsmPrimitive osm : objects) {
95 String oldValue = osm.get(key);
96 if (oldValue != null || osm.hasKey(newKey)) {
97 osm.setModified(true);
98 osm.put(newKey, oldValue);
99 osm.remove(key);
100 }
101 }
102 return true;
103 }
104
105 @Override
106 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
107 modified.addAll(objects);
108 }
109
110 @Override
111 public String getDescriptionText() {
112 String text = tr("Replace \"{0}\" by \"{1}\" for", key, newKey);
113 if (objects.size() == 1) {
114 NameVisitor v = new NameVisitor();
115 objects.get(0).accept(v);
116 text += " "+tr(v.className)+" "+v.name;
117 } else {
118 text += " "+objects.size()+" "+trn("object", "objects", objects.size());
119 }
120 return text;
121 }
122
123 @Override
124 public Icon getDescriptionIcon() {
125 return ImageProvider.get("data", "key");
126 }
127
128 @Override
129 public Collection<PseudoCommand> getChildren() {
130 if (objects.size() == 1)
131 return null;
132 List<PseudoCommand> children = new ArrayList<>();
133
134 final NameVisitor v = new NameVisitor();
135 for (final OsmPrimitive osm : objects) {
136 osm.accept(v);
137 children.add(new SinglePrimitivePseudoCommand(v.name, osm, v.icon));
138 }
139 return children;
140 }
141
142 @Override
143 public int hashCode() {
144 return Objects.hash(super.hashCode(), objects, key, newKey);
145 }
146
147 @Override
148 public boolean equals(Object obj) {
149 if (this == obj) return true;
150 if (obj == null || getClass() != obj.getClass()) return false;
151 if (!super.equals(obj)) return false;
152 ChangePropertyKeyCommand that = (ChangePropertyKeyCommand) obj;
153 return Objects.equals(objects, that.objects) &&
154 Objects.equals(key, that.key) &&
155 Objects.equals(newKey, that.newKey);
156 }
157}
Note: See TracBrowser for help on using the repository browser.