1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.Preferences;
|
---|
11 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
12 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
|
---|
13 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
14 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
15 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
18 | import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
|
---|
19 | import org.w3c.dom.Element;
|
---|
20 | import org.w3c.dom.Node;
|
---|
21 | import org.w3c.dom.NodeList;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Session importer for both TMSLayer and WMSLayer.
|
---|
25 | */
|
---|
26 | public class ImagerySessionImporter implements SessionLayerImporter {
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
30 | String version = elem.getAttribute("version");
|
---|
31 | if (!"0.1".equals(version)) {
|
---|
32 | throw new IllegalDataException(tr("Version ''{0}'' of meta data for imagery layer is not supported. Expected: 0.1", version));
|
---|
33 | }
|
---|
34 | Map<String, String> attributes = new HashMap<>();
|
---|
35 |
|
---|
36 | NodeList nodes = elem.getChildNodes();
|
---|
37 |
|
---|
38 | for (int i=0; i<nodes.getLength(); ++i) {
|
---|
39 | Node node = nodes.item(i);
|
---|
40 | if (node.getNodeType() == Node.ELEMENT_NODE) {
|
---|
41 | Element e = (Element) node;
|
---|
42 | attributes.put(e.getTagName(), e.getTextContent());
|
---|
43 | }
|
---|
44 | }
|
---|
45 | ImageryPreferenceEntry prefEntry = Preferences.deserializeStruct(attributes, ImageryPreferenceEntry.class);
|
---|
46 | ImageryInfo i = new ImageryInfo(prefEntry);
|
---|
47 | ImageryLayer layer = ImageryLayer.create(i);
|
---|
48 | if (layer instanceof WMSLayer) {
|
---|
49 | WMSLayer wms = (WMSLayer) layer;
|
---|
50 | String autoDownload = attributes.get("automatic-downloading");
|
---|
51 | if (autoDownload != null) {
|
---|
52 | wms.setAutoDownload(Boolean.parseBoolean(autoDownload));
|
---|
53 | }
|
---|
54 | String autoResolution = attributes.get("automatically-change-resolution");
|
---|
55 | if (autoResolution != null) {
|
---|
56 | wms.setAutoResolution(Boolean.parseBoolean(autoResolution));
|
---|
57 | }
|
---|
58 | }
|
---|
59 | return layer;
|
---|
60 | }
|
---|
61 | }
|
---|