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

Last change on this file since 3779 was 3779, checked in by Upliner, 13 years ago

Identify projections in offset bookmarks by EPSG codes, bugfixes in getPreferencesFromCode() functions as they're critical now.

  • Property svn:eol-style set to native
File size: 4.6 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.data.projection.Projection;
16import org.openstreetmap.josm.data.projection.ProjectionInfo;
17import org.openstreetmap.josm.gui.layer.ImageryLayer;
18
19public class OffsetBookmark {
20 public static List<OffsetBookmark> allBookmarks = new ArrayList<OffsetBookmark>();
21
22 public Projection proj;
23 public String layerName;
24 public String name;
25 public double dx, dy;
26 public double centerX, centerY;
27
28 public boolean isUsable(ImageryLayer layer) {
29 if (proj == null) return false;
30 if (!Main.proj.toCode().equals(proj.toCode())) return false;
31 return layer.getInfo().getName().equals(layerName);
32 }
33
34 public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy) {
35 this(proj, layerName, name, dx, dy, 0, 0);
36 }
37
38 public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy, double centerX, double centerY) {
39 this.proj = proj;
40 this.layerName = layerName;
41 this.name = name;
42 this.dx = dx;
43 this.dy = dy;
44 this.centerX = centerX;
45 this.centerY = centerY;
46 }
47
48 public OffsetBookmark(Collection<String> list) {
49 ArrayList<String> array = new ArrayList<String>(list);
50 String projectionStr = array.get(0);
51 proj = ProjectionInfo.getProjectionByCode(projectionStr);
52 if (proj == null) {
53 for (Projection proj : Projection.allProjections) {
54 if (proj.getCacheDirectoryName().equals(projectionStr)) {
55 this.proj = proj;
56 break;
57 }
58 }
59 }
60 this.layerName = array.get(1);
61 this.name = array.get(2);
62 this.dx = Double.valueOf(array.get(3));
63 this.dy = Double.valueOf(array.get(4));
64 if (array.size() >= 7) {
65 this.centerX = Double.valueOf(array.get(5));
66 this.centerY = Double.valueOf(array.get(6));
67 }
68 if (proj == null) {
69 System.err.println(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projectionStr, name));
70 }
71 }
72
73 public ArrayList<String> getInfoArray() {
74 ArrayList<String> res = new ArrayList<String>(7);
75 if (proj != null) {
76 res.add(proj.toCode());
77 } else {
78 res.add("");
79 }
80 res.add(layerName);
81 res.add(name);
82 res.add(String.valueOf(dx));
83 res.add(String.valueOf(dy));
84 if (this.centerX != 0 || this.centerY != 0) {
85 res.add(String.valueOf(centerX));
86 res.add(String.valueOf(centerY));
87 }
88 return res;
89 }
90
91 public static void loadBookmarks() {
92 for(Collection<String> c : Main.pref.getArray("imagery.offsets",
93 Collections.<Collection<String>>emptySet())) {
94 allBookmarks.add(new OffsetBookmark(c));
95 }
96 }
97
98 public static void saveBookmarks() {
99 LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
100 for (OffsetBookmark b : allBookmarks) {
101 coll.add(b.getInfoArray());
102 }
103 Main.pref.putArray("imagery.offsets", coll);
104 }
105
106 public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
107 for (OffsetBookmark b : allBookmarks) {
108 if (b.isUsable(layer) && name.equals(b.name))
109 return b;
110 }
111 return null;
112 }
113
114 public static void bookmarkOffset(String name, ImageryLayer layer) {
115 LatLon center;
116 if (Main.map != null && Main.map.mapView != null) {
117 center = Main.proj.eastNorth2latlon(Main.map.mapView.getCenter());
118 } else {
119 center = new LatLon(0,0);
120 }
121 OffsetBookmark nb = new OffsetBookmark(
122 Main.proj, layer.getInfo().getName(),
123 name, layer.getDx(), layer.getDy(), center.lon(), center.lat());
124 for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator();it.hasNext();) {
125 OffsetBookmark b = it.next();
126 if (b.isUsable(layer) && name.equals(b.name)) {
127 it.set(nb);
128 saveBookmarks();
129 return;
130 }
131 }
132 allBookmarks.add(nb);
133 saveBookmarks();
134 }
135}
Note: See TracBrowser for help on using the repository browser.