source: josm/trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java@ 6340

Last change on this file since 6340 was 6069, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

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