source: osm/applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java@ 9851

Last change on this file since 9851 was 9851, checked in by stotz, 18 years ago

Updated because of changes in JMapViewer

File size: 7.7 KB
Line 
1// This code has been adapted and copied from code that has been written by Immanuel Scholz and others for JOSM.
2// License: GPL. Copyright 2007 by Tim Haussmann
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Color;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.Graphics;
11import java.awt.Point;
12import java.awt.Toolkit;
13import java.awt.geom.Point2D;
14import java.util.Vector;
15
16import javax.swing.JLabel;
17import javax.swing.JPanel;
18
19import org.openstreetmap.gui.jmapviewer.JMapViewer;
20import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
21import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
22import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
23import org.openstreetmap.gui.jmapviewer.OsmMercator;
24import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
25import org.openstreetmap.gui.jmapviewer.OsmTileSource;
26import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
27import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
28import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
29import org.openstreetmap.josm.gui.download.DownloadDialog;
30import org.openstreetmap.josm.gui.download.DownloadSelection;
31
32/**
33 * JComponent that displays the slippy map tiles
34 *
35 * @author Tim Haussmann
36 *
37 */
38public class SlippyMapChooser extends JMapViewer implements DownloadSelection {
39
40 private DownloadDialog iGui;
41
42 // upper left and lower right corners of the selection rectangle (x/y on
43 // ZOOM_MAX)
44 Point iSelectionRectStart;
45 Point iSelectionRectEnd;
46
47 private SizeButton iSizeButton = new SizeButton();
48 private SourceButton iSourceButton = new SourceButton();
49
50 // standard dimension
51 private Dimension iDownloadDialogDimension;
52 // screen size
53 private Dimension iScreenSize;
54
55 private TileSource[] sources = { new OsmTileSource.Mapnik(), new OsmTileSource.TilesAtHome() };
56 TileLoader cachedLoader;
57 TileLoader uncachedLoader;
58
59 /**
60 * Create the chooser component.
61 */
62 public SlippyMapChooser() {
63 super();
64 cachedLoader = new OsmFileCacheTileLoader(this);
65 uncachedLoader = new OsmTileLoader(this);
66 setZoomContolsVisible(false);
67 setMapMarkerVisible(false);
68 setMinimumSize(new Dimension(350, 350 / 2));
69 setFileCacheEnabled(SlippyMapChooserPlugin.ENABLE_FILE_CACHE);
70 setMaxTilesInmemory(SlippyMapChooserPlugin.MAX_TILES_IN_MEMORY);
71 }
72
73 public void setMaxTilesInmemory(int tiles) {
74 ((MemoryTileCache) getTileCache()).setCacheSize(tiles);
75 }
76
77 public void setFileCacheEnabled(boolean enabled) {
78 if (enabled)
79 setTileLoader(cachedLoader);
80 else
81 setTileLoader(uncachedLoader);
82 }
83
84 public void addGui(final DownloadDialog gui) {
85 iGui = gui;
86 JPanel temp = new JPanel();
87 temp.setLayout(new BorderLayout());
88 temp.add(this, BorderLayout.CENTER);
89 temp.add(new JLabel((tr("Zoom: Mousewheel or double click. "
90 + "Move map: Hold right mousebutton and move mouse. Select: Click."))),
91 BorderLayout.SOUTH);
92 iGui.tabpane.add(temp, tr("Slippy map"));
93
94 new OsmMapControl(this, temp, iSizeButton, iSourceButton);
95 boundingBoxChanged(gui);
96 }
97
98 protected Point getTopLeftCoordinates() {
99 return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
100 }
101
102 /**
103 * Draw the map.
104 */
105 @Override
106 public void paint(Graphics g) {
107 try {
108 super.paint(g);
109
110 // draw selection rectangle
111 if (iSelectionRectStart != null && iSelectionRectEnd != null) {
112
113 int zoomDiff = MAX_ZOOM - zoom;
114 Point tlc = getTopLeftCoordinates();
115 int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x;
116 int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y;
117 int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x;
118 int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y;
119
120 int w = x_max - x_min;
121 int h = y_max - y_min;
122 g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
123 g.fillRect(x_min, y_min, w, h);
124
125 g.setColor(Color.BLACK);
126 g.drawRect(x_min, y_min, w, h);
127
128 }
129
130 iSizeButton.paint(g);
131 iSourceButton.paint(g);
132 } catch (Exception e) {
133 e.printStackTrace();
134 }
135 }
136
137 public void boundingBoxChanged(DownloadDialog gui) {
138
139 // test if a bounding box has been set set
140 if (gui.minlat == 0.0 && gui.minlon == 0.0 && gui.maxlat == 0.0 && gui.maxlon == 0.0)
141 return;
142
143 int y1 = OsmMercator.LatToY(gui.minlat, MAX_ZOOM);
144 int y2 = OsmMercator.LatToY(gui.maxlat, MAX_ZOOM);
145 int x1 = OsmMercator.LonToX(gui.minlon, MAX_ZOOM);
146 int x2 = OsmMercator.LonToX(gui.maxlon, MAX_ZOOM);
147
148 iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2));
149 iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2));
150
151 // calc the screen coordinates for the new selection rectangle
152 MapMarkerDot xmin_ymin = new MapMarkerDot(gui.minlat, gui.minlon);
153 MapMarkerDot xmax_ymax = new MapMarkerDot(gui.maxlat, gui.maxlon);
154
155 Vector<MapMarker> marker = new Vector<MapMarker>(2);
156 marker.add(xmin_ymin);
157 marker.add(xmax_ymax);
158 setMapMarkerList(marker);
159 setDisplayToFitMapMarkers();
160 zoomOut();
161 }
162
163 /**
164 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the
165 * selection rectangle.
166 *
167 * @param aStart
168 * @param aEnd
169 */
170 public void setSelection(Point aStart, Point aEnd) {
171 if (aStart == null || aEnd == null)
172 return;
173 Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y));
174 Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y));
175
176 Point tlc = getTopLeftCoordinates();
177 int zoomDiff = MAX_ZOOM - zoom;
178 Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y);
179 Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y);
180
181 pEnd.x <<= zoomDiff;
182 pEnd.y <<= zoomDiff;
183 pStart.x <<= zoomDiff;
184 pStart.y <<= zoomDiff;
185
186 iSelectionRectStart = pStart;
187 iSelectionRectEnd = pEnd;
188
189 Point2D.Double l1 = getPosition(p_max);
190 Point2D.Double l2 = getPosition(p_min);
191 iGui.minlat = Math.min(l2.x, l1.x);
192 iGui.minlon = Math.min(l1.y, l2.y);
193 iGui.maxlat = Math.max(l2.x, l1.x);
194 iGui.maxlon = Math.max(l1.y, l2.y);
195
196 iGui.boundingBoxChanged(this);
197 repaint();
198 }
199
200 /**
201 * Performs resizing of the DownloadDialog in order to enlarge or shrink the
202 * map.
203 */
204 public void resizeSlippyMap() {
205 if (iScreenSize == null) {
206 Component c =
207 iGui.getParent().getParent().getParent().getParent().getParent().getParent()
208 .getParent().getParent().getParent();
209 // remember the initial set screen dimensions
210 iDownloadDialogDimension = c.getSize();
211 // retrive the size of the display
212 iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
213 }
214
215 // resize
216 Component co =
217 iGui.getParent().getParent().getParent().getParent().getParent().getParent()
218 .getParent().getParent().getParent();
219 Dimension currentDimension = co.getSize();
220
221 // enlarge
222 if (currentDimension.equals(iDownloadDialogDimension)) {
223 // make the each dimension 90% of the absolute display size and
224 // center the DownloadDialog
225 int w = iScreenSize.width * 90 / 100;
226 int h = iScreenSize.height * 90 / 100;
227 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
228
229 }
230 // shrink
231 else {
232 // set the size back to the initial dimensions and center the
233 // DownloadDialog
234 int w = iDownloadDialogDimension.width;
235 int h = iDownloadDialogDimension.height;
236 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
237
238 }
239
240 repaint();
241 }
242
243 public void toggleMapSource(int mapSource) {
244 this.tileCache = new MemoryTileCache();
245 if (mapSource == SourceButton.MAPNIK) {
246 this.setTileSource(sources[0]);
247 } else {
248 this.setTileSource(sources[1]);
249 }
250 }
251
252}
Note: See TracBrowser for help on using the repository browser.