source: josm/trunk/src/org/openstreetmap/josm/data/imagery/OffsetBookmark.java@ 8393

Last change on this file since 8393 was 8393, checked in by Don-vip, 9 years ago

see #11447 - partial revert of r8384

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.ListIterator;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.coor.LatLon;
15import org.openstreetmap.josm.gui.layer.ImageryLayer;
16
17public class OffsetBookmark {
18 public static final List<OffsetBookmark> allBookmarks = new ArrayList<>();
19
20 public String projectionCode;
21 public String layerName;
22 public String name;
23 public double dx, dy;
24 public double centerX, centerY;
25
26 public boolean isUsable(ImageryLayer layer) {
27 if (projectionCode == null) return false;
28 if (!Main.getProjection().toCode().equals(projectionCode)) return false;
29 return layer.getInfo().getName().equals(layerName);
30 }
31
32 public OffsetBookmark(String projectionCode, String layerName, String name, double dx, double dy) {
33 this(projectionCode, layerName, name, dx, dy, 0, 0);
34 }
35
36 public OffsetBookmark(String projectionCode, String layerName, String name, double dx, double dy, double centerX, double centerY) {
37 this.projectionCode = projectionCode;
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 List<String> array = new ArrayList<>(list);
48 this.projectionCode = array.get(0);
49 this.layerName = array.get(1);
50 this.name = array.get(2);
51 this.dx = Double.valueOf(array.get(3));
52 this.dy = Double.valueOf(array.get(4));
53 if (array.size() >= 7) {
54 this.centerX = Double.valueOf(array.get(5));
55 this.centerY = Double.valueOf(array.get(6));
56 }
57 if (projectionCode == null) {
58 Main.error(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projectionCode, name));
59 }
60 }
61
62 public List<String> getInfoArray() {
63 List<String> res = new ArrayList<>(7);
64 if (projectionCode != null) {
65 res.add(projectionCode);
66 } else {
67 res.add("");
68 }
69 res.add(layerName);
70 res.add(name);
71 res.add(String.valueOf(dx));
72 res.add(String.valueOf(dy));
73 if (centerX != 0 || centerY != 0) {
74 res.add(String.valueOf(centerX));
75 res.add(String.valueOf(centerY));
76 }
77 return res;
78 }
79
80 public static void loadBookmarks() {
81 for(Collection<String> c : Main.pref.getArray("imagery.offsets",
82 Collections.<Collection<String>>emptySet())) {
83 allBookmarks.add(new OffsetBookmark(c));
84 }
85 }
86
87 public static void saveBookmarks() {
88 List<Collection<String>> coll = new LinkedList<>();
89 for (OffsetBookmark b : allBookmarks) {
90 coll.add(b.getInfoArray());
91 }
92 Main.pref.putArray("imagery.offsets", coll);
93 }
94
95 public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
96 for (OffsetBookmark b : allBookmarks) {
97 if (b.isUsable(layer) && name.equals(b.name))
98 return b;
99 }
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}
Note: See TracBrowser for help on using the repository browser.