Changeset 2322 in josm for trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskList.java
- Timestamp:
- 2009-10-25T23:09:53+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskList.java
r2273 r2322 5 5 6 6 import java.awt.EventQueue; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 7 9 import java.awt.geom.Area; 8 10 import java.awt.geom.Rectangle2D; … … 10 12 import java.util.Collection; 11 13 import java.util.HashSet; 14 import java.util.LinkedHashSet; 12 15 import java.util.LinkedList; 13 16 import java.util.List; 14 17 import java.util.Set; 18 import java.util.concurrent.Future; 15 19 16 20 import javax.swing.JOptionPane; … … 20 24 import org.openstreetmap.josm.data.osm.DataSet; 21 25 import org.openstreetmap.josm.data.osm.OsmPrimitive; 22 import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;23 26 import org.openstreetmap.josm.gui.layer.Layer; 24 27 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 25 28 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 29 import org.openstreetmap.josm.gui.progress.ProgressMonitor.CancelListener; 30 import org.openstreetmap.josm.tools.ExceptionUtil; 26 31 27 32 /** … … 34 39 public class DownloadOsmTaskList implements Runnable { 35 40 private List<DownloadTask> osmTasks = new LinkedList<DownloadTask>(); 41 private List<Future<?>> osmTaskFutures = new LinkedList<Future<?>>(); 36 42 private ProgressMonitor progressMonitor; 37 43 … … 41 47 * @param The List of Rectangle2D to download 42 48 */ 43 public voiddownload(boolean newLayer, List<Rectangle2D> rects, ProgressMonitor progressMonitor) {49 public Future<?> download(boolean newLayer, List<Rectangle2D> rects, ProgressMonitor progressMonitor) { 44 50 this.progressMonitor = progressMonitor; 45 51 if(newLayer) { … … 50 56 51 57 progressMonitor.beginTask(null, rects.size()); 52 try { 53 int i = 0; 54 for(Rectangle2D td : rects) { 55 i++; 56 DownloadTask dt = new DownloadOsmTask(); 57 ProgressMonitor childProgress = progressMonitor.createSubTaskMonitor(1, false); 58 childProgress.setSilent(true); 59 childProgress.setCustomText(tr("Download {0} of {1} ({2} left)", i, rects.size(), rects.size()-i)); 60 dt.download(null, td.getMinY(), td.getMinX(), td.getMaxY(), td.getMaxX(), childProgress); 61 osmTasks.add(dt); 62 } 63 } finally { 64 // If we try to get the error message now the download task will never have been started 65 // and we'd be stuck in a classical dead lock. Instead attach this to the worker and once 66 // run() gets called all downloadTasks have finished and we can grab the error messages. 67 Main.worker.execute(this); 68 } 58 int i = 0; 59 for(Rectangle2D td : rects) { 60 i++; 61 DownloadTask dt = new DownloadOsmTask(); 62 ProgressMonitor childProgress = progressMonitor.createSubTaskMonitor(1, false); 63 childProgress.setSilent(true); 64 childProgress.setCustomText(tr("Download {0} of {1} ({2} left)", i, rects.size(), rects.size()-i)); 65 Future<?> future = dt.download(null, td.getMinY(), td.getMinX(), td.getMaxY(), td.getMaxX(), childProgress); 66 osmTaskFutures.add(future); 67 osmTasks.add(dt); 68 } 69 progressMonitor.addCancelListener( 70 new CancelListener() { 71 public void operationCanceled() { 72 for (DownloadTask dt: osmTasks) { 73 dt.cancel(); 74 } 75 } 76 } 77 ); 78 return Main.worker.submit(this); 69 79 } 70 80 … … 93 103 public void run() { 94 104 progressMonitor.finishTask(); 95 String errors = ""; 96 97 LinkedList<Integer> shown = new LinkedList<Integer>(); 105 106 // wait for all tasks to finish 107 // 108 for (Future<?> future: osmTaskFutures) { 109 try { 110 future.get(); 111 } catch(Exception e) { 112 e.printStackTrace(); 113 return; 114 } 115 } 116 LinkedHashSet<Object> errors = new LinkedHashSet<Object>(); 98 117 for(DownloadTask dt : osmTasks) { 99 String err = dt.getErrorMessage(); 100 // avoid display of identical messages 101 if (err.equals("") || shown.contains(err.hashCode())) { 102 continue; 103 } 104 shown.add(err.hashCode()); 105 errors += "<br>* " + err; 106 } 107 108 if(! errors.equals("")) { 118 errors.addAll(dt.getErrorObjects()); 119 } 120 if (!errors.isEmpty()) { 121 StringBuffer sb = new StringBuffer(); 122 for (Object error:errors) { 123 if (error instanceof String) { 124 sb.append("<li>").append(error).append("</li>").append("<br>"); 125 } else if (error instanceof Exception) { 126 sb.append("<li>").append(ExceptionUtil.explainException((Exception)error)).append("</li>").append("<br>"); 127 } 128 } 129 sb.insert(0, "<ul>"); 130 sb.append("</ul>"); 131 109 132 JOptionPane.showMessageDialog( 110 133 Main.parent, 111 "<html>"+tr("The following errors occurred during mass download: {0}", errors)134 "<html>"+tr("The following errors occurred during mass download: {0}", sb.toString()) 112 135 +"</html>", 113 136 tr("Errors during Download"),
Note:
See TracChangeset
for help on using the changeset viewer.