source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/ChangePropertyKeyCommand.java@ 3669

Last change on this file since 3669 was 3669, checked in by bastiK, 13 years ago

add validator plugin to josm core. Original author: Francisco R. Santos (frsantos); major contributions by bilbo, daeron, delta_foxtrot, imi, jttt, jrreid, gabriel, guggis, pieren, rrankin, skela, stoecker, stotz and others

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.LinkedList;
11import java.util.List;
12
13import javax.swing.JLabel;
14
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.command.PseudoCommand;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.validation.util.NameVisitor;
19import org.openstreetmap.josm.tools.ImageProvider;
20
21/**
22 * Command that replaces the key of several objects
23 *
24 */
25public 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}
Note: See TracBrowser for help on using the repository browser.