[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[4806] | 2 | package org.openstreetmap.josm.command;
|
---|
[3669] | 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;
|
---|
[12726] | 12 | import java.util.NoSuchElementException;
|
---|
[9371] | 13 | import java.util.Objects;
|
---|
[8378] | 14 |
|
---|
[4918] | 15 | import javax.swing.Icon;
|
---|
[3669] | 16 |
|
---|
[12726] | 17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[3669] | 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 | /**
|
---|
[6329] | 23 | * Command that replaces the key of one or several objects
|
---|
[11357] | 24 | * @since 3669
|
---|
[3669] | 25 | */
|
---|
| 26 | public class ChangePropertyKeyCommand extends Command {
|
---|
[11362] | 27 | static final class SinglePrimitivePseudoCommand implements PseudoCommand {
|
---|
[11357] | 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 |
|
---|
[3669] | 54 | /**
|
---|
| 55 | * All primitives, that are affected with this command.
|
---|
| 56 | */
|
---|
[8777] | 57 | private final List<? extends OsmPrimitive> objects;
|
---|
[3669] | 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 | /**
|
---|
[6329] | 68 | * Constructs a new {@code ChangePropertyKeyCommand}.
|
---|
[3669] | 69 | *
|
---|
[12726] | 70 | * @param object the object subject to change replacement. Must not be null, and belong to a data set
|
---|
[6329] | 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 | }
|
---|
[7509] | 78 |
|
---|
[6329] | 79 | /**
|
---|
| 80 | * Constructs a new {@code ChangePropertyKeyCommand}.
|
---|
| 81 | *
|
---|
[12726] | 82 | * @param objects all objects subject to change replacement. Must not be null or empty, and objects must belong to a data set
|
---|
[3669] | 83 | * @param key The key to replace
|
---|
| 84 | * @param newKey the new value of the key
|
---|
[12726] | 85 | * @throws NullPointerException if objects is null or contain null item
|
---|
| 86 | * @throws NoSuchElementException if objects is empty
|
---|
[3669] | 87 | */
|
---|
| 88 | public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
|
---|
[12726] | 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);
|
---|
[7005] | 103 | this.objects = new LinkedList<>(objects);
|
---|
[3669] | 104 | this.key = key;
|
---|
| 105 | this.newKey = newKey;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[3671] | 108 | @Override
|
---|
| 109 | public boolean executeCommand() {
|
---|
| 110 | if (!super.executeCommand())
|
---|
| 111 | return false; // save old
|
---|
[3669] | 112 | for (OsmPrimitive osm : objects) {
|
---|
[11608] | 113 | String oldValue = osm.get(key);
|
---|
| 114 | if (oldValue != null || osm.hasKey(newKey)) {
|
---|
[3669] | 115 | osm.setModified(true);
|
---|
| 116 | osm.put(newKey, oldValue);
|
---|
| 117 | osm.remove(key);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | return true;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[3671] | 123 | @Override
|
---|
| 124 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[3669] | 125 | modified.addAll(objects);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[3671] | 128 | @Override
|
---|
[4918] | 129 | public String getDescriptionText() {
|
---|
[8443] | 130 | String text = tr("Replace \"{0}\" by \"{1}\" for", key, newKey);
|
---|
[3669] | 131 | if (objects.size() == 1) {
|
---|
| 132 | NameVisitor v = new NameVisitor();
|
---|
[8846] | 133 | objects.get(0).accept(v);
|
---|
[10663] | 134 | text += " "+tr(v.className)+" "+v.name;
|
---|
[3671] | 135 | } else {
|
---|
[10663] | 136 | text += " "+objects.size()+" "+trn("object", "objects", objects.size());
|
---|
[3671] | 137 | }
|
---|
[4918] | 138 | return text;
|
---|
[3669] | 139 | }
|
---|
| 140 |
|
---|
[3671] | 141 | @Override
|
---|
[4918] | 142 | public Icon getDescriptionIcon() {
|
---|
| 143 | return ImageProvider.get("data", "key");
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | @Override
|
---|
[3671] | 147 | public Collection<PseudoCommand> getChildren() {
|
---|
[3669] | 148 | if (objects.size() == 1)
|
---|
| 149 | return null;
|
---|
[7005] | 150 | List<PseudoCommand> children = new ArrayList<>();
|
---|
[3669] | 151 |
|
---|
| 152 | final NameVisitor v = new NameVisitor();
|
---|
| 153 | for (final OsmPrimitive osm : objects) {
|
---|
[6009] | 154 | osm.accept(v);
|
---|
[11357] | 155 | children.add(new SinglePrimitivePseudoCommand(v.name, osm, v.icon));
|
---|
[3669] | 156 | }
|
---|
| 157 | return children;
|
---|
| 158 | }
|
---|
[8456] | 159 |
|
---|
| 160 | @Override
|
---|
| 161 | public int hashCode() {
|
---|
[9371] | 162 | return Objects.hash(super.hashCode(), objects, key, newKey);
|
---|
[8456] | 163 | }
|
---|
| 164 |
|
---|
| 165 | @Override
|
---|
| 166 | public boolean equals(Object obj) {
|
---|
[9371] | 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);
|
---|
[8456] | 174 | }
|
---|
[3669] | 175 | }
|
---|