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