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.OptionPaneUtil;
|
---|
18 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
20 |
|
---|
21 | public class UpdateDataAction extends JosmAction{
|
---|
22 | public UpdateDataAction() {
|
---|
23 | super(tr("Update Data"),
|
---|
24 | "updatedata",
|
---|
25 | tr("Updates the current data layer from the server (re-downloads data)"),
|
---|
26 | Shortcut.registerShortcut("file:updatedata",
|
---|
27 | tr("Update Data"),
|
---|
28 | KeyEvent.VK_U,
|
---|
29 | Shortcut.GROUP_HOTKEY),
|
---|
30 | true);
|
---|
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 | int bboxCount = 0;
|
---|
46 | List<Area> areas = new ArrayList<Area>();
|
---|
47 | for(DataSource ds : Main.map.mapView.getEditLayer().data.dataSources) {
|
---|
48 | areas.add(new Area(ds.bounds.asRect()));
|
---|
49 | }
|
---|
50 |
|
---|
51 | // The next two blocks removes every intersection from every DataSource Area
|
---|
52 | // This prevents downloading the same data numerous times at intersections
|
---|
53 | // and also skips smaller bounding boxes that are contained within larger ones
|
---|
54 | // entirely.
|
---|
55 | for(int i = 0; i < areas.size(); i++) {
|
---|
56 | for(int j = i+1; j < areas.size(); j++) {
|
---|
57 | areas.get(i).subtract(areas.get(j));
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | for(int i = areas.size()-1; i > 0 ; i--) {
|
---|
62 | for(int j = i-1; j > 0; j--) {
|
---|
63 | areas.get(i).subtract(areas.get(j));
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | for(Area a : areas) {
|
---|
68 | if(a.isEmpty()) {
|
---|
69 | continue;
|
---|
70 | }
|
---|
71 | bboxCount++;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if(bboxCount == 0) {
|
---|
75 | OptionPaneUtil.showMessageDialog(
|
---|
76 | Main.parent,
|
---|
77 | tr("No data to update found. Have you already opened or downloaded a data layer?"),
|
---|
78 | tr("No data"),
|
---|
79 | JOptionPane.WARNING_MESSAGE
|
---|
80 | );
|
---|
81 | return;
|
---|
82 | }
|
---|
83 |
|
---|
84 | new DownloadOsmTaskList().download(false, areas, new PleaseWaitProgressMonitor());
|
---|
85 | }
|
---|
86 | }
|
---|