Changeset 2758 in josm for trunk/src


Ignore:
Timestamp:
2010-01-07T18:20:47+01:00 (15 years ago)
Author:
mjulius
Message:

fixes #1653 - zoom to the previously shown area
This adds menu entries and the shortcuts '8' and '9' to zoom to the previous and next areas. See also http://josm.openstreetmap.de/wiki/Help/Menu/View

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AutoScaleAction.java

    r2711 r2758  
    2828public class AutoScaleAction extends JosmAction {
    2929
    30     public static final String[] MODES = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
     30    public static final String[] MODES = {
     31        marktr("data"),
     32        marktr("layer"),
     33        marktr("selection"),
     34        marktr("conflict"),
     35        marktr("download"),
     36        marktr("previous"),
     37        marktr("next")};
    3138
    3239    /**
     
    8188            shortcut = KeyEvent.VK_5;
    8289        }
     90        if (mode.equals("previous")) {
     91            shortcut = KeyEvent.VK_8;
     92        }
     93        if (mode.equals("next")) {
     94            shortcut = KeyEvent.VK_9;
     95        }
    8396
    8497        return shortcut;
     
    99112        } else if (mode.equals("conflict")) {
    100113            putValue("help", ht("/Action/ZoomToConflict"));
    101         }else if (mode.equals("download")) {
     114        } else if (mode.equals("download")) {
    102115            putValue("help", ht("/Action/ZoomToDownload"));
     116        } else if (mode.equals("previous")) {
     117            putValue("help", ht("/Action/ZoomPrevious"));
     118        } else if (mode.equals("next")) {
     119            putValue("help", ht("/Action/ZoomNext"));
    103120        }
    104121    }
     
    106123    public void autoScale()  {
    107124        if (Main.map != null) {
    108             BoundingXYVisitor bbox = getBoundingBox();
    109             if (bbox != null && bbox.getBounds() != null) {
    110                 Main.map.mapView.recalculateCenterScale(bbox);
     125            if (mode.equals("previous")) {
     126                Main.map.mapView.zoomPrevious();
     127            } else if (mode.equals("next")) {
     128                Main.map.mapView.zoomNext();
     129            } else {
     130                BoundingXYVisitor bbox = getBoundingBox();
     131                if (bbox != null && bbox.getBounds() != null) {
     132                    Main.map.mapView.recalculateCenterScale(bbox);
     133                }
    111134            }
    112135        }
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r2578 r2758  
    88import java.util.Collection;
    99import java.util.Collections;
     10import java.util.Date;
    1011import java.util.HashSet;
    1112import java.util.LinkedList;
     
    205206     */
    206207    private void zoomTo(EastNorth newCenter, double newScale) {
    207         boolean rep = false;
    208 
    209208        Bounds b = getProjection().getWorldBoundsLatLon();
    210209        CachedLatLon cl = new CachedLatLon(newCenter);
     
    219218            newCenter = new CachedLatLon(lat, lon).getEastNorth();
    220219        }
    221         if (!newCenter.equals(center)) {
    222             EastNorth oldCenter = center;
    223             center = newCenter;
    224             rep = true;
    225             firePropertyChange("center", oldCenter, newCenter);
    226         }
    227 
    228220        int width = getWidth()/2;
    229221        int height = getHeight()/2;
     
    250242            }
    251243        }
     244
     245        if (!newCenter.equals(center) || (scale != newScale)) {
     246            pushZoomUndo(center, scale);
     247            zoomNoUndoTo(newCenter, newScale);
     248        }
     249    }
     250
     251    /**
     252     * Zoom to the given coordinate without adding to the zoom undo buffer.
     253     * @param newCenter The center x-value (easting) to zoom to.
     254     * @param scale The scale to use.
     255     */
     256    private void zoomNoUndoTo(EastNorth newCenter, double newScale) {
     257        if (!newCenter.equals(center)) {
     258            EastNorth oldCenter = center;
     259            center = newCenter;
     260            firePropertyChange("center", oldCenter, newCenter);
     261        }
    252262        if (scale != newScale) {
    253263            double oldScale = scale;
    254264            scale = newScale;
    255             rep = true;
    256265            firePropertyChange("scale", oldScale, newScale);
    257266        }
    258267
    259         if(rep) {
    260             repaint();
    261         }
     268        repaint();
    262269    }
    263270
     
    313320        zoomTo(new ProjectionBounds(getProjection().latlon2eastNorth(box.getMin()),
    314321                getProjection().latlon2eastNorth(box.getMax())));
     322    }
     323
     324    private class ZoomData {
     325        LatLon center;
     326        double scale;
     327
     328        public ZoomData(EastNorth center, double scale) {
     329            this.center = new CachedLatLon(center);
     330            this.scale = scale;
     331        }
     332
     333        public EastNorth getCenterEastNorth() {
     334            return getProjection().latlon2eastNorth(center);
     335        }
     336
     337        public double getScale() {
     338            return scale;
     339        }
     340    }
     341
     342    private LinkedList<ZoomData> zoomUndoBuffer = new LinkedList<ZoomData>();
     343    private LinkedList<ZoomData> zoomRedoBuffer = new LinkedList<ZoomData>();
     344    private Date zoomTimestamp = new Date();
     345
     346    private void pushZoomUndo(EastNorth center, double scale) {
     347        Date now = new Date();
     348        if ((now.getTime() - zoomTimestamp.getTime()) > (Main.pref.getDouble("zoom.delay", 1.0) * 1000)) {
     349            zoomUndoBuffer.push(new ZoomData(center, scale));
     350            zoomRedoBuffer.clear();
     351        }
     352        zoomTimestamp = now;
     353    }
     354
     355    public void zoomPrevious() {
     356        if (!zoomUndoBuffer.isEmpty()) {
     357            ZoomData zoom = zoomUndoBuffer.pop();
     358            zoomRedoBuffer.push(new ZoomData(center, scale));
     359            zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale());
     360        }
     361    }
     362
     363    public void zoomNext() {
     364        if (!zoomRedoBuffer.isEmpty()) {
     365            ZoomData zoom = zoomRedoBuffer.pop();
     366            zoomUndoBuffer.push(new ZoomData(center, scale));
     367            zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale());
     368        }
    315369    }
    316370
Note: See TracChangeset for help on using the changeset viewer.