    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()) {
    050      // A new streetside layer is created, so the active layer is not changed
    051      StreetsideLayer.getInstance();
    052      return;
    053    } else if (!MainApplication.getLayerManager().containsLayer(StreetsideLayer.getInstance())) {
    054      MainApplication.getLayerManager().addLayer(StreetsideLayer.getInstance());
    055      return;
    056    }
    057
    058    try {
    059      // Successive calls to this action toggle the active layer between the OSM data layer and the streetside layer
    060      OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
    061      if (MainApplication.getLayerManager().getActiveLayer() != StreetsideLayer.getInstance()) {
    062        MainApplication.getLayerManager().setActiveLayer(StreetsideLayer.getInstance());
    063      } else if (editLayer != null) {
    064        MainApplication.getLayerManager().setActiveLayer(editLayer);
    065      }
    066    } catch (IllegalArgumentException e) {
    067      // If the StreetsideLayer is not managed by LayerManager but you try to set it as active layer
    068      Logging.warn(e);
    069    }
    070  }
    071}