source: josm/trunk/src/org/openstreetmap/josm/actions/DownloadAlongAction.java@ 12675

Last change on this file since 12675 was 12675, checked in by Don-vip, 7 years ago

see #15182 - move the Swing-based ProgressMonitor implementations from gui.progress to gui.progress.swing. Progress monitor concept is used in very large parts of JOSM, a console-based implementation could be added later

  • Property svn:eol-style set to native
File size: 5.9 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.GraphicsEnvironment;
7import java.awt.GridBagLayout;
8import java.awt.geom.Area;
9import java.awt.geom.Rectangle2D;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.List;
13import java.util.concurrent.Future;
14
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
21import org.openstreetmap.josm.gui.progress.ProgressMonitor;
22import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
23import org.openstreetmap.josm.tools.GBC;
24import org.openstreetmap.josm.tools.Shortcut;
25
26/**
27 * Abstract superclass of DownloadAlongTrackAction and DownloadAlongWayAction
28 * @since 6054
29 */
30public abstract class DownloadAlongAction extends JosmAction {
31
32 /**
33 * Constructs a new {@code DownloadAlongAction}
34 * @param name the action's text as displayed in the menu
35 * @param iconName the filename of the icon to use
36 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
37 * that html is not supported for menu actions on some platforms.
38 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
39 * do want a shortcut, remember you can always register it with group=none, so you
40 * won't be assigned a shortcut unless the user configures one. If you pass null here,
41 * the user CANNOT configure a shortcut for your action.
42 * @param registerInToolbar register this action for the toolbar preferences?
43 */
44 public DownloadAlongAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
45 super(name, iconName, tooltip, shortcut, registerInToolbar);
46 }
47
48 protected static void addToDownload(Area a, Rectangle2D r, Collection<Rectangle2D> results, double maxArea) {
49 Area tmp = new Area(r);
50 // intersect with sought-after area
51 tmp.intersect(a);
52 if (tmp.isEmpty()) {
53 return;
54 }
55 Rectangle2D bounds = tmp.getBounds2D();
56 if (bounds.getWidth() * bounds.getHeight() > maxArea) {
57 // the rectangle gets too large; split it and make recursive call.
58 Rectangle2D r1;
59 Rectangle2D r2;
60 if (bounds.getWidth() > bounds.getHeight()) {
61 // rectangles that are wider than high are split into a left and right half,
62 r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth() / 2, bounds.getHeight());
63 r2 = new Rectangle2D.Double(bounds.getX() + bounds.getWidth() / 2, bounds.getY(),
64 bounds.getWidth() / 2, bounds.getHeight());
65 } else {
66 // others into a top and bottom half.
67 r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() / 2);
68 r2 = new Rectangle2D.Double(bounds.getX(), bounds.getY() + bounds.getHeight() / 2, bounds.getWidth(),
69 bounds.getHeight() / 2);
70 }
71 addToDownload(a, r1, results, maxArea);
72 addToDownload(a, r2, results, maxArea);
73 } else {
74 results.add(bounds);
75 }
76 }
77
78 /**
79 * Area "a" contains the hull that we would like to download data for. however we
80 * can only download rectangles, so the following is an attempt at finding a number of
81 * rectangles to download.
82 *
83 * The idea is simply: Start out with the full bounding box. If it is too large, then
84 * split it in half and repeat recursively for each half until you arrive at something
85 * small enough to download. The algorithm is improved by always using the intersection
86 * between the rectangle and the actual desired area. For example, if you have a track
87 * that goes like this: +----+ | /| | / | | / | |/ | +----+ then we would first look at
88 * downloading the whole rectangle (assume it's too big), after that we split it in half
89 * (upper and lower half), but we donot request the full upper and lower rectangle, only
90 * the part of the upper/lower rectangle that actually has something in it.
91 *
92 * This functions calculates the rectangles, asks the user to continue and downloads
93 * the areas if applicable.
94 *
95 * @param a download area hull
96 * @param maxArea maximum area size for a single download
97 * @param osmDownload Set to true if OSM data should be downloaded
98 * @param gpxDownload Set to true if GPX data should be downloaded
99 * @param title the title string for the confirmation dialog
100 * @param progressMonitor the progress monitor
101 */
102 protected static void confirmAndDownloadAreas(Area a, double maxArea, boolean osmDownload, boolean gpxDownload, String title,
103 ProgressMonitor progressMonitor) {
104 List<Rectangle2D> toDownload = new ArrayList<>();
105 addToDownload(a, a.getBounds(), toDownload, maxArea);
106 if (toDownload.isEmpty()) {
107 return;
108 }
109 JPanel msg = new JPanel(new GridBagLayout());
110 msg.add(new JLabel(
111 tr("<html>This action will require {0} individual<br>" + "download requests. Do you wish<br>to continue?</html>",
112 toDownload.size())), GBC.eol());
113 if (!GraphicsEnvironment.isHeadless() && JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(
114 Main.parent, msg, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
115 return;
116 }
117 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
118 final Future<?> future = new DownloadTaskList().download(false, toDownload, osmDownload, gpxDownload, monitor);
119 waitFuture(future, monitor);
120 }
121}
Note: See TracBrowser for help on using the repository browser.