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

Last change on this file since 2325 was 2325, checked in by Gubaer, 15 years ago

fixed #3783: does not finish download API
Also improved help

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