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