source: josm/trunk/src/org/openstreetmap/josm/actions/UpdateDataAction.java @ 12675

Last change on this file since 12675 was 12675, checked in by Don-vip, 11 months ago

see #15182 - move the Swing-based ProgressMonitor implementations from gui.progress to gui.progress.swing. Progress monitor concept is used in very large parts of JOSM, a console-based implementation could be added later

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.awt.geom.Area;
10import java.util.ArrayList;
11import java.util.List;
12import java.util.concurrent.Future;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
16import org.openstreetmap.josm.data.DataSource;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
19import org.openstreetmap.josm.io.OnlineResource;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * This action synchronizes the dataset with the current state on the server.
24 *
25 * It does so by re-downloading all areas and thereby merging all compatible
26 * changes from the current server version.
27 */
28public class UpdateDataAction extends JosmAction {
29
30    /**
31     * Constructs a new {@code UpdateDataAction}.
32     */
33    public UpdateDataAction() {
34        super(tr("Update data"),
35                "updatedata",
36                tr("Updates the objects in the active data layer from the server."),
37                Shortcut.registerShortcut("file:updatedata",
38                        tr("File: {0}", tr("Update data")),
39                        KeyEvent.VK_U, Shortcut.CTRL),
40                true);
41        putValue("help", ht("/Action/UpdateData"));
42    }
43
44    /**
45     * Refreshes the enabled state
46     */
47    @Override
48    protected void updateEnabledState() {
49        setEnabled(getLayerManager().getEditLayer() != null && !Main.isOffline(OnlineResource.OSM_API));
50    }
51
52    @Override
53    public void actionPerformed(ActionEvent e) {
54        OsmDataLayer editLayer = getLayerManager().getEditLayer();
55        if (!isEnabled() || editLayer == null)
56            return;
57
58        List<Area> areas = new ArrayList<>();
59        for (DataSource ds : editLayer.data.getDataSources()) {
60            areas.add(new Area(ds.bounds.asRect()));
61        }
62
63        // The next two blocks removes every intersection from every DataSource Area
64        // This prevents downloading the same data numerous times at intersections
65        // and also skips smaller bounding boxes that are contained within larger ones entirely.
66        for (int i = 0; i < areas.size(); i++) {
67            for (int j = i+1; j < areas.size(); j++) {
68                areas.get(i).subtract(areas.get(j));
69            }
70        }
71
72        for (int i = areas.size()-1; i > 0; i--) {
73            for (int j = i-1; j > 0; j--) {
74                areas.get(i).subtract(areas.get(j));
75            }
76        }
77
78        List<Area> areasToDownload = new ArrayList<>();
79        for (Area a : areas) {
80            if (a.isEmpty()) {
81                continue;
82            }
83            areasToDownload.add(a);
84        }
85
86        if (areasToDownload.isEmpty()) {
87            // no bounds defined in the dataset? we update all primitives in the data set using a series of multi fetch requests
88            UpdateSelectionAction.updatePrimitives(editLayer.data.allPrimitives());
89        } else {
90            // bounds defined? => use the bbox downloader
91            final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
92            final Future<?> future = new DownloadTaskList().download(false /* no new layer */, areasToDownload, true, false, monitor);
93            waitFuture(future, monitor);
94        }
95    }
96}
Note: See TracBrowser for help on using the repository browser.