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.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
14 | import javax.swing.tree.MutableTreeNode;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
18 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Command that manipulate the key/value structure of several objects. Manages deletion,
|
---|
23 | * adding and modify of values and keys.
|
---|
24 | *
|
---|
25 | * @author imi
|
---|
26 | */
|
---|
27 | public class ChangePropertyCommand extends Command {
|
---|
28 | /**
|
---|
29 | * All primitives that are affected with this command.
|
---|
30 | */
|
---|
31 | private final List<OsmPrimitive> objects;
|
---|
32 | /**
|
---|
33 | * The key that is subject to change.
|
---|
34 | */
|
---|
35 | private final String key;
|
---|
36 | /**
|
---|
37 | * The key value. If it is <code>null</code>, delete all key references with the given
|
---|
38 | * key. Otherwise, change the properties of all objects to the given value or create keys of
|
---|
39 | * those objects that do not have the key yet.
|
---|
40 | */
|
---|
41 | private final String value;
|
---|
42 |
|
---|
43 | public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, String key, String value) {
|
---|
44 | super();
|
---|
45 | this.objects = new LinkedList<OsmPrimitive>();
|
---|
46 | this.key = key;
|
---|
47 | this.value = value;
|
---|
48 | if (value == null) {
|
---|
49 | for (OsmPrimitive osm : objects) {
|
---|
50 | if(osm.get(key) != null) {
|
---|
51 | this.objects.add(osm);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | } else {
|
---|
55 | for (OsmPrimitive osm : objects) {
|
---|
56 | String val = osm.get(key);
|
---|
57 | if (val == null || !value.equals(val)) {
|
---|
58 | this.objects.add(osm);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
|
---|
65 | this.objects = new LinkedList<OsmPrimitive>();
|
---|
66 | this.key = key;
|
---|
67 | this.value = value;
|
---|
68 | String val = object.get(key);
|
---|
69 | if ((value == null && val != null)
|
---|
70 | || (value != null && (val == null || !value.equals(val)))) {
|
---|
71 | this.objects.add(object);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override public boolean executeCommand() {
|
---|
76 | super.executeCommand(); // save old
|
---|
77 | if (value == null) {
|
---|
78 | for (OsmPrimitive osm : objects) {
|
---|
79 | osm.setModified(true);
|
---|
80 | osm.remove(key);
|
---|
81 | }
|
---|
82 | } else {
|
---|
83 | for (OsmPrimitive osm : objects) {
|
---|
84 | osm.setModified(true);
|
---|
85 | osm.put(key, value);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
92 | modified.addAll(objects);
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override public MutableTreeNode description() {
|
---|
96 | String text;
|
---|
97 | if (objects.size() == 1) {
|
---|
98 | OsmPrimitive primitive = objects.iterator().next();
|
---|
99 | String msg = "";
|
---|
100 | if (value == null) {
|
---|
101 | switch(OsmPrimitiveType.from(primitive)) {
|
---|
102 | case NODE: msg = marktr("Remove \"{0}\" for node ''{1}''"); break;
|
---|
103 | case WAY: msg = marktr("Remove \"{0}\" for way ''{1}''"); break;
|
---|
104 | case RELATION: msg = marktr("Remove \"{0}\" for relation ''{1}''"); break;
|
---|
105 | }
|
---|
106 | text = tr(msg, key, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
107 | } else {
|
---|
108 | switch(OsmPrimitiveType.from(primitive)) {
|
---|
109 | case NODE: msg = marktr("Set {0}={1} for node ''{2}''"); break;
|
---|
110 | case WAY: msg = marktr("Set {0}={1} for way ''{2}''"); break;
|
---|
111 | case RELATION: msg = marktr("Set {0}={1} for relation ''{2}''"); break;
|
---|
112 | }
|
---|
113 | text = tr(msg, key, value, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
114 | }
|
---|
115 | } else {
|
---|
116 | text = value == null
|
---|
117 | ? tr("Remove \"{0}\" for {1} objects", key, objects.size())
|
---|
118 | : tr("Set {0}={1} for {2} objects", key, value, objects.size());
|
---|
119 | }
|
---|
120 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
|
---|
121 | if (objects.size() == 1)
|
---|
122 | return root;
|
---|
123 | for (OsmPrimitive osm : objects) {
|
---|
124 | root.add(new DefaultMutableTreeNode(
|
---|
125 | new JLabel(
|
---|
126 | osm.getDisplayName(DefaultNameFormatter.getInstance()),
|
---|
127 | ImageProvider.get(OsmPrimitiveType.from(osm)),
|
---|
128 | JLabel.HORIZONTAL)
|
---|
129 | )
|
---|
130 | );
|
---|
131 | }
|
---|
132 | return root;
|
---|
133 | }
|
---|
134 | }
|
---|