    001//License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.gui.imageinfo;
    003
    004import java.awt.BorderLayout;
    005import java.text.MessageFormat;
    006
    007import javax.swing.JCheckBox;
    008import javax.swing.JPanel;
    009import javax.swing.SwingUtilities;
    010
    011import org.apache.log4j.Logger;
    012import org.openstreetmap.josm.data.preferences.AbstractProperty.ValueChangeListener;
    013import org.openstreetmap.josm.plugins.streetside.StreetsideAbstractImage;
    014import org.openstreetmap.josm.plugins.streetside.StreetsideDataListener;
    015import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
    016import org.openstreetmap.josm.plugins.streetside.actions.ImageReloadAction;
    017import org.openstreetmap.josm.plugins.streetside.cubemap.CubemapBuilder;
    018import org.openstreetmap.josm.plugins.streetside.cubemap.CubemapUtils;
    019import org.openstreetmap.josm.plugins.streetside.cubemap.GraphicsUtils;
    020import org.openstreetmap.josm.plugins.streetside.gui.boilerplate.StreetsideButton;
    021import org.openstreetmap.josm.plugins.streetside.utils.CubemapBox;
    022import org.openstreetmap.josm.plugins.streetside.utils.StreetsideProperties;
    023import org.openstreetmap.josm.plugins.streetside.utils.StreetsideURL;
    024import org.openstreetmap.josm.tools.I18n;
    025
    026public final class StreetsideViewerPanel extends JPanel
    027                implements StreetsideDataListener {
    028
    029        private static final long serialVersionUID = 4141847503072417190L;
    030
    031        final static Logger logger = Logger.getLogger(StreetsideViewerPanel.class);
    032
    033        private JCheckBox highResImageryCheck;
    034        private WebLinkAction imgLinkAction;
    035        private ImageReloadAction imgReloadAction;
    036        private ValueChangeListener<Boolean> imageLinkChangeListener;
    037
    038        private static ThreeSixtyDegreeViewerPanel threeSixtyDegreeViewerPanel;
    039
    040        public StreetsideViewerPanel() {
    041
    042                super(new BorderLayout());
    043
    044                SwingUtilities.invokeLater(new Runnable() {
    045                     @Override
    046                     public void run() {
    047                         initializeAndStartGUI();
    048                     }
    049                 });
    050
    051                selectedImageChanged(null, null);
    052
    053                setToolTipText(I18n.tr("Select Microsoft Streetside from the Imagery menu, then click on a blue vector bubble.."));
    054        }
    055
    056        private void initializeAndStartGUI() {
    057
    058                threeSixtyDegreeViewerPanel = new ThreeSixtyDegreeViewerPanel();
    059
    060                GraphicsUtils.PlatformHelper.run(() -> {
    061                threeSixtyDegreeViewerPanel.initialize();
    062                });
    063
    064                add(threeSixtyDegreeViewerPanel, BorderLayout.CENTER);
    065                revalidate();
    066                repaint();
    067            JPanel checkPanel = new JPanel();
    068
    069            // TODO: I18n for all languages?
    070            imgReloadAction = new ImageReloadAction(/*I18n.tr(*/"Reload"/*)*/);
    071
    072            StreetsideButton imgReloadButton = new StreetsideButton(imgReloadAction);
    073
    074            // TODO: I18n for all languages?
    075      highResImageryCheck = new JCheckBox(/*I18n.tr(*/"High resolution"/*)*/);
    076            highResImageryCheck.setSelected(StreetsideProperties.SHOW_HIGH_RES_STREETSIDE_IMAGERY.get());
    077            highResImageryCheck.addActionListener(
    078              action -> StreetsideProperties.SHOW_HIGH_RES_STREETSIDE_IMAGERY.put(highResImageryCheck.isSelected())
    079            );
    080            StreetsideProperties.SHOW_HIGH_RES_STREETSIDE_IMAGERY.addListener(
    081              valueChange -> highResImageryCheck.setSelected(StreetsideProperties.SHOW_HIGH_RES_STREETSIDE_IMAGERY.get())
    082            );
    083            checkPanel.add(highResImageryCheck, BorderLayout.WEST);
    084            checkPanel.add(imgReloadButton, BorderLayout.EAST);
    085
    086            JPanel privacyLink = new JPanel();
    087
    088            imgLinkAction = new WebLinkAction("Report a privacy concern with this image", null);
    089            privacyLink.add(new StreetsideButton(imgLinkAction, true));
    090            checkPanel.add(privacyLink, BorderLayout.PAGE_END);
    091
    092            add(threeSixtyDegreeViewerPanel, BorderLayout.CENTER);
    093
    094            JPanel bottomPanel = new JPanel();
    095            bottomPanel.add(checkPanel, BorderLayout.NORTH);
    096            bottomPanel.add(privacyLink, BorderLayout.SOUTH);
    097
    098            add(bottomPanel, BorderLayout.PAGE_END);
    099        }
    100
    101        /*
    102         * (non-Javadoc)
    103         *
    104         * @see
    105         * org.openstreetmap.josm.plugins.streetside.StreetsideDataListener#imagesAdded(
    106         * )
    107         */
    108        @Override
    109        public void imagesAdded() {
    110                // Method is not needed, but enforcesd by the interface StreetsideDataListener
    111        }
    112
    113        /*
    114         * (non-Javadoc)
    115         *
    116         * @see org.openstreetmap.josm.plugins.streetside.StreetsideDataListener#
    117         * selectedImageChanged(org.openstreetmap.josm.plugins.streetside.
    118         * StreetsideAbstractImage,
    119         * org.openstreetmap.josm.plugins.streetside.StreetsideAbstractImage)
    120         */
    121        @Override
    122        public synchronized void selectedImageChanged(final StreetsideAbstractImage oldImage,
    123                        final StreetsideAbstractImage newImage) {
    124
    125                // method is invoked with null initially by framework
    126                if(newImage!=null) {
    127
    128                    logger.info(String.format(
    129                      "Selected Streetside image changed from %s to %s.",
    130                      oldImage instanceof StreetsideImage ? ((StreetsideImage) oldImage).getId() : "‹none›",
    131                      newImage instanceof StreetsideImage ? ((StreetsideImage) newImage).getId() : "‹none›"
    132                    ));
    133
    134                    final String newImageId = CubemapBuilder.getInstance().getCubemap() !=null ? CubemapBuilder.getInstance().getCubemap().getId() : newImage instanceof StreetsideImage ? ((StreetsideImage) newImage).getId(): null;
    135                    if (newImageId != null) {
    136                      final String bubbleId = CubemapUtils.convertQuaternary2Decimal(newImageId);
    137                      imageLinkChangeListener = b -> imgLinkAction.setURL(
    138                        StreetsideURL.MainWebsite.streetsidePrivacyLink(bubbleId)
    139                      );
    140
    141                      if(StreetsideProperties.DEBUGING_ENABLED.get()) {
    142                        logger.debug(MessageFormat.format("Privacy link set for Streetside image {0} quadKey {1}", bubbleId, newImageId));
    143                      }
    144
    145                      imageLinkChangeListener.valueChanged(null);
    146                      StreetsideProperties.CUBEMAP_LINK_TO_BLUR_EDITOR.addListener(imageLinkChangeListener);
    147                    } else {
    148                      if (imageLinkChangeListener != null) {
    149                        StreetsideProperties.CUBEMAP_LINK_TO_BLUR_EDITOR.removeListener(imageLinkChangeListener);
    150                        imageLinkChangeListener = null;
    151                      }
    152                      imgLinkAction.setURL(null);
    153                    }
    154                  }
    155        }
    156
    157        public CubemapBox getCubemapBox() {
    158                return threeSixtyDegreeViewerPanel.getCubemapBox();
    159        }
    160
    161        /**
    162         * @return the threeSixtyDegreeViewerPanel
    163         */
    164        public static ThreeSixtyDegreeViewerPanel getThreeSixtyDegreeViewerPanel() {
    165                return threeSixtyDegreeViewerPanel;
    166        }
    167}