001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.event.ActionEvent; 008import java.awt.event.KeyEvent; 009import java.awt.geom.Area; 010import java.util.ArrayList; 011import java.util.List; 012import java.util.concurrent.Future; 013 014import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList; 015import org.openstreetmap.josm.data.DataSource; 016import org.openstreetmap.josm.gui.layer.OsmDataLayer; 017import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor; 018import org.openstreetmap.josm.io.NetworkManager; 019import org.openstreetmap.josm.io.OnlineResource; 020import org.openstreetmap.josm.spi.preferences.Config; 021import org.openstreetmap.josm.tools.Shortcut; 022 023/** 024 * This action synchronizes the dataset with the current state on the server. 025 * 026 * It does so by re-downloading all areas and thereby merging all compatible 027 * changes from the current server version. 028 */ 029public class UpdateDataAction extends JosmAction { 030 031 /** 032 * Constructs a new {@code UpdateDataAction}. 033 */ 034 public UpdateDataAction() { 035 super(tr("Update data"), 036 "updatedata", 037 tr("Updates the objects in the active data layer from the server."), 038 Shortcut.registerShortcut("file:updatedata", 039 tr("File: {0}", tr("Update data")), 040 KeyEvent.VK_U, Shortcut.CTRL), 041 true); 042 setHelpId(ht("/Action/UpdateData")); 043 } 044 045 /** 046 * Refreshes the enabled state 047 */ 048 @Override 049 protected void updateEnabledState() { 050 OsmDataLayer editLayer = getLayerManager().getEditLayer(); 051 setEnabled(editLayer != null && editLayer.isDownloadable() && !NetworkManager.isOffline(OnlineResource.OSM_API)); 052 } 053 054 @Override 055 public void actionPerformed(ActionEvent e) { 056 OsmDataLayer editLayer = getLayerManager().getEditLayer(); 057 if (!isEnabled() || editLayer == null || !editLayer.isDownloadable()) 058 return; 059 060 List<Area> areas = new ArrayList<>(); 061 for (DataSource ds : editLayer.data.getDataSources()) { 062 areas.add(new Area(ds.bounds.asRect())); 063 } 064 065 // The next two blocks removes every intersection from every DataSource Area 066 // This prevents downloading the same data numerous times at intersections 067 // and also skips smaller bounding boxes that are contained within larger ones entirely. 068 for (int i = 0; i < areas.size(); i++) { 069 for (int j = i+1; j < areas.size(); j++) { 070 areas.get(i).subtract(areas.get(j)); 071 } 072 } 073 074 for (int i = areas.size()-1; i > 0; i--) { 075 for (int j = i-1; j > 0; j--) { 076 areas.get(i).subtract(areas.get(j)); 077 } 078 } 079 080 List<Area> areasToDownload = new ArrayList<>(); 081 for (Area a : areas) { 082 if (a.isEmpty()) { 083 continue; 084 } 085 areasToDownload.add(a); 086 } 087 088 if (areasToDownload.isEmpty()) { 089 // no bounds defined in the dataset? we update all primitives in the data set using a series of multi fetch requests 090 UpdateSelectionAction.updatePrimitives(editLayer.data.allPrimitives()); 091 } else { 092 // bounds defined? => use the bbox downloader 093 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data")); 094 final Future<?> future = new DownloadTaskList(Config.getPref().getBoolean("update.data.zoom-after-download")) 095 .download(false /* no new layer */, areasToDownload, true, false, monitor); 096 waitFuture(future, monitor); 097 } 098 } 099}