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.layer.Layer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
|
---|
19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
20 |
|
---|
21 | public class UpdateDataAction extends JosmAction implements LayerChangeListener{
|
---|
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 | refreshEnabled();
|
---|
32 | Layer.listeners.add(this);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Refreshes the enabled state
|
---|
37 | *
|
---|
38 | */
|
---|
39 | protected void refreshEnabled() {
|
---|
40 | setEnabled(Main.main != null
|
---|
41 | && Main.map != null
|
---|
42 | && Main.map.mapView !=null
|
---|
43 | && Main.map.mapView.getEditLayer() != null
|
---|
44 | );
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void actionPerformed(ActionEvent e) {
|
---|
48 | if (! isEnabled())
|
---|
49 | return;
|
---|
50 | int bboxCount = 0;
|
---|
51 | List<Area> areas = new ArrayList<Area>();
|
---|
52 | for(DataSource ds : Main.main.createOrGetEditLayer().data.dataSources) {
|
---|
53 | areas.add(new Area(ds.bounds.asRect()));
|
---|
54 | }
|
---|
55 |
|
---|
56 | // The next two blocks removes every intersection from every DataSource Area
|
---|
57 | // This prevents downloading the same data numerous times at intersections
|
---|
58 | // and also skips smaller bounding boxes that are contained within larger ones
|
---|
59 | // entirely.
|
---|
60 | for(int i = 0; i < areas.size(); i++) {
|
---|
61 | for(int j = i+1; j < areas.size(); j++) {
|
---|
62 | areas.get(i).subtract(areas.get(j));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | for(int i = areas.size()-1; i > 0 ; i--) {
|
---|
67 | for(int j = i-1; j > 0; j--) {
|
---|
68 | areas.get(i).subtract(areas.get(j));
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | for(Area a : areas) {
|
---|
73 | if(a.isEmpty()) {
|
---|
74 | continue;
|
---|
75 | }
|
---|
76 | bboxCount++;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if(bboxCount == 0) {
|
---|
80 | JOptionPane.showMessageDialog(
|
---|
81 | Main.parent,
|
---|
82 | tr("No data to update found. Have you already opened or downloaded a data layer?"),
|
---|
83 | tr("No data"),
|
---|
84 | JOptionPane.WARNING_MESSAGE
|
---|
85 | );
|
---|
86 | return;
|
---|
87 | }
|
---|
88 |
|
---|
89 | new DownloadOsmTaskList().download(false, areas);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* ---------------------------------------------------------------------------------- */
|
---|
93 | /* Interface LayerChangeListener */
|
---|
94 | /* ---------------------------------------------------------------------------------- */
|
---|
95 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
96 | refreshEnabled();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void layerAdded(Layer newLayer) {
|
---|
100 | refreshEnabled();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void layerRemoved(Layer oldLayer) {
|
---|
104 | refreshEnabled();
|
---|
105 | }
|
---|
106 | }
|
---|