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

Last change on this file since 5299 was 4982, checked in by stoecker, 12 years ago

see #7226 - patch by akks (fixed a bit) - fix shortcut deprecations

  • 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("File: {0}", tr("Update data")),
28 KeyEvent.VK_U, Shortcut.CTRL),
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 updateLayer(OsmDataLayer layer) {
43
44 }
45
46 public void actionPerformed(ActionEvent e) {
47 if (! isEnabled())
48 return;
49 if (getEditLayer() == null)
50 return;
51
52 List<Area> areas = new ArrayList<Area>();
53 for(DataSource ds : getEditLayer().data.dataSources) {
54 areas.add(new Area(ds.bounds.asRect()));
55 }
56
57 // The next two blocks removes every intersection from every DataSource Area
58 // This prevents downloading the same data numerous times at intersections
59 // and also skips smaller bounding boxes that are contained within larger ones
60 // entirely.
61 for(int i = 0; i < areas.size(); i++) {
62 for(int j = i+1; j < areas.size(); j++) {
63 areas.get(i).subtract(areas.get(j));
64 }
65 }
66
67 for(int i = areas.size()-1; i > 0 ; i--) {
68 for(int j = i-1; j > 0; j--) {
69 areas.get(i).subtract(areas.get(j));
70 }
71 }
72
73 List<Area> areasToDownload = new ArrayList<Area>();
74 for(Area a : areas) {
75 if(a.isEmpty()) {
76 continue;
77 }
78 areasToDownload.add(a);
79 }
80
81 if(areasToDownload.isEmpty()) {
82 // no bounds defined in the dataset? we update all primitives in the data set
83 // using a series of multi fetch requests
84 //
85 new UpdateSelectionAction().updatePrimitives(getEditLayer().data.allPrimitives());
86 } else {
87 // bounds defined? => use the bbox downloader
88 //
89 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
90 final Future<?> future = new DownloadOsmTaskList().download(false /* no new layer */, areasToDownload, monitor);
91 Main.worker.submit(
92 new Runnable() {
93 public void run() {
94 try {
95 future.get();
96 } catch(Exception e) {
97 e.printStackTrace();
98 return;
99 }
100 monitor.close();
101 }
102 }
103 );
104 }
105 }
106}
Note: See TracBrowser for help on using the repository browser.