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

Last change on this file since 9537 was 9537, checked in by stotz, 16 years ago

Slippy-Map-Chooser now uses an extended version of JMapViewer instead of it's own implementation

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