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


Ignore:
Timestamp:
2017-05-07T17:34:41+02:00 (7 years ago)
Author:
bastiK
Message:

offset bookmarks: migrate preferences from lists to key=value maps (+add getters & setters) (see #14734)

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

Legend:

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

    r11651 r12084  
    77import java.util.Collection;
    88import java.util.Collections;
    9 import java.util.LinkedList;
    109import java.util.List;
    1110import java.util.ListIterator;
    1211
    1312import org.openstreetmap.josm.Main;
     13import org.openstreetmap.josm.data.Preferences.pref;
     14import org.openstreetmap.josm.data.coor.EastNorth;
    1415import org.openstreetmap.josm.data.coor.LatLon;
    1516import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
     
    1920    private static final List<OffsetBookmark> allBookmarks = new ArrayList<>();
    2021
    21     public String projectionCode;
    22     public String layerName;
    23     public String name;
    24     public double dx, dy;
    25     public double centerX, centerY;
     22    @pref private String projection_code;
     23    @pref private String imagery_name;
     24    @pref private String name;
     25    @pref private double dx, dy;
     26    @pref private double center_lon, center_lat;
    2627
    2728    public boolean isUsable(ImageryLayer layer) {
    28         if (projectionCode == null) return false;
    29         if (!Main.getProjection().toCode().equals(projectionCode)) return false;
    30         return layer.getInfo().getName().equals(layerName);
    31     }
    32 
    33     public OffsetBookmark(String projectionCode, String layerName, String name, double dx, double dy) {
    34         this(projectionCode, layerName, name, dx, dy, 0, 0);
    35     }
    36 
    37     public OffsetBookmark(String projectionCode, String layerName, String name, double dx, double dy, double centerX, double centerY) {
    38         this.projectionCode = projectionCode;
    39         this.layerName = layerName;
     29        if (projection_code == null) return false;
     30        if (!Main.getProjection().toCode().equals(projection_code)) return false;
     31        return layer.getInfo().getName().equals(imagery_name);
     32    }
     33
     34    /**
     35     * Construct new empty OffsetBookmark.
     36     *
     37     * Only used for preferences handling.
     38     */
     39    public OffsetBookmark() {
     40        // do nothing
     41    }
     42
     43    public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy) {
     44        this(projectionCode, imageryName, name, dx, dy, 0, 0);
     45    }
     46
     47    public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy, double centerLon, double centerLat) {
     48        this.projection_code = projectionCode;
     49        this.imagery_name = imageryName;
    4050        this.name = name;
    4151        this.dx = dx;
    4252        this.dy = dy;
    43         this.centerX = centerX;
    44         this.centerY = centerY;
     53        this.center_lon = centerLon;
     54        this.center_lat = centerLat;
    4555    }
    4656
    4757    public OffsetBookmark(Collection<String> list) {
    4858        List<String> array = new ArrayList<>(list);
    49         this.projectionCode = array.get(0);
    50         this.layerName = array.get(1);
     59        this.projection_code = array.get(0);
     60        this.imagery_name = array.get(1);
    5161        this.name = array.get(2);
    5262        this.dx = Double.parseDouble(array.get(3));
    5363        this.dy = Double.parseDouble(array.get(4));
    5464        if (array.size() >= 7) {
    55             this.centerX = Double.parseDouble(array.get(5));
    56             this.centerY = Double.parseDouble(array.get(6));
    57         }
    58         if (projectionCode == null) {
    59             Main.error(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projectionCode, name));
    60         }
    61     }
    62 
    63     public List<String> getInfoArray() {
    64         List<String> res = new ArrayList<>(7);
    65         if (projectionCode != null) {
    66             res.add(projectionCode);
     65            this.center_lon = Double.parseDouble(array.get(5));
     66            this.center_lat = Double.parseDouble(array.get(6));
     67        }
     68        if (projection_code == null) {
     69            Main.error(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projection_code, name));
     70        }
     71    }
     72
     73    public String getProjectionCode() {
     74        return projection_code;
     75    }
     76
     77    public String getName() {
     78        return name;
     79    }
     80
     81    public String getImageryName() {
     82        return imagery_name;
     83    }
     84
     85    public EastNorth getOffset() {
     86        return new EastNorth(dx, dy);
     87    }
     88
     89    public LatLon getCenter() {
     90        return new LatLon(center_lat, center_lon);
     91    }
     92
     93    public void setProjectionCode(String projectionCode) {
     94        this.projection_code = projectionCode;
     95    }
     96
     97    public void setName(String name) {
     98        this.name = name;
     99    }
     100
     101    public void setImageryName(String imageryName) {
     102        this.imagery_name = imageryName;
     103    }
     104
     105    public void setOffset(EastNorth offset) {
     106        this.dx = offset.east();
     107        this.dy = offset.north();
     108    }
     109
     110    public static void loadBookmarks() {
     111        List<OffsetBookmark> bookmarks = Main.pref.getListOfStructs("imagery.offsetbookmarks", null, OffsetBookmark.class);
     112        if (bookmarks == null) {
     113            loadBookmarksOld();
     114            saveBookmarks();
    67115        } else {
    68             res.add("");
    69         }
    70         res.add(layerName);
    71         res.add(name);
    72         res.add(String.valueOf(dx));
    73         res.add(String.valueOf(dy));
    74         if (centerX != 0 || centerY != 0) {
    75             res.add(String.valueOf(centerX));
    76             res.add(String.valueOf(centerY));
    77         }
    78         return res;
    79     }
    80 
    81     public static void loadBookmarks() {
     116            allBookmarks.addAll(bookmarks);
     117        }
     118    }
     119
     120    // migration code - remove Nov. 2017
     121    private static void loadBookmarksOld() {
    82122        for (Collection<String> c : Main.pref.getArray("imagery.offsets",
    83123                Collections.<Collection<String>>emptySet())) {
     
    87127
    88128    public static void saveBookmarks() {
    89         List<Collection<String>> coll = new LinkedList<>();
    90         for (OffsetBookmark b : allBookmarks) {
    91             coll.add(b.getInfoArray());
    92         }
    93         Main.pref.putArray("imagery.offsets", coll);
     129        Main.pref.putListOfStructs("imagery.offsetbookmarks", allBookmarks, OffsetBookmark.class);
    94130    }
    95131
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r11960 r12084  
    5454import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
    5555import org.openstreetmap.josm.Main;
     56import org.openstreetmap.josm.data.coor.EastNorth;
    5657import org.openstreetmap.josm.data.imagery.ImageryInfo;
    5758import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
     
    783784
    784785        private static boolean confirmEulaAcceptance(PreferenceTabbedPane gui, String eulaUrl) {
    785             URL url = null;
     786            URL url;
    786787            try {
    787788                url = new URL(eulaUrl.replaceAll("\\{lang\\}", LanguageInfo.getWikiLanguagePrefix()));
    788                 JosmEditorPane htmlPane = null;
     789                JosmEditorPane htmlPane;
    789790                try {
    790791                    htmlPane = new JosmEditorPane(url);
     
    904905                switch (column) {
    905906                case 0:
    906                     if (info.projectionCode == null) return "";
    907                     return info.projectionCode;
     907                    if (info.getProjectionCode() == null) return "";
     908                    return info.getProjectionCode();
    908909                case 1:
    909                     return info.layerName;
     910                    return info.getImageryName();
    910911                case 2:
    911                     return info.name;
     912                    return info.getName();
    912913                case 3:
    913                     return info.dx;
     914                    return info.getOffset().east();
    914915                case 4:
    915                     return info.dy;
     916                    return info.getOffset().north();
    916917                default:
    917918                    throw new ArrayIndexOutOfBoundsException();
     
    924925                switch (column) {
    925926                case 1:
    926                     info.layerName = o.toString();
     927                    info.setImageryName(o.toString());
    927928                    break;
    928929                case 2:
    929                     info.name = o.toString();
     930                    info.setName(o.toString());
    930931                    break;
    931932                case 3:
    932                     info.dx = Double.parseDouble((String) o);
     933                    double dx = Double.parseDouble((String) o);
     934                    info.setOffset(new EastNorth(dx, info.getOffset().north()));
    933935                    break;
    934936                case 4:
    935                     info.dy = Double.parseDouble((String) o);
     937                    double dy = Double.parseDouble((String) o);
     938                    info.setOffset(new EastNorth(info.getOffset().east(), dy));
    936939                    break;
    937940                default:
Note: See TracChangeset for help on using the changeset viewer.