1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileInputStream;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 |
|
---|
12 | import javax.swing.SwingUtilities;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
18 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 |
|
---|
21 | public class OsmImporter extends FileImporter {
|
---|
22 |
|
---|
23 | public OsmImporter() {
|
---|
24 | super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
|
---|
25 | }
|
---|
26 |
|
---|
27 | public OsmImporter(ExtensionFileFilter filter) {
|
---|
28 | super(filter);
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
32 | try {
|
---|
33 | FileInputStream in = new FileInputStream(file);
|
---|
34 | importData(in, file);
|
---|
35 | } catch (FileNotFoundException e) {
|
---|
36 | e.printStackTrace();
|
---|
37 | throw new IOException(tr("File ''{0}'' does not exist.", file.getName()));
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected void importData(InputStream in, File associatedFile) throws IllegalDataException {
|
---|
42 | DataSet dataSet = OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
43 | final OsmDataLayer layer = new OsmDataLayer(dataSet, associatedFile.getName(), associatedFile);
|
---|
44 | // FIXME: remove UI stuff from IO subsystem
|
---|
45 | //
|
---|
46 | Runnable uiStuff = new Runnable() {
|
---|
47 | public void run() {
|
---|
48 | Main.main.addLayer(layer);
|
---|
49 | layer.onPostLoadFromFile();
|
---|
50 | }
|
---|
51 | };
|
---|
52 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
53 | uiStuff.run();
|
---|
54 | } else {
|
---|
55 | SwingUtilities.invokeLater(uiStuff);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|