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