[2305] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.command;
|
---|
| 3 |
|
---|
[2844] | 4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[2305] | 5 |
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.Collection;
|
---|
[3262] | 8 | import java.util.HashSet;
|
---|
[2305] | 9 | import java.util.List;
|
---|
[8456] | 10 |
|
---|
[4918] | 11 | import javax.swing.Icon;
|
---|
[2305] | 12 |
|
---|
[3703] | 13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[3257] | 14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[3440] | 15 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
[2305] | 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
[3660] | 18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[5953] | 19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[2305] | 20 |
|
---|
[3703] | 21 | /**
|
---|
| 22 | * Add primitives to a data layer.
|
---|
[5953] | 23 | * @since 2305
|
---|
[3703] | 24 | */
|
---|
[2305] | 25 | public class AddPrimitivesCommand extends Command {
|
---|
| 26 |
|
---|
[7005] | 27 | private List<PrimitiveData> data = new ArrayList<>();
|
---|
| 28 | private Collection<PrimitiveData> toSelect = new ArrayList<>();
|
---|
[2305] | 29 |
|
---|
[3703] | 30 | // only filled on undo
|
---|
| 31 | private List<OsmPrimitive> createdPrimitives = null;
|
---|
[5953] | 32 | private Collection<OsmPrimitive> createdPrimitivesToSelect = null;
|
---|
[3703] | 33 |
|
---|
[5953] | 34 | /**
|
---|
| 35 | * Constructs a new {@code AddPrimitivesCommand} to add data to the current edit layer.
|
---|
| 36 | * @param data The OSM primitives data to add. Must not be {@code null}
|
---|
| 37 | */
|
---|
[2305] | 38 | public AddPrimitivesCommand(List<PrimitiveData> data) {
|
---|
[5953] | 39 | this(data, data);
|
---|
[2305] | 40 | }
|
---|
[5953] | 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Constructs a new {@code AddPrimitivesCommand} to add data to the current edit layer.
|
---|
| 44 | * @param data The OSM primitives to add. Must not be {@code null}
|
---|
| 45 | * @param toSelect The OSM primitives to select at the end. Can be {@code null}
|
---|
| 46 | * @since 5953
|
---|
| 47 | */
|
---|
| 48 | public AddPrimitivesCommand(List<PrimitiveData> data, List<PrimitiveData> toSelect) {
|
---|
| 49 | init(data, toSelect);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Constructs a new {@code AddPrimitivesCommand} to add data to the given layer.
|
---|
| 54 | * @param data The OSM primitives data to add. Must not be {@code null}
|
---|
| 55 | * @param toSelect The OSM primitives to select at the end. Can be {@code null}
|
---|
| 56 | * @param layer The target data layer. Must not be {@code null}
|
---|
| 57 | */
|
---|
| 58 | public AddPrimitivesCommand(List<PrimitiveData> data, List<PrimitiveData> toSelect, OsmDataLayer layer) {
|
---|
| 59 | super(layer);
|
---|
| 60 | init(data, toSelect);
|
---|
| 61 | }
|
---|
[6069] | 62 |
|
---|
[8512] | 63 | private void init(List<PrimitiveData> data, List<PrimitiveData> toSelect) {
|
---|
[5953] | 64 | CheckParameterUtil.ensureParameterNotNull(data, "data");
|
---|
[3660] | 65 | this.data.addAll(data);
|
---|
[5953] | 66 | if (toSelect != null) {
|
---|
| 67 | this.toSelect.addAll(toSelect);
|
---|
| 68 | }
|
---|
[3660] | 69 | }
|
---|
[2305] | 70 |
|
---|
[8510] | 71 | @Override
|
---|
| 72 | public boolean executeCommand() {
|
---|
[5953] | 73 | Collection<OsmPrimitive> primitivesToSelect;
|
---|
[3703] | 74 | if (createdPrimitives == null) { // first time execution
|
---|
[7005] | 75 | List<OsmPrimitive> newPrimitives = new ArrayList<>(data.size());
|
---|
| 76 | primitivesToSelect = new ArrayList<>(toSelect.size());
|
---|
[2305] | 77 |
|
---|
[3703] | 78 | for (PrimitiveData pd : data) {
|
---|
| 79 | OsmPrimitive primitive = getLayer().data.getPrimitiveById(pd);
|
---|
| 80 | boolean created = primitive == null;
|
---|
| 81 | if (created) {
|
---|
| 82 | primitive = pd.getType().newInstance(pd.getUniqueId(), true);
|
---|
| 83 | }
|
---|
| 84 | if (pd instanceof NodeData) { // Load nodes immediately because they can't be added to dataset without coordinates
|
---|
| 85 | primitive.load(pd);
|
---|
| 86 | }
|
---|
| 87 | if (created) {
|
---|
| 88 | getLayer().data.addPrimitive(primitive);
|
---|
| 89 | }
|
---|
| 90 | newPrimitives.add(primitive);
|
---|
[5953] | 91 | if (toSelect.contains(pd)) {
|
---|
| 92 | primitivesToSelect.add(primitive);
|
---|
| 93 | }
|
---|
[3703] | 94 | }
|
---|
[2305] | 95 |
|
---|
[5953] | 96 | // Then load ways and relations
|
---|
[8510] | 97 | for (int i = 0; i < newPrimitives.size(); i++) {
|
---|
[3703] | 98 | if (!(newPrimitives.get(i) instanceof Node)) {
|
---|
| 99 | newPrimitives.get(i).load(data.get(i));
|
---|
| 100 | }
|
---|
[3257] | 101 | }
|
---|
[3703] | 102 | } else { // redo
|
---|
| 103 | // When redoing this command, we have to add the same objects, otherwise
|
---|
| 104 | // a subsequent command (e.g. MoveCommand) cannot be redone.
|
---|
| 105 | for (OsmPrimitive osm : createdPrimitives) {
|
---|
| 106 | getLayer().data.addPrimitive(osm);
|
---|
[3440] | 107 | }
|
---|
[5953] | 108 | primitivesToSelect = createdPrimitivesToSelect;
|
---|
[2305] | 109 | }
|
---|
[3257] | 110 |
|
---|
[5953] | 111 | getLayer().data.setSelected(primitivesToSelect);
|
---|
[2305] | 112 | return true;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | @Override public void undoCommand() {
|
---|
[3703] | 116 | DataSet ds = getLayer().data;
|
---|
[6069] | 117 |
|
---|
[3703] | 118 | if (createdPrimitives == null) {
|
---|
[7005] | 119 | createdPrimitives = new ArrayList<>(data.size());
|
---|
| 120 | createdPrimitivesToSelect = new ArrayList<>(toSelect.size());
|
---|
[6069] | 121 |
|
---|
[5953] | 122 | for (PrimitiveData pd : data) {
|
---|
| 123 | OsmPrimitive p = ds.getPrimitiveById(pd);
|
---|
| 124 | createdPrimitives.add(p);
|
---|
| 125 | if (toSelect.contains(pd)) {
|
---|
| 126 | createdPrimitivesToSelect.add(p);
|
---|
| 127 | }
|
---|
[3703] | 128 | }
|
---|
| 129 | createdPrimitives = PurgeCommand.topoSort(createdPrimitives);
|
---|
[6069] | 130 |
|
---|
[3703] | 131 | for (PrimitiveData p : data) {
|
---|
| 132 | ds.removePrimitive(p);
|
---|
| 133 | }
|
---|
| 134 | data = null;
|
---|
[5953] | 135 | toSelect = null;
|
---|
[6069] | 136 |
|
---|
[3703] | 137 | } else {
|
---|
| 138 | for (OsmPrimitive osm : createdPrimitives) {
|
---|
| 139 | ds.removePrimitive(osm);
|
---|
| 140 | }
|
---|
[2305] | 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[4918] | 144 | @Override
|
---|
| 145 | public String getDescriptionText() {
|
---|
[3703] | 146 | int size = data != null ? data.size() : createdPrimitives.size();
|
---|
[4918] | 147 | return trn("Added {0} object", "Added {0} objects", size, size);
|
---|
[2305] | 148 | }
|
---|
| 149 |
|
---|
| 150 | @Override
|
---|
[4918] | 151 | public Icon getDescriptionIcon() {
|
---|
| 152 | return null;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | @Override
|
---|
[2305] | 156 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
| 157 | Collection<OsmPrimitive> added) {
|
---|
| 158 | // Does nothing because we don't want to create OsmPrimitives.
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[3262] | 161 | @Override
|
---|
| 162 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
[3703] | 163 | if (createdPrimitives != null)
|
---|
| 164 | return createdPrimitives;
|
---|
[6069] | 165 |
|
---|
[7005] | 166 | Collection<OsmPrimitive> prims = new HashSet<>();
|
---|
[3262] | 167 | for (PrimitiveData d : data) {
|
---|
| 168 | OsmPrimitive osm = getLayer().data.getPrimitiveById(d);
|
---|
| 169 | if (osm == null)
|
---|
| 170 | throw new RuntimeException();
|
---|
| 171 | prims.add(osm);
|
---|
| 172 | }
|
---|
| 173 | return prims;
|
---|
| 174 | }
|
---|
[8456] | 175 |
|
---|
| 176 | @Override
|
---|
| 177 | public int hashCode() {
|
---|
| 178 | final int prime = 31;
|
---|
| 179 | int result = super.hashCode();
|
---|
| 180 | result = prime * result + ((createdPrimitives == null) ? 0 : createdPrimitives.hashCode());
|
---|
| 181 | result = prime * result + ((createdPrimitivesToSelect == null) ? 0 : createdPrimitivesToSelect.hashCode());
|
---|
| 182 | result = prime * result + ((data == null) ? 0 : data.hashCode());
|
---|
| 183 | result = prime * result + ((toSelect == null) ? 0 : toSelect.hashCode());
|
---|
| 184 | return result;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | @Override
|
---|
| 188 | public boolean equals(Object obj) {
|
---|
| 189 | if (this == obj)
|
---|
| 190 | return true;
|
---|
| 191 | if (!super.equals(obj))
|
---|
| 192 | return false;
|
---|
| 193 | if (getClass() != obj.getClass())
|
---|
| 194 | return false;
|
---|
| 195 | AddPrimitivesCommand other = (AddPrimitivesCommand) obj;
|
---|
| 196 | if (createdPrimitives == null) {
|
---|
| 197 | if (other.createdPrimitives != null)
|
---|
| 198 | return false;
|
---|
| 199 | } else if (!createdPrimitives.equals(other.createdPrimitives))
|
---|
| 200 | return false;
|
---|
| 201 | if (createdPrimitivesToSelect == null) {
|
---|
| 202 | if (other.createdPrimitivesToSelect != null)
|
---|
| 203 | return false;
|
---|
| 204 | } else if (!createdPrimitivesToSelect.equals(other.createdPrimitivesToSelect))
|
---|
| 205 | return false;
|
---|
| 206 | if (data == null) {
|
---|
| 207 | if (other.data != null)
|
---|
| 208 | return false;
|
---|
| 209 | } else if (!data.equals(other.data))
|
---|
| 210 | return false;
|
---|
| 211 | if (toSelect == null) {
|
---|
| 212 | if (other.toSelect != null)
|
---|
| 213 | return false;
|
---|
| 214 | } else if (!toSelect.equals(other.toSelect))
|
---|
| 215 | return false;
|
---|
| 216 | return true;
|
---|
| 217 | }
|
---|
[2305] | 218 | }
|
---|