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 javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTaskList;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
17 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
18 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
19 |
|
---|
20 | public class UpdateDataAction extends JosmAction {
|
---|
21 | public UpdateDataAction() {
|
---|
22 | super(tr("Update Data"),
|
---|
23 | "updatedata",
|
---|
24 | tr("Updates the current data layer from the server (re-downloads data)"),
|
---|
25 | Shortcut.registerShortcut("file:updatedata",
|
---|
26 | tr("Update Data"),
|
---|
27 | KeyEvent.VK_U,
|
---|
28 | Shortcut.GROUP_NONE),
|
---|
29 | true);
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void actionPerformed(ActionEvent e) {
|
---|
33 | int bboxCount = 0;
|
---|
34 | List<Area> areas = new ArrayList<Area>();
|
---|
35 | for(DataSource ds : Main.main.editLayer().data.dataSources)
|
---|
36 | areas.add(new Area(ds.bounds.asRect()));
|
---|
37 |
|
---|
38 | // This would loop over all DataLayers but download all data to the currently
|
---|
39 | // selected one
|
---|
40 | /*for(Layer l : Main.map.mapView.getAllLayers()) {
|
---|
41 | if(!(l instanceof OsmDataLayer)) continue;
|
---|
42 |
|
---|
43 | for(DataSource ds : ((OsmDataLayer)l).data.dataSources)
|
---|
44 | areas.add(new Area(ds.bounds.asRect()));
|
---|
45 | }*/
|
---|
46 |
|
---|
47 | // The next two blocks removes every intersection from every DataSource Area
|
---|
48 | // This prevents downloading the same data numerous times at intersections
|
---|
49 | // and also skips smaller bounding boxes that are contained within larger ones
|
---|
50 | // entirely.
|
---|
51 | for(int i = 0; i < areas.size(); i++) {
|
---|
52 | for(int j = i+1; j < areas.size(); j++) {
|
---|
53 | areas.get(i).subtract(areas.get(j));
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | for(int i = areas.size()-1; i > 0 ; i--) {
|
---|
58 | for(int j = i-1; j > 0; j--) {
|
---|
59 | areas.get(i).subtract(areas.get(j));
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | for(Area a : areas) {
|
---|
64 | if(a.isEmpty())
|
---|
65 | continue;
|
---|
66 | bboxCount++;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if(bboxCount == 0) {
|
---|
70 | JOptionPane.showMessageDialog(Main.parent,
|
---|
71 | tr("No data to update found. Have you already opened or downloaded a data layer?"));
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int result = new ExtendedDialog(Main.parent,
|
---|
76 | tr("Update Data"),
|
---|
77 | tr("This action will require {0} individual download requests. "
|
---|
78 | + "Do you wish to continue?", bboxCount),
|
---|
79 | new String[] { tr("Update Data"), tr("Cancel") },
|
---|
80 | new String[] { "updatedata.png", "cancel.png" }).getValue();
|
---|
81 |
|
---|
82 | if(result != 1)
|
---|
83 | return;
|
---|
84 |
|
---|
85 | new DownloadOsmTaskList().download(false, areas);
|
---|
86 | }
|
---|
87 |
|
---|
88 | }
|
---|