001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.actions.mapmode;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.awt.Rectangle;
007    import java.awt.event.KeyEvent;
008    import java.awt.event.MouseEvent;
009    
010    import org.openstreetmap.josm.Main;
011    import org.openstreetmap.josm.gui.MapFrame;
012    import org.openstreetmap.josm.gui.MapView;
013    import org.openstreetmap.josm.gui.SelectionManager;
014    import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
015    import org.openstreetmap.josm.tools.ImageProvider;
016    import 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     */
031    public class ZoomAction extends MapMode implements SelectionEnded {
032    
033        /**
034         * Manager that manages the selection rectangle with the aspect ratio of the
035         * MapView.
036         */
037        private final SelectionManager selectionManager;
038    
039        /**
040         * Construct a ZoomAction without a label.
041         * @param mapFrame The MapFrame, whose zoom mode should be enabled.
042         */
043        public ZoomAction(MapFrame mapFrame) {
044            super(tr("Zoom"), "zoom", tr("Zoom and move map"),
045                    Shortcut.registerShortcut("mapmode:zoom", tr("Mode: {0}", tr("Zoom")), KeyEvent.VK_Z, Shortcut.DIRECT),
046                    mapFrame, ImageProvider.getCursor("normal", "zoom"));
047            selectionManager = new SelectionManager(this, true, mapFrame.mapView);
048        }
049    
050        /**
051         * Zoom to the rectangle on the map.
052         */
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    }