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

Last change on this file since 8926 was 8926, checked in by Don-vip, 9 years ago

javadoc fixes

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