1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Set;
|
---|
13 | import java.util.Stack;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.SwingUtilities;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
20 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
25 | import org.openstreetmap.josm.data.osm.Way;
|
---|
26 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
27 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
28 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
29 | import org.openstreetmap.josm.gui.io.UploadSelectionDialog;
|
---|
30 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
31 | import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
|
---|
32 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
33 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
34 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
35 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
36 | import org.xml.sax.SAXException;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Uploads the current selection to the server.
|
---|
40 | * @since 2250
|
---|
41 | */
|
---|
42 | public class UploadSelectionAction extends JosmAction {
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code UploadSelectionAction}.
|
---|
45 | */
|
---|
46 | public UploadSelectionAction() {
|
---|
47 | super(
|
---|
48 | tr("Upload selection"),
|
---|
49 | "uploadselection",
|
---|
50 | tr("Upload all changes in the current selection to the OSM server."),
|
---|
51 | // CHECKSTYLE.OFF: LineLength
|
---|
52 | Shortcut.registerShortcut("file:uploadSelection", tr("File: {0}", tr("Upload selection")), KeyEvent.VK_U, Shortcut.ALT_CTRL_SHIFT),
|
---|
53 | // CHECKSTYLE.ON: LineLength
|
---|
54 | true);
|
---|
55 | putValue("help", ht("/Action/UploadSelection"));
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | protected void updateEnabledState() {
|
---|
60 | if (getCurrentDataSet() == null) {
|
---|
61 | setEnabled(false);
|
---|
62 | } else {
|
---|
63 | updateEnabledState(getCurrentDataSet().getAllSelected());
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
69 | setEnabled(selection != null && !selection.isEmpty());
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected Set<OsmPrimitive> getDeletedPrimitives(DataSet ds) {
|
---|
73 | Set<OsmPrimitive> ret = new HashSet<>();
|
---|
74 | for (OsmPrimitive p: ds.allPrimitives()) {
|
---|
75 | if (p.isDeleted() && !p.isNew() && p.isVisible() && p.isModified()) {
|
---|
76 | ret.add(p);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return ret;
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected Set<OsmPrimitive> getModifiedPrimitives(Collection<OsmPrimitive> primitives) {
|
---|
83 | Set<OsmPrimitive> ret = new HashSet<>();
|
---|
84 | for (OsmPrimitive p: primitives) {
|
---|
85 | if (p.isNewOrUndeleted()) {
|
---|
86 | ret.add(p);
|
---|
87 | } else if (p.isModified() && !p.isIncomplete()) {
|
---|
88 | ret.add(p);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return ret;
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public void actionPerformed(ActionEvent e) {
|
---|
96 | if (!isEnabled())
|
---|
97 | return;
|
---|
98 | if (getEditLayer().isUploadDiscouraged()) {
|
---|
99 | if (UploadAction.warnUploadDiscouraged(getEditLayer())) {
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | Collection<OsmPrimitive> modifiedCandidates = getModifiedPrimitives(getEditLayer().data.getAllSelected());
|
---|
104 | Collection<OsmPrimitive> deletedCandidates = getDeletedPrimitives(getEditLayer().data);
|
---|
105 | if (modifiedCandidates.isEmpty() && deletedCandidates.isEmpty()) {
|
---|
106 | JOptionPane.showMessageDialog(
|
---|
107 | Main.parent,
|
---|
108 | tr("No changes to upload."),
|
---|
109 | tr("Warning"),
|
---|
110 | JOptionPane.INFORMATION_MESSAGE
|
---|
111 | );
|
---|
112 | return;
|
---|
113 | }
|
---|
114 | UploadSelectionDialog dialog = new UploadSelectionDialog();
|
---|
115 | dialog.populate(
|
---|
116 | modifiedCandidates,
|
---|
117 | deletedCandidates
|
---|
118 | );
|
---|
119 | dialog.setVisible(true);
|
---|
120 | if (dialog.isCanceled())
|
---|
121 | return;
|
---|
122 | Collection<OsmPrimitive> toUpload = new UploadHullBuilder().build(dialog.getSelectedPrimitives());
|
---|
123 | if (toUpload.isEmpty()) {
|
---|
124 | JOptionPane.showMessageDialog(
|
---|
125 | Main.parent,
|
---|
126 | tr("No changes to upload."),
|
---|
127 | tr("Warning"),
|
---|
128 | JOptionPane.INFORMATION_MESSAGE
|
---|
129 | );
|
---|
130 | return;
|
---|
131 | }
|
---|
132 | uploadPrimitives(getEditLayer(), toUpload);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Replies true if there is at least one non-new, deleted primitive in
|
---|
137 | * <code>primitives</code>
|
---|
138 | *
|
---|
139 | * @param primitives the primitives to scan
|
---|
140 | * @return true if there is at least one non-new, deleted primitive in
|
---|
141 | * <code>primitives</code>
|
---|
142 | */
|
---|
143 | protected boolean hasPrimitivesToDelete(Collection<OsmPrimitive> primitives) {
|
---|
144 | for (OsmPrimitive p: primitives) {
|
---|
145 | if (p.isDeleted() && p.isModified() && !p.isNew())
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Uploads the primitives in <code>toUpload</code> to the server. Only
|
---|
153 | * uploads primitives which are either new, modified or deleted.
|
---|
154 | *
|
---|
155 | * Also checks whether <code>toUpload</code> has to be extended with
|
---|
156 | * deleted parents in order to avoid precondition violations on the server.
|
---|
157 | *
|
---|
158 | * @param layer the data layer from which we upload a subset of primitives
|
---|
159 | * @param toUpload the primitives to upload. If null or empty returns immediatelly
|
---|
160 | */
|
---|
161 | public void uploadPrimitives(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) {
|
---|
162 | if (toUpload == null || toUpload.isEmpty()) return;
|
---|
163 | UploadHullBuilder builder = new UploadHullBuilder();
|
---|
164 | toUpload = builder.build(toUpload);
|
---|
165 | if (hasPrimitivesToDelete(toUpload)) {
|
---|
166 | // runs the check for deleted parents and then invokes
|
---|
167 | // processPostParentChecker()
|
---|
168 | //
|
---|
169 | Main.worker.submit(new DeletedParentsChecker(layer, toUpload));
|
---|
170 | } else {
|
---|
171 | processPostParentChecker(layer, toUpload);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | protected void processPostParentChecker(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) {
|
---|
176 | APIDataSet ds = new APIDataSet(toUpload);
|
---|
177 | UploadAction action = new UploadAction();
|
---|
178 | action.uploadData(layer, ds);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Computes the collection of primitives to upload, given a collection of candidate
|
---|
183 | * primitives.
|
---|
184 | * Some of the candidates are excluded, i.e. if they aren't modified.
|
---|
185 | * Other primitives are added. A typical case is a primitive which is new and and
|
---|
186 | * which is referred by a modified relation. In order to upload the relation the
|
---|
187 | * new primitive has to be uploaded as well, even if it isn't included in the
|
---|
188 | * list of candidate primitives.
|
---|
189 | *
|
---|
190 | */
|
---|
191 | static class UploadHullBuilder implements Visitor {
|
---|
192 | private Set<OsmPrimitive> hull;
|
---|
193 |
|
---|
194 | UploadHullBuilder() {
|
---|
195 | hull = new HashSet<>();
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public void visit(Node n) {
|
---|
200 | if (n.isNewOrUndeleted() || n.isModified() || n.isDeleted()) {
|
---|
201 | // upload new nodes as well as modified and deleted ones
|
---|
202 | hull.add(n);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Override
|
---|
207 | public void visit(Way w) {
|
---|
208 | if (w.isNewOrUndeleted() || w.isModified() || w.isDeleted()) {
|
---|
209 | // upload new ways as well as modified and deleted ones
|
---|
210 | hull.add(w);
|
---|
211 | for (Node n: w.getNodes()) {
|
---|
212 | // we upload modified nodes even if they aren't in the current
|
---|
213 | // selection.
|
---|
214 | n.accept(this);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public void visit(Relation r) {
|
---|
221 | if (r.isNewOrUndeleted() || r.isModified() || r.isDeleted()) {
|
---|
222 | hull.add(r);
|
---|
223 | for (OsmPrimitive p : r.getMemberPrimitives()) {
|
---|
224 | // add new relation members. Don't include modified
|
---|
225 | // relation members. r shouldn't refer to deleted primitives,
|
---|
226 | // so wont check here for deleted primitives here
|
---|
227 | //
|
---|
228 | if (p.isNewOrUndeleted()) {
|
---|
229 | p.accept(this);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | @Override
|
---|
236 | public void visit(Changeset cs) {
|
---|
237 | // do nothing
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Builds the "hull" of primitives to be uploaded given a base collection
|
---|
242 | * of osm primitives.
|
---|
243 | *
|
---|
244 | * @param base the base collection. Must not be null.
|
---|
245 | * @return the "hull"
|
---|
246 | * @throws IllegalArgumentException if base is null
|
---|
247 | */
|
---|
248 | public Set<OsmPrimitive> build(Collection<OsmPrimitive> base) {
|
---|
249 | CheckParameterUtil.ensureParameterNotNull(base, "base");
|
---|
250 | hull = new HashSet<>();
|
---|
251 | for (OsmPrimitive p: base) {
|
---|
252 | p.accept(this);
|
---|
253 | }
|
---|
254 | return hull;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | class DeletedParentsChecker extends PleaseWaitRunnable {
|
---|
259 | private boolean canceled;
|
---|
260 | private Exception lastException;
|
---|
261 | private final Collection<OsmPrimitive> toUpload;
|
---|
262 | private final OsmDataLayer layer;
|
---|
263 | private OsmServerBackreferenceReader reader;
|
---|
264 |
|
---|
265 | /**
|
---|
266 | *
|
---|
267 | * @param layer the data layer for which a collection of selected primitives is uploaded
|
---|
268 | * @param toUpload the collection of primitives to upload
|
---|
269 | */
|
---|
270 | DeletedParentsChecker(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) {
|
---|
271 | super(tr("Checking parents for deleted objects"));
|
---|
272 | this.toUpload = toUpload;
|
---|
273 | this.layer = layer;
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | protected void cancel() {
|
---|
278 | this.canceled = true;
|
---|
279 | synchronized (this) {
|
---|
280 | if (reader != null) {
|
---|
281 | reader.cancel();
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | @Override
|
---|
287 | protected void finish() {
|
---|
288 | if (canceled)
|
---|
289 | return;
|
---|
290 | if (lastException != null) {
|
---|
291 | ExceptionUtil.explainException(lastException);
|
---|
292 | return;
|
---|
293 | }
|
---|
294 | Runnable r = new Runnable() {
|
---|
295 | @Override
|
---|
296 | public void run() {
|
---|
297 | processPostParentChecker(layer, toUpload);
|
---|
298 | }
|
---|
299 | };
|
---|
300 | SwingUtilities.invokeLater(r);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Replies the collection of deleted OSM primitives for which we have to check whether
|
---|
305 | * there are dangling references on the server.
|
---|
306 | *
|
---|
307 | * @return primitives to check
|
---|
308 | */
|
---|
309 | protected Set<OsmPrimitive> getPrimitivesToCheckForParents() {
|
---|
310 | Set<OsmPrimitive> ret = new HashSet<>();
|
---|
311 | for (OsmPrimitive p: toUpload) {
|
---|
312 | if (p.isDeleted() && !p.isNewOrUndeleted()) {
|
---|
313 | ret.add(p);
|
---|
314 | }
|
---|
315 | }
|
---|
316 | return ret;
|
---|
317 | }
|
---|
318 |
|
---|
319 | @Override
|
---|
320 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
321 | try {
|
---|
322 | Stack<OsmPrimitive> toCheck = new Stack<>();
|
---|
323 | toCheck.addAll(getPrimitivesToCheckForParents());
|
---|
324 | Set<OsmPrimitive> checked = new HashSet<>();
|
---|
325 | while (!toCheck.isEmpty()) {
|
---|
326 | if (canceled) return;
|
---|
327 | OsmPrimitive current = toCheck.pop();
|
---|
328 | synchronized (this) {
|
---|
329 | reader = new OsmServerBackreferenceReader(current);
|
---|
330 | }
|
---|
331 | getProgressMonitor().subTask(tr("Reading parents of ''{0}''", current.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
332 | DataSet ds = reader.parseOsm(getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
333 | synchronized (this) {
|
---|
334 | reader = null;
|
---|
335 | }
|
---|
336 | checked.add(current);
|
---|
337 | getProgressMonitor().subTask(tr("Checking for deleted parents in the local dataset"));
|
---|
338 | for (OsmPrimitive p: ds.allPrimitives()) {
|
---|
339 | if (canceled) return;
|
---|
340 | OsmPrimitive myDeletedParent = layer.data.getPrimitiveById(p);
|
---|
341 | // our local dataset includes a deleted parent of a primitive we want
|
---|
342 | // to delete. Include this parent in the collection of uploaded primitives
|
---|
343 | if (myDeletedParent != null && myDeletedParent.isDeleted()) {
|
---|
344 | if (!toUpload.contains(myDeletedParent)) {
|
---|
345 | toUpload.add(myDeletedParent);
|
---|
346 | }
|
---|
347 | if (!checked.contains(myDeletedParent)) {
|
---|
348 | toCheck.push(myDeletedParent);
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | } catch (OsmTransferException e) {
|
---|
354 | if (canceled)
|
---|
355 | // ignore exception
|
---|
356 | return;
|
---|
357 | lastException = e;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|