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 public boolean executeCommand() {
|
---|
53 | if (!super.executeCommand()) return false; // save old
|
---|
54 | for (OsmPrimitive osm : objects) {
|
---|
55 | if(osm.hasKeys())
|
---|
56 | {
|
---|
57 | osm.setModified(true);
|
---|
58 | String oldValue = osm.get(key);
|
---|
59 | osm.put(newKey, oldValue);
|
---|
60 | osm.remove(key);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
67 | modified.addAll(objects);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override public JLabel getDescription() {
|
---|
71 | String text = tr( "Replace \"{0}\" by \"{1}\" for", key, newKey);
|
---|
72 | if (objects.size() == 1) {
|
---|
73 | NameVisitor v = new NameVisitor();
|
---|
74 | objects.iterator().next().visit(v);
|
---|
75 | text += " "+tr(v.className)+" "+v.name;
|
---|
76 | } else
|
---|
77 | text += " "+objects.size()+" "+trn("object","objects",objects.size());
|
---|
78 | return new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override public Collection<PseudoCommand> getChildren() {
|
---|
82 | if (objects.size() == 1)
|
---|
83 | return null;
|
---|
84 | List<PseudoCommand> children = new ArrayList<PseudoCommand>();
|
---|
85 |
|
---|
86 | final NameVisitor v = new NameVisitor();
|
---|
87 | for (final OsmPrimitive osm : objects) {
|
---|
88 | osm.visit(v);
|
---|
89 | children.add(new PseudoCommand() {
|
---|
90 | @Override public JLabel getDescription() {
|
---|
91 | return v.toLabel();
|
---|
92 | }
|
---|
93 | @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
94 | return Collections.singleton(osm);
|
---|
95 | }
|
---|
96 | });
|
---|
97 | }
|
---|
98 | return children;
|
---|
99 | }
|
---|
100 | }
|
---|