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

Last change on this file since 1347 was 1347, checked in by stoecker, 16 years ago

fixed typo

  • Property svn:eol-style set to native
File size: 3.9 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. Otherwise, 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<? extends OsmPrimitive> objects, String key, String value) {
42 this.objects = new LinkedList<OsmPrimitive>();
43 this.key = key;
44 this.value = value;
45 if (value == null) {
46 for (OsmPrimitive osm : objects) {
47 if(osm.get(key) != null)
48 this.objects.add(osm);
49 }
50 } else {
51 for (OsmPrimitive osm : objects) {
52 String val = osm.get(key);
53 if (val == null || !value.equals(val)) {
54 this.objects.add(osm);
55 }
56 }
57 }
58 }
59
60 public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
61 this.objects = new LinkedList<OsmPrimitive>();
62 this.key = key;
63 this.value = value;
64 String val = object.get(key);
65 if ((value == null && val != null)
66 || (value != null && (val == null || !value.equals(val))))
67 this.objects.add(object);
68 }
69
70 @Override public boolean executeCommand() {
71 super.executeCommand(); // save old
72 if (value == null) {
73 for (OsmPrimitive osm : objects) {
74 osm.modified = true;
75 osm.remove(key);
76 }
77 } else {
78 for (OsmPrimitive osm : objects) {
79 osm.modified = true;
80 osm.put(key, value);
81 }
82 }
83 return true;
84 }
85
86 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
87 modified.addAll(objects);
88 }
89
90 @Override public MutableTreeNode description() {
91 String text;
92 if (objects.size() == 1) {
93 NameVisitor v = new NameVisitor();
94 objects.iterator().next().visit(v);
95 text = value == null
96 ? tr("Remove \"{0}\" for {1} ''{2}''", key, tr(v.className), v.name)
97 : tr("Set {0}={1} for {2} ''{3}''",key,value, tr(v.className), v.name);
98 }
99 else
100 {
101 text = value == null
102 ? tr("Remove \"{0}\" for {1} {2}", key, objects.size(), trn("object","objects",objects.size()))
103 : tr("Set {0}={1} for {2} {3}",key,value, objects.size(), trn("object","objects",objects.size()));
104 }
105 DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
106 if (objects.size() == 1)
107 return root;
108 NameVisitor v = new NameVisitor();
109 for (OsmPrimitive osm : objects) {
110 osm.visit(v);
111 root.add(new DefaultMutableTreeNode(v.toLabel()));
112 }
113 return root;
114 }
115}
Note: See TracBrowser for help on using the repository browser.