Changeset 16847 in josm for trunk/src/org


Ignore:
Timestamp:
2020-08-04T22:19:41+02:00 (4 years ago)
Author:
simon04
Message:

fix #10488 - GpxLayer: jump to previous/next segment

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r16553 r16847  
    1313import java.util.ArrayList;
    1414import java.util.Arrays;
     15import java.util.Collections;
    1516import java.util.Date;
    1617import java.util.List;
     18import java.util.NoSuchElementException;
    1719import java.util.stream.Collectors;
    1820
     
    3436import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeListener;
    3537import org.openstreetmap.josm.data.gpx.IGpxTrack;
     38import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
    3639import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    3740import org.openstreetmap.josm.data.projection.Projection;
     41import org.openstreetmap.josm.gui.MainApplication;
    3842import org.openstreetmap.josm.gui.MapView;
    3943import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    4044import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
    4145import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
     46import org.openstreetmap.josm.gui.layer.JumpToMarkerActions.JumpToMarkerLayer;
     47import org.openstreetmap.josm.gui.layer.JumpToMarkerActions.JumpToNextMarker;
     48import org.openstreetmap.josm.gui.layer.JumpToMarkerActions.JumpToPreviousMarker;
    4249import org.openstreetmap.josm.gui.layer.gpx.ChooseTrackVisibilityAction;
    4350import org.openstreetmap.josm.gui.layer.gpx.ConvertFromGpxLayerAction;
     
    6067 * A layer that displays data from a Gpx file / the OSM gpx downloads.
    6168 */
    62 public class GpxLayer extends AbstractModifiableLayer implements ExpertModeChangeListener {
     69public class GpxLayer extends AbstractModifiableLayer implements ExpertModeChangeListener, JumpToMarkerLayer {
    6370
    6471    /** GPX data */
     
    8390     */
    8491    private MarkerLayer linkedMarkerLayer;
     92
     93    /**
     94     * Current segment for {@link JumpToMarkerLayer}.
     95     */
     96    private IGpxTrackSegment currentSegment = null;
    8597
    8698    /**
     
    239251    @Override
    240252    public Action[] getMenuEntries() {
     253        JumpToNextMarker jumpToNext = new JumpToNextMarker(this);
     254        jumpToNext.putValue(Action.NAME, tr("Jump to next segment"));
     255        JumpToPreviousMarker jumpToPrevious = new JumpToPreviousMarker(this);
     256        jumpToPrevious.putValue(Action.NAME, tr("Jump to previous segment"));
    241257        List<Action> entries = new ArrayList<>(Arrays.asList(
    242258                LayerListDialog.getInstance().createShowHideLayerAction(),
     
    251267                new ImportAudioAction(this),
    252268                new MarkersFromNamedPointsAction(this),
     269                jumpToNext,
     270                jumpToPrevious,
    253271                new ConvertFromGpxLayerAction(this),
    254272                new DownloadAlongTrackAction(data),
     
    555573        return data;
    556574    }
     575
     576    /**
     577     * Jump (move the viewport) to the next track segment.
     578     */
     579    @Override
     580    public void jumpToNextMarker() {
     581        List<IGpxTrackSegment> segments = data.getTrackSegmentsStream().collect(Collectors.toList());
     582        jumpToNext(segments);
     583    }
     584
     585    /**
     586     * Jump (move the viewport) to the previous track segment.
     587     */
     588    @Override
     589    public void jumpToPreviousMarker() {
     590        List<IGpxTrackSegment> segments = data.getTrackSegmentsStream().collect(Collectors.toList());
     591        Collections.reverse(segments);
     592        jumpToNext(segments);
     593    }
     594
     595    private void jumpToNext(List<IGpxTrackSegment> segments) {
     596        if (segments.isEmpty()) {
     597            return;
     598        } else if (currentSegment == null) {
     599            currentSegment = segments.get(0);
     600            MainApplication.getMap().mapView.zoomTo(currentSegment.getBounds());
     601        } else {
     602            try {
     603                int index = segments.indexOf(currentSegment);
     604                currentSegment = segments.listIterator(index + 1).next();
     605                MainApplication.getMap().mapView.zoomTo(currentSegment.getBounds());
     606            } catch (IndexOutOfBoundsException | NoSuchElementException ignore) {
     607                Logging.trace(ignore);
     608            }
     609        }
     610    }
    557611}
Note: See TracChangeset for help on using the changeset viewer.