source: josm/trunk/src/org/openstreetmap/josm/io/OsmImporter.java@ 10404

Last change on this file since 10404 was 10371, checked in by Don-vip, 8 years ago

fix #12944 - Reduce use of addLayer(..., bounds) - patch by michael2402 - gsoc-core

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.io.InputStream;
10import java.util.Arrays;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.ExtensionFileFilter;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20
21public class OsmImporter extends FileImporter {
22
23 /**
24 * The OSM file filter (*.osm and *.xml files).
25 */
26 public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
27 "osm,xml", "osm", tr("OSM Server Files") + " (*.osm, *.osm.gz, *.osm.bz2, *.osm.zip, *.xml)",
28 ExtensionFileFilter.AddArchiveExtension.NONE, Arrays.asList("gz", "bz", "bz2", "zip"));
29
30 /**
31 * Utility class containing imported OSM layer, and a task to run after it is added to MapView.
32 */
33 public static class OsmImporterData {
34
35 private final OsmDataLayer layer;
36 private final Runnable postLayerTask;
37
38 public OsmImporterData(OsmDataLayer layer, Runnable postLayerTask) {
39 this.layer = layer;
40 this.postLayerTask = postLayerTask;
41 }
42
43 public OsmDataLayer getLayer() {
44 return layer;
45 }
46
47 public Runnable getPostLayerTask() {
48 return postLayerTask;
49 }
50 }
51
52 /**
53 * Constructs a new {@code OsmImporter}.
54 */
55 public OsmImporter() {
56 super(FILE_FILTER);
57 }
58
59 /**
60 * Constructs a new {@code OsmImporter} with the given extension file filter.
61 * @param filter The extension file filter
62 */
63 public OsmImporter(ExtensionFileFilter filter) {
64 super(filter);
65 }
66
67 /**
68 * Imports OSM data from file
69 * @param file file to read data from
70 * @param progressMonitor handler for progress monitoring and canceling
71 */
72 @Override
73 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
74 try (InputStream in = Compression.getUncompressedFileInputStream(file)) {
75 importData(in, file, progressMonitor);
76 } catch (FileNotFoundException e) {
77 Main.error(e);
78 throw new IOException(tr("File ''{0}'' does not exist.", file.getName()), e);
79 }
80 }
81
82 /**
83 * Imports OSM data from stream
84 * @param in input stream
85 * @param associatedFile filename of data (layer name will be generated from name of file)
86 * @param pm handler for progress monitoring and canceling
87 * @throws IllegalDataException if an error was found while parsing the OSM data
88 */
89 protected void importData(InputStream in, final File associatedFile, ProgressMonitor pm) throws IllegalDataException {
90 final OsmImporterData data = loadLayer(in, associatedFile,
91 associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(), pm);
92
93 // FIXME: remove UI stuff from IO subsystem
94 GuiHelper.runInEDT(new Runnable() {
95 @Override
96 public void run() {
97 OsmDataLayer layer = data.getLayer();
98 Main.main.addLayer(layer);
99 data.getPostLayerTask().run();
100 data.getLayer().onPostLoadFromFile();
101 }
102 });
103 }
104
105 /**
106 * Load osm data layer from InputStream.
107 * @param in input stream
108 * @param associatedFile filename of data (can be <code>null</code> if the stream does not come from a file)
109 * @param layerName name of generated layer
110 * @param progressMonitor handler for progress monitoring and canceling
111 * @return Utility class containing imported OSM layer, and a task to run after it is added to MapView
112 * @throws IllegalDataException if an error was found while parsing the OSM data
113 */
114 public OsmImporterData loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor)
115 throws IllegalDataException {
116 final DataSet dataSet = parseDataSet(in, progressMonitor);
117 if (dataSet == null) {
118 throw new IllegalDataException(tr("Invalid dataset"));
119 }
120 OsmDataLayer layer = createLayer(dataSet, associatedFile, layerName);
121 Runnable postLayerTask = createPostLayerTask(dataSet, associatedFile, layerName, layer);
122 return new OsmImporterData(layer, postLayerTask);
123 }
124
125 protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException {
126 return OsmReader.parseDataSet(in, progressMonitor);
127 }
128
129 protected OsmDataLayer createLayer(final DataSet dataSet, final File associatedFile, final String layerName) {
130 return new OsmDataLayer(dataSet, layerName, associatedFile);
131 }
132
133 protected Runnable createPostLayerTask(final DataSet dataSet, final File associatedFile, final String layerName, final OsmDataLayer layer) {
134 return new Runnable() {
135 @Override
136 public void run() {
137 if (dataSet.allPrimitives().isEmpty()) {
138 String msg;
139 if (associatedFile == null) {
140 msg = tr("No data found for layer ''{0}''.", layerName);
141 } else {
142 msg = tr("No data found in file ''{0}''.", associatedFile.getPath());
143 }
144 JOptionPane.showMessageDialog(
145 Main.parent,
146 msg,
147 tr("Open OSM file"),
148 JOptionPane.INFORMATION_MESSAGE);
149 }
150 layer.onPostLoadFromFile();
151 }
152 };
153 }
154}
Note: See TracBrowser for help on using the repository browser.