source: josm/trunk/src/org/openstreetmap/josm/actions/UploadSelectionAction.java@ 10548

Last change on this file since 10548 was 10548, checked in by simon04, 8 years ago

Remove duplicated code

Use updateEnabledStateOnCurrentSelection introduced r10409

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