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.awt.Toolkit;
008import java.beans.PropertyChangeEvent;
009import java.beans.PropertyChangeListener;
010
011import javax.swing.ActionMap;
012import javax.swing.JPanel;
013
014import org.openstreetmap.josm.data.Bounds;
015import org.openstreetmap.josm.gui.bbox.BBoxChooser;
016import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
017
018/**
019 * JComponent that displays the slippy map tiles.
020 *
021 * @author Tim Haussmann
022 * @since 1390
023 */
024public class SlippyMapChooser extends JPanel 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        repaint();
049    }
050
051    @Override
052    public void propertyChange(PropertyChangeEvent evt) {
053        if (evt.getPropertyName().equals(BBoxChooser.BBOX_PROP)) {
054            if (iGui != null) {
055                iGui.boundingBoxChanged((Bounds) evt.getNewValue(), this);
056            }
057        } else if (evt.getPropertyName().equals(SlippyMapBBoxChooser.RESIZE_PROP)) {
058            int w, h;
059
060            // retrieve the size of the display
061            Dimension iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
062
063            if (iDownloadDialogDimension == null) {
064                // enlarge: make the each dimension 90% of the absolute display size
065                w = iScreenSize.width * 90 / 100;
066                h = iScreenSize.height * 90 / 100;
067                iDownloadDialogDimension = iGui.getSize();
068            } else {
069                // shrink: set the size back to the initial dimensions
070                w = iDownloadDialogDimension.width;
071                h = iDownloadDialogDimension.height;
072                iDownloadDialogDimension = null;
073            }
074
075            // resize and center the DownloadDialog
076            iGui.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
077            repaint();
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}