001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Color;
007import java.awt.Font;
008import java.awt.GridBagLayout;
009
010import javax.swing.JButton;
011import javax.swing.JCheckBox;
012import javax.swing.JPanel;
013import javax.swing.border.CompoundBorder;
014import javax.swing.border.EmptyBorder;
015import javax.swing.border.EtchedBorder;
016
017import org.openstreetmap.josm.data.imagery.ImageryInfo;
018import org.openstreetmap.josm.data.preferences.BooleanProperty;
019import org.openstreetmap.josm.gui.MainApplication;
020import org.openstreetmap.josm.gui.MapFrame;
021import org.openstreetmap.josm.gui.util.GuiHelper;
022import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
023import org.openstreetmap.josm.gui.widgets.UrlLabel;
024import org.openstreetmap.josm.tools.GBC;
025import org.openstreetmap.josm.tools.ImageProvider;
026
027/**
028 * The panel to nag a user ONCE that he/she has to align imagery.
029 *
030 * @author zverik
031 */
032public class AlignImageryPanel extends JPanel {
033
034    /**
035     * @param oneLine if true, show the nagging message in one line, otherwise - in two lines
036     * @param showAgain show again property
037     * @param infoToAdd imagery info for which the nagging message is shown
038     */
039    public AlignImageryPanel(boolean oneLine, final BooleanProperty showAgain, ImageryInfo infoToAdd) {
040        Font font = getFont().deriveFont(Font.PLAIN, 14.0f);
041        JMultilineLabel nagLabel = new JMultilineLabel(
042                tr("Aerial imagery \"{0}\" might be misaligned. Please check its offset using GPS tracks!", infoToAdd.getName()));
043        UrlLabel detailsList = new UrlLabel(tr("http://wiki.openstreetmap.org/wiki/Using_Imagery"), tr("Details..."));
044        nagLabel.setFont(font);
045        nagLabel.setForeground(Color.BLACK);
046        detailsList.setFont(font);
047        final JCheckBox doNotShowAgain = new JCheckBox(tr("Do not show this message again"));
048        doNotShowAgain.setOpaque(false);
049        doNotShowAgain.setForeground(Color.BLACK);
050
051        JButton closeButton = new JButton(ImageProvider.get("misc", "black_x"));
052        closeButton.setContentAreaFilled(false);
053        closeButton.setRolloverEnabled(true);
054        closeButton.setBorderPainted(false);
055        closeButton.setToolTipText(tr("Hide this message and never show it again"));
056        closeButton.addActionListener(e -> {
057            if (MainApplication.isDisplayingMapView()) {
058                MainApplication.getMap().removeTopPanel(AlignImageryPanel.class);
059                if (doNotShowAgain.isSelected()) {
060                    showAgain.put(Boolean.FALSE);
061                }
062            }
063        });
064
065        setLayout(new GridBagLayout());
066        if (!oneLine) { // tune for small screens
067            add(nagLabel, GBC.std(1, 1).fill());
068            add(detailsList, GBC.std(1, 2).fill());
069            add(doNotShowAgain, GBC.std(1, 3).fill());
070            add(closeButton, GBC.std(2, 1).span(1, 2).anchor(GBC.EAST));
071        } else {
072            add(nagLabel, GBC.std(1, 1).fill());
073            add(detailsList, GBC.std(2, 1).fill());
074            add(doNotShowAgain, GBC.std(1, 2).fill());
075            add(closeButton, GBC.std(3, 1).anchor(GBC.EAST));
076        }
077        setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.LOWERED), new EmptyBorder(12, 12, 12, 12)));
078        setBackground(new Color(224, 236, 249));
079    }
080
081    /**
082     * @param infoToAdd ImageryInfo for which the nag panel should be created
083     */
084    public static void addNagPanelIfNeeded(ImageryInfo infoToAdd) {
085        BooleanProperty showAgain = new BooleanProperty("message.imagery.nagPanel." + infoToAdd.getUrl(), true);
086        MapFrame map = MainApplication.getMap();
087        if (MainApplication.isDisplayingMapView() && showAgain.get() && !infoToAdd.isGeoreferenceValid()
088                && map.getTopPanel(AlignImageryPanel.class) == null) {
089            double w = GuiHelper.getScreenSize().getWidth();
090            map.addTopPanel(new AlignImageryPanel(w > 1300, showAgain, infoToAdd));
091        }
092    }
093}