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 |
|
---|
11 | import javax.swing.Icon;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Add primitives to a data layer.
|
---|
23 | * @since 2305
|
---|
24 | */
|
---|
25 | public class AddPrimitivesCommand extends Command {
|
---|
26 |
|
---|
27 | private List<PrimitiveData> data = new ArrayList<>();
|
---|
28 | private Collection<PrimitiveData> toSelect = new ArrayList<>();
|
---|
29 |
|
---|
30 | // only filled on undo
|
---|
31 | private List<OsmPrimitive> createdPrimitives = null;
|
---|
32 | private Collection<OsmPrimitive> createdPrimitivesToSelect = null;
|
---|
33 |
|
---|
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 | */
|
---|
38 | public AddPrimitivesCommand(List<PrimitiveData> data) {
|
---|
39 | this(data, data);
|
---|
40 | }
|
---|
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 | }
|
---|
62 |
|
---|
63 | private void init(List<PrimitiveData> data, List<PrimitiveData> toSelect) {
|
---|
64 | CheckParameterUtil.ensureParameterNotNull(data, "data");
|
---|
65 | this.data.addAll(data);
|
---|
66 | if (toSelect != null) {
|
---|
67 | this.toSelect.addAll(toSelect);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public boolean executeCommand() {
|
---|
73 | Collection<OsmPrimitive> primitivesToSelect;
|
---|
74 | if (createdPrimitives == null) { // first time execution
|
---|
75 | List<OsmPrimitive> newPrimitives = new ArrayList<>(data.size());
|
---|
76 | primitivesToSelect = new ArrayList<>(toSelect.size());
|
---|
77 |
|
---|
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);
|
---|
91 | if (toSelect.contains(pd)) {
|
---|
92 | primitivesToSelect.add(primitive);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Then load ways and relations
|
---|
97 | for (int i = 0; i < newPrimitives.size(); i++) {
|
---|
98 | if (!(newPrimitives.get(i) instanceof Node)) {
|
---|
99 | newPrimitives.get(i).load(data.get(i));
|
---|
100 | }
|
---|
101 | }
|
---|
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);
|
---|
107 | }
|
---|
108 | primitivesToSelect = createdPrimitivesToSelect;
|
---|
109 | }
|
---|
110 |
|
---|
111 | getLayer().data.setSelected(primitivesToSelect);
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override public void undoCommand() {
|
---|
116 | DataSet ds = getLayer().data;
|
---|
117 |
|
---|
118 | if (createdPrimitives == null) {
|
---|
119 | createdPrimitives = new ArrayList<>(data.size());
|
---|
120 | createdPrimitivesToSelect = new ArrayList<>(toSelect.size());
|
---|
121 |
|
---|
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 | }
|
---|
128 | }
|
---|
129 | createdPrimitives = PurgeCommand.topoSort(createdPrimitives);
|
---|
130 |
|
---|
131 | for (PrimitiveData p : data) {
|
---|
132 | ds.removePrimitive(p);
|
---|
133 | }
|
---|
134 | data = null;
|
---|
135 | toSelect = null;
|
---|
136 |
|
---|
137 | } else {
|
---|
138 | for (OsmPrimitive osm : createdPrimitives) {
|
---|
139 | ds.removePrimitive(osm);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public String getDescriptionText() {
|
---|
146 | int size = data != null ? data.size() : createdPrimitives.size();
|
---|
147 | return trn("Added {0} object", "Added {0} objects", size, size);
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public Icon getDescriptionIcon() {
|
---|
152 | return null;
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
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 |
|
---|
161 | @Override
|
---|
162 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
163 | if (createdPrimitives != null)
|
---|
164 | return createdPrimitives;
|
---|
165 |
|
---|
166 | Collection<OsmPrimitive> prims = new HashSet<>();
|
---|
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 | }
|
---|
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 | }
|
---|
218 | }
|
---|