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

Last change on this file since 11192 was 10663, checked in by Don-vip, 8 years ago

fix #13223 - Minor command class fixes (patch by michael2402, modified) - gsoc-core

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