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