001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.imagery;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.ArrayList;
007import java.util.Collection;
008import java.util.Collections;
009import java.util.LinkedList;
010import java.util.List;
011import java.util.ListIterator;
012
013import org.openstreetmap.josm.Main;
014import org.openstreetmap.josm.data.coor.LatLon;
015import org.openstreetmap.josm.gui.layer.ImageryLayer;
016
017public class OffsetBookmark {
018    public static final List<OffsetBookmark> allBookmarks = new ArrayList<>();
019
020    public String projectionCode;
021    public String layerName;
022    public String name;
023    public double dx, dy;
024    public double centerX, centerY;
025
026    public boolean isUsable(ImageryLayer layer) {
027        if (projectionCode == null) return false;
028        if (!Main.getProjection().toCode().equals(projectionCode)) return false;
029        return layer.getInfo().getName().equals(layerName);
030    }
031
032    public OffsetBookmark(String projectionCode, String layerName, String name, double dx, double dy) {
033        this(projectionCode, layerName, name, dx, dy, 0, 0);
034    }
035
036    public OffsetBookmark(String projectionCode, String layerName, String name, double dx, double dy, double centerX, double centerY) {
037        this.projectionCode = projectionCode;
038        this.layerName = layerName;
039        this.name = name;
040        this.dx = dx;
041        this.dy = dy;
042        this.centerX = centerX;
043        this.centerY = centerY;
044    }
045
046    public OffsetBookmark(Collection<String> list) {
047        List<String> array = new ArrayList<>(list);
048        this.projectionCode = array.get(0);
049        this.layerName = array.get(1);
050        this.name = array.get(2);
051        this.dx = Double.parseDouble(array.get(3));
052        this.dy = Double.parseDouble(array.get(4));
053        if (array.size() >= 7) {
054            this.centerX = Double.parseDouble(array.get(5));
055            this.centerY = Double.parseDouble(array.get(6));
056        }
057        if (projectionCode == null) {
058            Main.error(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projectionCode, name));
059        }
060    }
061
062    public List<String> getInfoArray() {
063        List<String> res = new ArrayList<>(7);
064        if (projectionCode != null) {
065            res.add(projectionCode);
066        } else {
067            res.add("");
068        }
069        res.add(layerName);
070        res.add(name);
071        res.add(String.valueOf(dx));
072        res.add(String.valueOf(dy));
073        if (centerX != 0 || centerY != 0) {
074            res.add(String.valueOf(centerX));
075            res.add(String.valueOf(centerY));
076        }
077        return res;
078    }
079
080    public static void loadBookmarks() {
081        for (Collection<String> c : Main.pref.getArray("imagery.offsets",
082                Collections.<Collection<String>>emptySet())) {
083            allBookmarks.add(new OffsetBookmark(c));
084        }
085    }
086
087    public static void saveBookmarks() {
088        List<Collection<String>> coll = new LinkedList<>();
089        for (OffsetBookmark b : allBookmarks) {
090            coll.add(b.getInfoArray());
091        }
092        Main.pref.putArray("imagery.offsets", coll);
093    }
094
095    public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
096        for (OffsetBookmark b : allBookmarks) {
097            if (b.isUsable(layer) && name.equals(b.name))
098                return b;
099        }
100        return null;
101    }
102
103    public static void bookmarkOffset(String name, ImageryLayer layer) {
104        LatLon center;
105        if (Main.isDisplayingMapView()) {
106            center = Main.getProjection().eastNorth2latlon(Main.map.mapView.getCenter());
107        } else {
108            center = new LatLon(0, 0);
109        }
110        OffsetBookmark nb = new OffsetBookmark(
111                Main.getProjection().toCode(), layer.getInfo().getName(),
112                name, layer.getDx(), layer.getDy(), center.lon(), center.lat());
113        for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator(); it.hasNext();) {
114            OffsetBookmark b = it.next();
115            if (b.isUsable(layer) && name.equals(b.name)) {
116                it.set(nb);
117                saveBookmarks();
118                return;
119            }
120        }
121        allBookmarks.add(nb);
122        saveBookmarks();
123    }
124}