source: josm/trunk/src/org/openstreetmap/josm/gui/DownloadParamType.java@ 14628

Last change on this file since 14628 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

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