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

Last change on this file since 4325 was 4302, checked in by stoecker, 13 years ago

fix #6364 - patch by simon04 - can't remove tag with preset (hopefully don't break anything with this patch)

  • Property svn:eol-style set to native
File size: 5.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.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.LinkedList;
12import java.util.List;
13
14import javax.swing.JLabel;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
19import org.openstreetmap.josm.gui.DefaultNameFormatter;
20import org.openstreetmap.josm.tools.ImageProvider;
21
22/**
23 * Command that manipulate the key/value structure of several objects. Manages deletion,
24 * adding and modify of values and keys.
25 *
26 * @author imi
27 */
28public class ChangePropertyCommand extends Command {
29 /**
30 * All primitives that are affected with this command.
31 */
32 private final List<OsmPrimitive> objects;
33 /**
34 * The key that is subject to change.
35 */
36 private final String key;
37 /**
38 * The key value. If it is <code>null</code>, delete all key references with the given
39 * key. Otherwise, change the properties of all objects to the given value or create keys of
40 * those objects that do not have the key yet.
41 */
42 private final String value;
43
44 public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, String key, String value) {
45 super();
46 this.objects = new LinkedList<OsmPrimitive>();
47 this.key = key;
48 this.value = (value == null || value.isEmpty()) ? null : value;
49 if (this.value == null) {
50 for (OsmPrimitive osm : objects) {
51 if(osm.get(key) != null) {
52 this.objects.add(osm);
53 }
54 }
55 } else {
56 for (OsmPrimitive osm : objects) {
57 String val = osm.get(key);
58 if (val == null || !this.value.equals(val)) {
59 this.objects.add(osm);
60 }
61 }
62 }
63 }
64
65 public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
66 this(Arrays.asList(object), key, value);
67 }
68
69 @Override public boolean executeCommand() {
70 Main.main.getCurrentDataSet().beginUpdate();
71 try {
72 super.executeCommand(); // save old
73 if (value == null) {
74 for (OsmPrimitive osm : objects) {
75 osm.setModified(true);
76 osm.remove(key);
77 }
78 } else {
79 for (OsmPrimitive osm : objects) {
80 osm.setModified(true);
81 osm.put(key, value);
82 }
83 }
84 return true;
85 }
86 finally {
87 Main.main.getCurrentDataSet().endUpdate();
88 }
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 JLabel getDescription() {
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 return new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL);
121 }
122
123 @Override public Collection<PseudoCommand> getChildren() {
124 if (objects.size() == 1)
125 return null;
126 List<PseudoCommand> children = new ArrayList<PseudoCommand>();
127 for (final OsmPrimitive osm : objects) {
128 children.add(new PseudoCommand() {
129 @Override public JLabel getDescription() {
130 return new JLabel(
131 osm.getDisplayName(DefaultNameFormatter.getInstance()),
132 ImageProvider.get(OsmPrimitiveType.from(osm)),
133 JLabel.HORIZONTAL);
134
135 }
136 @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
137 return Collections.singleton(osm);
138 }
139
140 });
141 }
142 return children;
143 }
144}
Note: See TracBrowser for help on using the repository browser.