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

Last change on this file since 1808 was 1808, checked in by Gubaer, 15 years ago

improved enabling/disabling of menu entries and action buttons depending on current state of JOSM (number of open layers, type of active layer, etc.)

File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.awt.geom.Area;
9import java.util.ArrayList;
10import java.util.List;
11
12import javax.swing.JOptionPane;
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.Layer;
18import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
19import org.openstreetmap.josm.tools.Shortcut;
20
21public 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}
Note: See TracBrowser for help on using the repository browser.