001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.mapmode; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Rectangle; 007import java.awt.event.KeyEvent; 008import java.awt.event.MouseEvent; 009 010import org.openstreetmap.josm.Main; 011import org.openstreetmap.josm.gui.MapFrame; 012import org.openstreetmap.josm.gui.MapView; 013import org.openstreetmap.josm.gui.SelectionManager; 014import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded; 015import org.openstreetmap.josm.tools.ImageProvider; 016import org.openstreetmap.josm.tools.Shortcut; 017 018/** 019 * Enable the zoom mode within the MapFrame. 020 * 021 * Holding down the left mouse button select a rectangle with the same aspect 022 * ratio than the current map view. 023 * Holding down left and right let the user move the former selected rectangle. 024 * Releasing the left button zoom to the selection. 025 * 026 * Rectangle selections with either height or width smaller than 3 pixels 027 * are ignored. 028 * 029 * @author imi 030 */ 031public class ZoomAction extends MapMode implements SelectionEnded { 032 033 /** 034 * Manager that manages the selection rectangle with the aspect ratio of the MapView. 035 */ 036 private final transient SelectionManager selectionManager; 037 038 /** 039 * Construct a ZoomAction without a label. 040 * @param mapFrame The MapFrame, whose zoom mode should be enabled. 041 */ 042 public ZoomAction(MapFrame mapFrame) { 043 super(tr("Zoom"), "zoom", tr("Zoom and move map"), 044 Shortcut.registerShortcut("mapmode:zoom", tr("Mode: {0}", tr("Zoom")), KeyEvent.VK_Z, Shortcut.DIRECT), 045 mapFrame, ImageProvider.getCursor("normal", "zoom")); 046 selectionManager = new SelectionManager(this, true, mapFrame.mapView); 047 } 048 049 /** 050 * Zoom to the rectangle on the map. 051 */ 052 @Override 053 public void selectionEnded(Rectangle r, MouseEvent e) { 054 if (r.width >= 3 && r.height >= 3 && Main.isDisplayingMapView()) { 055 MapView mv = Main.map.mapView; 056 mv.zoomToFactor(mv.getEastNorth(r.x+r.width/2, r.y+r.height/2), r.getWidth()/mv.getWidth()); 057 } 058 } 059 060 @Override public void enterMode() { 061 super.enterMode(); 062 selectionManager.register(Main.map.mapView, false); 063 } 064 065 @Override public void exitMode() { 066 super.exitMode(); 067 selectionManager.unregister(Main.map.mapView); 068 } 069 070 @Override public String getModeHelpText() { 071 return tr("Zoom by dragging or Ctrl+. or Ctrl+,; move with Ctrl+up, left, down, right; move zoom with right button"); 072 } 073}