001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.GraphicsEnvironment; 007import java.awt.GridBagLayout; 008import java.awt.geom.Area; 009import java.awt.geom.Rectangle2D; 010import java.util.ArrayList; 011import java.util.Collection; 012import java.util.List; 013import java.util.concurrent.Future; 014 015import javax.swing.JLabel; 016import javax.swing.JOptionPane; 017import javax.swing.JPanel; 018 019import org.openstreetmap.josm.Main; 020import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList; 021import org.openstreetmap.josm.gui.progress.ProgressMonitor; 022import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor; 023import org.openstreetmap.josm.tools.GBC; 024import org.openstreetmap.josm.tools.Shortcut; 025 026/** 027 * Abstract superclass of DownloadAlongTrackAction and DownloadAlongWayAction 028 * @since 6054 029 */ 030public abstract class DownloadAlongAction extends JosmAction { 031 032 /** 033 * Constructs a new {@code DownloadAlongAction} 034 * @param name the action's text as displayed in the menu 035 * @param iconName the filename of the icon to use 036 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note 037 * that html is not supported for menu actions on some platforms. 038 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always 039 * do want a shortcut, remember you can always register it with group=none, so you 040 * won't be assigned a shortcut unless the user configures one. If you pass null here, 041 * the user CANNOT configure a shortcut for your action. 042 * @param registerInToolbar register this action for the toolbar preferences? 043 */ 044 public DownloadAlongAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) { 045 super(name, iconName, tooltip, shortcut, registerInToolbar); 046 } 047 048 protected static void addToDownload(Area a, Rectangle2D r, Collection<Rectangle2D> results, double maxArea) { 049 Area tmp = new Area(r); 050 // intersect with sought-after area 051 tmp.intersect(a); 052 if (tmp.isEmpty()) { 053 return; 054 } 055 Rectangle2D bounds = tmp.getBounds2D(); 056 if (bounds.getWidth() * bounds.getHeight() > maxArea) { 057 // the rectangle gets too large; split it and make recursive call. 058 Rectangle2D r1; 059 Rectangle2D r2; 060 if (bounds.getWidth() > bounds.getHeight()) { 061 // rectangles that are wider than high are split into a left and right half, 062 r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth() / 2, bounds.getHeight()); 063 r2 = new Rectangle2D.Double(bounds.getX() + bounds.getWidth() / 2, bounds.getY(), 064 bounds.getWidth() / 2, bounds.getHeight()); 065 } else { 066 // others into a top and bottom half. 067 r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() / 2); 068 r2 = new Rectangle2D.Double(bounds.getX(), bounds.getY() + bounds.getHeight() / 2, bounds.getWidth(), 069 bounds.getHeight() / 2); 070 } 071 addToDownload(a, r1, results, maxArea); 072 addToDownload(a, r2, results, maxArea); 073 } else { 074 results.add(bounds); 075 } 076 } 077 078 /** 079 * Area "a" contains the hull that we would like to download data for. however we 080 * can only download rectangles, so the following is an attempt at finding a number of 081 * rectangles to download. 082 * 083 * The idea is simply: Start out with the full bounding box. If it is too large, then 084 * split it in half and repeat recursively for each half until you arrive at something 085 * small enough to download. The algorithm is improved by always using the intersection 086 * between the rectangle and the actual desired area. For example, if you have a track 087 * that goes like this: +----+ | /| | / | | / | |/ | +----+ then we would first look at 088 * downloading the whole rectangle (assume it's too big), after that we split it in half 089 * (upper and lower half), but we donot request the full upper and lower rectangle, only 090 * the part of the upper/lower rectangle that actually has something in it. 091 * 092 * This functions calculates the rectangles, asks the user to continue and downloads 093 * the areas if applicable. 094 * 095 * @param a download area hull 096 * @param maxArea maximum area size for a single download 097 * @param osmDownload Set to true if OSM data should be downloaded 098 * @param gpxDownload Set to true if GPX data should be downloaded 099 * @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}