source: josm/trunk/src/org/openstreetmap/josm/gui/bbox/TileSelectionBBoxChooser.java@ 6084

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 25.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.bbox;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.AWTKeyStroke;
7import java.awt.BorderLayout;
8import java.awt.Color;
9import java.awt.FlowLayout;
10import java.awt.Graphics;
11import java.awt.GridBagConstraints;
12import java.awt.GridBagLayout;
13import java.awt.Insets;
14import java.awt.KeyboardFocusManager;
15import java.awt.Point;
16import java.awt.event.ActionEvent;
17import java.awt.event.ActionListener;
18import java.awt.event.FocusEvent;
19import java.awt.event.FocusListener;
20import java.awt.event.KeyEvent;
21import java.beans.PropertyChangeEvent;
22import java.beans.PropertyChangeListener;
23import java.util.HashSet;
24import java.util.Set;
25import java.util.Vector;
26import java.util.regex.Matcher;
27import java.util.regex.Pattern;
28
29import javax.swing.AbstractAction;
30import javax.swing.BorderFactory;
31import javax.swing.JButton;
32import javax.swing.JLabel;
33import javax.swing.JPanel;
34import javax.swing.JSpinner;
35import javax.swing.KeyStroke;
36import javax.swing.SpinnerNumberModel;
37import javax.swing.event.ChangeEvent;
38import javax.swing.event.ChangeListener;
39import javax.swing.text.JTextComponent;
40
41import org.openstreetmap.gui.jmapviewer.JMapViewer;
42import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
43import org.openstreetmap.gui.jmapviewer.OsmMercator;
44import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
45import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
46import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
47import org.openstreetmap.josm.data.Bounds;
48import org.openstreetmap.josm.data.Version;
49import org.openstreetmap.josm.data.coor.LatLon;
50import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
51import org.openstreetmap.josm.gui.widgets.HtmlPanel;
52import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
53import org.openstreetmap.josm.tools.ImageProvider;
54import org.openstreetmap.josm.gui.widgets.JosmTextField;
55
56/**
57 * TileSelectionBBoxChooser allows to select a bounding box (i.e. for downloading) based
58 * on OSM tile numbers.
59 *
60 * TileSelectionBBoxChooser can be embedded as component in a Swing container. Example:
61 * <pre>
62 * JFrame f = new JFrame(....);
63 * f.getContentPane().setLayout(new BorderLayout()));
64 * TileSelectionBBoxChooser chooser = new TileSelectionBBoxChooser();
65 * f.add(chooser, BorderLayout.CENTER);
66 * chooser.addPropertyChangeListener(new PropertyChangeListener() {
67 * public void propertyChange(PropertyChangeEvent evt) {
68 * // listen for BBOX events
69 * if (evt.getPropertyName().equals(BBoxChooser.BBOX_PROP)) {
70 * System.out.println("new bbox based on OSM tiles selected: " + (Bounds)evt.getNewValue());
71 * }
72 * }
73 * });
74 *
75 * // init the chooser with a bounding box
76 * chooser.setBoundingBox(....);
77 *
78 * f.setVisible(true);
79 * </pre>
80 */
81public class TileSelectionBBoxChooser extends JPanel implements BBoxChooser{
82
83 /** the current bounding box */
84 private Bounds bbox;
85 /** the map viewer showing the selected bounding box */
86 private TileBoundsMapView mapViewer;
87 /** a panel for entering a bounding box given by a tile grid and a zoom level */
88 private TileGridInputPanel pnlTileGrid;
89 /** a panel for entering a bounding box given by the address of an individual OSM tile at
90 * a given zoom level
91 */
92 private TileAddressInputPanel pnlTileAddress;
93
94 /**
95 * builds the UI
96 */
97 protected void build() {
98 setLayout(new GridBagLayout());
99
100 GridBagConstraints gc = new GridBagConstraints();
101 gc.weightx = 0.5;
102 gc.fill = GridBagConstraints.HORIZONTAL;
103 gc.anchor = GridBagConstraints.NORTHWEST;
104 add(pnlTileGrid = new TileGridInputPanel(), gc);
105
106 gc.gridx = 1;
107 add(pnlTileAddress = new TileAddressInputPanel(), gc);
108
109 gc.gridx = 0;
110 gc.gridy = 1;
111 gc.gridwidth = 2;
112 gc.weightx = 1.0;
113 gc.weighty = 1.0;
114 gc.fill = GridBagConstraints.BOTH;
115 gc.insets = new Insets(2,2,2,2);
116 add(mapViewer = new TileBoundsMapView(), gc);
117 mapViewer.setFocusable(false);
118 mapViewer.setZoomContolsVisible(false);
119 mapViewer.setMapMarkerVisible(false);
120
121 pnlTileAddress.addPropertyChangeListener(pnlTileGrid);
122 pnlTileGrid.addPropertyChangeListener(new TileBoundsChangeListener());
123 }
124
125 /**
126 * Constructs a new {@code TileSelectionBBoxChooser}.
127 */
128 public TileSelectionBBoxChooser() {
129 build();
130 }
131
132 /**
133 * Replies the current bounding box. null, if no valid bounding box is currently selected.
134 *
135 */
136 @Override
137 public Bounds getBoundingBox() {
138 return bbox;
139 }
140
141 /**
142 * Sets the current bounding box.
143 *
144 * @param bbox the bounding box. null, if this widget isn't initialized with a bounding box
145 */
146 @Override
147 public void setBoundingBox(Bounds bbox) {
148 pnlTileGrid.initFromBoundingBox(bbox);
149 }
150
151 protected void refreshMapView() {
152 if (bbox == null) return;
153
154 // calc the screen coordinates for the new selection rectangle
155 MapMarkerDot xmin_ymin = new MapMarkerDot(bbox.getMin().lat(), bbox.getMin().lon());
156 MapMarkerDot xmax_ymax = new MapMarkerDot(bbox.getMax().lat(), bbox.getMax().lon());
157
158 Vector<MapMarker> marker = new Vector<MapMarker>(2);
159 marker.add(xmin_ymin);
160 marker.add(xmax_ymax);
161 mapViewer.setBoundingBox(bbox);
162 mapViewer.setMapMarkerList(marker);
163 mapViewer.setDisplayToFitMapMarkers();
164 mapViewer.zoomOut();
165 }
166
167 /**
168 * Computes the bounding box given a tile grid.
169 *
170 * @param tb the description of the tile grid
171 * @return the bounding box
172 */
173 protected Bounds convertTileBoundsToBoundingBox(TileBounds tb) {
174 LatLon min = getNorthWestLatLonOfTile(tb.min, tb.zoomLevel);
175 Point p = new Point(tb.max);
176 p.x++;
177 p.y++;
178 LatLon max = getNorthWestLatLonOfTile(p, tb.zoomLevel);
179 return new Bounds(max.lat(), min.lon(), min.lat(), max.lon());
180 }
181
182 /**
183 * Replies lat/lon of the north/west-corner of a tile at a specific zoom level
184 *
185 * @param tile the tile address (x,y)
186 * @param zoom the zoom level
187 * @return lat/lon of the north/west-corner of a tile at a specific zoom level
188 */
189 protected LatLon getNorthWestLatLonOfTile(Point tile, int zoom) {
190 double lon = tile.x / Math.pow(2.0, zoom) * 360.0 - 180;
191 double lat = Math.toDegrees(Math.atan(Math.sinh(Math.PI - (2.0 * Math.PI * tile.y) / Math.pow(2.0, zoom))));
192 return new LatLon(lat, lon);
193 }
194
195 /**
196 * Listens to changes in the selected tile bounds, refreshes the map view and emits
197 * property change events for {@link BBoxChooser#BBOX_PROP}
198 */
199 class TileBoundsChangeListener implements PropertyChangeListener {
200 @Override
201 public void propertyChange(PropertyChangeEvent evt) {
202 if (!evt.getPropertyName().equals(TileGridInputPanel.TILE_BOUNDS_PROP)) return;
203 TileBounds tb = (TileBounds)evt.getNewValue();
204 Bounds oldValue = TileSelectionBBoxChooser.this.bbox;
205 TileSelectionBBoxChooser.this.bbox = convertTileBoundsToBoundingBox(tb);
206 firePropertyChange(BBOX_PROP, oldValue, TileSelectionBBoxChooser.this.bbox);
207 refreshMapView();
208 }
209 }
210
211 /**
212 * A panel for describing a rectangular area of OSM tiles at a given zoom level.
213 *
214 * The panel emits PropertyChangeEvents for the property {@link TileGridInputPanel#TILE_BOUNDS_PROP}
215 * when the user successfully enters a valid tile grid specification.
216 *
217 */
218 static private class TileGridInputPanel extends JPanel implements PropertyChangeListener{
219 static public final String TILE_BOUNDS_PROP = TileGridInputPanel.class.getName() + ".tileBounds";
220
221 private JosmTextField tfMaxY;
222 private JosmTextField tfMinY;
223 private JosmTextField tfMaxX;
224 private JosmTextField tfMinX;
225 private TileCoordinateValidator valMaxY;
226 private TileCoordinateValidator valMinY;
227 private TileCoordinateValidator valMaxX;
228 private TileCoordinateValidator valMinX;
229 private JSpinner spZoomLevel;
230 private TileBoundsBuilder tileBoundsBuilder = new TileBoundsBuilder();
231 private boolean doFireTileBoundChanged = true;
232
233 protected JPanel buildTextPanel() {
234 JPanel pnl = new JPanel(new BorderLayout());
235 HtmlPanel msg = new HtmlPanel();
236 msg.setText(tr("<html>Please select a <strong>range of OSM tiles</strong> at a given zoom level.</html>"));
237 pnl.add(msg);
238 return pnl;
239 }
240
241 protected JPanel buildZoomLevelPanel() {
242 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
243 pnl.add(new JLabel(tr("Zoom level:")));
244 pnl.add(spZoomLevel = new JSpinner(new SpinnerNumberModel(0,0,18,1)));
245 spZoomLevel.addChangeListener(new ZomeLevelChangeHandler());
246 spZoomLevel.addChangeListener(tileBoundsBuilder);
247 return pnl;
248 }
249
250 protected JPanel buildTileGridInputPanel() {
251 JPanel pnl = new JPanel(new GridBagLayout());
252 pnl.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
253 GridBagConstraints gc = new GridBagConstraints();
254 gc.anchor = GridBagConstraints.NORTHWEST;
255 gc.insets = new Insets(0, 0, 2, 2);
256
257 gc.gridwidth = 2;
258 gc.gridx = 1;
259 gc.fill = GridBagConstraints.HORIZONTAL;
260 pnl.add(buildZoomLevelPanel(), gc);
261
262 gc.gridwidth = 1;
263 gc.gridy = 1;
264 gc.gridx = 1;
265 pnl.add(new JLabel(tr("from tile")), gc);
266
267 gc.gridx = 2;
268 pnl.add(new JLabel(tr("up to tile")), gc);
269
270 gc.gridx = 0;
271 gc.gridy = 2;
272 gc.weightx = 0.0;
273 pnl.add(new JLabel("X:"), gc);
274
275
276 gc.gridx = 1;
277 gc.weightx = 0.5;
278 pnl.add(tfMinX = new JosmTextField(), gc);
279 valMinX = new TileCoordinateValidator(tfMinX);
280 SelectAllOnFocusGainedDecorator.decorate(tfMinX);
281 tfMinX.addActionListener(tileBoundsBuilder);
282 tfMinX.addFocusListener(tileBoundsBuilder);
283
284 gc.gridx = 2;
285 gc.weightx = 0.5;
286 pnl.add(tfMaxX = new JosmTextField(), gc);
287 valMaxX = new TileCoordinateValidator(tfMaxX);
288 SelectAllOnFocusGainedDecorator.decorate(tfMaxX);
289 tfMaxX.addActionListener(tileBoundsBuilder);
290 tfMaxX.addFocusListener(tileBoundsBuilder);
291
292 gc.gridx = 0;
293 gc.gridy = 3;
294 gc.weightx = 0.0;
295 pnl.add(new JLabel("Y:"), gc);
296
297 gc.gridx = 1;
298 gc.weightx = 0.5;
299 pnl.add(tfMinY = new JosmTextField(), gc);
300 valMinY = new TileCoordinateValidator(tfMinY);
301 SelectAllOnFocusGainedDecorator.decorate(tfMinY);
302 tfMinY.addActionListener(tileBoundsBuilder);
303 tfMinY.addFocusListener(tileBoundsBuilder);
304
305 gc.gridx = 2;
306 gc.weightx = 0.5;
307 pnl.add(tfMaxY = new JosmTextField(), gc);
308 valMaxY = new TileCoordinateValidator(tfMaxY);
309 SelectAllOnFocusGainedDecorator.decorate(tfMaxY);
310 tfMaxY.addActionListener(tileBoundsBuilder);
311 tfMaxY.addFocusListener(tileBoundsBuilder);
312
313 gc.gridy = 4;
314 gc.gridx = 0;
315 gc.gridwidth = 3;
316 gc.weightx = 1.0;
317 gc.weighty = 1.0;
318 gc.fill = GridBagConstraints.BOTH;
319 pnl.add(new JPanel(), gc);
320 return pnl;
321 }
322
323 protected void build() {
324 setLayout(new BorderLayout());
325 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
326 add(buildTextPanel(), BorderLayout.NORTH);
327 add(buildTileGridInputPanel(), BorderLayout.CENTER);
328
329 Set<AWTKeyStroke> forwardKeys = new HashSet<AWTKeyStroke>(getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
330 forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
331 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,forwardKeys);
332 }
333
334 public TileGridInputPanel() {
335 build();
336 }
337
338 public void initFromBoundingBox(Bounds bbox) {
339 if (bbox == null)
340 return;
341 TileBounds tb = new TileBounds();
342 tb.zoomLevel = (Integer) spZoomLevel.getValue();
343 tb.min = new Point(
344 Math.max(0,lonToTileX(tb.zoomLevel, bbox.getMin().lon())),
345 Math.max(0,latToTileY(tb.zoomLevel, bbox.getMax().lat()-.00001))
346 );
347 tb.max = new Point(
348 Math.max(0,lonToTileX(tb.zoomLevel, bbox.getMax().lon())),
349 Math.max(0,latToTileY(tb.zoomLevel, bbox.getMin().lat()-.00001))
350 );
351 doFireTileBoundChanged = false;
352 setTileBounds(tb);
353 doFireTileBoundChanged = true;
354 }
355
356 public static int latToTileY(int zoom, double lat) {
357 if ((zoom < 3) || (zoom > 18)) return -1;
358 double l = lat / 180 * Math.PI;
359 double pf = Math.log(Math.tan(l) + (1/Math.cos(l)));
360 return (int) ((1<<(zoom-1)) * (Math.PI - pf) / Math.PI);
361 }
362
363 public static int lonToTileX(int zoom, double lon) {
364 if ((zoom < 3) || (zoom > 18)) return -1;
365 return (int) ((1<<(zoom-3)) * (lon + 180.0) / 45.0);
366 }
367
368 public void setTileBounds(TileBounds tileBounds) {
369 tfMinX.setText(Integer.toString(tileBounds.min.x));
370 tfMinY.setText(Integer.toString(tileBounds.min.y));
371 tfMaxX.setText(Integer.toString(tileBounds.max.x));
372 tfMaxY.setText(Integer.toString(tileBounds.max.y));
373 spZoomLevel.setValue(tileBounds.zoomLevel);
374 }
375
376 @Override
377 public void propertyChange(PropertyChangeEvent evt) {
378 if (evt.getPropertyName().equals(TileAddressInputPanel.TILE_BOUNDS_PROP)) {
379 TileBounds tb = (TileBounds)evt.getNewValue();
380 setTileBounds(tb);
381 fireTileBoundsChanged(tb);
382 }
383 }
384
385 protected void fireTileBoundsChanged(TileBounds tb) {
386 if (!doFireTileBoundChanged) return;
387 firePropertyChange(TILE_BOUNDS_PROP, null, tb);
388 }
389
390 class ZomeLevelChangeHandler implements ChangeListener {
391 @Override
392 public void stateChanged(ChangeEvent e) {
393 int zoomLevel = (Integer)spZoomLevel.getValue();
394 valMaxX.setZoomLevel(zoomLevel);
395 valMaxY.setZoomLevel(zoomLevel);
396 valMinX.setZoomLevel(zoomLevel);
397 valMinY.setZoomLevel(zoomLevel);
398 }
399 }
400
401 class TileBoundsBuilder implements ActionListener, FocusListener, ChangeListener {
402 protected void buildTileBounds() {
403 if (!valMaxX.isValid()) return;
404 if (!valMaxY.isValid()) return;
405 if (!valMinX.isValid()) return;
406 if (!valMinY.isValid()) return;
407 Point min = new Point(valMinX.getTileIndex(), valMinY.getTileIndex());
408 Point max = new Point(valMaxX.getTileIndex(), valMaxY.getTileIndex());
409 if (min.x > max.x) {
410
411 }
412 int zoomlevel = (Integer)spZoomLevel.getValue();
413 TileBounds tb = new TileBounds(min, max, zoomlevel);
414 fireTileBoundsChanged(tb);
415 }
416
417 @Override
418 public void focusGained(FocusEvent e) {/* irrelevant */}
419
420 @Override
421 public void focusLost(FocusEvent e) {
422 buildTileBounds();
423 }
424
425 @Override
426 public void actionPerformed(ActionEvent e) {
427 buildTileBounds();
428 }
429
430 @Override
431 public void stateChanged(ChangeEvent e) {
432 buildTileBounds();
433 }
434 }
435 }
436
437 /**
438 * A panel for entering the address of a single OSM tile at a given zoom level.
439 *
440 */
441 static private class TileAddressInputPanel extends JPanel {
442
443 static public final String TILE_BOUNDS_PROP = TileAddressInputPanel.class.getName() + ".tileBounds";
444
445 private JosmTextField tfTileAddress;
446 private TileAddressValidator valTileAddress;
447
448 protected JPanel buildTextPanel() {
449 JPanel pnl = new JPanel(new BorderLayout());
450 HtmlPanel msg = new HtmlPanel();
451 msg.setText(tr("<html>Alternatively you may enter a <strong>tile address</strong> for a single tile "
452 + "in the format <i>zoomlevel/x/y</i>, i.e. <i>15/256/223</i>. Tile addresses "
453 + "in the format <i>zoom,x,y</i> or <i>zoom;x;y</i> are valid too.</html>"));
454 pnl.add(msg);
455 return pnl;
456 }
457
458 protected JPanel buildTileAddressInputPanel() {
459 JPanel pnl = new JPanel(new GridBagLayout());
460 GridBagConstraints gc = new GridBagConstraints();
461 gc.anchor = GridBagConstraints.NORTHWEST;
462 gc.fill = GridBagConstraints.HORIZONTAL;
463 gc.weightx = 0.0;
464 gc.insets = new Insets(0,0,2,2);
465 pnl.add(new JLabel(tr("Tile address:")), gc);
466
467 gc.weightx = 1.0;
468 gc.gridx = 1;
469 pnl.add(tfTileAddress = new JosmTextField(), gc);
470 valTileAddress = new TileAddressValidator(tfTileAddress);
471 SelectAllOnFocusGainedDecorator.decorate(tfTileAddress);
472
473 gc.weightx = 0.0;
474 gc.gridx = 2;
475 ApplyTileAddressAction applyTileAddressAction = new ApplyTileAddressAction();
476 JButton btn = new JButton(applyTileAddressAction);
477 btn.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
478 pnl.add(btn, gc);
479 tfTileAddress.addActionListener(applyTileAddressAction);
480 return pnl;
481 }
482
483 protected void build() {
484 setLayout(new GridBagLayout());
485 GridBagConstraints gc = new GridBagConstraints();
486 gc.anchor = GridBagConstraints.NORTHWEST;
487 gc.fill = GridBagConstraints.HORIZONTAL;
488 gc.weightx = 1.0;
489 gc.insets = new Insets(0,0,5,0);
490 add(buildTextPanel(), gc);
491
492 gc.gridy = 1;
493 add(buildTileAddressInputPanel(), gc);
494
495 // filler - grab remaining space
496 gc.gridy = 2;
497 gc.fill = GridBagConstraints.BOTH;
498 gc.weighty = 1.0;
499 add(new JPanel(), gc);
500 }
501
502 public TileAddressInputPanel() {
503 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
504 build();
505 }
506
507 protected void fireTileBoundsChanged(TileBounds tb){
508 firePropertyChange(TILE_BOUNDS_PROP, null, tb);
509 }
510
511 class ApplyTileAddressAction extends AbstractAction {
512 public ApplyTileAddressAction() {
513 putValue(SMALL_ICON, ImageProvider.get("apply"));
514 putValue(SHORT_DESCRIPTION, tr("Apply the tile address"));
515 }
516
517 @Override
518 public void actionPerformed(ActionEvent e) {
519 TileBounds tb = valTileAddress.getTileBounds();
520 if (tb != null) {
521 fireTileBoundsChanged(tb);
522 }
523 }
524 }
525 }
526
527 /**
528 * Validates a tile address
529 */
530 static private class TileAddressValidator extends AbstractTextComponentValidator {
531
532 private TileBounds tileBounds = null;
533
534 public TileAddressValidator(JTextComponent tc) throws IllegalArgumentException {
535 super(tc);
536 }
537
538 @Override
539 public boolean isValid() {
540 String value = getComponent().getText().trim();
541 Matcher m = Pattern.compile("(\\d+)[^\\d]+(\\d+)[^\\d]+(\\d+)").matcher(value);
542 tileBounds = null;
543 if (!m.matches()) return false;
544 int zoom;
545 try {
546 zoom = Integer.parseInt(m.group(1));
547 } catch(NumberFormatException e){
548 return false;
549 }
550 if (zoom < 0 || zoom > 18) return false;
551
552 int x;
553 try {
554 x = Integer.parseInt(m.group(2));
555 } catch(NumberFormatException e){
556 return false;
557 }
558 if (x < 0 || x >= Math.pow(2, zoom)) return false;
559 int y;
560 try {
561 y = Integer.parseInt(m.group(3));
562 } catch(NumberFormatException e){
563 return false;
564 }
565 if (y < 0 || y >= Math.pow(2, zoom)) return false;
566
567 tileBounds = new TileBounds(new Point(x,y), new Point(x,y), zoom);
568 return true;
569 }
570
571 @Override
572 public void validate() {
573 if (isValid()) {
574 feedbackValid(tr("Please enter a tile address"));
575 } else {
576 feedbackInvalid(tr("The current value isn''t a valid tile address", getComponent().getText()));
577 }
578 }
579
580 public TileBounds getTileBounds() {
581 return tileBounds;
582 }
583 }
584
585 /**
586 * Validates the x- or y-coordinate of a tile at a given zoom level.
587 *
588 */
589 static private class TileCoordinateValidator extends AbstractTextComponentValidator {
590 private int zoomLevel;
591 private int tileIndex;
592
593 public TileCoordinateValidator(JTextComponent tc) throws IllegalArgumentException {
594 super(tc);
595 }
596
597 public void setZoomLevel(int zoomLevel) {
598 this.zoomLevel = zoomLevel;
599 validate();
600 }
601
602 @Override
603 public boolean isValid() {
604 String value = getComponent().getText().trim();
605 try {
606 if (value.equals("")) {
607 tileIndex = 0;
608 } else {
609 tileIndex = Integer.parseInt(value);
610 }
611 } catch(NumberFormatException e) {
612 return false;
613 }
614 if (tileIndex < 0 || tileIndex >= Math.pow(2, zoomLevel)) return false;
615
616 return true;
617 }
618
619 @Override
620 public void validate() {
621 if (isValid()) {
622 feedbackValid(tr("Please enter a tile index"));
623 } else {
624 feedbackInvalid(tr("The current value isn''t a valid tile index for the given zoom level", getComponent().getText()));
625 }
626 }
627
628 public int getTileIndex() {
629 return tileIndex;
630 }
631 }
632
633 /**
634 * Represents a rectangular area of tiles at a given zoom level.
635 *
636 */
637 static private class TileBounds {
638 public Point min;
639 public Point max;
640 public int zoomLevel;
641
642 public TileBounds() {
643 zoomLevel = 0;
644 min = new Point(0,0);
645 max = new Point(0,0);
646 }
647
648 public TileBounds(Point min, Point max, int zoomLevel) {
649 this.min = min;
650 this.max = max;
651 this.zoomLevel = zoomLevel;
652 }
653
654 @Override
655 public String toString() {
656 StringBuffer sb = new StringBuffer();
657 sb.append("min=").append(min.x).append(",").append(min.y).append(",");
658 sb.append("max=").append(max.x).append(",").append(max.y).append(",");
659 sb.append("zoom=").append(zoomLevel);
660 return sb.toString();
661 }
662 }
663
664 /**
665 * The map view used in this bounding box chooser
666 */
667 static private class TileBoundsMapView extends JMapViewer {
668 private Point min;
669 private Point max;
670
671 public TileBoundsMapView() {
672 setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
673 TileLoader loader = tileController.getTileLoader();
674 if (loader instanceof OsmTileLoader) {
675 ((OsmTileLoader)loader).headers.put("User-Agent", Version.getInstance().getFullAgentString());
676 }
677 }
678
679 public void setBoundingBox(Bounds bbox){
680 if (bbox == null) {
681 min = null;
682 max = null;
683 } else {
684 int y1 = OsmMercator.LatToY(bbox.getMin().lat(), MAX_ZOOM);
685 int y2 = OsmMercator.LatToY(bbox.getMax().lat(), MAX_ZOOM);
686 int x1 = OsmMercator.LonToX(bbox.getMin().lon(), MAX_ZOOM);
687 int x2 = OsmMercator.LonToX(bbox.getMax().lon(), MAX_ZOOM);
688
689 min = new Point(Math.min(x1, x2), Math.min(y1, y2));
690 max = new Point(Math.max(x1, x2), Math.max(y1, y2));
691 }
692 repaint();
693 }
694
695 protected Point getTopLeftCoordinates() {
696 return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
697 }
698
699 /**
700 * Draw the map.
701 */
702 @Override
703 public void paint(Graphics g) {
704 try {
705 super.paint(g);
706 if (min == null || max == null) return;
707 int zoomDiff = MAX_ZOOM - zoom;
708 Point tlc = getTopLeftCoordinates();
709 int x_min = (min.x >> zoomDiff) - tlc.x;
710 int y_min = (min.y >> zoomDiff) - tlc.y;
711 int x_max = (max.x >> zoomDiff) - tlc.x;
712 int y_max = (max.y >> zoomDiff) - tlc.y;
713
714 int w = x_max - x_min;
715 int h = y_max - y_min;
716 g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
717 g.fillRect(x_min, y_min, w, h);
718
719 g.setColor(Color.BLACK);
720 g.drawRect(x_min, y_min, w, h);
721 } catch (Exception e) {
722 e.printStackTrace();
723 }
724 }
725 }
726}
Note: See TracBrowser for help on using the repository browser.