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

Last change on this file since 9623 was 9623, checked in by tim, 18 years ago

ADDED:
Quick and dirty implementation to switch between Mapnik and Osmarender map tiles

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