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.OsmPrimitiveType;
|
---|
17 | import org.openstreetmap.josm.gui.PrimitiveNameFormatter;
|
---|
18 | import 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 | */
|
---|
26 | public 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<? extends OsmPrimitive> objects, String key, String value) {
|
---|
43 | super();
|
---|
44 | this.objects = new LinkedList<OsmPrimitive>();
|
---|
45 | this.key = key;
|
---|
46 | this.value = value;
|
---|
47 | if (value == null) {
|
---|
48 | for (OsmPrimitive osm : objects) {
|
---|
49 | if(osm.get(key) != null) {
|
---|
50 | this.objects.add(osm);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | } else {
|
---|
54 | for (OsmPrimitive osm : objects) {
|
---|
55 | String val = osm.get(key);
|
---|
56 | if (val == null || !value.equals(val)) {
|
---|
57 | this.objects.add(osm);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
|
---|
64 | this.objects = new LinkedList<OsmPrimitive>();
|
---|
65 | this.key = key;
|
---|
66 | this.value = value;
|
---|
67 | String val = object.get(key);
|
---|
68 | if ((value == null && val != null)
|
---|
69 | || (value != null && (val == null || !value.equals(val)))) {
|
---|
70 | this.objects.add(object);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override public boolean executeCommand() {
|
---|
75 | super.executeCommand(); // save old
|
---|
76 | if (value == null) {
|
---|
77 | for (OsmPrimitive osm : objects) {
|
---|
78 | osm.modified = true;
|
---|
79 | osm.remove(key);
|
---|
80 | }
|
---|
81 | } else {
|
---|
82 | for (OsmPrimitive osm : objects) {
|
---|
83 | osm.modified = true;
|
---|
84 | osm.put(key, value);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
91 | modified.addAll(objects);
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override public MutableTreeNode description() {
|
---|
95 | String text;
|
---|
96 | PrimitiveNameFormatter formatter = new PrimitiveNameFormatter();
|
---|
97 | if (objects.size() == 1) {
|
---|
98 | OsmPrimitive primitive = objects.iterator().next();
|
---|
99 | String name = formatter.getName(primitive);
|
---|
100 | text = value == null
|
---|
101 | ? tr("Remove \"{0}\" for {1} ''{2}''", key,
|
---|
102 | OsmPrimitiveType.from(primitive).getLocalizedDisplayNameSingular(),
|
---|
103 | name)
|
---|
104 | : tr("Set {0}={1} for {2} ''{3}''",
|
---|
105 | key,
|
---|
106 | value,
|
---|
107 | OsmPrimitiveType.from(primitive).getLocalizedDisplayNameSingular(),
|
---|
108 | name
|
---|
109 | );
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | text = value == null
|
---|
114 | ? tr("Remove \"{0}\" for {1} {2}", key, objects.size(), trn("object","objects",objects.size()))
|
---|
115 | : tr("Set {0}={1} for {2} {3}",key,value, objects.size(), trn("object","objects",objects.size()));
|
---|
116 | }
|
---|
117 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
|
---|
118 | if (objects.size() == 1)
|
---|
119 | return root;
|
---|
120 | for (OsmPrimitive osm : objects) {
|
---|
121 | root.add(new DefaultMutableTreeNode(
|
---|
122 | new JLabel(
|
---|
123 | formatter.getName(osm),
|
---|
124 | ImageProvider.get(OsmPrimitiveType.from(osm)),
|
---|
125 | JLabel.HORIZONTAL)
|
---|
126 | )
|
---|
127 | );
|
---|
128 | }
|
---|
129 | return root;
|
---|
130 | }
|
---|
131 | }
|
---|