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

Last change on this file since 6361 was 6053, checked in by Don-vip, 11 years ago

see #2283 - allow to download both osm and gpx data in DownloadOsmTaskList, thereby renamed to DownloadTaskList

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