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