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