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.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
13 | import org.openstreetmap.josm.data.notes.Note;
|
---|
14 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
17 | import org.xml.sax.SAXException;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * File importer that reads note dump files (*.osn and .osn.bz2)
|
---|
21 | * @since 7538
|
---|
22 | */
|
---|
23 | public class NoteImporter extends FileImporter {
|
---|
24 |
|
---|
25 | private static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
26 | "osn,osn.bz2", "osn", tr("Note Files") + " (*.osn *.osn.bz2)");
|
---|
27 |
|
---|
28 | /** Create an importer for note dump files */
|
---|
29 | public NoteImporter() {
|
---|
30 | super(FILE_FILTER);
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public void importData(final File file, ProgressMonitor progressMonitor) throws IOException {
|
---|
35 | if (Main.isDebugEnabled()) {
|
---|
36 | Main.debug("importing notes file " + file.getAbsolutePath());
|
---|
37 | }
|
---|
38 | try (InputStream is = Compression.getUncompressedFileInputStream(file)) {
|
---|
39 | final List<Note> fileNotes = new NoteReader(is).parse();
|
---|
40 |
|
---|
41 | List<NoteLayer> noteLayers = null;
|
---|
42 | if (Main.map != null) {
|
---|
43 | noteLayers = Main.map.mapView.getLayersOfType(NoteLayer.class);
|
---|
44 | }
|
---|
45 | if (noteLayers != null && !noteLayers.isEmpty()) {
|
---|
46 | noteLayers.get(0).getNoteData().addNotes(fileNotes);
|
---|
47 | } else {
|
---|
48 | GuiHelper.runInEDT(new Runnable() {
|
---|
49 | @Override
|
---|
50 | public void run() {
|
---|
51 | Main.main.addLayer(new NoteLayer(fileNotes, file.getName()));
|
---|
52 | }
|
---|
53 | });
|
---|
54 | }
|
---|
55 | } catch (SAXException e) {
|
---|
56 | Main.error("error opening up notes file");
|
---|
57 | Main.error(e, true);
|
---|
58 | throw new IOException(e.getMessage(), e);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|