1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.awt.geom.Area;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTaskList;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
14 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
16 |
|
---|
17 | public class UpdateDataAction extends JosmAction{
|
---|
18 | public UpdateDataAction() {
|
---|
19 | super(tr("Update Data"),
|
---|
20 | "updatedata",
|
---|
21 | tr("Updates the current data layer from the server (re-downloads data)"),
|
---|
22 | Shortcut.registerShortcut("file:updatedata",
|
---|
23 | tr("Update Data"),
|
---|
24 | KeyEvent.VK_U,
|
---|
25 | Shortcut.GROUP_HOTKEY),
|
---|
26 | true);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Refreshes the enabled state
|
---|
31 | *
|
---|
32 | */
|
---|
33 | @Override
|
---|
34 | protected void updateEnabledState() {
|
---|
35 | setEnabled(getEditLayer() != null);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void actionPerformed(ActionEvent e) {
|
---|
39 | if (! isEnabled())
|
---|
40 | return;
|
---|
41 | if (getEditLayer() == null)
|
---|
42 | return;
|
---|
43 |
|
---|
44 | int bboxCount = 0;
|
---|
45 | List<Area> areas = new ArrayList<Area>();
|
---|
46 | for(DataSource ds : getEditLayer().data.dataSources) {
|
---|
47 | areas.add(new Area(ds.bounds.asRect()));
|
---|
48 | }
|
---|
49 |
|
---|
50 | // The next two blocks removes every intersection from every DataSource Area
|
---|
51 | // This prevents downloading the same data numerous times at intersections
|
---|
52 | // and also skips smaller bounding boxes that are contained within larger ones
|
---|
53 | // entirely.
|
---|
54 | for(int i = 0; i < areas.size(); i++) {
|
---|
55 | for(int j = i+1; j < areas.size(); j++) {
|
---|
56 | areas.get(i).subtract(areas.get(j));
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | for(int i = areas.size()-1; i > 0 ; i--) {
|
---|
61 | for(int j = i-1; j > 0; j--) {
|
---|
62 | areas.get(i).subtract(areas.get(j));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | for(Area a : areas) {
|
---|
67 | if(a.isEmpty()) {
|
---|
68 | continue;
|
---|
69 | }
|
---|
70 | bboxCount++;
|
---|
71 | }
|
---|
72 |
|
---|
73 | if(bboxCount == 0) {
|
---|
74 | // no bounds defined in the dataset? we update all primitives in the data set
|
---|
75 | // using a series of multi fetch requests
|
---|
76 | //
|
---|
77 | new UpdateSelectionAction().updatePrimitives(getEditLayer().data.allPrimitives());
|
---|
78 | } else {
|
---|
79 | // bounds defined? => use the bbox downloader
|
---|
80 | //
|
---|
81 | new DownloadOsmTaskList().download(false, areas, new PleaseWaitProgressMonitor(tr("Updating data")));
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|