001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside;
003
004import org.openstreetmap.josm.gui.MainApplication;
005import org.openstreetmap.josm.gui.MainMenu;
006import org.openstreetmap.josm.gui.MapFrame;
007import org.openstreetmap.josm.gui.MapView;
008import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
009import org.openstreetmap.josm.plugins.Plugin;
010import org.openstreetmap.josm.plugins.PluginInformation;
011import org.openstreetmap.josm.plugins.streetside.actions.StreetsideDownloadAction;
012import org.openstreetmap.josm.plugins.streetside.actions.StreetsideDownloadViewAction;
013import org.openstreetmap.josm.plugins.streetside.actions.StreetsideExportAction;
014import org.openstreetmap.josm.plugins.streetside.actions.StreetsideWalkAction;
015import org.openstreetmap.josm.plugins.streetside.actions.StreetsideZoomAction;
016import org.openstreetmap.josm.plugins.streetside.cubemap.CubemapBuilder;
017import org.openstreetmap.josm.plugins.streetside.gui.StreetsideMainDialog;
018import org.openstreetmap.josm.plugins.streetside.gui.StreetsidePreferenceSetting;
019import org.openstreetmap.josm.plugins.streetside.gui.StreetsideViewerDialog;
020import org.openstreetmap.josm.plugins.streetside.gui.imageinfo.ImageInfoHelpPopup;
021import org.openstreetmap.josm.plugins.streetside.gui.imageinfo.ImageInfoPanel;
022import org.openstreetmap.josm.plugins.streetside.oauth.StreetsideUser;
023import org.openstreetmap.josm.plugins.streetside.utils.StreetsideProperties;
024import org.openstreetmap.josm.tools.ImageProvider;
025
026/**
027 * This is the main class of the Streetside plugin.
028 */
029public class StreetsidePlugin extends Plugin {
030
031  public static final ImageProvider LOGO = new ImageProvider("streetside-logo");
032
033  /** Zoom action */
034  private static final StreetsideZoomAction ZOOM_ACTION = new StreetsideZoomAction();
035  /** Walk action */
036  private static final StreetsideWalkAction WALK_ACTION = new StreetsideWalkAction();
037
038  static {
039      MainMenu.add(MainApplication.getMenu().fileMenu, new StreetsideExportAction(), false, 14);
040      MainMenu.add(MainApplication.getMenu().imagerySubMenu, new StreetsideDownloadAction(), false);
041      MainMenu.add(MainApplication.getMenu().viewMenu, ZOOM_ACTION, false, 15);
042      MainMenu.add(MainApplication.getMenu().fileMenu, new StreetsideDownloadViewAction(), false, 14);
043      MainMenu.add(MainApplication.getMenu().moreToolsMenu, WALK_ACTION, false);
044  }
045
046  /**
047   * Main constructor.
048   *
049   * @param info
050   *          Required information of the plugin. Obtained from the jar file.
051   */
052  public StreetsidePlugin(PluginInformation info) {
053    super(info);
054
055    if (StreetsideProperties.ACCESS_TOKEN.get() == null) {
056      StreetsideUser.setTokenValid(false);
057    }
058  }
059
060  static StreetsideDataListener[] getStreetsideDataListeners() {
061        return new StreetsideDataListener[]{WALK_ACTION, ZOOM_ACTION, CubemapBuilder.getInstance()};
062  }
063
064
065  /**
066   * @return the {@link StreetsideWalkAction} for the plugin
067   */
068  public static StreetsideWalkAction getStreetsideWalkAction() {
069    return WALK_ACTION;
070  }
071
072  /**
073   * Called when the JOSM map frame is created or destroyed.
074   */
075  @Override
076  public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
077    if (oldFrame == null && newFrame != null) { // map frame added
078      MainApplication.getMap().addToggleDialog(StreetsideMainDialog.getInstance(), false);
079      StreetsideMainDialog.getInstance().setImageInfoHelp(new ImageInfoHelpPopup(
080          MainApplication.getMap().addToggleDialog(ImageInfoPanel.getInstance(), false)
081      ));
082      MainApplication.getMap().addToggleDialog(StreetsideViewerDialog.getInstance(), false);
083    }
084    if (oldFrame != null && newFrame == null) { // map frame destroyed
085      StreetsideMainDialog.destroyInstance();
086      ImageInfoPanel.destroyInstance();
087      CubemapBuilder.destroyInstance();
088      StreetsideViewerDialog.destroyInstance();
089    }
090  }
091
092  @Override
093  public PreferenceSetting getPreferenceSetting() {
094    return new StreetsidePreferenceSetting();
095  }
096
097  /**
098   * @return the current {@link MapView} without throwing a {@link NullPointerException}
099   */
100  public static MapView getMapView() {
101    final MapFrame mf = MainApplication.getMap();
102    if (mf != null) {
103      return mf.mapView;
104    }
105    return null;
106  }
107}