001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.downloadtasks; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006import static org.openstreetmap.josm.tools.I18n.trn; 007 008import java.awt.EventQueue; 009import java.awt.geom.Area; 010import java.awt.geom.Rectangle2D; 011import java.util.ArrayList; 012import java.util.Collection; 013import java.util.LinkedHashSet; 014import java.util.LinkedList; 015import java.util.List; 016import java.util.Objects; 017import java.util.Set; 018import java.util.concurrent.CancellationException; 019import java.util.concurrent.ExecutionException; 020import java.util.concurrent.Future; 021import java.util.stream.Collectors; 022 023import javax.swing.JOptionPane; 024 025import org.openstreetmap.josm.actions.UpdateSelectionAction; 026import org.openstreetmap.josm.data.Bounds; 027import org.openstreetmap.josm.data.osm.DataSet; 028import org.openstreetmap.josm.data.osm.OsmPrimitive; 029import org.openstreetmap.josm.gui.HelpAwareOptionPane; 030import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec; 031import org.openstreetmap.josm.gui.MainApplication; 032import org.openstreetmap.josm.gui.Notification; 033import org.openstreetmap.josm.gui.layer.Layer; 034import org.openstreetmap.josm.gui.layer.OsmDataLayer; 035import org.openstreetmap.josm.gui.progress.ProgressMonitor; 036import org.openstreetmap.josm.gui.util.GuiHelper; 037import org.openstreetmap.josm.tools.ExceptionUtil; 038import org.openstreetmap.josm.tools.ImageProvider; 039import org.openstreetmap.josm.tools.Logging; 040import org.openstreetmap.josm.tools.Utils; 041 042/** 043 * This class encapsulates the downloading of several bounding boxes that would otherwise be too 044 * large to download in one go. Error messages will be collected for all downloads and displayed as 045 * a list in the end. 046 * @author xeen 047 * @since 6053 048 */ 049public class DownloadTaskList { 050 private final List<DownloadTask> tasks = new LinkedList<>(); 051 private final List<Future<?>> taskFutures = new LinkedList<>(); 052 private ProgressMonitor progressMonitor; 053 054 private void addDownloadTask(ProgressMonitor progressMonitor, DownloadTask dt, Rectangle2D td, int i, int n) { 055 ProgressMonitor childProgress = progressMonitor.createSubTaskMonitor(1, false); 056 childProgress.setCustomText(tr("Download {0} of {1} ({2} left)", i, n, n - i)); 057 Future<?> future = dt.download(new DownloadParams(), new Bounds(td), childProgress); 058 taskFutures.add(future); 059 tasks.add(dt); 060 } 061 062 /** 063 * Downloads a list of areas from the OSM Server 064 * @param newLayer Set to true if all areas should be put into a single new layer 065 * @param rects The List of Rectangle2D to download 066 * @param osmData Set to true if OSM data should be downloaded 067 * @param gpxData Set to true if GPX data should be downloaded 068 * @param progressMonitor The progress monitor 069 * @return The Future representing the asynchronous download task 070 */ 071 public Future<?> download(boolean newLayer, List<Rectangle2D> rects, boolean osmData, boolean gpxData, ProgressMonitor progressMonitor) { 072 this.progressMonitor = progressMonitor; 073 if (newLayer) { 074 Layer l = new OsmDataLayer(new DataSet(), OsmDataLayer.createNewName(), null); 075 MainApplication.getLayerManager().addLayer(l); 076 MainApplication.getLayerManager().setActiveLayer(l); 077 } 078 079 int n = (osmData && gpxData ? 2 : 1)*rects.size(); 080 progressMonitor.beginTask(null, n); 081 int i = 0; 082 for (Rectangle2D td : rects) { 083 i++; 084 if (osmData) { 085 addDownloadTask(progressMonitor, new DownloadOsmTask(), td, i, n); 086 } 087 if (gpxData) { 088 addDownloadTask(progressMonitor, new DownloadGpsTask(), td, i, n); 089 } 090 } 091 progressMonitor.addCancelListener(() -> { 092 for (DownloadTask dt : tasks) { 093 dt.cancel(); 094 } 095 }); 096 return MainApplication.worker.submit(new PostDownloadProcessor(osmData)); 097 } 098 099 /** 100 * Downloads a list of areas from the OSM Server 101 * @param newLayer Set to true if all areas should be put into a single new layer 102 * @param areas The Collection of Areas to download 103 * @param osmData Set to true if OSM data should be downloaded 104 * @param gpxData Set to true if GPX data should be downloaded 105 * @param progressMonitor The progress monitor 106 * @return The Future representing the asynchronous download task 107 */ 108 public Future<?> download(boolean newLayer, Collection<Area> areas, boolean osmData, boolean gpxData, ProgressMonitor progressMonitor) { 109 progressMonitor.beginTask(tr("Updating data")); 110 try { 111 List<Rectangle2D> rects = new ArrayList<>(areas.size()); 112 for (Area a : areas) { 113 rects.add(a.getBounds2D()); 114 } 115 116 return download(newLayer, rects, osmData, gpxData, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)); 117 } finally { 118 progressMonitor.finishTask(); 119 } 120 } 121 122 /** 123 * Replies the set of ids of all complete, non-new primitives (i.e. those with !primitive.incomplete) 124 * @param ds data set 125 * 126 * @return the set of ids of all complete, non-new primitives 127 */ 128 protected Set<OsmPrimitive> getCompletePrimitives(DataSet ds) { 129 return ds.allPrimitives().stream().filter(p -> !p.isIncomplete() && !p.isNew()).collect(Collectors.toSet()); 130 } 131 132 /** 133 * Updates the local state of a set of primitives (given by a set of primitive ids) with the 134 * state currently held on the server. 135 * 136 * @param potentiallyDeleted a set of ids to check update from the server 137 */ 138 protected void updatePotentiallyDeletedPrimitives(Set<OsmPrimitive> potentiallyDeleted) { 139 final List<OsmPrimitive> toSelect = new ArrayList<>(); 140 for (OsmPrimitive primitive : potentiallyDeleted) { 141 if (primitive != null) { 142 toSelect.add(primitive); 143 } 144 } 145 EventQueue.invokeLater(() -> UpdateSelectionAction.updatePrimitives(toSelect)); 146 } 147 148 /** 149 * Processes a set of primitives (given by a set of their ids) which might be deleted on the 150 * server. First prompts the user whether he wants to check the current state on the server. If 151 * yes, retrieves the current state on the server and checks whether the primitives are indeed 152 * deleted on the server. 153 * 154 * @param potentiallyDeleted a set of primitives (given by their ids) 155 */ 156 protected void handlePotentiallyDeletedPrimitives(Set<OsmPrimitive> potentiallyDeleted) { 157 ButtonSpec[] options = new ButtonSpec[] { 158 new ButtonSpec( 159 tr("Check on the server"), 160 new ImageProvider("ok"), 161 tr("Click to check whether objects in your local dataset are deleted on the server"), 162 null /* no specific help topic */), 163 new ButtonSpec( 164 tr("Ignore"), 165 new ImageProvider("cancel"), 166 tr("Click to abort and to resume editing"), 167 null /* no specific help topic */), 168 }; 169 170 String message = "<html>" + trn( 171 "There is {0} object in your local dataset which " 172 + "might be deleted on the server.<br>If you later try to delete or " 173 + "update this the server is likely to report a conflict.", 174 "There are {0} objects in your local dataset which " 175 + "might be deleted on the server.<br>If you later try to delete or " 176 + "update them the server is likely to report a conflict.", 177 potentiallyDeleted.size(), potentiallyDeleted.size()) 178 + "<br>" 179 + trn("Click <strong>{0}</strong> to check the state of this object on the server.", 180 "Click <strong>{0}</strong> to check the state of these objects on the server.", 181 potentiallyDeleted.size(), 182 options[0].text) + "<br>" 183 + tr("Click <strong>{0}</strong> to ignore." + "</html>", options[1].text); 184 185 int ret = HelpAwareOptionPane.showOptionDialog( 186 MainApplication.getMainFrame(), 187 message, 188 tr("Deleted or moved objects"), 189 JOptionPane.WARNING_MESSAGE, 190 null, 191 options, 192 options[0], 193 ht("/Action/UpdateData#SyncPotentiallyDeletedObjects") 194 ); 195 if (ret != 0 /* OK */) 196 return; 197 198 updatePotentiallyDeletedPrimitives(potentiallyDeleted); 199 } 200 201 /** 202 * Replies the set of primitive ids which have been downloaded by this task list 203 * 204 * @return the set of primitive ids which have been downloaded by this task list 205 */ 206 public Set<OsmPrimitive> getDownloadedPrimitives() { 207 return tasks.stream() 208 .filter(t -> t instanceof DownloadOsmTask) 209 .map(t -> ((DownloadOsmTask) t).getDownloadedData()) 210 .filter(Objects::nonNull) 211 .flatMap(ds -> ds.allPrimitives().stream()) 212 .collect(Collectors.toSet()); 213 } 214 215 class PostDownloadProcessor implements Runnable { 216 217 private final boolean osmData; 218 219 PostDownloadProcessor(boolean osmData) { 220 this.osmData = osmData; 221 } 222 223 /** 224 * Grabs and displays the error messages after all download threads have finished. 225 */ 226 @Override 227 public void run() { 228 progressMonitor.finishTask(); 229 230 // wait for all download tasks to finish 231 // 232 for (Future<?> future : taskFutures) { 233 try { 234 future.get(); 235 } catch (InterruptedException | ExecutionException | CancellationException e) { 236 Logging.error(e); 237 return; 238 } 239 } 240 Set<Object> errors = new LinkedHashSet<>(); 241 for (DownloadTask dt : tasks) { 242 errors.addAll(dt.getErrorObjects()); 243 } 244 if (!errors.isEmpty()) { 245 final Collection<String> items = new ArrayList<>(); 246 for (Object error : errors) { 247 if (error instanceof String) { 248 items.add((String) error); 249 } else if (error instanceof Exception) { 250 items.add(ExceptionUtil.explainException((Exception) error)); 251 } 252 } 253 254 GuiHelper.runInEDT(() -> { 255 if (items.size() == 1 && tr("No data found in this area.").equals(items.iterator().next())) { 256 new Notification(items.iterator().next()).setIcon(JOptionPane.WARNING_MESSAGE).show(); 257 } else { 258 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "<html>" 259 + tr("The following errors occurred during mass download: {0}", 260 Utils.joinAsHtmlUnorderedList(items)) + "</html>", 261 tr("Errors during download"), JOptionPane.ERROR_MESSAGE); 262 } 263 }); 264 265 return; 266 } 267 268 // FIXME: this is a hack. We assume that the user canceled the whole download if at 269 // least one task was canceled or if it failed 270 // 271 for (DownloadTask task : tasks) { 272 if (task instanceof AbstractDownloadTask) { 273 AbstractDownloadTask<?> absTask = (AbstractDownloadTask<?>) task; 274 if (absTask.isCanceled() || absTask.isFailed()) 275 return; 276 } 277 } 278 final OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer(); 279 if (editLayer != null && osmData) { 280 final Set<OsmPrimitive> myPrimitives = getCompletePrimitives(editLayer.getDataSet()); 281 for (DownloadTask task : tasks) { 282 if (task instanceof DownloadOsmTask) { 283 DataSet ds = ((DownloadOsmTask) task).getDownloadedData(); 284 if (ds != null) { 285 // myPrimitives.removeAll(ds.allPrimitives()) will do the same job but much slower 286 for (OsmPrimitive primitive: ds.allPrimitives()) { 287 myPrimitives.remove(primitive); 288 } 289 } 290 } 291 } 292 if (!myPrimitives.isEmpty()) { 293 GuiHelper.runInEDT(() -> handlePotentiallyDeletedPrimitives(myPrimitives)); 294 } 295 } 296 } 297 } 298}