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

Last change on this file since 4701 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 3.6 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.DownloadOsmTaskList;
16import org.openstreetmap.josm.data.osm.DataSource;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
19import org.openstreetmap.josm.tools.Shortcut;
20
21public class UpdateDataAction extends JosmAction{
22 public UpdateDataAction() {
23 super(tr("Update data"),
24 "updatedata",
25 tr("Updates the objects in the active data layer from the server."),
26 Shortcut.registerShortcut("file:updatedata",
27 tr("Update data"),
28 KeyEvent.VK_U,
29 Shortcut.GROUP_HOTKEY),
30 true);
31 putValue("help", ht("/Action/UpdateData"));
32 }
33
34 /**
35 * Refreshes the enabled state
36 *
37 */
38 @Override
39 protected void updateEnabledState() {
40 setEnabled(getEditLayer() != null);
41 }
42
43 public void updateLayer(OsmDataLayer layer) {
44
45 }
46
47 public void actionPerformed(ActionEvent e) {
48 if (! isEnabled())
49 return;
50 if (getEditLayer() == null)
51 return;
52
53 List<Area> areas = new ArrayList<Area>();
54 for(DataSource ds : getEditLayer().data.dataSources) {
55 areas.add(new Area(ds.bounds.asRect()));
56 }
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
74 List<Area> areasToDownload = new ArrayList<Area>();
75 for(Area a : areas) {
76 if(a.isEmpty()) {
77 continue;
78 }
79 areasToDownload.add(a);
80 }
81
82 if(areasToDownload.isEmpty()) {
83 // no bounds defined in the dataset? we update all primitives in the data set
84 // using a series of multi fetch requests
85 //
86 new UpdateSelectionAction().updatePrimitives(getEditLayer().data.allPrimitives());
87 } else {
88 // bounds defined? => use the bbox downloader
89 //
90 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
91 final Future<?> future = new DownloadOsmTaskList().download(false /* no new layer */, areasToDownload, monitor);
92 Main.worker.submit(
93 new Runnable() {
94 public void run() {
95 try {
96 future.get();
97 } catch(Exception e) {
98 e.printStackTrace();
99 return;
100 }
101 monitor.close();
102 }
103 }
104 );
105 }
106 }
107}
Note: See TracBrowser for help on using the repository browser.