Changeset 8171 in josm


Ignore:
Timestamp:
2015-04-06T14:59:42+02:00 (9 years ago)
Author:
stoecker
Message:

fix #7630 - provide a zoom to all downloaded areas function

File:
1 edited

Legend:

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

    r7817 r8171  
    88import java.awt.event.ActionEvent;
    99import java.awt.event.KeyEvent;
     10import java.util.ArrayList;
    1011import java.util.Arrays;
    1112import java.util.Collection;
     
    2223import org.openstreetmap.josm.Main;
    2324import org.openstreetmap.josm.data.Bounds;
     25import org.openstreetmap.josm.data.DataSource;
    2426import org.openstreetmap.josm.data.conflict.Conflict;
    2527import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    3133import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    3234import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor;
    33 import org.openstreetmap.josm.gui.download.DownloadDialog;
    3435import org.openstreetmap.josm.gui.layer.Layer;
    3536import org.openstreetmap.josm.tools.Shortcut;
     
    5556    protected ZoomChangeAdapter zoomChangeAdapter;
    5657    protected MapFrameAdapter mapFrameAdapter;
     58    /** Time of last zoom to bounds action */
     59    protected long lastZoomTime = -1;
     60    /** Last zommed bounds */
     61    protected int lastZoomArea = -1;
    5762
    5863    /**
     
    6368     */
    6469    public static void zoomToSelection() {
    65         if (Main.main == null || !Main.main.hasEditLayer()) return;
     70        if (Main.main == null || !Main.main.hasEditLayer())
     71            return;
    6672        Collection<OsmPrimitive> sel = Main.main.getEditLayer().data.getSelected();
    6773        if (sel.isEmpty()) {
     
    7076                    tr("Nothing selected to zoom to."),
    7177                    tr("Information"),
    72                     JOptionPane.INFORMATION_MESSAGE
    73             );
     78                    JOptionPane.INFORMATION_MESSAGE);
    7479            return;
    7580        }
     
    126131    public AutoScaleAction(final String mode) {
    127132        super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
    128                 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.DIRECT),
    129                 true, null, false);
     133                Shortcut.registerShortcut("view:zoom" + mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))),
     134                        getModeShortcut(mode), Shortcut.DIRECT), true, null, false);
    130135        String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
    131136        putValue("help", "Action/AutoScale/" + modeHelp);
     
    157162            break;
    158163        default:
    159             throw new IllegalArgumentException("Unknown mode: "+mode);
     164            throw new IllegalArgumentException("Unknown mode: " + mode);
    160165        }
    161166        installAdapters();
    162167    }
    163168
    164     public void autoScale()  {
     169    public void autoScale() {
    165170        if (Main.isDisplayingMapView()) {
    166             switch(mode) {
     171            switch (mode) {
    167172            case "previous":
    168173                Main.map.mapView.zoomPrevious();
     
    195200    protected Layer getFirstSelectedLayer() {
    196201        List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
    197         if (layers.isEmpty()) return null;
     202        if (layers.isEmpty())
     203            return null;
    198204        return layers.get(0);
    199205    }
     
    202208        BoundingXYVisitor v = "problem".equals(mode) ? new ValidatorBoundingXYVisitor() : new BoundingXYVisitor();
    203209
    204         switch(mode) {
     210        switch (mode) {
    205211        case "problem":
    206212            TestError error = Main.map.validatorDialog.getSelectedError();
    207             if (error == null) return null;
     213            if (error == null)
     214                return null;
    208215            ((ValidatorBoundingXYVisitor) v).visit(error);
    209             if (v.getBounds() == null) return null;
     216            if (v.getBounds() == null)
     217                return null;
    210218            v.enlargeBoundingBox(Main.pref.getDouble("validator.zoom-enlarge-bbox", 0.0002));
    211219            break;
     
    220228            // try to zoom to the first selected layer
    221229            Layer l = getFirstSelectedLayer();
    222             if (l == null) return null;
     230            if (l == null)
     231                return null;
    223232            l.visitBoundingBox(v);
    224233            break;
     
    241250                        ("selection".equals(mode) ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
    242251                        tr("Information"),
    243                         JOptionPane.INFORMATION_MESSAGE
    244                 );
     252                        JOptionPane.INFORMATION_MESSAGE);
    245253                return null;
    246254            }
     
    256264            break;
    257265        case "download":
    258             Bounds bounds = DownloadDialog.getSavedDownloadBounds();
    259             if (bounds != null) {
    260                 try {
    261                     v.visit(bounds);
    262                 } catch (Exception e) {
    263                     Main.warn(e);
    264                 }
     266
     267            if (lastZoomTime > 0 && System.currentTimeMillis() - lastZoomTime > Main.pref.getLong("zoom.bounds.reset.time", 10*1000)) {
     268                lastZoomTime = -1;
     269            }
     270            ArrayList<DataSource> dataSources = new ArrayList<>(Main.main.getCurrentDataSet().getDataSources());
     271            int s = dataSources.size();
     272            if(s > 0) {
     273                if(lastZoomTime == -1 || lastZoomArea == -1 || lastZoomArea > s) {
     274                    lastZoomArea = s-1;
     275                    v.visit(dataSources.get(lastZoomArea).bounds);
     276                } else if(lastZoomArea > 0) {
     277                    lastZoomArea -= 1;
     278                    v.visit(dataSources.get(lastZoomArea).bounds);
     279                } else {
     280                    lastZoomArea = -1;
     281                    v.visit(new Bounds(Main.main.getCurrentDataSet().getDataSourceArea().getBounds2D()));
     282                }
     283                lastZoomTime = System.currentTimeMillis();
     284            } else {
     285                lastZoomTime = -1;
     286                lastZoomArea = -1;
    265287            }
    266288            break;
     
    271293    @Override
    272294    protected void updateEnabledState() {
    273         switch(mode) {
     295        switch (mode) {
    274296        case "selection":
    275             setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
     297            setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
    276298            break;
    277299        case "layer":
     
    296318            break;
    297319        default:
    298             setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasLayers()
    299             );
     320            setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasLayers());
    300321        }
    301322    }
     
    338359            if ("conflict".equals(mode)) {
    339360                conflictSelectionListener = new ListSelectionListener() {
    340                     @Override public void valueChanged(ListSelectionEvent e) {
     361                    @Override
     362                    public void valueChanged(ListSelectionEvent e) {
    341363                        updateEnabledState();
    342364                    }
     
    344366            } else if ("problem".equals(mode)) {
    345367                validatorSelectionListener = new TreeSelectionListener() {
    346                     @Override public void valueChanged(TreeSelectionEvent e) {
     368                    @Override
     369                    public void valueChanged(TreeSelectionEvent e) {
    347370                        updateEnabledState();
    348371                    }
     
    351374        }
    352375
    353         @Override public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
     376        @Override
     377        public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
    354378            if (conflictSelectionListener != null) {
    355379                if (newFrame != null) {
Note: See TracChangeset for help on using the changeset viewer.