1 | package org.openstreetmap.josm.gui.bbox;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.awt.Toolkit;
|
---|
9 | import java.util.Vector;
|
---|
10 |
|
---|
11 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
12 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
---|
13 | import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
|
---|
14 | import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
|
---|
15 | import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
|
---|
16 | import org.openstreetmap.gui.jmapviewer.OsmMercator;
|
---|
17 | import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
|
---|
18 | import org.openstreetmap.gui.jmapviewer.OsmTileSource;
|
---|
19 | import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
|
---|
20 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
21 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 |
|
---|
26 | public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser{
|
---|
27 | static private TileSource[] TILE_SOURCES = { new OsmTileSource.Mapnik(),
|
---|
28 | new OsmTileSource.TilesAtHome(), new OsmTileSource.CycleMap() };
|
---|
29 |
|
---|
30 | // standard dimension
|
---|
31 | private Dimension iDownloadDialogDimension;
|
---|
32 |
|
---|
33 | private TileLoader cachedLoader;
|
---|
34 | private TileLoader uncachedLoader;
|
---|
35 |
|
---|
36 | private SizeButton iSizeButton = new SizeButton();
|
---|
37 | private SourceButton iSourceButton = new SourceButton();
|
---|
38 | private Bounds bbox;
|
---|
39 |
|
---|
40 | // upper left and lower right corners of the selection rectangle (x/y on
|
---|
41 | // ZOOM_MAX)
|
---|
42 | Point iSelectionRectStart;
|
---|
43 | Point iSelectionRectEnd;
|
---|
44 |
|
---|
45 | public SlippyMapBBoxChooser() {
|
---|
46 | super();
|
---|
47 | try {
|
---|
48 | cachedLoader = new OsmFileCacheTileLoader(this);
|
---|
49 | } catch (SecurityException e) {
|
---|
50 | // set to null if a SecurityException was thrown
|
---|
51 | // while creating the cachedLoader
|
---|
52 | //
|
---|
53 | cachedLoader = null;
|
---|
54 | }
|
---|
55 | uncachedLoader = new OsmTileLoader(this);
|
---|
56 | setZoomContolsVisible(false);
|
---|
57 | setMapMarkerVisible(false);
|
---|
58 | setMinimumSize(new Dimension(350, 350 / 2));
|
---|
59 | // We need to set an initial size - this prevents a wrong zoom selection
|
---|
60 | // for
|
---|
61 | // the area before the component has been displayed the first time
|
---|
62 | setBounds(new Rectangle(getMinimumSize()));
|
---|
63 | if (cachedLoader == null) {
|
---|
64 | setFileCacheEnabled(false);
|
---|
65 | } else {
|
---|
66 | setFileCacheEnabled(Main.pref.getBoolean("slippy_map_chooser.file_cache", true));
|
---|
67 | }
|
---|
68 | setMaxTilesInMemory(Main.pref.getInteger("slippy_map_chooser.max_tiles", 1000));
|
---|
69 |
|
---|
70 | String mapStyle = Main.pref.get("slippy_map_chooser.mapstyle", "mapnik");
|
---|
71 | if (mapStyle.equals("osmarender")) {
|
---|
72 | iSourceButton.setMapStyle(SourceButton.OSMARENDER);
|
---|
73 | this.setTileSource(TILE_SOURCES[1]);
|
---|
74 | } else if (mapStyle.equals("cyclemap")) {
|
---|
75 | iSourceButton.setMapStyle(SourceButton.CYCLEMAP);
|
---|
76 | this.setTileSource(TILE_SOURCES[2]);
|
---|
77 | } else {
|
---|
78 | if (!mapStyle.equals("mapnik")) {
|
---|
79 | Main.pref.put("slippy_map_chooser", "mapnik");
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | new SlippyMapControler(this, this, iSizeButton, iSourceButton);
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected Point getTopLeftCoordinates() {
|
---|
87 | return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Draw the map.
|
---|
92 | */
|
---|
93 | @Override
|
---|
94 | public void paint(Graphics g) {
|
---|
95 | try {
|
---|
96 | super.paint(g);
|
---|
97 |
|
---|
98 | // draw selection rectangle
|
---|
99 | if (iSelectionRectStart != null && iSelectionRectEnd != null) {
|
---|
100 |
|
---|
101 | int zoomDiff = MAX_ZOOM - zoom;
|
---|
102 | Point tlc = getTopLeftCoordinates();
|
---|
103 | int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x;
|
---|
104 | int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y;
|
---|
105 | int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x;
|
---|
106 | int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y;
|
---|
107 |
|
---|
108 | int w = x_max - x_min;
|
---|
109 | int h = y_max - y_min;
|
---|
110 | g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
|
---|
111 | g.fillRect(x_min, y_min, w, h);
|
---|
112 |
|
---|
113 | g.setColor(Color.BLACK);
|
---|
114 | g.drawRect(x_min, y_min, w, h);
|
---|
115 | }
|
---|
116 |
|
---|
117 | iSizeButton.paint(g);
|
---|
118 | iSourceButton.paint(g);
|
---|
119 | } catch (Exception e) {
|
---|
120 | e.printStackTrace();
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void setFileCacheEnabled(boolean enabled) {
|
---|
125 | if (enabled) {
|
---|
126 | setTileLoader(cachedLoader);
|
---|
127 | } else {
|
---|
128 | setTileLoader(uncachedLoader);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void setMaxTilesInMemory(int tiles) {
|
---|
133 | ((MemoryTileCache) getTileCache()).setCacheSize(tiles);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Callback for the OsmMapControl. (Re-)Sets the start and end point of the
|
---|
139 | * selection rectangle.
|
---|
140 | *
|
---|
141 | * @param aStart
|
---|
142 | * @param aEnd
|
---|
143 | */
|
---|
144 | public void setSelection(Point aStart, Point aEnd) {
|
---|
145 | if (aStart == null || aEnd == null || aStart.x == aEnd.x || aStart.y == aEnd.y)
|
---|
146 | return;
|
---|
147 |
|
---|
148 | Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y));
|
---|
149 | Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y));
|
---|
150 |
|
---|
151 | Point tlc = getTopLeftCoordinates();
|
---|
152 | int zoomDiff = MAX_ZOOM - zoom;
|
---|
153 | Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y);
|
---|
154 | Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y);
|
---|
155 |
|
---|
156 | pEnd.x <<= zoomDiff;
|
---|
157 | pEnd.y <<= zoomDiff;
|
---|
158 | pStart.x <<= zoomDiff;
|
---|
159 | pStart.y <<= zoomDiff;
|
---|
160 |
|
---|
161 | iSelectionRectStart = pStart;
|
---|
162 | iSelectionRectEnd = pEnd;
|
---|
163 |
|
---|
164 | Coordinate l1 = getPosition(p_max);
|
---|
165 | Coordinate l2 = getPosition(p_min);
|
---|
166 | Bounds b = new Bounds(
|
---|
167 | new LatLon(
|
---|
168 | Math.min(l2.getLat(), l1.getLat()),
|
---|
169 | Math.min(l1.getLon(), l2.getLon())
|
---|
170 | ),
|
---|
171 | new LatLon(
|
---|
172 | Math.max(l2.getLat(), l1.getLat()),
|
---|
173 | Math.max(l1.getLon(), l2.getLon()))
|
---|
174 | );
|
---|
175 | Bounds oldValue = this.bbox;
|
---|
176 | this.bbox = b;
|
---|
177 | firePropertyChange(BBOX_PROP, oldValue, this.bbox);
|
---|
178 | repaint();
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Performs resizing of the DownloadDialog in order to enlarge or shrink the
|
---|
183 | * map.
|
---|
184 | */
|
---|
185 | public void resizeSlippyMap() {
|
---|
186 | int w, h;
|
---|
187 |
|
---|
188 | // retrieve the size of the display
|
---|
189 | Dimension iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
190 |
|
---|
191 | // enlarge
|
---|
192 | if(iDownloadDialogDimension == null) {
|
---|
193 | // make the each dimension 90% of the absolute display size
|
---|
194 | w = iScreenSize.width * 90 / 100;
|
---|
195 | h = iScreenSize.height * 90 / 100;
|
---|
196 | //iDownloadDialogDimension = iGui.getSize();
|
---|
197 | }
|
---|
198 | // shrink
|
---|
199 | else {
|
---|
200 | // set the size back to the initial dimensions
|
---|
201 | w = iDownloadDialogDimension.width;
|
---|
202 | h = iDownloadDialogDimension.height;
|
---|
203 | iDownloadDialogDimension = null;
|
---|
204 | }
|
---|
205 | // resize and center the DownloadDialog
|
---|
206 | //iGui.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
|
---|
207 |
|
---|
208 | repaint();
|
---|
209 | }
|
---|
210 |
|
---|
211 | public void toggleMapSource(int mapSource) {
|
---|
212 | this.tileController.setTileCache(new MemoryTileCache());
|
---|
213 | if (mapSource == SourceButton.MAPNIK) {
|
---|
214 | this.setTileSource(TILE_SOURCES[0]);
|
---|
215 | Main.pref.put("slippy_map_chooser.mapstyle", "mapnik");
|
---|
216 | } else if (mapSource == SourceButton.CYCLEMAP) {
|
---|
217 | this.setTileSource(TILE_SOURCES[2]);
|
---|
218 | Main.pref.put("slippy_map_chooser.mapstyle", "cyclemap");
|
---|
219 | } else {
|
---|
220 | this.setTileSource(TILE_SOURCES[1]);
|
---|
221 | Main.pref.put("slippy_map_chooser.mapstyle", "osmarender");
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public Bounds getBoundingBox() {
|
---|
227 | return bbox;
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public void setBoundingBox(Bounds bbox) {
|
---|
232 | if (bbox == null) {
|
---|
233 | Bounds oldValue = this.bbox;
|
---|
234 | this.bbox = null;
|
---|
235 | firePropertyChange(BBOX_PROP, oldValue, this.bbox);
|
---|
236 | return;
|
---|
237 | }
|
---|
238 | // test if a bounding box has been set
|
---|
239 | if (bbox.getMin().lat() == 0.0 && bbox.getMin().lon() == 0.0 && bbox.getMax().lat() == 0.0 && bbox.getMax().lon() == 0.0) {
|
---|
240 | Bounds oldValue = this.bbox;
|
---|
241 | this.bbox = null;
|
---|
242 | firePropertyChange(BBOX_PROP, oldValue, this.bbox);
|
---|
243 | }
|
---|
244 |
|
---|
245 | int y1 = OsmMercator.LatToY(bbox.getMin().lat(), MAX_ZOOM);
|
---|
246 | int y2 = OsmMercator.LatToY(bbox.getMax().lat(), MAX_ZOOM);
|
---|
247 | int x1 = OsmMercator.LonToX(bbox.getMin().lon(), MAX_ZOOM);
|
---|
248 | int x2 = OsmMercator.LonToX(bbox.getMax().lon(), MAX_ZOOM);
|
---|
249 |
|
---|
250 | iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2));
|
---|
251 | iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2));
|
---|
252 |
|
---|
253 | // calc the screen coordinates for the new selection rectangle
|
---|
254 | MapMarkerDot xmin_ymin = new MapMarkerDot(bbox.getMin().lat(), bbox.getMin().lon());
|
---|
255 | MapMarkerDot xmax_ymax = new MapMarkerDot(bbox.getMax().lat(), bbox.getMax().lon());
|
---|
256 |
|
---|
257 | Vector<MapMarker> marker = new Vector<MapMarker>(2);
|
---|
258 | marker.add(xmin_ymin);
|
---|
259 | marker.add(xmax_ymax);
|
---|
260 | setMapMarkerList(marker);
|
---|
261 | setDisplayToFitMapMarkers();
|
---|
262 | zoomOut();
|
---|
263 |
|
---|
264 | Bounds oldValue = this.bbox;
|
---|
265 | this.bbox = bbox;
|
---|
266 | firePropertyChange(BBOX_PROP, oldValue, this.bbox);
|
---|
267 | }
|
---|
268 | }
|
---|