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