| 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 |
|
|---|
| 7 | import java.util.ArrayList;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.Collections;
|
|---|
| 10 | import java.util.LinkedList;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.JLabel;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 17 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
|---|
| 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.setModified(true);
|
|---|
| 79 | osm.remove(key);
|
|---|
| 80 | }
|
|---|
| 81 | } else {
|
|---|
| 82 | for (OsmPrimitive osm : objects) {
|
|---|
| 83 | osm.setModified(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 JLabel getDescription() {
|
|---|
| 95 | String text;
|
|---|
| 96 | if (objects.size() == 1) {
|
|---|
| 97 | OsmPrimitive primitive = objects.iterator().next();
|
|---|
| 98 | String msg = "";
|
|---|
| 99 | if (value == null) {
|
|---|
| 100 | switch(OsmPrimitiveType.from(primitive)) {
|
|---|
| 101 | case NODE: msg = marktr("Remove \"{0}\" for node ''{1}''"); break;
|
|---|
| 102 | case WAY: msg = marktr("Remove \"{0}\" for way ''{1}''"); break;
|
|---|
| 103 | case RELATION: msg = marktr("Remove \"{0}\" for relation ''{1}''"); break;
|
|---|
| 104 | }
|
|---|
| 105 | text = tr(msg, key, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
|---|
| 106 | } else {
|
|---|
| 107 | switch(OsmPrimitiveType.from(primitive)) {
|
|---|
| 108 | case NODE: msg = marktr("Set {0}={1} for node ''{2}''"); break;
|
|---|
| 109 | case WAY: msg = marktr("Set {0}={1} for way ''{2}''"); break;
|
|---|
| 110 | case RELATION: msg = marktr("Set {0}={1} for relation ''{2}''"); break;
|
|---|
| 111 | }
|
|---|
| 112 | text = tr(msg, key, value, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
|---|
| 113 | }
|
|---|
| 114 | } else {
|
|---|
| 115 | text = value == null
|
|---|
| 116 | ? tr("Remove \"{0}\" for {1} objects", key, objects.size())
|
|---|
| 117 | : tr("Set {0}={1} for {2} objects", key, value, objects.size());
|
|---|
| 118 | }
|
|---|
| 119 | return new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | @Override public Collection<PseudoCommand> getChildren() {
|
|---|
| 123 | if (objects.size() == 1)
|
|---|
| 124 | return null;
|
|---|
| 125 | List<PseudoCommand> children = new ArrayList<PseudoCommand>();
|
|---|
| 126 | for (final OsmPrimitive osm : objects) {
|
|---|
| 127 | children.add(new PseudoCommand() {
|
|---|
| 128 | @Override public JLabel getDescription() {
|
|---|
| 129 | return new JLabel(
|
|---|
| 130 | osm.getDisplayName(DefaultNameFormatter.getInstance()),
|
|---|
| 131 | ImageProvider.get(OsmPrimitiveType.from(osm)),
|
|---|
| 132 | JLabel.HORIZONTAL);
|
|---|
| 133 |
|
|---|
| 134 | }
|
|---|
| 135 | @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
|---|
| 136 | return Collections.singleton(osm);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | });
|
|---|
| 140 | }
|
|---|
| 141 | return children;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|