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

Last change on this file since 5898 was 5898, checked in by Don-vip, 11 years ago

see #8606 - Set User-Agent for JMapViewer connections

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