source: josm/trunk/src/org/openstreetmap/josm/command/ChangePropertyKeyCommand.java@ 5694

Last change on this file since 5694 was 4918, checked in by simon04, 12 years ago

fix #7370 - Refactor Command.getDescription

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.command;
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;
12import javax.swing.Icon;
13
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.validation.util.NameVisitor;
16import org.openstreetmap.josm.tools.ImageProvider;
17
18/**
19 * Command that replaces the key of several objects
20 *
21 */
22public class ChangePropertyKeyCommand extends Command {
23 /**
24 * All primitives, that are affected with this command.
25 */
26 private final List<OsmPrimitive> objects;
27 /**
28 * The key that is subject to change.
29 */
30 private final String key;
31 /**
32 * The mew key.
33 */
34 private final String newKey;
35
36 /**
37 * Constructor
38 *
39 * @param objects all objects subject to change replacement
40 * @param key The key to replace
41 * @param newKey the new value of the key
42 */
43 public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
44 this.objects = new LinkedList<OsmPrimitive>(objects);
45 this.key = key;
46 this.newKey = newKey;
47 }
48
49 @Override
50 public boolean executeCommand() {
51 if (!super.executeCommand())
52 return false; // save old
53 for (OsmPrimitive osm : objects) {
54 if (osm.hasKeys()) {
55 osm.setModified(true);
56 String oldValue = osm.get(key);
57 osm.put(newKey, oldValue);
58 osm.remove(key);
59 }
60 }
61 return true;
62 }
63
64 @Override
65 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
66 modified.addAll(objects);
67 }
68
69 @Override
70 public String getDescriptionText() {
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 }
79 return text;
80 }
81
82 @Override
83 public Icon getDescriptionIcon() {
84 return ImageProvider.get("data", "key");
85 }
86
87 @Override
88 public Collection<PseudoCommand> getChildren() {
89 if (objects.size() == 1)
90 return null;
91 List<PseudoCommand> children = new ArrayList<PseudoCommand>();
92
93 final NameVisitor v = new NameVisitor();
94 for (final OsmPrimitive osm : objects) {
95 osm.visit(v);
96 children.add(new PseudoCommand() {
97 @Override
98 public String getDescriptionText() {
99 return v.name;
100 }
101 @Override
102 public Icon getDescriptionIcon() {
103 return v.icon;
104 }
105 @Override
106 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
107 return Collections.singleton(osm);
108 }
109 });
110 }
111 return children;
112 }
113}
Note: See TracBrowser for help on using the repository browser.