Changeset 13797 in josm


Ignore:
Timestamp:
2018-05-20T23:43:01+02:00 (4 weeks ago)
Author:
Don-vip
Message:

fix #13937 - save imagery offset bookmarks by locale-insensitive id (when available)

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/ImageryAdjustAction.java

    r13795 r13797  
    8383        tempOffset = new OffsetBookmark(
    8484                Main.getProjection().toCode(),
     85                layer.getInfo().getId(),
    8586                layer.getInfo().getName(),
    8687                null,
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    r13537 r13797  
    430430        return null;
    431431    }
     432
     433    /**
     434     * Returns imagery layer info for the given id.
     435     * @param id imagery layer id.
     436     * @return imagery layer info for the given id, or {@code null}
     437     * @since 13797
     438     */
     439    public ImageryInfo getLayer(String id) {
     440        return layerIds.get(id);
     441    }
    432442}
  • trunk/src/org/openstreetmap/josm/data/imagery/OffsetBookmark.java

    r13493 r13797  
    22package org.openstreetmap.josm.data.imagery;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    64import java.util.ArrayList;
    7 import java.util.Collection;
    85import java.util.Collections;
    96import java.util.List;
    107import java.util.ListIterator;
    118import java.util.Map;
     9import java.util.Objects;
     10import java.util.stream.Collectors;
    1211
    1312import org.openstreetmap.josm.Main;
     
    3635
    3736    @StructEntry private String projection_code;
     37    @StructEntry private String imagery_id;
     38    /** Imagery localized name. Locale insensitive {@link #imagery_id} is preferred. */
    3839    @StructEntry private String imagery_name;
    3940    @StructEntry private String name;
     
    4950        if (projection_code == null) return false;
    5051        if (!Main.getProjection().toCode().equals(projection_code) && !hasCenter()) return false;
    51         return layer.getInfo().getName().equals(imagery_name);
     52        ImageryInfo info = layer.getInfo();
     53        return imagery_id != null ? Objects.equals(info.getId(), imagery_id) : Objects.equals(info.getName(), imagery_name);
    5254    }
    5355
     
    6466     * Create a new {@link OffsetBookmark} object using (0, 0) as center
    6567     * <p>
    66      * The use of the {@link #OffsetBookmark(String, String, String, EastNorth, ILatLon)} constructor is preferred.
     68     * The use of the {@link #OffsetBookmark(String, String, String, String, EastNorth, ILatLon)} constructor is preferred.
    6769     * @param projectionCode The projection for which this object was created
    68      * @param imageryName The name of the imagery on the layer
     70     * @param imageryId The id of the imagery on the layer (locale insensitive)
     71     * @param imageryName The name of the imagery on the layer (locale sensitive)
    6972     * @param name The name of the new bookmark
    7073     * @param dx The x displacement
    7174     * @param dy The y displacement
    72      */
    73     public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy) {
    74         this(projectionCode, imageryName, name, dx, dy, 0, 0);
     75     * @since 13797
     76     */
     77    public OffsetBookmark(String projectionCode, String imageryId, String imageryName, String name, double dx, double dy) {
     78        this(projectionCode, imageryId, imageryName, name, dx, dy, 0, 0);
    7579    }
    7680
     
    7882     * Create a new {@link OffsetBookmark} object
    7983     * @param projectionCode The projection for which this object was created
    80      * @param imageryName The name of the imagery on the layer
     84     * @param imageryId The id of the imagery on the layer (locale insensitive)
     85     * @param imageryName The name of the imagery on the layer (locale sensitive)
    8186     * @param name The name of the new bookmark
    8287     * @param displacement The displacement in east/north space.
    8388     * @param center The point on earth that was used as reference to align the image.
    84      * @since 13243
    85      */
    86     public OffsetBookmark(String projectionCode, String imageryName, String name, EastNorth displacement, ILatLon center) {
    87         this(projectionCode, imageryName, name, displacement.east(), displacement.north(), center.lon(), center.lat());
     89     * @since 13797
     90     */
     91    public OffsetBookmark(String projectionCode, String imageryId, String imageryName, String name, EastNorth displacement, ILatLon center) {
     92        this(projectionCode, imageryId, imageryName, name, displacement.east(), displacement.north(), center.lon(), center.lat());
    8893    }
    8994
     
    9196     * Create a new {@link OffsetBookmark} by specifying all values.
    9297     * <p>
    93      * The use of the {@link #OffsetBookmark(String, String, String, EastNorth, ILatLon)} constructor is preferred.
     98     * The use of the {@link #OffsetBookmark(String, String, String, String, EastNorth, ILatLon)} constructor is preferred.
    9499     * @param projectionCode The projection for which this object was created
    95      * @param imageryName The name of the imagery on the layer
     100     * @param imageryId The id of the imagery on the layer (locale insensitive)
     101     * @param imageryName The name of the imagery on the layer (locale sensitive)
    96102     * @param name The name of the new bookmark
    97103     * @param dx The x displacement
     
    99105     * @param centerLon The point on earth that was used as reference to align the image.
    100106     * @param centerLat The point on earth that was used as reference to align the image.
    101      */
    102     public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy, double centerLon, double centerLat) {
     107     * @since 13797
     108     */
     109    public OffsetBookmark(String projectionCode, String imageryId, String imageryName, String name,
     110            double dx, double dy, double centerLon, double centerLat) {
    103111        this.projection_code = projectionCode;
     112        this.imagery_id = imageryId;
    104113        this.imagery_name = imageryName;
    105114        this.name = name;
     
    111120
    112121    /**
    113      * Loads an old bookmark. For backward compatibility with settings. Do not use.
    114      * @param list The settings that were read
    115      */
    116     public OffsetBookmark(Collection<String> list) {
    117         List<String> array = new ArrayList<>(list);
    118         this.projection_code = array.get(0);
    119         this.imagery_name = array.get(1);
    120         this.name = array.get(2);
    121         this.dx = Double.parseDouble(array.get(3));
    122         this.dy = Double.parseDouble(array.get(4));
    123         if (array.size() >= 7) {
    124             this.center_lon = Double.parseDouble(array.get(5));
    125             this.center_lat = Double.parseDouble(array.get(6));
    126         }
    127         if (projection_code == null) {
    128             Logging.error(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projection_code, name));
    129         }
    130     }
    131 
    132     /**
    133122     * Get the projection code for which this bookmark was created.
    134123     * @return The projection.
     
    147136
    148137    /**
    149      * Get the name of the imagery for which this bookmark was created. It is used to match the bookmark to the right layers.
     138     * Get the id of the imagery for which this bookmark was created. It is used to match the bookmark to the right layers.
     139     * @return The imagery identifier
     140     * @since 13797
     141     */
     142    public String getImageryId() {
     143        return imagery_id;
     144    }
     145
     146    /**
     147     * Get the name of the imagery for which this bookmark was created.
     148     * It is used to match the bookmark to the right layers if id is missing.
    150149     * @return The name
    151150     */
     
    233232
    234233    /**
     234     * Sets the id of the imagery
     235     * @param imageryId The identifier
     236     * @see #getImageryId()
     237     * @since xxx
     238     */
     239    public void setImageryId(String imageryId) {
     240        this.imagery_id = imageryId;
     241    }
     242
     243    /**
    235244     * Update the displacement of this imagery.
    236245     * @param displacement The displacement
     
    248257                Config.getPref(), "imagery.offsetbookmarks", null, OffsetBookmark.class);
    249258        if (bookmarks == null) {
    250             loadBookmarksOld();
    251259            saveBookmarks();
    252260        } else {
     261            sanitizeBookmarks(bookmarks);
    253262            allBookmarks.addAll(bookmarks);
    254263        }
    255264    }
    256265
    257     // migration code - remove Nov. 2017
    258     private static void loadBookmarksOld() {
    259         for (Collection<String> c : Config.getPref().getListOfLists("imagery.offsets")) {
    260             allBookmarks.add(new OffsetBookmark(c));
    261         }
     266    static void sanitizeBookmarks(List<OffsetBookmark> bookmarks) {
     267        // Retrieve layer id from layer name (it was not available before #13937)
     268        bookmarks.stream().filter(b -> b.getImageryId() == null).forEach(b -> {
     269            List<ImageryInfo> candidates = ImageryLayerInfo.instance.getLayers().stream()
     270                .filter(l -> Objects.equals(l.getName(), b.getImageryName()))
     271                .collect(Collectors.toList());
     272            // Make sure there is no ambiguity
     273            if (candidates.size() == 1) {
     274                b.setImageryId(candidates.get(0).getId());
     275            } else {
     276                Logging.warn("Not a single layer for the name '" + b.getImageryName() + "': " + candidates);
     277            }
     278        });
     279        // Update layer name (locale sensitive) if the locale has changed
     280        bookmarks.stream().filter(b -> b.getImageryId() != null).forEach(b -> {
     281            ImageryInfo info = ImageryLayerInfo.instance.getLayer(b.getImageryId());
     282            if (info != null && !Objects.equals(info.getName(), b.getImageryName())) {
     283                b.setImageryName(info.getName());
     284            }
     285        });
    262286    }
    263287
     
    346370        }
    347371        OffsetBookmark nb = new OffsetBookmark(
    348                 Main.getProjection().toCode(), layer.getInfo().getName(),
     372                Main.getProjection().toCode(), layer.getInfo().getId(), layer.getInfo().getName(),
    349373                name, layer.getDisplaySettings().getDisplacement(), center);
    350374        for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator(); it.hasNext();) {
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r13761 r13797  
    2424import java.util.List;
    2525import java.util.Map;
     26import java.util.Objects;
    2627import java.util.Set;
     28import java.util.stream.Collectors;
    2729
    2830import javax.swing.AbstractAction;
     
    853855            JButton add = new JButton(tr("Add"));
    854856            buttonPanel.add(add, GBC.std().insets(0, 5, 0, 0));
    855             add.addActionListener(e -> model.addRow(new OffsetBookmark(Main.getProjection().toCode(), "", "", 0, 0)));
     857            add.addActionListener(e -> model.addRow(new OffsetBookmark(Main.getProjection().toCode(), "", "", "", 0, 0)));
    856858
    857859            JButton delete = new JButton(tr("Delete"));
     
    920922                    return info.getDisplacement().north();
    921923                default:
    922                     throw new ArrayIndexOutOfBoundsException();
     924                    throw new ArrayIndexOutOfBoundsException(column);
    923925                }
    924926            }
     
    929931                switch (column) {
    930932                case 1:
    931                     info.setImageryName(o.toString());
     933                    String name = o.toString();
     934                    info.setImageryName(name);
     935                    List<ImageryInfo> layers = ImageryLayerInfo.instance.getLayers().stream()
     936                            .filter(l -> Objects.equals(name, l.getName())).collect(Collectors.toList());
     937                    if (layers.size() == 1) {
     938                        info.setImageryId(layers.get(0).getId());
     939                    } else {
     940                        Logging.warn("Not a single layer for the name '" + info.getImageryName() + "': " + layers);
     941                    }
    932942                    break;
    933943                case 2:
     
    943953                    break;
    944954                default:
    945                     throw new ArrayIndexOutOfBoundsException();
     955                    throw new ArrayIndexOutOfBoundsException(column);
    946956                }
    947957            }
  • trunk/test/unit/org/openstreetmap/josm/io/session/SessionWriterTest.java

    r12636 r13797  
    144144        TMSLayer layer = new TMSLayer(new ImageryInfo("the name", "http://www.url.com/"));
    145145        layer.getDisplaySettings().setOffsetBookmark(
    146                 new OffsetBookmark(Main.getProjection().toCode(), layer.getInfo().getName(), "", 12, 34));
     146                new OffsetBookmark(Main.getProjection().toCode(), layer.getInfo().getId(), layer.getInfo().getName(), "", 12, 34));
    147147        return layer;
    148148    }
Note: See TracChangeset for help on using the changeset viewer.