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.Main; 015import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList; 016import org.openstreetmap.josm.data.osm.DataSource; 017import org.openstreetmap.josm.gui.layer.OsmDataLayer; 018import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 019import org.openstreetmap.josm.tools.Shortcut; 020 021public class UpdateDataAction extends JosmAction{ 022 public UpdateDataAction() { 023 super(tr("Update data"), 024 "updatedata", 025 tr("Updates the objects in the active data layer from the server."), 026 Shortcut.registerShortcut("file:updatedata", 027 tr("File: {0}", tr("Update data")), 028 KeyEvent.VK_U, Shortcut.CTRL), 029 true); 030 putValue("help", ht("/Action/UpdateData")); 031 } 032 033 /** 034 * Refreshes the enabled state 035 * 036 */ 037 @Override 038 protected void updateEnabledState() { 039 setEnabled(getEditLayer() != null); 040 } 041 042 public void updateLayer(OsmDataLayer layer) { 043 044 } 045 046 @Override 047 public void actionPerformed(ActionEvent e) { 048 if (! isEnabled()) 049 return; 050 if (getEditLayer() == null) 051 return; 052 053 List<Area> areas = new ArrayList<Area>(); 054 for(DataSource ds : getEditLayer().data.dataSources) { 055 areas.add(new Area(ds.bounds.asRect())); 056 } 057 058 // The next two blocks removes every intersection from every DataSource Area 059 // This prevents downloading the same data numerous times at intersections 060 // and also skips smaller bounding boxes that are contained within larger ones 061 // entirely. 062 for(int i = 0; i < areas.size(); i++) { 063 for(int j = i+1; j < areas.size(); j++) { 064 areas.get(i).subtract(areas.get(j)); 065 } 066 } 067 068 for(int i = areas.size()-1; i > 0 ; i--) { 069 for(int j = i-1; j > 0; j--) { 070 areas.get(i).subtract(areas.get(j)); 071 } 072 } 073 074 List<Area> areasToDownload = new ArrayList<Area>(); 075 for(Area a : areas) { 076 if(a.isEmpty()) { 077 continue; 078 } 079 areasToDownload.add(a); 080 } 081 082 if(areasToDownload.isEmpty()) { 083 // no bounds defined in the dataset? we update all primitives in the data set 084 // using a series of multi fetch requests 085 // 086 UpdateSelectionAction.updatePrimitives(getEditLayer().data.allPrimitives()); 087 } else { 088 // bounds defined? => use the bbox downloader 089 // 090 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data")); 091 final Future<?> future = new DownloadTaskList().download(false /* no new layer */, areasToDownload, true, false, monitor); 092 Main.worker.submit( 093 new Runnable() { 094 @Override 095 public void run() { 096 try { 097 future.get(); 098 } catch(Exception e) { 099 e.printStackTrace(); 100 return; 101 } 102 monitor.close(); 103 } 104 } 105 ); 106 } 107 } 108}