[1434] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2942] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[1434] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
| 9 | import java.awt.geom.Area;
|
---|
| 10 | import java.util.ArrayList;
|
---|
| 11 | import java.util.List;
|
---|
[2325] | 12 | import java.util.concurrent.Future;
|
---|
[1434] | 13 |
|
---|
[2325] | 14 | import org.openstreetmap.josm.Main;
|
---|
[6053] | 15 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
|
---|
[7575] | 16 | import org.openstreetmap.josm.data.DataSource;
|
---|
[1872] | 17 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
[7434] | 18 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
[1434] | 19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 20 |
|
---|
[7434] | 21 | public class UpdateDataAction extends JosmAction {
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Constructs a new {@code UpdateDataAction}.
|
---|
| 25 | */
|
---|
[1434] | 26 | public UpdateDataAction() {
|
---|
[2253] | 27 | super(tr("Update data"),
|
---|
[1434] | 28 | "updatedata",
|
---|
[2317] | 29 | tr("Updates the objects in the active data layer from the server."),
|
---|
[1434] | 30 | Shortcut.registerShortcut("file:updatedata",
|
---|
[4927] | 31 | tr("File: {0}", tr("Update data")),
|
---|
[4982] | 32 | KeyEvent.VK_U, Shortcut.CTRL),
|
---|
[4851] | 33 | true);
|
---|
[2323] | 34 | putValue("help", ht("/Action/UpdateData"));
|
---|
[1434] | 35 | }
|
---|
| 36 |
|
---|
[1808] | 37 | /**
|
---|
| 38 | * Refreshes the enabled state
|
---|
| 39 | */
|
---|
[1820] | 40 | @Override
|
---|
| 41 | protected void updateEnabledState() {
|
---|
[7434] | 42 | setEnabled(getEditLayer() != null && !Main.isOffline(OnlineResource.OSM_API));
|
---|
[1808] | 43 | }
|
---|
| 44 |
|
---|
[6046] | 45 | @Override
|
---|
[1434] | 46 | public void actionPerformed(ActionEvent e) {
|
---|
[8443] | 47 | if (!isEnabled())
|
---|
[1808] | 48 | return;
|
---|
[1881] | 49 | if (getEditLayer() == null)
|
---|
| 50 | return;
|
---|
| 51 |
|
---|
[7005] | 52 | List<Area> areas = new ArrayList<>();
|
---|
[8510] | 53 | for (DataSource ds : getEditLayer().data.dataSources) {
|
---|
[2943] | 54 | areas.add(new Area(ds.bounds.asRect()));
|
---|
[1670] | 55 | }
|
---|
[1434] | 56 |
|
---|
| 57 | // The next two blocks removes every intersection from every DataSource Area
|
---|
| 58 | // This prevents downloading the same data numerous times at intersections
|
---|
| 59 | // and also skips smaller bounding boxes that are contained within larger ones
|
---|
| 60 | // entirely.
|
---|
[8510] | 61 | for (int i = 0; i < areas.size(); i++) {
|
---|
| 62 | for (int j = i+1; j < areas.size(); j++) {
|
---|
[1434] | 63 | areas.get(i).subtract(areas.get(j));
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[8510] | 67 | for (int i = areas.size()-1; i > 0; i--) {
|
---|
| 68 | for (int j = i-1; j > 0; j--) {
|
---|
[1434] | 69 | areas.get(i).subtract(areas.get(j));
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[7005] | 73 | List<Area> areasToDownload = new ArrayList<>();
|
---|
[8510] | 74 | for (Area a : areas) {
|
---|
| 75 | if (a.isEmpty()) {
|
---|
[1434] | 76 | continue;
|
---|
[1670] | 77 | }
|
---|
[2942] | 78 | areasToDownload.add(a);
|
---|
[1434] | 79 | }
|
---|
| 80 |
|
---|
[8510] | 81 | if (areasToDownload.isEmpty()) {
|
---|
[1881] | 82 | // no bounds defined in the dataset? we update all primitives in the data set
|
---|
| 83 | // using a series of multi fetch requests
|
---|
| 84 | //
|
---|
[6046] | 85 | UpdateSelectionAction.updatePrimitives(getEditLayer().data.allPrimitives());
|
---|
[1881] | 86 | } else {
|
---|
| 87 | // bounds defined? => use the bbox downloader
|
---|
| 88 | //
|
---|
[2325] | 89 | final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
|
---|
[6053] | 90 | final Future<?> future = new DownloadTaskList().download(false /* no new layer */, areasToDownload, true, false, monitor);
|
---|
[2325] | 91 | Main.worker.submit(
|
---|
| 92 | new Runnable() {
|
---|
[6046] | 93 | @Override
|
---|
[2325] | 94 | public void run() {
|
---|
| 95 | try {
|
---|
| 96 | future.get();
|
---|
[8510] | 97 | } catch (Exception e) {
|
---|
[6643] | 98 | Main.error(e);
|
---|
[2325] | 99 | return;
|
---|
| 100 | }
|
---|
| 101 | monitor.close();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | );
|
---|
[1464] | 105 | }
|
---|
[1434] | 106 | }
|
---|
| 107 | }
|
---|