source: josm/src/org/openstreetmap/josm/command/ChangePropertyCommand.java@ 298

Last change on this file since 298 was 298, checked in by imi, 17 years ago
  • added license description to head of each source file
File size: 2.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.Collection;
8import java.util.LinkedList;
9import java.util.List;
10
11import javax.swing.JLabel;
12import javax.swing.tree.DefaultMutableTreeNode;
13import javax.swing.tree.MutableTreeNode;
14
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
17import org.openstreetmap.josm.tools.ImageProvider;
18
19/**
20 * Command that manipulate the key/value structure of several objects. Manages deletion,
21 * adding and modify of values and keys.
22 *
23 * @author imi
24 */
25public class ChangePropertyCommand 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 key value. If it is <code>null</code>, delete all key references with the given
36 * key. Else, change the properties of all objects to the given value or create keys of
37 * those objects that do not have the key yet.
38 */
39 private final String value;
40
41 public ChangePropertyCommand(Collection<OsmPrimitive> objects, String key, String value) {
42 this.objects = new LinkedList<OsmPrimitive>(objects);
43 this.key = key;
44 this.value = value;
45 }
46
47 @Override public void executeCommand() {
48 super.executeCommand(); // save old
49 if (value == null) {
50 for (OsmPrimitive osm : objects) {
51 osm.modified = true;
52 osm.remove(key);
53 }
54 } else {
55 for (OsmPrimitive osm : objects) {
56 osm.modified = true;
57 osm.put(key, value);
58 }
59 }
60 }
61
62 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
63 modified.addAll(objects);
64 }
65
66 @Override public MutableTreeNode description() {
67 String text = value == null ? tr( "Remove \"{0}\" for", key) : tr("Set {0}={1} for",key,value);
68 if (objects.size() == 1) {
69 NameVisitor v = new NameVisitor();
70 objects.iterator().next().visit(v);
71 text += " "+tr(v.className)+" "+v.name;
72 } else
73 text += " "+objects.size()+" "+trn("object","objects",objects.size());
74 DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
75 if (objects.size() == 1)
76 return root;
77 NameVisitor v = new NameVisitor();
78 for (OsmPrimitive osm : objects) {
79 osm.visit(v);
80 root.add(new DefaultMutableTreeNode(v.toLabel()));
81 }
82 return root;
83 }
84}
Note: See TracBrowser for help on using the repository browser.