001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GraphicsEnvironment;
007import java.io.File;
008import java.net.URI;
009import java.net.URISyntaxException;
010import java.util.Collection;
011import java.util.Collections;
012import java.util.List;
013import java.util.StringTokenizer;
014import java.util.concurrent.Future;
015
016import javax.swing.JOptionPane;
017
018import org.openstreetmap.josm.Main;
019import org.openstreetmap.josm.actions.OpenLocationAction;
020import org.openstreetmap.josm.data.Bounds;
021import org.openstreetmap.josm.data.coor.LatLon;
022import org.openstreetmap.josm.tools.Logging;
023import org.openstreetmap.josm.tools.OsmUrlToBounds;
024
025/**
026 * The type of a command line parameter, to be used in switch statements.
027 * @since 12633 (extracted from {@code Main})
028 */
029public enum DownloadParamType {
030    /** http(s):// URL */
031    httpUrl {
032        @Override
033        public List<Future<?>> download(String s, Collection<File> fileList) {
034            return new OpenLocationAction().openUrl(false, s);
035        }
036
037        @Override
038        public List<Future<?>> downloadGps(String s) {
039            final Bounds b = OsmUrlToBounds.parse(s);
040            if (b == null) {
041                JOptionPane.showMessageDialog(
042                        Main.parent,
043                        tr("Ignoring malformed URL: \"{0}\"", s),
044                        tr("Warning"),
045                        JOptionPane.WARNING_MESSAGE
046                );
047                return Collections.emptyList();
048            }
049            return MainApplication.downloadFromParamBounds(true, b);
050        }
051    },
052    /** file:// URL */
053    fileUrl {
054        @Override
055        public List<Future<?>> download(String s, Collection<File> fileList) {
056            File f = null;
057            try {
058                f = new File(new URI(s));
059            } catch (URISyntaxException e) {
060                Logging.warn(e);
061                JOptionPane.showMessageDialog(
062                        Main.parent,
063                        tr("Ignoring malformed file URL: \"{0}\"", s),
064                        tr("Warning"),
065                        JOptionPane.WARNING_MESSAGE
066                );
067            }
068            if (f != null) {
069                fileList.add(f);
070            }
071            return Collections.emptyList();
072        }
073    },
074    /** geographic area */
075    bounds {
076
077        /**
078         * Download area specified on the command line as bounds string.
079         * @param rawGps Flag to download raw GPS tracks
080         * @param s The bounds parameter. Coordinates must use dot decimal separator as comma is used to delimit values
081         * @return the complete download task (including post-download handler), or {@code null}
082         */
083        private List<Future<?>> downloadFromParamBounds(final boolean rawGps, String s) {
084            final StringTokenizer st = new StringTokenizer(s, ",");
085            if (st.countTokens() == 4) {
086                return MainApplication.downloadFromParamBounds(rawGps, new Bounds(
087                        new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())),
088                        new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()))
089                ));
090            }
091            return Collections.emptyList();
092        }
093
094        @Override
095        public List<Future<?>> download(String param, Collection<File> fileList) {
096            return downloadFromParamBounds(false, param);
097        }
098
099        @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}