001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.download;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Dimension;
007import java.beans.PropertyChangeEvent;
008import java.beans.PropertyChangeListener;
009
010import javax.swing.ActionMap;
011
012import org.openstreetmap.josm.data.Bounds;
013import org.openstreetmap.josm.data.coor.ILatLon;
014import org.openstreetmap.josm.gui.bbox.BBoxChooser;
015import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
016import org.openstreetmap.josm.gui.util.GuiHelper;
017
018/**
019 * JComponent that displays the slippy map tiles.
020 *
021 * @author Tim Haussmann
022 * @since 1390
023 */
024public class SlippyMapChooser implements DownloadSelection, PropertyChangeListener {
025
026    private DownloadDialog iGui;
027    private final SlippyMapBBoxChooser pnlSlippyMapBBoxChooser;
028    // standard dimension
029    private Dimension iDownloadDialogDimension;
030
031    /**
032     * Create the chooser component.
033     */
034    public SlippyMapChooser() {
035        pnlSlippyMapBBoxChooser = new SlippyMapBBoxChooser();
036        pnlSlippyMapBBoxChooser.addPropertyChangeListener(this);
037    }
038
039    @Override
040    public void addGui(final DownloadDialog gui) {
041        iGui = gui;
042        iGui.addDownloadAreaSelector(pnlSlippyMapBBoxChooser, tr("Slippy map"));
043    }
044
045    @Override
046    public void setDownloadArea(Bounds area) {
047        pnlSlippyMapBBoxChooser.setBoundingBox(area);
048    }
049
050    @Override
051    public void propertyChange(PropertyChangeEvent evt) {
052        if (evt.getPropertyName().equals(BBoxChooser.BBOX_PROP)) {
053            if (iGui != null) {
054                iGui.boundingBoxChanged((Bounds) evt.getNewValue(), this);
055            }
056        } else if (evt.getPropertyName().equals(SlippyMapBBoxChooser.RESIZE_PROP)) {
057            int w, h;
058
059            // retrieve the size of the display
060            Dimension iScreenSize = GuiHelper.getScreenSize();
061
062            if (iDownloadDialogDimension == null) {
063                // enlarge: make the each dimension 90% of the absolute display size
064                w = iScreenSize.width * 90 / 100;
065                h = iScreenSize.height * 90 / 100;
066                iDownloadDialogDimension = iGui.getSize();
067            } else {
068                // shrink: set the size back to the initial dimensions
069                w = iDownloadDialogDimension.width;
070                h = iDownloadDialogDimension.height;
071                iDownloadDialogDimension = null;
072            }
073
074            // resize and center the DownloadDialog
075            iGui.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
076        } else if (evt.getPropertyName().equals(SlippyMapBBoxChooser.CURSOR_COORDINATE_PROP)) {
077            iGui.mapCursorChanged((ILatLon) evt.getNewValue());
078        }
079    }
080
081    /**
082     * Refreshes the tile sources
083     * @since 6364
084     */
085    public final void refreshTileSources() {
086        if (pnlSlippyMapBBoxChooser != null) {
087            pnlSlippyMapBBoxChooser.refreshTileSources();
088        }
089    }
090
091    /**
092     * Returns the action map of the underlying navigation component.
093     * @return the action map of the underlying navigation component
094     * @since 8932
095     */
096    public final ActionMap getNavigationComponentActionMap() {
097        return pnlSlippyMapBBoxChooser.getActionMap();
098    }
099}