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.projection.Projection;
|
---|
15 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
16 |
|
---|
17 | public class OffsetBookmark {
|
---|
18 | public static List<OffsetBookmark> allBookmarks = new ArrayList<OffsetBookmark>();
|
---|
19 |
|
---|
20 | public Projection proj;
|
---|
21 | public String layerName;
|
---|
22 | public String name;
|
---|
23 | public double dx, dy;
|
---|
24 |
|
---|
25 | public boolean isUsable(ImageryLayer layer) {
|
---|
26 | return Main.proj.getClass() == proj.getClass() &&
|
---|
27 | layer.getInfo().getName().equals(layerName);
|
---|
28 | }
|
---|
29 | public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy) {
|
---|
30 | this.proj = proj;
|
---|
31 | this.layerName = layerName;
|
---|
32 | this.name = name;
|
---|
33 | this.dx = dx;
|
---|
34 | this.dy = dy;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public OffsetBookmark(Collection<String> list) {
|
---|
38 | ArrayList<String> array = new ArrayList<String>(list);
|
---|
39 | String projectionName = array.get(0);
|
---|
40 | for (Projection proj : Projection.allProjections) {
|
---|
41 | if (proj.getCacheDirectoryName().equals(projectionName)) {
|
---|
42 | this.proj = proj;
|
---|
43 | break;
|
---|
44 | }
|
---|
45 | }
|
---|
46 | if (this.proj == null)
|
---|
47 | throw new IllegalStateException(tr("Projection ''{0}'' not found", projectionName));
|
---|
48 | this.layerName = array.get(1);
|
---|
49 | this.name = array.get(2);
|
---|
50 | this.dx = Double.valueOf(array.get(3));
|
---|
51 | this.dy = Double.valueOf(array.get(4));
|
---|
52 | }
|
---|
53 |
|
---|
54 | public ArrayList<String> getInfoArray() {
|
---|
55 | ArrayList<String> res = new ArrayList<String>(5);
|
---|
56 | res.add(proj.getCacheDirectoryName()); // we should use non-localized projection name
|
---|
57 | res.add(layerName);
|
---|
58 | res.add(name);
|
---|
59 | res.add(String.valueOf(dx));
|
---|
60 | res.add(String.valueOf(dy));
|
---|
61 | return res;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static void loadBookmarks() {
|
---|
65 | for(Collection<String> c : Main.pref.getArray("imagery.offsets",
|
---|
66 | Collections.<Collection<String>>emptySet())) {
|
---|
67 | allBookmarks.add(new OffsetBookmark(c));
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static void saveBookmarks() {
|
---|
72 | LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
|
---|
73 | for (OffsetBookmark b : allBookmarks) {
|
---|
74 | coll.add(b.getInfoArray());
|
---|
75 | }
|
---|
76 | Main.pref.putArray("imagery.offsets", coll);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
|
---|
80 | for (OffsetBookmark b : allBookmarks) {
|
---|
81 | if (b.isUsable(layer) && name.equals(b.name))
|
---|
82 | return b;
|
---|
83 | }
|
---|
84 | return null;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static void bookmarkOffset(String name, ImageryLayer layer) {
|
---|
88 | OffsetBookmark nb = new OffsetBookmark(
|
---|
89 | Main.proj, layer.getInfo().getName(),
|
---|
90 | name, layer.getDx(), layer.getDy());
|
---|
91 | for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator();it.hasNext();) {
|
---|
92 | OffsetBookmark b = it.next();
|
---|
93 | if (b.isUsable(layer) && name.equals(b.name)) {
|
---|
94 | it.set(nb);
|
---|
95 | saveBookmarks();
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | allBookmarks.add(nb);
|
---|
100 | saveBookmarks();
|
---|
101 | }
|
---|
102 | }
|
---|