| 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.tr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
|---|
| 6 |
|
|---|
| 7 | import java.util.Collection;
|
|---|
| 8 | import java.util.Collections;
|
|---|
| 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.visitor.NameVisitor;
|
|---|
| 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<OsmPrimitive> objects, String key, String value) {
|
|---|
| 43 | this.objects = new LinkedList<OsmPrimitive>(objects);
|
|---|
| 44 | this.key = key;
|
|---|
| 45 | this.value = value;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
|
|---|
| 49 | this.objects = new LinkedList<OsmPrimitive>(Collections.singleton(object));
|
|---|
| 50 | this.key = key;
|
|---|
| 51 | this.value = value;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Override public boolean executeCommand() {
|
|---|
| 55 | super.executeCommand(); // save old
|
|---|
| 56 | if (value == null) {
|
|---|
| 57 | for (OsmPrimitive osm : objects) {
|
|---|
| 58 | osm.modified = true;
|
|---|
| 59 | osm.remove(key);
|
|---|
| 60 | }
|
|---|
| 61 | } else {
|
|---|
| 62 | for (OsmPrimitive osm : objects) {
|
|---|
| 63 | osm.modified = true;
|
|---|
| 64 | osm.put(key, value);
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | return true;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
|---|
| 71 | modified.addAll(objects);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | @Override public MutableTreeNode description() {
|
|---|
| 75 | String text = value == null ? tr( "Remove \"{0}\" for", key) : tr("Set {0}={1} for",key,value);
|
|---|
| 76 | if (objects.size() == 1) {
|
|---|
| 77 | NameVisitor v = new NameVisitor();
|
|---|
| 78 | objects.iterator().next().visit(v);
|
|---|
| 79 | text += " "+tr(v.className)+" "+v.name;
|
|---|
| 80 | } else
|
|---|
| 81 | text += " "+objects.size()+" "+trn("object","objects",objects.size());
|
|---|
| 82 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
|
|---|
| 83 | if (objects.size() == 1)
|
|---|
| 84 | return root;
|
|---|
| 85 | NameVisitor v = new NameVisitor();
|
|---|
| 86 | for (OsmPrimitive osm : objects) {
|
|---|
| 87 | osm.visit(v);
|
|---|
| 88 | root.add(new DefaultMutableTreeNode(v.toLabel()));
|
|---|
| 89 | }
|
|---|
| 90 | return root;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|