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