source: josm/trunk/src/org/openstreetmap/josm/gui/io/importexport/WMSLayerImporter.java@ 12851

Last change on this file since 12851 was 12851, checked in by bastiK, 7 years ago

see #15229 - extract "struct" handling from Preference to StructUtils

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io.importexport;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.IOException;
9import java.io.InvalidClassException;
10import java.io.ObjectInputStream;
11import java.util.Map;
12
13import org.openstreetmap.josm.actions.ExtensionFileFilter;
14import org.openstreetmap.josm.data.StructUtils;
15import org.openstreetmap.josm.data.coor.EastNorth;
16import org.openstreetmap.josm.data.imagery.ImageryInfo;
17import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
18import org.openstreetmap.josm.gui.MainApplication;
19import org.openstreetmap.josm.gui.layer.ImageryLayer;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.openstreetmap.josm.gui.util.GuiHelper;
22import org.openstreetmap.josm.io.IllegalDataException;
23import org.openstreetmap.josm.tools.CheckParameterUtil;
24
25/**
26 * Import a WMS layer from a serialized binary file previously exported via {@link WMSLayerExporter}.
27 * @since 5457
28 */
29public class WMSLayerImporter extends FileImporter {
30
31 /**
32 * The file filter used in "open" and "save" dialogs for WMS layers.
33 */
34 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
35 "wms", "wms", tr("WMS Files (*.wms)"));
36
37 /**
38 * Constructs a new {@code WMSLayerImporter}.
39 */
40 public WMSLayerImporter() {
41 super(FILE_FILTER);
42 }
43
44 @Override
45 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
46 CheckParameterUtil.ensureParameterNotNull(file, "file");
47 final EastNorth zoomTo;
48 ImageryInfo info = null;
49 final ImageryLayer layer;
50
51 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
52 int sfv = ois.readInt();
53 if (sfv < 5) {
54 throw new InvalidClassException(tr("Unsupported WMS file version; found {0}, expected {1}", sfv, 5));
55 } else if (sfv == 5) {
56 ois.readInt(); // dax - not needed
57 ois.readInt(); // day - not needed
58 zoomTo = null;
59
60 int imageSize = ois.readInt();
61 double pixelPerDegree = ois.readDouble();
62
63 String name = (String) ois.readObject();
64 String extendedUrl = (String) ois.readObject();
65
66 info = new ImageryInfo(name);
67 info.setExtendedUrl(extendedUrl);
68 info.setPixelPerDegree(pixelPerDegree);
69 info.setTileSize(imageSize);
70 } else if (sfv == WMSLayerExporter.CURRENT_FILE_VERSION) {
71 zoomTo = (EastNorth) ois.readObject();
72
73 @SuppressWarnings("unchecked")
74 ImageryPreferenceEntry entry = StructUtils.deserializeStruct(
75 (Map<String, String>) ois.readObject(),
76 ImageryPreferenceEntry.class);
77 info = new ImageryInfo(entry);
78 } else {
79 throw new InvalidClassException(tr("Unsupported WMS file version; found {0}, expected {1}", sfv, 6));
80 }
81 } catch (ClassNotFoundException e) {
82 throw new IllegalDataException(e);
83 }
84 layer = ImageryLayer.create(info);
85
86
87 // FIXME: remove UI stuff from IO subsystem
88 GuiHelper.runInEDT(() -> {
89 MainApplication.getLayerManager().addLayer(layer);
90 if (zoomTo != null) {
91 MainApplication.getMap().mapView.zoomTo(zoomTo);
92 }
93 });
94 }
95}
Note: See TracBrowser for help on using the repository browser.