1 | // JOSM opendata plugin.
|
---|
2 | // Copyright (C) 2011-2012 Don-vip
|
---|
3 | //
|
---|
4 | // This program is free software: you can redistribute it and/or modify
|
---|
5 | // it under the terms of the GNU General Public License as published by
|
---|
6 | // the Free Software Foundation, either version 3 of the License, or
|
---|
7 | // (at your option) any later version.
|
---|
8 | //
|
---|
9 | // This program is distributed in the hope that it will be useful,
|
---|
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | // GNU General Public License for more details.
|
---|
13 | //
|
---|
14 | // You should have received a copy of the GNU General Public License
|
---|
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
16 | package org.openstreetmap.josm.plugins.opendata.core.actions;
|
---|
17 |
|
---|
18 | import java.io.File;
|
---|
19 | import java.util.concurrent.Future;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
25 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
26 | import org.openstreetmap.josm.io.AbstractReader;
|
---|
27 | import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler;
|
---|
28 | import org.openstreetmap.josm.plugins.opendata.core.datasets.DataSetUpdater;
|
---|
29 | import org.openstreetmap.josm.plugins.opendata.core.io.NetworkReader;
|
---|
30 | import org.openstreetmap.josm.plugins.opendata.core.layers.OdDataLayer;
|
---|
31 | import org.openstreetmap.josm.plugins.opendata.core.modules.Module;
|
---|
32 | import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleHandler;
|
---|
33 |
|
---|
34 | public class DownloadDataTask extends DownloadOsmTask {
|
---|
35 |
|
---|
36 | private AbstractDataSetHandler handler;
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
45 | Class<? extends AbstractReader> readerClass = null; // TODO
|
---|
46 | downloadTask = new InternDownloadTasK(newLayer, new NetworkReader(url, handler, readerClass), progressMonitor);
|
---|
47 | currentBounds = null;
|
---|
48 | // Extract .osm filename from URL to set the new layer name
|
---|
49 | //Matcher matcher = Pattern.compile("http://.*/(.*\\.osm)").matcher(url);
|
---|
50 | //newLayerName = matcher.matches() ? matcher.group(1) : null;
|
---|
51 | return Main.worker.submit(downloadTask);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public boolean acceptsUrl(String url) {
|
---|
56 | for (Module module : ModuleHandler.moduleList) {
|
---|
57 | for (AbstractDataSetHandler handler : module.getHandlers()) {
|
---|
58 | if (handler.acceptsUrl(url)) {
|
---|
59 | this.handler = handler;
|
---|
60 | return true;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected class InternDownloadTasK extends DownloadTask {
|
---|
68 |
|
---|
69 | public InternDownloadTasK(boolean newLayer, NetworkReader reader, ProgressMonitor progressMonitor) {
|
---|
70 | super(newLayer, reader, progressMonitor);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /* (non-Javadoc)
|
---|
74 | * @see org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask.DownloadTask#createNewLayer(java.lang.String)
|
---|
75 | */
|
---|
76 | @Override
|
---|
77 | protected OsmDataLayer createNewLayer(String layerName) {
|
---|
78 | File associatedFile = ((NetworkReader)reader).getReadFile();
|
---|
79 | String filename = ((NetworkReader)reader).getReadFileName();
|
---|
80 | if (layerName == null || layerName.isEmpty()) {
|
---|
81 | if (associatedFile != null) {
|
---|
82 | layerName = associatedFile.getName();
|
---|
83 | } else if (filename != null && !filename.isEmpty()) {
|
---|
84 | layerName = filename;
|
---|
85 | } else {
|
---|
86 | layerName = OsmDataLayer.createNewName();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | DataSetUpdater.updateDataSet(dataSet, handler, associatedFile);
|
---|
90 | return new OdDataLayer(dataSet, layerName, associatedFile, handler);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|