[7937] | 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 |
|
---|
[9804] | 6 | import java.awt.GraphicsEnvironment;
|
---|
[7937] | 7 | import java.awt.GridBagLayout;
|
---|
| 8 | import java.awt.geom.Area;
|
---|
| 9 | import java.awt.geom.Rectangle2D;
|
---|
| 10 | import java.util.ArrayList;
|
---|
| 11 | import java.util.Collection;
|
---|
| 12 | import java.util.List;
|
---|
| 13 | import java.util.concurrent.Future;
|
---|
| 14 |
|
---|
| 15 | import javax.swing.JLabel;
|
---|
| 16 | import javax.swing.JOptionPane;
|
---|
| 17 | import javax.swing.JPanel;
|
---|
| 18 |
|
---|
| 19 | import org.openstreetmap.josm.Main;
|
---|
| 20 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
|
---|
| 21 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
| 22 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 23 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * Abstract superclass of DownloadAlongTrackAction and DownloadAlongWayAction
|
---|
| 28 | * @since 6054
|
---|
| 29 | */
|
---|
| 30 | public 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 | }
|
---|
[8509] | 47 |
|
---|
[7937] | 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 | }
|
---|
[8509] | 77 |
|
---|
[7937] | 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.
|
---|
[8926] | 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
|
---|
[7937] | 101 | */
|
---|
[8509] | 102 | protected static void confirmAndDownloadAreas(Area a, double maxArea, boolean osmDownload, boolean gpxDownload, String title,
|
---|
| 103 | ProgressMonitor progressMonitor) {
|
---|
[7937] | 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());
|
---|
[8509] | 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());
|
---|
[9804] | 113 | if (!GraphicsEnvironment.isHeadless() && JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(
|
---|
| 114 | Main.parent, msg, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
|
---|
[7937] | 115 | return;
|
---|
| 116 | }
|
---|
| 117 | final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
|
---|
| 118 | final Future<?> future = new DownloadTaskList().download(false, toDownload, osmDownload, gpxDownload, monitor);
|
---|
[10074] | 119 | waitFuture(future, monitor);
|
---|
[7937] | 120 | }
|
---|
| 121 | }
|
---|