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 |
|
---|
20 | public class OsmImporter extends FileImporter {
|
---|
21 |
|
---|
22 | public OsmImporter() {
|
---|
23 | super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
|
---|
24 | }
|
---|
25 |
|
---|
26 | public OsmImporter(ExtensionFileFilter filter) {
|
---|
27 | super(filter);
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override public void importData(File file) throws IOException, IllegalDataException {
|
---|
31 | try {
|
---|
32 | FileInputStream in = new FileInputStream(file);
|
---|
33 | importData(in, file);
|
---|
34 | } catch (FileNotFoundException e) {
|
---|
35 | e.printStackTrace();
|
---|
36 | throw new IOException(tr("File ''{0}'' does not exist.", file.getName()));
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected void importData(InputStream in, File associatedFile) throws IllegalDataException {
|
---|
41 | DataSet dataSet = OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
42 | final OsmDataLayer layer = new OsmDataLayer(dataSet, associatedFile.getName(), associatedFile);
|
---|
43 | // FIXME: remove UI stuff from IO subsystem
|
---|
44 | //
|
---|
45 | Runnable uiStuff = new Runnable() {
|
---|
46 | public void run() {
|
---|
47 | Main.main.addLayer(layer);
|
---|
48 | layer.fireDataChange();
|
---|
49 | layer.onPostLoadFromFile();
|
---|
50 | }
|
---|
51 | };
|
---|
52 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
53 | uiStuff.run();
|
---|
54 | } else {
|
---|
55 | SwingUtilities.invokeLater(uiStuff);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|