1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.ListIterator;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
16 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
17 |
|
---|
18 | public class OffsetBookmark {
|
---|
19 | public static List<OffsetBookmark> allBookmarks = new ArrayList<OffsetBookmark>();
|
---|
20 |
|
---|
21 | public Projection proj;
|
---|
22 | public String layerName;
|
---|
23 | public String name;
|
---|
24 | public double dx, dy;
|
---|
25 | public double centerX, centerY;
|
---|
26 |
|
---|
27 | public boolean isUsable(ImageryLayer layer) {
|
---|
28 | return Main.proj.getClass() == proj.getClass() &&
|
---|
29 | layer.getInfo().getName().equals(layerName);
|
---|
30 | }
|
---|
31 |
|
---|
32 | public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy) {
|
---|
33 | this(proj, layerName, name, dx, dy, 0, 0);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy, double centerX, double centerY) {
|
---|
37 | this.proj = proj;
|
---|
38 | this.layerName = layerName;
|
---|
39 | this.name = name;
|
---|
40 | this.dx = dx;
|
---|
41 | this.dy = dy;
|
---|
42 | this.centerX = centerX;
|
---|
43 | this.centerY = centerY;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public OffsetBookmark(Collection<String> list) {
|
---|
47 | ArrayList<String> array = new ArrayList<String>(list);
|
---|
48 | String projectionName = array.get(0);
|
---|
49 | for (Projection proj : Projection.allProjections) {
|
---|
50 | if (proj.getCacheDirectoryName().equals(projectionName)) {
|
---|
51 | this.proj = proj;
|
---|
52 | break;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | if (this.proj == null)
|
---|
56 | throw new IllegalStateException(tr("Projection ''{0}'' not found", projectionName));
|
---|
57 | this.layerName = array.get(1);
|
---|
58 | this.name = array.get(2);
|
---|
59 | this.dx = Double.valueOf(array.get(3));
|
---|
60 | this.dy = Double.valueOf(array.get(4));
|
---|
61 | if (array.size() >= 7) {
|
---|
62 | this.centerX = Double.valueOf(array.get(5));
|
---|
63 | this.centerY = Double.valueOf(array.get(6));
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public ArrayList<String> getInfoArray() {
|
---|
68 | ArrayList<String> res = new ArrayList<String>(5);
|
---|
69 | res.add(proj.getCacheDirectoryName()); // we should use non-localized projection name
|
---|
70 | res.add(layerName);
|
---|
71 | res.add(name);
|
---|
72 | res.add(String.valueOf(dx));
|
---|
73 | res.add(String.valueOf(dy));
|
---|
74 | if (this.centerX != 0 || this.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() {
|
---|
82 | for(Collection<String> c : Main.pref.getArray("imagery.offsets",
|
---|
83 | Collections.<Collection<String>>emptySet())) {
|
---|
84 | allBookmarks.add(new OffsetBookmark(c));
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public static void saveBookmarks() {
|
---|
89 | LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
|
---|
90 | for (OffsetBookmark b : allBookmarks) {
|
---|
91 | coll.add(b.getInfoArray());
|
---|
92 | }
|
---|
93 | Main.pref.putArray("imagery.offsets", coll);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
|
---|
97 | for (OffsetBookmark b : allBookmarks) {
|
---|
98 | if (b.isUsable(layer) && name.equals(b.name))
|
---|
99 | return b;
|
---|
100 | }
|
---|
101 | return null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public static void bookmarkOffset(String name, ImageryLayer layer) {
|
---|
105 | LatLon center;
|
---|
106 | if (Main.map != null && Main.map.mapView != null) {
|
---|
107 | center = Main.proj.eastNorth2latlon(Main.map.mapView.getCenter());
|
---|
108 | } else {
|
---|
109 | center = new LatLon(0,0);
|
---|
110 | }
|
---|
111 | OffsetBookmark nb = new OffsetBookmark(
|
---|
112 | Main.proj, layer.getInfo().getName(),
|
---|
113 | name, layer.getDx(), layer.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 | }
|
---|