Changeset 12134 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2017-05-13T12:58:12+02:00 (8 years ago)
Author:
bastiK
Message:

see #14773 - improve session export/import for imagery layer offset data

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/OffsetBookmark.java

    r12118 r12134  
    99import java.util.List;
    1010import java.util.ListIterator;
     11import java.util.Map;
    1112
    1213import org.openstreetmap.josm.Main;
     14import org.openstreetmap.josm.data.Preferences;
    1315import org.openstreetmap.josm.data.Preferences.pref;
    1416import org.openstreetmap.josm.data.Preferences.writeExplicitly;
     
    262264        saveBookmarks();
    263265    }
     266
     267    /**
     268     * Converts the offset bookmark to a properties map.
     269     *
     270     * The map contains all the information to restore the offset bookmark.
     271     * @return properties map of all data
     272     * @since 12134
     273     * @see #fromPropertiesMap(java.util.Map)
     274     */
     275    public Map<String, String> toPropertiesMap() {
     276        return Preferences.serializeStruct(this, OffsetBookmark.class);
     277    }
     278
     279    /**
     280     * Creates an offset bookmark from a properties map.
     281     * @param properties the properties map
     282     * @return corresponding offset bookmark
     283     * @since 12134
     284     * @see #toPropertiesMap()
     285     */
     286    public static OffsetBookmark fromPropertiesMap(Map<String, String> properties) {
     287        return Preferences.deserializeStruct(properties, OffsetBookmark.class);
     288    }
    264289}
  • trunk/src/org/openstreetmap/josm/gui/layer/imagery/TileSourceDisplaySettings.java

    r12117 r12134  
    248248    /**
    249249     * Stores the current settings object to the given hashmap.
     250     * The offset data is not stored and needs to be handled separately.
    250251     * @param data The map to store the settings to.
    251252     * @see #loadFrom(Map)
     253     * @see OffsetBookmark#toPropertiesMap()
    252254     */
    253255    public void storeTo(Map<String, String> data) {
     
    255257        data.put(AUTO_ZOOM, Boolean.toString(autoZoom));
    256258        data.put(SHOW_ERRORS, Boolean.toString(showErrors));
    257         data.put("dx", String.valueOf(getDx()));
    258         data.put("dy", String.valueOf(getDy()));
    259259    }
    260260
    261261    /**
    262262     * Load the settings from the given data instance.
     263     * The offset data is not loaded and needs to be handled separately.
    263264     * @param data The data
    264265     * @see #storeTo(Map)
     266     * @see OffsetBookmark#fromPropertiesMap(java.util.Map)
    265267     */
    266268    public void loadFrom(Map<String, String> data) {
     
    279281            if (doShowErrors != null) {
    280282                setShowErrors(Boolean.parseBoolean(doShowErrors));
    281             }
    282 
    283             String dx = data.get("dx");
    284             String dy = data.get("dy");
    285             if (dx != null && dy != null) {
    286                 setDisplacement(new EastNorth(Double.parseDouble(dx), Double.parseDouble(dy)));
    287283            }
    288284        } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
  • trunk/src/org/openstreetmap/josm/io/session/ImagerySessionExporter.java

    r10578 r12134  
    1414import org.openstreetmap.josm.data.Preferences;
    1515import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
     16import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    1617import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    1718import org.openstreetmap.josm.gui.layer.ImageryLayer;
     
    8283        Map<String, String> data = new LinkedHashMap<>(Preferences.serializeStruct(e, ImageryPreferenceEntry.class));
    8384        if (layer instanceof AbstractTileSourceLayer) {
    84             AbstractTileSourceLayer<?> tsLayer = (AbstractTileSourceLayer<?>) layer;
    85             tsLayer.getDisplaySettings().storeTo(data);
     85            ((AbstractTileSourceLayer<?>) layer).getDisplaySettings().storeTo(data);
    8686        }
    87         for (Map.Entry<String, String> entry : data.entrySet()) {
    88             Element attrElem = support.createElement(entry.getKey());
    89             layerElem.appendChild(attrElem);
    90             attrElem.appendChild(support.createTextNode(entry.getValue()));
     87        addAttributes(layerElem, data, support);
     88        if (layer instanceof AbstractTileSourceLayer) {
     89            OffsetBookmark offset = ((AbstractTileSourceLayer<?>) layer).getDisplaySettings().getOffsetBookmark();
     90            if (offset != null) {
     91                Map<String, String> offsetProps = offset.toPropertiesMap();
     92                Element offsetEl = support.createElement("offset");
     93                layerElem.appendChild(offsetEl);
     94                addAttributes(offsetEl, offsetProps, support);
     95            }
    9196        }
    9297        return layerElem;
    9398    }
     99
     100    private void addAttributes(Element element, Map<String, String> props, ExportSupport support) {
     101        for (Map.Entry<String, String> entry : props.entrySet()) {
     102            Element attrElem = support.createElement(entry.getKey());
     103            element.appendChild(attrElem);
     104            attrElem.appendChild(support.createTextNode(entry.getValue()));
     105        }
     106    }
    94107}
  • trunk/src/org/openstreetmap/josm/io/session/ImagerySessionImporter.java

    r10571 r12134  
    1111import org.openstreetmap.josm.data.imagery.ImageryInfo;
    1212import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
     13import org.openstreetmap.josm.data.imagery.OffsetBookmark;
    1314import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    1415import org.openstreetmap.josm.gui.layer.ImageryLayer;
     
    3637            throw new IllegalDataException(tr("Version ''{0}'' of meta data for imagery layer is not supported. Expected: 0.1", version));
    3738        }
     39        Map<String, String> attributes = readProperties(elem);
     40
     41        ImageryPreferenceEntry prefEntry = Preferences.deserializeStruct(attributes, ImageryPreferenceEntry.class);
     42        ImageryInfo info = new ImageryInfo(prefEntry);
     43        ImageryLayer layer = ImageryLayer.create(info);
     44        if (layer instanceof AbstractTileSourceLayer) {
     45            AbstractTileSourceLayer<?> tsLayer = (AbstractTileSourceLayer<?>) layer;
     46            tsLayer.getDisplaySettings().loadFrom(attributes);
     47            NodeList nodes = elem.getChildNodes();
     48            for (int i = 0; i < nodes.getLength(); ++i) {
     49                Node node = nodes.item(i);
     50                if (node.getNodeType() == Node.ELEMENT_NODE && "offset".equals(node.getLocalName())) {
     51                    Map<String, String> offsetAttributes = readProperties((Element) node);
     52                    OffsetBookmark offset = OffsetBookmark.fromPropertiesMap(offsetAttributes);
     53                    tsLayer.getDisplaySettings().setOffsetBookmark(offset);
     54                    break;
     55                }
     56            }
     57        }
     58        return layer;
     59    }
     60
     61    private Map<String, String> readProperties(Element elem) {
    3862        Map<String, String> attributes = new HashMap<>();
    39 
    4063        NodeList nodes = elem.getChildNodes();
    41 
    4264        for (int i = 0; i < nodes.getLength(); ++i) {
    4365            Node node = nodes.item(i);
    44             if (node.getNodeType() == Node.ELEMENT_NODE) {
     66            if (node.getNodeType() == Node.ELEMENT_NODE && node.getChildNodes().getLength() <= 1) {
    4567                Element e = (Element) node;
    4668                attributes.put(e.getTagName(), e.getTextContent());
    4769            }
    4870        }
    49         ImageryPreferenceEntry prefEntry = Preferences.deserializeStruct(attributes, ImageryPreferenceEntry.class);
    50         ImageryInfo i = new ImageryInfo(prefEntry);
    51         ImageryLayer layer = ImageryLayer.create(i);
    52         if (layer instanceof AbstractTileSourceLayer) {
    53             AbstractTileSourceLayer<?> tsLayer = (AbstractTileSourceLayer<?>) layer;
    54             tsLayer.getDisplaySettings().loadFrom(attributes);
    55         }
    56         return layer;
     71        return attributes;
    5772    }
    5873}
Note: See TracChangeset for help on using the changeset viewer.