001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.actions;
003
004 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005 import static org.openstreetmap.josm.tools.I18n.tr;
006
007 import java.awt.event.ActionEvent;
008 import java.awt.event.KeyEvent;
009 import java.awt.geom.Area;
010 import java.util.ArrayList;
011 import java.util.List;
012 import java.util.concurrent.Future;
013
014 import org.openstreetmap.josm.Main;
015 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTaskList;
016 import org.openstreetmap.josm.data.osm.DataSource;
017 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
018 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
019 import org.openstreetmap.josm.tools.Shortcut;
020
021 public 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 public void actionPerformed(ActionEvent e) {
047 if (! isEnabled())
048 return;
049 if (getEditLayer() == null)
050 return;
051
052 List<Area> areas = new ArrayList<Area>();
053 for(DataSource ds : getEditLayer().data.dataSources) {
054 areas.add(new Area(ds.bounds.asRect()));
055 }
056
057 // The next two blocks removes every intersection from every DataSource Area
058 // This prevents downloading the same data numerous times at intersections
059 // and also skips smaller bounding boxes that are contained within larger ones
060 // entirely.
061 for(int i = 0; i < areas.size(); i++) {
062 for(int j = i+1; j < areas.size(); j++) {
063 areas.get(i).subtract(areas.get(j));
064 }
065 }
066
067 for(int i = areas.size()-1; i > 0 ; i--) {
068 for(int j = i-1; j > 0; j--) {
069 areas.get(i).subtract(areas.get(j));
070 }
071 }
072
073 List<Area> areasToDownload = new ArrayList<Area>();
074 for(Area a : areas) {
075 if(a.isEmpty()) {
076 continue;
077 }
078 areasToDownload.add(a);
079 }
080
081 if(areasToDownload.isEmpty()) {
082 // no bounds defined in the dataset? we update all primitives in the data set
083 // using a series of multi fetch requests
084 //
085 new UpdateSelectionAction().updatePrimitives(getEditLayer().data.allPrimitives());
086 } else {
087 // bounds defined? => use the bbox downloader
088 //
089 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
090 final Future<?> future = new DownloadOsmTaskList().download(false /* no new layer */, areasToDownload, monitor);
091 Main.worker.submit(
092 new Runnable() {
093 public void run() {
094 try {
095 future.get();
096 } catch(Exception e) {
097 e.printStackTrace();
098 return;
099 }
100 monitor.close();
101 }
102 }
103 );
104 }
105 }
106 }