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

Last change on this file since 655 was 655, checked in by ramack, 16 years ago

patch by bruce89, closes #812; thanks bruce

  • Property svn:eol-style set to native
File size: 2.8 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.Collections;
9import java.util.LinkedList;
10import java.util.List;
11
12import javax.swing.JLabel;
13import javax.swing.tree.DefaultMutableTreeNode;
14import javax.swing.tree.MutableTreeNode;
15
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * Command that manipulate the key/value structure of several objects. Manages deletion,
22 * adding and modify of values and keys.
23 *
24 * @author imi
25 */
26public class ChangePropertyCommand extends Command {
27 /**
28 * All primitives that are affected with this command.
29 */
30 private final List<OsmPrimitive> objects;
31 /**
32 * The key that is subject to change.
33 */
34 private final String key;
35 /**
36 * The key value. If it is <code>null</code>, delete all key references with the given
37 * key. Otherwise, change the properties of all objects to the given value or create keys of
38 * those objects that do not have the key yet.
39 */
40 private final String value;
41
42 public ChangePropertyCommand(Collection<OsmPrimitive> objects, String key, String value) {
43 this.objects = new LinkedList<OsmPrimitive>(objects);
44 this.key = key;
45 this.value = value;
46 }
47
48 public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
49 this.objects = new LinkedList<OsmPrimitive>(Collections.singleton(object));
50 this.key = key;
51 this.value = value;
52 }
53
54 @Override public boolean executeCommand() {
55 super.executeCommand(); // save old
56 if (value == null) {
57 for (OsmPrimitive osm : objects) {
58 osm.modified = true;
59 osm.remove(key);
60 }
61 } else {
62 for (OsmPrimitive osm : objects) {
63 osm.modified = true;
64 osm.put(key, value);
65 }
66 }
67 return true;
68 }
69
70 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
71 modified.addAll(objects);
72 }
73
74 @Override public MutableTreeNode description() {
75 String text = value == null ? tr( "Remove \"{0}\" for", key) : tr("Set {0}={1} for",key,value);
76 if (objects.size() == 1) {
77 NameVisitor v = new NameVisitor();
78 objects.iterator().next().visit(v);
79 text += " "+tr(v.className)+" "+v.name;
80 } else
81 text += " "+objects.size()+" "+trn("object","objects",objects.size());
82 DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
83 if (objects.size() == 1)
84 return root;
85 NameVisitor v = new NameVisitor();
86 for (OsmPrimitive osm : objects) {
87 osm.visit(v);
88 root.add(new DefaultMutableTreeNode(v.toLabel()));
89 }
90 return root;
91 }
92}
Note: See TracBrowser for help on using the repository browser.