1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
13 | import javax.swing.tree.MutableTreeNode;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
17 | import 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 | */
|
---|
25 | public 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 | super();
|
---|
43 | this.objects = new LinkedList<OsmPrimitive>();
|
---|
44 | this.key = key;
|
---|
45 | this.value = value;
|
---|
46 | if (value == null) {
|
---|
47 | for (OsmPrimitive osm : objects) {
|
---|
48 | if(osm.get(key) != null) {
|
---|
49 | this.objects.add(osm);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | } else {
|
---|
53 | for (OsmPrimitive osm : objects) {
|
---|
54 | String val = osm.get(key);
|
---|
55 | if (val == null || !value.equals(val)) {
|
---|
56 | this.objects.add(osm);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
|
---|
63 | this.objects = new LinkedList<OsmPrimitive>();
|
---|
64 | this.key = key;
|
---|
65 | this.value = value;
|
---|
66 | String val = object.get(key);
|
---|
67 | if ((value == null && val != null)
|
---|
68 | || (value != null && (val == null || !value.equals(val)))) {
|
---|
69 | this.objects.add(object);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override public boolean executeCommand() {
|
---|
74 | super.executeCommand(); // save old
|
---|
75 | if (value == null) {
|
---|
76 | for (OsmPrimitive osm : objects) {
|
---|
77 | osm.modified = true;
|
---|
78 | osm.remove(key);
|
---|
79 | }
|
---|
80 | } else {
|
---|
81 | for (OsmPrimitive osm : objects) {
|
---|
82 | osm.modified = true;
|
---|
83 | osm.put(key, value);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
90 | modified.addAll(objects);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override public MutableTreeNode description() {
|
---|
94 | String text;
|
---|
95 | if (objects.size() == 1) {
|
---|
96 | NameVisitor v = new NameVisitor();
|
---|
97 | objects.iterator().next().visit(v);
|
---|
98 | text = value == null
|
---|
99 | ? tr("Remove \"{0}\" for {1} ''{2}''", key, tr(v.className), v.name)
|
---|
100 | : tr("Set {0}={1} for {2} ''{3}''",key,value, tr(v.className), v.name);
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | text = value == null
|
---|
105 | ? tr("Remove \"{0}\" for {1} {2}", key, objects.size(), trn("object","objects",objects.size()))
|
---|
106 | : tr("Set {0}={1} for {2} {3}",key,value, objects.size(), trn("object","objects",objects.size()));
|
---|
107 | }
|
---|
108 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
|
---|
109 | if (objects.size() == 1)
|
---|
110 | return root;
|
---|
111 | NameVisitor v = new NameVisitor();
|
---|
112 | for (OsmPrimitive osm : objects) {
|
---|
113 | osm.visit(v);
|
---|
114 | root.add(new DefaultMutableTreeNode(v.toLabel()));
|
---|
115 | }
|
---|
116 | return root;
|
---|
117 | }
|
---|
118 | }
|
---|