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