Ticket #7630: 7630.2.patch

File 7630.2.patch, 14.2 KB (added by harvey, 9 years ago)
  • src/org/openstreetmap/josm/actions/AutoScaleAction.java

     
    77
    88import java.awt.event.ActionEvent;
    99import java.awt.event.KeyEvent;
     10import java.awt.geom.Area;
     11import java.util.ArrayList;
    1012import java.util.Arrays;
    1113import java.util.Collection;
    1214import java.util.Collections;
    1315import java.util.HashSet;
    1416import java.util.List;
     17import java.util.ListIterator;
     18import java.util.concurrent.TimeUnit;
    1519
    1620import javax.swing.JOptionPane;
    1721import javax.swing.event.ListSelectionEvent;
     
    2125
    2226import org.openstreetmap.josm.Main;
    2327import org.openstreetmap.josm.data.Bounds;
     28import org.openstreetmap.josm.data.DataSource;
    2429import org.openstreetmap.josm.data.conflict.Conflict;
    2530import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2631import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     
    3035import org.openstreetmap.josm.gui.MapView;
    3136import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    3237import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor;
    33 import org.openstreetmap.josm.gui.download.DownloadDialog;
    3438import org.openstreetmap.josm.gui.layer.Layer;
    3539import org.openstreetmap.josm.tools.Shortcut;
    3640
     
    4145public class AutoScaleAction extends JosmAction {
    4246
    4347    public static final Collection<String> MODES = Collections.unmodifiableList(Arrays.asList(
    44         marktr(/* ICON(dialogs/autoscale/) */ "data"),
    45         marktr(/* ICON(dialogs/autoscale/) */ "layer"),
    46         marktr(/* ICON(dialogs/autoscale/) */ "selection"),
    47         marktr(/* ICON(dialogs/autoscale/) */ "conflict"),
    48         marktr(/* ICON(dialogs/autoscale/) */ "download"),
    49         marktr(/* ICON(dialogs/autoscale/) */ "problem"),
    50         marktr(/* ICON(dialogs/autoscale/) */ "previous"),
    51         marktr(/* ICON(dialogs/autoscale/) */ "next")));
     48            marktr(/* ICON(dialogs/autoscale/) */"data"), marktr(/* ICON(dialogs/autoscale/) */"layer"),
     49            marktr(/* ICON(dialogs/autoscale/) */"selection"), marktr(/* ICON(dialogs/autoscale/) */"conflict"),
     50            marktr(/* ICON(dialogs/autoscale/) */"download"), marktr(/* ICON(dialogs/autoscale/) */"problem"),
     51            marktr(/* ICON(dialogs/autoscale/) */"previous"), marktr(/* ICON(dialogs/autoscale/) */"next")));
    5252
    5353    private final String mode;
    5454
    5555    protected ZoomChangeAdapter zoomChangeAdapter;
    5656    protected MapFrameAdapter mapFrameAdapter;
     57    protected DataSource previousDataSource;
     58    protected Collection<DataSource> dataSources;
     59    protected ArrayList<DataSource> dataSourcesList;
     60    protected int firstPass = 0;
     61    protected long lastZoomTime = -1;
    5762
     63    private long timeDifference;
     64
     65    private long seconds;
     66
    5867    /**
    5968     * Zooms the current map view to the currently selected primitives.
    6069     * Does nothing if there either isn't a current map view or if there isn't a current data
     
    6271     *
    6372     */
    6473    public static void zoomToSelection() {
    65         if (Main.main == null || !Main.main.hasEditLayer()) return;
     74        if (Main.main == null || !Main.main.hasEditLayer())
     75            return;
    6676        Collection<OsmPrimitive> sel = Main.main.getEditLayer().data.getSelected();
    6777        if (sel.isEmpty()) {
    68             JOptionPane.showMessageDialog(
    69                     Main.parent,
    70                     tr("Nothing selected to zoom to."),
    71                     tr("Information"),
    72                     JOptionPane.INFORMATION_MESSAGE
    73             );
     78            JOptionPane.showMessageDialog(Main.parent, tr("Nothing selected to zoom to."), tr("Information"),
     79                    JOptionPane.INFORMATION_MESSAGE);
    7480            return;
    7581        }
    7682        zoomTo(sel);
     
    97103
    98104        // TODO: convert this to switch/case and make sure the parsing still works
    99105        /* leave as single line for shortcut overview parsing! */
    100         if (mode.equals("data")) { shortcut = KeyEvent.VK_1; }
    101         else if (mode.equals("layer")) { shortcut = KeyEvent.VK_2; }
    102         else if (mode.equals("selection")) { shortcut = KeyEvent.VK_3; }
    103         else if (mode.equals("conflict")) { shortcut = KeyEvent.VK_4; }
    104         else if (mode.equals("download")) { shortcut = KeyEvent.VK_5; }
    105         else if (mode.equals("problem")) { shortcut = KeyEvent.VK_6; }
    106         else if (mode.equals("previous")) { shortcut = KeyEvent.VK_8; }
    107         else if (mode.equals("next")) { shortcut = KeyEvent.VK_9; }
     106        if (mode.equals("data")) {
     107            shortcut = KeyEvent.VK_1;
     108        } else if (mode.equals("layer")) {
     109            shortcut = KeyEvent.VK_2;
     110        } else if (mode.equals("selection")) {
     111            shortcut = KeyEvent.VK_3;
     112        } else if (mode.equals("conflict")) {
     113            shortcut = KeyEvent.VK_4;
     114        } else if (mode.equals("download")) {
     115            shortcut = KeyEvent.VK_5;
     116        } else if (mode.equals("problem")) {
     117            shortcut = KeyEvent.VK_6;
     118        } else if (mode.equals("previous")) {
     119            shortcut = KeyEvent.VK_8;
     120        } else if (mode.equals("next")) {
     121            shortcut = KeyEvent.VK_9;
     122        }
    108123
    109124        return shortcut;
    110125    }
     
    124139     * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
    125140     */
    126141    public AutoScaleAction(final String mode) {
    127         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);
     142        super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)), Shortcut
     143                .registerShortcut("view:zoom" + mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))),
     144                        getModeShortcut(mode), Shortcut.DIRECT), true, null, false);
    130145        String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
    131146        putValue("help", "Action/AutoScale/" + modeHelp);
    132147        this.mode = mode;
     
    156171            putValue("help", ht("/Action/ZoomToNext"));
    157172            break;
    158173        default:
    159             throw new IllegalArgumentException("Unknown mode: "+mode);
     174            throw new IllegalArgumentException("Unknown mode: " + mode);
    160175        }
    161176        installAdapters();
    162177    }
    163178
    164     public void autoScale()  {
     179    public void autoScale() {
    165180        if (Main.isDisplayingMapView()) {
    166             switch(mode) {
     181            switch (mode) {
    167182            case "previous":
    168183                Main.map.mapView.zoomPrevious();
    169184                break;
     
    194209     */
    195210    protected Layer getFirstSelectedLayer() {
    196211        List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
    197         if (layers.isEmpty()) return null;
     212        if (layers.isEmpty())
     213            return null;
    198214        return layers.get(0);
    199215    }
    200216
     
    201217    private BoundingXYVisitor getBoundingBox() {
    202218        BoundingXYVisitor v = "problem".equals(mode) ? new ValidatorBoundingXYVisitor() : new BoundingXYVisitor();
    203219
    204         switch(mode) {
     220        switch (mode) {
    205221        case "problem":
    206222            TestError error = Main.map.validatorDialog.getSelectedError();
    207             if (error == null) return null;
     223            if (error == null)
     224                return null;
    208225            ((ValidatorBoundingXYVisitor) v).visit(error);
    209             if (v.getBounds() == null) return null;
     226            if (v.getBounds() == null)
     227                return null;
    210228            v.enlargeBoundingBox(Main.pref.getDouble("validator.zoom-enlarge-bbox", 0.0002));
    211229            break;
    212230        case "data":
     
    219237                return null;
    220238            // try to zoom to the first selected layer
    221239            Layer l = getFirstSelectedLayer();
    222             if (l == null) return null;
     240            if (l == null)
     241                return null;
    223242            l.visitBoundingBox(v);
    224243            break;
    225244        case "selection":
     
    236255                }
    237256            }
    238257            if (sel.isEmpty()) {
    239                 JOptionPane.showMessageDialog(
    240                         Main.parent,
    241                         ("selection".equals(mode) ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
    242                         tr("Information"),
    243                         JOptionPane.INFORMATION_MESSAGE
    244                 );
     258                JOptionPane
     259                        .showMessageDialog(Main.parent, ("selection".equals(mode) ? tr("Nothing selected to zoom to.")
     260                                : tr("No conflicts to zoom to")), tr("Information"), JOptionPane.INFORMATION_MESSAGE);
    245261                return null;
    246262            }
    247263            for (OsmPrimitive osm : sel) {
     
    255271            v.enlargeToMinSize(Main.pref.getDouble("zoom_to_selection_min_size_in_meter", 100));
    256272            break;
    257273        case "download":
    258             Bounds bounds = DownloadDialog.getSavedDownloadBounds();
     274
     275            if (lastZoomTime == -1) {
     276                seconds = 0;
     277            } else {
     278                timeDifference = System.currentTimeMillis() - lastZoomTime;
     279                seconds = TimeUnit.MILLISECONDS.toSeconds(timeDifference);
     280            }
     281
     282            Bounds bounds;
     283            dataSources = Main.main.getCurrentDataSet().getDataSources();
     284            dataSourcesList = new ArrayList<>(dataSources);
     285            System.out.println(seconds);
     286            //Calculate time difference. If greater than thirty seconds reset to last download
     287            if (seconds > 30) {
     288                previousDataSource = dataSourcesList.listIterator(dataSourcesList.size()).previous();
     289                v.visit(previousDataSource.bounds);
     290                lastZoomTime = System.currentTimeMillis();
     291                return v;
     292            }
     293
     294            if (firstPass == 0) {
     295                previousDataSource = dataSourcesList.listIterator(dataSourcesList.size()).previous();
     296                firstPass = 2;
     297            }
     298            if (firstPass == 3) {
     299                final Area dataSourceArea = Main.main.getCurrentDataSet().getDataSourceArea();
     300                bounds = new Bounds(dataSourceArea.getBounds2D());
     301                firstPass = 1;
     302            } else {
     303                getPreviousData();
     304                bounds = previousDataSource.bounds;
     305            }
     306
     307            if (previousDataSource == dataSourcesList.listIterator(1).previous()) {
     308                firstPass++;
     309            }
     310
    259311            if (bounds != null) {
    260312                try {
    261313                    v.visit(bounds);
     314                    lastZoomTime = System.currentTimeMillis();
    262315                } catch (Exception e) {
    263316                    Main.warn(e);
    264317                }
     
    268321        return v;
    269322    }
    270323
     324    public void getPreviousData() {
     325        for (ListIterator<DataSource> iterator = dataSourcesList.listIterator(dataSourcesList.size()); iterator
     326                .hasPrevious();) {
     327            DataSource currentSource = iterator.previous();
     328            if (currentSource.equals(previousDataSource)) {
     329                if (iterator.hasPrevious()) {
     330                    previousDataSource = iterator.previous();
     331                } else {
     332                    previousDataSource = dataSourcesList.listIterator(dataSourcesList.size()).previous();
     333                }
     334                return;
     335            }
     336        }
     337    }
     338
    271339    @Override
    272340    protected void updateEnabledState() {
    273         switch(mode) {
     341        switch (mode) {
    274342        case "selection":
    275             setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
     343            setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
    276344            break;
    277345        case "layer":
    278346            if (!Main.isDisplayingMapView() || Main.map.mapView.getAllLayersAsList().isEmpty()) {
     
    295363            setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasZoomRedoEntries());
    296364            break;
    297365        default:
    298             setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasLayers()
    299             );
     366            setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasLayers());
    300367        }
    301368    }
    302369
     
    337404        public MapFrameAdapter() {
    338405            if ("conflict".equals(mode)) {
    339406                conflictSelectionListener = new ListSelectionListener() {
    340                     @Override public void valueChanged(ListSelectionEvent e) {
     407                    @Override
     408                    public void valueChanged(ListSelectionEvent e) {
    341409                        updateEnabledState();
    342410                    }
    343411                };
    344412            } else if ("problem".equals(mode)) {
    345413                validatorSelectionListener = new TreeSelectionListener() {
    346                     @Override public void valueChanged(TreeSelectionEvent e) {
     414                    @Override
     415                    public void valueChanged(TreeSelectionEvent e) {
    347416                        updateEnabledState();
    348417                    }
    349418                };
     
    350419            }
    351420        }
    352421
    353         @Override public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
     422        @Override
     423        public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
    354424            if (conflictSelectionListener != null) {
    355425                if (newFrame != null) {
    356426                    newFrame.conflictDialog.addListSelectionListener(conflictSelectionListener);
  • src/org/openstreetmap/josm/gui/NavigatableComponent.java

     
    660660        }
    661661    }
    662662
     663    public Stack<ZoomData> getZoomUndoBuffer(){
     664        return zoomUndoBuffer;
     665    }
     666
     667    public Stack<ZoomData> getZoomRedoBuffer(){
     668        return zoomRedoBuffer;
     669    }
     670
    663671    public boolean hasZoomUndoEntries() {
    664672        return !zoomUndoBuffer.isEmpty();
    665673    }