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