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