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

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

mninor fix

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