| 1 | // License: GPL. See LICENSE file for details.
|
|---|
| 2 | package org.openstreetmap.josm.data.validation.tests;
|
|---|
| 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.JLabel;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.command.Command;
|
|---|
| 16 | import org.openstreetmap.josm.command.PseudoCommand;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 18 | import org.openstreetmap.josm.data.validation.util.NameVisitor;
|
|---|
| 19 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Command that replaces the key of several objects
|
|---|
| 23 | *
|
|---|
| 24 | */
|
|---|
| 25 | public class ChangePropertyKeyCommand extends Command {
|
|---|
| 26 | /**
|
|---|
| 27 | * All primitives, that are affected with this command.
|
|---|
| 28 | */
|
|---|
| 29 | private final List<OsmPrimitive> objects;
|
|---|
| 30 | /**
|
|---|
| 31 | * The key that is subject to change.
|
|---|
| 32 | */
|
|---|
| 33 | private final String key;
|
|---|
| 34 | /**
|
|---|
| 35 | * The mew key.
|
|---|
| 36 | */
|
|---|
| 37 | private final String newKey;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Constructor
|
|---|
| 41 | *
|
|---|
| 42 | * @param objects all objects subject to change replacement
|
|---|
| 43 | * @param key The key to replace
|
|---|
| 44 | * @param newKey the new value of the key
|
|---|
| 45 | */
|
|---|
| 46 | public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
|
|---|
| 47 | this.objects = new LinkedList<OsmPrimitive>(objects);
|
|---|
| 48 | this.key = key;
|
|---|
| 49 | this.newKey = newKey;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | @Override
|
|---|
| 53 | public boolean executeCommand() {
|
|---|
| 54 | if (!super.executeCommand())
|
|---|
| 55 | return false; // save old
|
|---|
| 56 | for (OsmPrimitive osm : objects) {
|
|---|
| 57 | if (osm.hasKeys()) {
|
|---|
| 58 | osm.setModified(true);
|
|---|
| 59 | String oldValue = osm.get(key);
|
|---|
| 60 | osm.put(newKey, oldValue);
|
|---|
| 61 | osm.remove(key);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | return true;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | @Override
|
|---|
| 68 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
|---|
| 69 | modified.addAll(objects);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @Override
|
|---|
| 73 | public JLabel getDescription() {
|
|---|
| 74 | String text = tr( "Replace \"{0}\" by \"{1}\" for", key, newKey);
|
|---|
| 75 | if (objects.size() == 1) {
|
|---|
| 76 | NameVisitor v = new NameVisitor();
|
|---|
| 77 | objects.iterator().next().visit(v);
|
|---|
| 78 | text += " "+tr(v.className)+" "+v.name;
|
|---|
| 79 | } else {
|
|---|
| 80 | text += " "+objects.size()+" "+trn("object","objects",objects.size());
|
|---|
| 81 | }
|
|---|
| 82 | return new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | @Override
|
|---|
| 86 | public Collection<PseudoCommand> getChildren() {
|
|---|
| 87 | if (objects.size() == 1)
|
|---|
| 88 | return null;
|
|---|
| 89 | List<PseudoCommand> children = new ArrayList<PseudoCommand>();
|
|---|
| 90 |
|
|---|
| 91 | final NameVisitor v = new NameVisitor();
|
|---|
| 92 | for (final OsmPrimitive osm : objects) {
|
|---|
| 93 | osm.visit(v);
|
|---|
| 94 | children.add(new PseudoCommand() {
|
|---|
| 95 | @Override
|
|---|
| 96 | public JLabel getDescription() {
|
|---|
| 97 | return v.toLabel();
|
|---|
| 98 | }
|
|---|
| 99 | @Override
|
|---|
| 100 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
|---|
| 101 | return Collections.singleton(osm);
|
|---|
| 102 | }
|
|---|
| 103 | });
|
|---|
| 104 | }
|
|---|
| 105 | return children;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|