[4533] | 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.JOptionPane;
|
---|
| 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;
|
---|
[5452] | 20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[4533] | 21 |
|
---|
| 22 | public class OsmChangeImporter extends FileImporter {
|
---|
| 23 |
|
---|
[5361] | 24 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
| 25 | "osc,osc.bz2,osc.bz,osc.gz", "osc", tr("OsmChange File") + " (*.osc *.osc.bz2 *.osc.bz *.osc.gz)");
|
---|
| 26 |
|
---|
[4533] | 27 | public OsmChangeImporter() {
|
---|
[5361] | 28 | super(FILE_FILTER);
|
---|
[4533] | 29 | }
|
---|
| 30 |
|
---|
| 31 | public OsmChangeImporter(ExtensionFileFilter filter) {
|
---|
| 32 | super(filter);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
| 36 | try {
|
---|
| 37 | FileInputStream in = new FileInputStream(file);
|
---|
[5361] | 38 |
|
---|
| 39 | if (file.getName().endsWith(".osc")) {
|
---|
| 40 | importData(in, file);
|
---|
| 41 | } else if (file.getName().endsWith(".gz")) {
|
---|
| 42 | importData(getGZipInputStream(in), file);
|
---|
| 43 | } else {
|
---|
| 44 | importData(getBZip2InputStream(in), file);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[4533] | 47 | } catch (FileNotFoundException e) {
|
---|
| 48 | e.printStackTrace();
|
---|
| 49 | throw new IOException(tr("File ''{0}'' does not exist.", file.getName()));
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | protected void importData(InputStream in, final File associatedFile) throws IllegalDataException {
|
---|
| 54 | final DataSet dataSet = OsmChangeReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
| 55 | final OsmDataLayer layer = new OsmDataLayer(dataSet, associatedFile.getName(), associatedFile);
|
---|
| 56 | addDataLayer(dataSet, layer, associatedFile.getPath());
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | protected void addDataLayer(final DataSet dataSet, final OsmDataLayer layer, final String filePath) {
|
---|
| 60 | // FIXME: remove UI stuff from IO subsystem
|
---|
| 61 | //
|
---|
[5452] | 62 | GuiHelper.runInEDT(new Runnable() {
|
---|
[4533] | 63 | @Override
|
---|
| 64 | public void run() {
|
---|
| 65 | if (dataSet.allPrimitives().isEmpty()) {
|
---|
| 66 | JOptionPane.showMessageDialog(
|
---|
| 67 | Main.parent,
|
---|
| 68 | tr("No data found in file {0}.", filePath),
|
---|
| 69 | tr("Open OsmChange file"),
|
---|
| 70 | JOptionPane.INFORMATION_MESSAGE);
|
---|
| 71 | }
|
---|
| 72 | Main.main.addLayer(layer);
|
---|
| 73 | layer.onPostLoadFromFile();
|
---|
| 74 | }
|
---|
[5452] | 75 | });
|
---|
[4533] | 76 | }
|
---|
| 77 | }
|
---|