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

Last change on this file since 4077 was 3910, checked in by stoecker, 13 years ago

apply #5209 - reduce repeated signaling

  • Property svn:eol-style set to native
File size: 5.4 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.Collection;
9import java.util.Collections;
10import java.util.LinkedList;
11import java.util.List;
12
13import javax.swing.JLabel;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
18import org.openstreetmap.josm.gui.DefaultNameFormatter;
19import 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 */
27public 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 Main.main.getCurrentDataSet().beginUpdate();
77 try {
78 super.executeCommand(); // save old
79 if (value == null) {
80 for (OsmPrimitive osm : objects) {
81 osm.setModified(true);
82 osm.remove(key);
83 }
84 } else {
85 for (OsmPrimitive osm : objects) {
86 osm.setModified(true);
87 osm.put(key, value);
88 }
89 }
90 return true;
91 }
92 finally {
93 Main.main.getCurrentDataSet().endUpdate();
94 }
95 }
96
97 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
98 modified.addAll(objects);
99 }
100
101 @Override public JLabel getDescription() {
102 String text;
103 if (objects.size() == 1) {
104 OsmPrimitive primitive = objects.iterator().next();
105 String msg = "";
106 if (value == null) {
107 switch(OsmPrimitiveType.from(primitive)) {
108 case NODE: msg = marktr("Remove \"{0}\" for node ''{1}''"); break;
109 case WAY: msg = marktr("Remove \"{0}\" for way ''{1}''"); break;
110 case RELATION: msg = marktr("Remove \"{0}\" for relation ''{1}''"); break;
111 }
112 text = tr(msg, key, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
113 } else {
114 switch(OsmPrimitiveType.from(primitive)) {
115 case NODE: msg = marktr("Set {0}={1} for node ''{2}''"); break;
116 case WAY: msg = marktr("Set {0}={1} for way ''{2}''"); break;
117 case RELATION: msg = marktr("Set {0}={1} for relation ''{2}''"); break;
118 }
119 text = tr(msg, key, value, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
120 }
121 } else {
122 text = value == null
123 ? tr("Remove \"{0}\" for {1} objects", key, objects.size())
124 : tr("Set {0}={1} for {2} objects", key, value, objects.size());
125 }
126 return new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL);
127 }
128
129 @Override public Collection<PseudoCommand> getChildren() {
130 if (objects.size() == 1)
131 return null;
132 List<PseudoCommand> children = new ArrayList<PseudoCommand>();
133 for (final OsmPrimitive osm : objects) {
134 children.add(new PseudoCommand() {
135 @Override public JLabel getDescription() {
136 return new JLabel(
137 osm.getDisplayName(DefaultNameFormatter.getInstance()),
138 ImageProvider.get(OsmPrimitiveType.from(osm)),
139 JLabel.HORIZONTAL);
140
141 }
142 @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
143 return Collections.singleton(osm);
144 }
145
146 });
147 }
148 return children;
149 }
150}
Note: See TracBrowser for help on using the repository browser.