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