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.JLabel;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
17 |
|
---|
18 | public class AddPrimitivesCommand extends Command {
|
---|
19 |
|
---|
20 | private final List<PrimitiveData> data = new ArrayList<PrimitiveData>();
|
---|
21 |
|
---|
22 | public AddPrimitivesCommand(List<PrimitiveData> data) {
|
---|
23 | this.data.addAll(data);
|
---|
24 | }
|
---|
25 |
|
---|
26 | @SuppressWarnings("null")
|
---|
27 | @Override public boolean executeCommand() {
|
---|
28 |
|
---|
29 | List<OsmPrimitive> createdPrimitives = new ArrayList<OsmPrimitive>(data.size());
|
---|
30 |
|
---|
31 | for (PrimitiveData pd:data) {
|
---|
32 | OsmPrimitive primitive = getLayer().data.getPrimitiveById(pd);
|
---|
33 | boolean created = primitive == null;
|
---|
34 | if (created) {
|
---|
35 | primitive = pd.getType().newInstance(pd.getUniqueId(), true);
|
---|
36 | }
|
---|
37 | if (pd instanceof NodeData) { // Load nodes immediately because they can't be added to dataset without coordinates
|
---|
38 | primitive.load(pd);
|
---|
39 | }
|
---|
40 | if (created) {
|
---|
41 | getLayer().data.addPrimitive(primitive);
|
---|
42 | }
|
---|
43 | createdPrimitives.add(primitive);
|
---|
44 | }
|
---|
45 |
|
---|
46 | //Then load ways and relations
|
---|
47 | for (int i=0; i<createdPrimitives.size(); i++) {
|
---|
48 | if (!(createdPrimitives.get(i) instanceof Node)) {
|
---|
49 | createdPrimitives.get(i).load(data.get(i));
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | getLayer().data.setSelected(createdPrimitives);
|
---|
54 | return true;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override public void undoCommand() {
|
---|
58 | for (PrimitiveData p:data) {
|
---|
59 | getLayer().data.removePrimitive(p);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override public JLabel getDescription() {
|
---|
64 | return new JLabel(trn("Added {0} object", "Added {0} objects", data.size(), data.size()), null,
|
---|
65 | JLabel.HORIZONTAL
|
---|
66 | );
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
71 | Collection<OsmPrimitive> added) {
|
---|
72 | // Does nothing because we don't want to create OsmPrimitives.
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
77 | Collection<OsmPrimitive> prims = new HashSet<OsmPrimitive>();
|
---|
78 | for (PrimitiveData d : data) {
|
---|
79 | OsmPrimitive osm = getLayer().data.getPrimitiveById(d);
|
---|
80 | if (osm == null)
|
---|
81 | throw new RuntimeException();
|
---|
82 | prims.add(osm);
|
---|
83 | }
|
---|
84 | return prims;
|
---|
85 | }
|
---|
86 | }
|
---|