[12633] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.GraphicsEnvironment;
|
---|
| 7 | import java.io.File;
|
---|
| 8 | import java.net.URI;
|
---|
| 9 | import java.net.URISyntaxException;
|
---|
| 10 | import java.util.Collection;
|
---|
| 11 | import java.util.Collections;
|
---|
| 12 | import java.util.List;
|
---|
| 13 | import java.util.StringTokenizer;
|
---|
| 14 | import java.util.concurrent.Future;
|
---|
| 15 |
|
---|
| 16 | import javax.swing.JOptionPane;
|
---|
| 17 |
|
---|
| 18 | import org.openstreetmap.josm.Main;
|
---|
| 19 | import org.openstreetmap.josm.actions.OpenLocationAction;
|
---|
| 20 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 22 | import org.openstreetmap.josm.tools.Logging;
|
---|
| 23 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * The type of a command line parameter, to be used in switch statements.
|
---|
| 27 | * @since 12633 (extracted from {@code Main})
|
---|
| 28 | */
|
---|
| 29 | public enum DownloadParamType {
|
---|
| 30 | /** http(s):// URL */
|
---|
| 31 | httpUrl {
|
---|
| 32 | @Override
|
---|
| 33 | public List<Future<?>> download(String s, Collection<File> fileList) {
|
---|
| 34 | return new OpenLocationAction().openUrl(false, s);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @Override
|
---|
| 38 | public List<Future<?>> downloadGps(String s) {
|
---|
| 39 | final Bounds b = OsmUrlToBounds.parse(s);
|
---|
| 40 | if (b == null) {
|
---|
| 41 | JOptionPane.showMessageDialog(
|
---|
| 42 | Main.parent,
|
---|
| 43 | tr("Ignoring malformed URL: \"{0}\"", s),
|
---|
| 44 | tr("Warning"),
|
---|
| 45 | JOptionPane.WARNING_MESSAGE
|
---|
| 46 | );
|
---|
| 47 | return Collections.emptyList();
|
---|
| 48 | }
|
---|
| 49 | return MainApplication.downloadFromParamBounds(true, b);
|
---|
| 50 | }
|
---|
| 51 | },
|
---|
| 52 | /** file:// URL */
|
---|
| 53 | fileUrl {
|
---|
| 54 | @Override
|
---|
| 55 | public List<Future<?>> download(String s, Collection<File> fileList) {
|
---|
| 56 | File f = null;
|
---|
| 57 | try {
|
---|
| 58 | f = new File(new URI(s));
|
---|
| 59 | } catch (URISyntaxException e) {
|
---|
| 60 | Logging.warn(e);
|
---|
| 61 | JOptionPane.showMessageDialog(
|
---|
| 62 | Main.parent,
|
---|
| 63 | tr("Ignoring malformed file URL: \"{0}\"", s),
|
---|
| 64 | tr("Warning"),
|
---|
| 65 | JOptionPane.WARNING_MESSAGE
|
---|
| 66 | );
|
---|
| 67 | }
|
---|
| 68 | if (f != null) {
|
---|
| 69 | fileList.add(f);
|
---|
| 70 | }
|
---|
| 71 | return Collections.emptyList();
|
---|
| 72 | }
|
---|
| 73 | },
|
---|
| 74 | /** geographic area */
|
---|
| 75 | bounds {
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Download area specified on the command line as bounds string.
|
---|
| 79 | * @param rawGps Flag to download raw GPS tracks
|
---|
[13050] | 80 | * @param s The bounds parameter. Coordinates must use dot decimal separator as comma is used to delimit values
|
---|
[12633] | 81 | * @return the complete download task (including post-download handler), or {@code null}
|
---|
| 82 | */
|
---|
| 83 | private List<Future<?>> downloadFromParamBounds(final boolean rawGps, String s) {
|
---|
| 84 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
| 85 | if (st.countTokens() == 4) {
|
---|
| 86 | return MainApplication.downloadFromParamBounds(rawGps, new Bounds(
|
---|
| 87 | new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())),
|
---|
| 88 | new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()))
|
---|
| 89 | ));
|
---|
| 90 | }
|
---|
| 91 | return Collections.emptyList();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @Override
|
---|
| 95 | public List<Future<?>> download(String param, Collection<File> fileList) {
|
---|
| 96 | return downloadFromParamBounds(false, param);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | @Override
|
---|
| 100 | public List<Future<?>> downloadGps(String param) {
|
---|
| 101 | return downloadFromParamBounds(true, param);
|
---|
| 102 | }
|
---|
| 103 | },
|
---|
| 104 | /** local file name */
|
---|
| 105 | fileName {
|
---|
| 106 | @Override
|
---|
| 107 | public List<Future<?>> download(String s, Collection<File> fileList) {
|
---|
| 108 | fileList.add(new File(s));
|
---|
| 109 | return Collections.emptyList();
|
---|
| 110 | }
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * Performs the download
|
---|
| 115 | * @param param represents the object to be downloaded
|
---|
| 116 | * @param fileList files which shall be opened, should be added to this collection
|
---|
| 117 | * @return the download task, or {@code null}
|
---|
| 118 | */
|
---|
| 119 | public abstract List<Future<?>> download(String param, Collection<File> fileList);
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * Performs the GPS download
|
---|
| 123 | * @param param represents the object to be downloaded
|
---|
| 124 | * @return the download task, or {@code null}
|
---|
| 125 | */
|
---|
| 126 | public List<Future<?>> downloadGps(String param) {
|
---|
| 127 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
| 128 | JOptionPane.showMessageDialog(
|
---|
| 129 | Main.parent,
|
---|
| 130 | tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
|
---|
| 131 | tr("Warning"),
|
---|
| 132 | JOptionPane.WARNING_MESSAGE
|
---|
| 133 | );
|
---|
| 134 | }
|
---|
| 135 | return Collections.emptyList();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
|
---|
| 140 | *
|
---|
| 141 | * @param s A parameter string
|
---|
| 142 | * @return The guessed parameter type
|
---|
| 143 | */
|
---|
| 144 | public static DownloadParamType paramType(String s) {
|
---|
| 145 | if (s.startsWith("http:") || s.startsWith("https:")) return DownloadParamType.httpUrl;
|
---|
| 146 | if (s.startsWith("file:")) return DownloadParamType.fileUrl;
|
---|
| 147 | String coorPattern = "\\s*[+-]?[0-9]+(\\.[0-9]+)?\\s*";
|
---|
| 148 | if (s.matches(coorPattern + "(," + coorPattern + "){3}")) return DownloadParamType.bounds;
|
---|
| 149 | // everything else must be a file name
|
---|
| 150 | return DownloadParamType.fileName;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|