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

Last change on this file since 2844 was 2844, checked in by mjulius, 15 years ago

fix messages for commands

  • Property svn:eol-style set to native
File size: 4.9 KB
RevLine 
[298]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[626]2package org.openstreetmap.josm.command;
3
[1990]4import static org.openstreetmap.josm.tools.I18n.marktr;
[626]5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.util.Collection;
9import java.util.LinkedList;
10import java.util.List;
11
12import javax.swing.JLabel;
13import javax.swing.tree.DefaultMutableTreeNode;
14import javax.swing.tree.MutableTreeNode;
15
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
[1814]17import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
[1990]18import org.openstreetmap.josm.gui.DefaultNameFormatter;
[626]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.
[1169]24 *
[626]25 * @author imi
26 */
27public class ChangePropertyCommand extends Command {
[1169]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;
[628]42
[1169]43 public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, String key, String value) {
[1750]44 super();
[1169]45 this.objects = new LinkedList<OsmPrimitive>();
46 this.key = key;
47 this.value = value;
48 if (value == null) {
49 for (OsmPrimitive osm : objects) {
[1750]50 if(osm.get(key) != null) {
[1169]51 this.objects.add(osm);
[1750]52 }
[1169]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 }
[626]63
[1169]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)
[1750]70 || (value != null && (val == null || !value.equals(val)))) {
[1169]71 this.objects.add(object);
[1750]72 }
[1169]73 }
[626]74
[1169]75 @Override public boolean executeCommand() {
76 super.executeCommand(); // save old
77 if (value == null) {
78 for (OsmPrimitive osm : objects) {
[2025]79 osm.setModified(true);
[1169]80 osm.remove(key);
81 }
82 } else {
83 for (OsmPrimitive osm : objects) {
[2025]84 osm.setModified(true);
[1169]85 osm.put(key, value);
86 }
87 }
88 return true;
[626]89 }
[1169]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() {
[1182]96 String text;
[1169]97 if (objects.size() == 1) {
[1814]98 OsmPrimitive primitive = objects.iterator().next();
[1989]99 String msg = "";
100 if (value == null) {
101 switch(OsmPrimitiveType.from(primitive)) {
[2008]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;
[1989]105 }
[1990]106 text = tr(msg, key, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
[1989]107 } else {
108 switch(OsmPrimitiveType.from(primitive)) {
[2008]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;
[1989]112 }
[1990]113 text = tr(msg, key, value, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
[1989]114 }
[2008]115 } else {
[1182]116 text = value == null
[2844]117 ? tr("Remove \"{0}\" for {1} objects", key, objects.size())
118 : tr("Set {0}={1} for {2} objects", key, value, objects.size());
[1182]119 }
[1169]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) {
[1814]124 root.add(new DefaultMutableTreeNode(
125 new JLabel(
[1990]126 osm.getDisplayName(DefaultNameFormatter.getInstance()),
[1814]127 ImageProvider.get(OsmPrimitiveType.from(osm)),
128 JLabel.HORIZONTAL)
129 )
130 );
[1169]131 }
132 return root;
133 }
[626]134}
Note: See TracBrowser for help on using the repository browser.