    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.actions;
    003
    004import static org.openstreetmap.josm.tools.I18n.tr;
    005
    006import java.awt.event.ActionEvent;
    007import java.awt.event.KeyEvent;
    008
    009import org.apache.log4j.Logger;
    010import org.openstreetmap.josm.actions.JosmAction;
    011import org.openstreetmap.josm.gui.MainApplication;
    012import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    013import org.openstreetmap.josm.plugins.streetside.StreetsideLayer;
    014import org.openstreetmap.josm.plugins.streetside.StreetsidePlugin;
    015import org.openstreetmap.josm.tools.ImageProvider;
    016import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
    017import org.openstreetmap.josm.tools.Shortcut;
    018
    019/**
    020 * Action that triggers the plugin. If in automatic mode, it will automatically
    021 * download the images in the areas where there is OSM data.
    022 *
    023 * @author nokutu
    024 *
    025 */
    026public class StreetsideDownloadAction extends JosmAction {
    027
    028  private static final long serialVersionUID = 4426446157849005029L;
    029
    030  public static final Shortcut SHORTCUT = Shortcut.registerShortcut("Streetside", "Open Streetside layer", KeyEvent.VK_COMMA, Shortcut.SHIFT);
    031
    032  final static Logger logger = Logger.getLogger(StreetsideDownloadAction.class);
    033
    034  /**
    035   * Main constructor.
    036   */
    037  public StreetsideDownloadAction() {
    038    super(
    039        tr("Streetside"),
    040        new ImageProvider(StreetsidePlugin.LOGO).setSize(ImageSizes.DEFAULT),
    041        tr("Open Streetside layer"),
    042        SHORTCUT,
    043        false,
    044        "streetsideDownload",
    045        false
    046    );
    047  }
    048
    049  @Override
    050  public void actionPerformed(ActionEvent ae) {
    051    if (!StreetsideLayer.hasInstance() || !MainApplication.getLayerManager().containsLayer(StreetsideLayer.getInstance())) {
    052      MainApplication.getLayerManager().addLayer(StreetsideLayer.getInstance());
    053      return;
    054    }
    055
    056    try {
    057      // Successive calls to this action toggle the active layer between the OSM data layer and the streetside layer
    058      OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
    059      if (MainApplication.getLayerManager().getActiveLayer() != StreetsideLayer.getInstance()) {
    060        MainApplication.getLayerManager().setActiveLayer(StreetsideLayer.getInstance());
    061      } else if (editLayer != null) {
    062        MainApplication.getLayerManager().setActiveLayer(editLayer);
    063      }
    064    } catch (IllegalArgumentException e) {
    065      // If the StreetsideLayer is not managed by LayerManager but you try to set it as active layer
    066      logger.warn(e);
    067    }
    068  }
    069}