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

Last change on this file since 7417 was 7392, checked in by Don-vip, 10 years ago

code cleanup, javadoc update

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