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

Last change on this file since 9462 was 8929, checked in by Don-vip, 8 years ago

javadoc fixes

  • Property svn:eol-style set to native
File size: 6.2 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.GridBagLayout;
7import java.awt.geom.Area;
8import java.awt.geom.Rectangle2D;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.List;
12import java.util.concurrent.Future;
13
14import javax.swing.JLabel;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
20import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
21import org.openstreetmap.josm.gui.progress.ProgressMonitor;
22import org.openstreetmap.josm.tools.GBC;
23import org.openstreetmap.josm.tools.Shortcut;
24
25/**
26 * Abstract superclass of DownloadAlongTrackAction and DownloadAlongWayAction
27 * @since 6054
28 */
29public 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 maxArea) {
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() > maxArea) {
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, maxArea);
71 addToDownload(a, r2, results, maxArea);
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 * @param a download area hull
95 * @param maxArea maximum area size for a single download
96 * @param osmDownload Set to true if OSM data should be downloaded
97 * @param gpxDownload Set to true if GPX data should be downloaded
98 * @param title the title string for the confirmation dialog
99 * @param progressMonitor the progress monitor
100 */
101 protected static void confirmAndDownloadAreas(Area a, double maxArea, boolean osmDownload, boolean gpxDownload, String title,
102 ProgressMonitor progressMonitor) {
103 List<Rectangle2D> toDownload = new ArrayList<>();
104 addToDownload(a, a.getBounds(), toDownload, maxArea);
105 if (toDownload.isEmpty()) {
106 return;
107 }
108 JPanel msg = new JPanel(new GridBagLayout());
109 msg.add(new JLabel(
110 tr("<html>This action will require {0} individual<br>" + "download requests. Do you wish<br>to continue?</html>",
111 toDownload.size())), GBC.eol());
112 if (JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(Main.parent, msg, title,
113 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
114 return;
115 }
116 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
117 final Future<?> future = new DownloadTaskList().download(false, toDownload, osmDownload, gpxDownload, monitor);
118 Main.worker.submit(new Runnable() {
119 @Override
120 public void run() {
121 try {
122 future.get();
123 } catch (Exception e) {
124 Main.error(e);
125 return;
126 }
127 monitor.close();
128 }
129 });
130 }
131}
Note: See TracBrowser for help on using the repository browser.