| 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>();
|
|---|
| 44 | this.key = key;
|
|---|
| 45 | this.value = value;
|
|---|
| 46 | if (value == null) {
|
|---|
| 47 | for (OsmPrimitive osm : objects) {
|
|---|
| 48 | if(osm.get(key) != null)
|
|---|
| 49 | this.objects.add(osm);
|
|---|
| 50 | }
|
|---|
| 51 | } else {
|
|---|
| 52 | for (OsmPrimitive osm : objects) {
|
|---|
| 53 | String val = osm.get(key);
|
|---|
| 54 | if(val == null || !value.equals(val))
|
|---|
| 55 | {
|
|---|
| 56 | this.objects.add(osm);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
|
|---|
| 63 | this.objects = new LinkedList<OsmPrimitive>();
|
|---|
| 64 | this.key = key;
|
|---|
| 65 | this.value = value;
|
|---|
| 66 | String val = object.get(key);
|
|---|
| 67 | if ((value == null && val != null)
|
|---|
| 68 | || (value != null && (val == null || !value.equals(val))))
|
|---|
| 69 | this.objects.add(object);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @Override public boolean executeCommand() {
|
|---|
| 73 | super.executeCommand(); // save old
|
|---|
| 74 | if (value == null) {
|
|---|
| 75 | for (OsmPrimitive osm : objects) {
|
|---|
| 76 | osm.modified = true;
|
|---|
| 77 | osm.remove(key);
|
|---|
| 78 | }
|
|---|
| 79 | } else {
|
|---|
| 80 | for (OsmPrimitive osm : objects) {
|
|---|
| 81 | osm.modified = true;
|
|---|
| 82 | osm.put(key, value);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | return true;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
|---|
| 89 | modified.addAll(objects);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | @Override public MutableTreeNode description() {
|
|---|
| 93 | String text = value == null ? tr( "Remove \"{0}\" for", key) : tr("Set {0}={1} for",key,value);
|
|---|
| 94 | if (objects.size() == 1) {
|
|---|
| 95 | NameVisitor v = new NameVisitor();
|
|---|
| 96 | objects.iterator().next().visit(v);
|
|---|
| 97 | text += " "+tr(v.className)+" "+v.name;
|
|---|
| 98 | } else
|
|---|
| 99 | text += " "+objects.size()+" "+trn("object","objects",objects.size());
|
|---|
| 100 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(text, ImageProvider.get("data", "key"), JLabel.HORIZONTAL));
|
|---|
| 101 | if (objects.size() == 1)
|
|---|
| 102 | return root;
|
|---|
| 103 | NameVisitor v = new NameVisitor();
|
|---|
| 104 | for (OsmPrimitive osm : objects) {
|
|---|
| 105 | osm.visit(v);
|
|---|
| 106 | root.add(new DefaultMutableTreeNode(v.toLabel()));
|
|---|
| 107 | }
|
|---|
| 108 | return root;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|