| 1 | // License: GPL. For details, see LICENSE file. |
| 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.awt.event.KeyEvent; |
| 8 | import java.awt.geom.Area; |
| 9 | import java.awt.geom.Rectangle2D; |
| 10 | import java.util.ArrayList; |
| 11 | import java.util.List; |
| 12 | |
| 13 | import org.openstreetmap.josm.Main; |
| 14 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; |
| 15 | import org.openstreetmap.josm.data.osm.DataSource; |
| 16 | import org.openstreetmap.josm.gui.ExtendedDialog; |
| 17 | import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask; |
| 18 | import org.openstreetmap.josm.tools.Shortcut; |
| 19 | |
| 20 | public class UpdateDataAction extends JosmAction { |
| 21 | public UpdateDataAction() { |
| 22 | super(tr("Update Data"), |
| 23 | "updatedata", |
| 24 | tr("Updates the current data layer from the server (re-downloads data)"), |
| 25 | Shortcut.registerShortcut("file:updatedata", |
| 26 | tr("Update Data"), |
| 27 | KeyEvent.VK_U, |
| 28 | Shortcut.GROUP_NONE), |
| 29 | true); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public void actionPerformed(ActionEvent e) { |
| 34 | int bboxCount = 0; |
| 35 | List<Area> areas = new ArrayList<Area>(); |
| 36 | for(DataSource ds : Main.main.editLayer().data.dataSources) |
| 37 | areas.add(new Area(ds.bounds.asRect())); |
| 38 | |
| 39 | // This would loop over all DataLayers but download all data to the currently |
| 40 | // selected one |
| 41 | /*for(Layer l : Main.map.mapView.getAllLayers()) { |
| 42 | if(!(l instanceof OsmDataLayer)) continue; |
| 43 | |
| 44 | for(DataSource ds : ((OsmDataLayer)l).data.dataSources) |
| 45 | areas.add(new Area(ds.bounds.asRect())); |
| 46 | }*/ |
| 47 | |
| 48 | // The next two blocks removes every intersection from every DataSource Area |
| 49 | // This prevents downloading the same data numerous times at intersections |
| 50 | // and also skips smaller bounding boxes that are contained within larger ones |
| 51 | // entirely. |
| 52 | for(int i = 0; i < areas.size(); i++) { |
| 53 | for(int j = i+1; j < areas.size(); j++) { |
| 54 | areas.get(i).subtract(areas.get(j)); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | for(int i = areas.size()-1; i > 0 ; i--) { |
| 59 | for(int j = i-1; j > 0; j--) { |
| 60 | areas.get(i).subtract(areas.get(j)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | for(Area a : areas) { |
| 65 | if(a.isEmpty()) |
| 66 | continue; |
| 67 | bboxCount++; |
| 68 | } |
| 69 | |
| 70 | int result = new ExtendedDialog(Main.parent, |
| 71 | tr("Update Data"), |
| 72 | tr("This action will require {0} individual download requests. " |
| 73 | + "Do you wish to continue?", bboxCount), |
| 74 | new String[] { "Update Data", "Cancel" }, |
| 75 | new String[] { "updatedata.png", "cancel.png" }).getValue(); |
| 76 | |
| 77 | if(result != 1) |
| 78 | return; |
| 79 | |
| 80 | DownloadTask osmTask = new DownloadOsmTask(); |
| 81 | for(Area a : areas) { |
| 82 | Rectangle2D td = a.getBounds2D(); |
| 83 | osmTask.download(null, td.getMinY(), td.getMinX(), td.getMaxY(), td.getMaxX()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } |