Ignore:
Timestamp:
2013-01-28T14:06:52+01:00 (11 years ago)
Author:
bastiK
Message:

add session support for marker layers (see #4029)

The data is exported to a separate GPX file that contains one waypoint for each marker.
This is not very elegant, because most of the time, all the info is already contained in the original GPX File.
However, when dealing with audio markers, they can be synchronized, or additional markers are added
at certain playback positions. This info must be retained.
Another complication is, that two or more MarkerLayers can be merged to one.

All these problems are avoided by explicitly exporting the markers to a separate file (as done in this commit).

File:
1 edited

Legend:

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

    r5681 r5684  
    88import java.net.MalformedURLException;
    99import java.net.URL;
     10import java.text.DateFormat;
     11import java.text.SimpleDateFormat;
    1012import java.util.ArrayList;
    1113import java.util.Collection;
     14import java.util.Date;
    1215import java.util.HashMap;
    1316import java.util.LinkedList;
    1417import java.util.List;
    1518import java.util.Map;
     19import java.util.TimeZone;
    1620
    1721import javax.swing.Icon;
     
    2327import org.openstreetmap.josm.data.coor.EastNorth;
    2428import org.openstreetmap.josm.data.coor.LatLon;
     29import org.openstreetmap.josm.data.gpx.Extensions;
    2530import org.openstreetmap.josm.data.gpx.GpxConstants;
    2631import org.openstreetmap.josm.data.gpx.GpxLink;
     
    181186        Marker.markerProducers.add(new MarkerProducers() {
    182187            @SuppressWarnings("unchecked")
     188            @Override
    183189            public Marker createMarker(WayPoint wpt, File relativePath, MarkerLayer parentLayer, double time, double offset) {
    184190                String uri = null;
     
    217223                }
    218224                else if (url.toString().endsWith(".wav")) {
    219                     return new AudioMarker(wpt.getCoor(), wpt, url, parentLayer, time, offset);
     225                    AudioMarker audioMarker = new AudioMarker(wpt.getCoor(), wpt, url, parentLayer, time, offset);
     226                    Extensions exts = (Extensions) wpt.get(GpxConstants.META_EXTENSIONS);
     227                    if (exts != null && exts.containsKey("offset")) {
     228                        try {
     229                            double syncOffset = Double.parseDouble(exts.get("sync-offset"));
     230                            audioMarker.syncOffset = syncOffset;
     231                        } catch (NumberFormatException nfe) {}
     232                    }
     233                    return audioMarker;
    220234                } else if (url.toString().endsWith(".png") || url.toString().endsWith(".jpg") || url.toString().endsWith(".jpeg") || url.toString().endsWith(".gif")) {
    221235                    return new ImageMarker(wpt.getCoor(), url, parentLayer, time, offset);
     
    234248     * @param relativePath An path to use for constructing relative URLs or
    235249     *        <code>null</code> for no relative URLs
     250     * @param parentLayer the <code>MarkerLayer</code> that will contain the created <code>Marker</code>
     251     * @param time time of the marker in seconds since epoch
    236252     * @param offset double in seconds as the time offset of this marker from
    237253     *        the GPX file from which it was derived (if any).
     
    247263    }
    248264
     265    private static final DateFormat timeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
     266    static {
     267         TimeZone tz = TimeZone.getTimeZone("UTC");
     268         timeFormatter.setTimeZone(tz);
     269    }
     270
    249271    public static final String MARKER_OFFSET = "waypointOffset";
    250272    public static final String MARKER_FORMATTED_OFFSET = "formattedWaypointOffset";
     
    254276    public static final String LABEL_PATTERN_DESC = "{desc}";
    255277
    256 
    257278    private final TemplateEngineDataProvider dataProvider;
    258279    private final String text;
     
    260281    public final Icon symbol;
    261282    public final MarkerLayer parentLayer;
    262     public double time; /* absolute time of marker since epoch */
     283    public double time; /* absolute time of marker in seconds since epoch */
    263284    public double offset; /* time offset in seconds from the gpx point from which it was derived,
    264285                             may be adjusted later to sync with other data, so not final */
     
    296317    }
    297318
     319    /**
     320     * Convert Marker to WayPoint so it can be exported to a GPX file.
     321     *
     322     * Override in subclasses to add all necessary attributes.
     323     *
     324     * @return the corresponding WayPoint with all relevant attributes
     325     */
     326    public WayPoint convertToWayPoint() {
     327        WayPoint wpt = new WayPoint(getCoor());
     328        wpt.put("time", timeFormatter.format(new Date(Math.round(time * 1000))));
     329        if (text != null) {
     330            wpt.addExtension("text", text);
     331        } else if (dataProvider != null) {
     332            for (String key : dataProvider.getTemplateKeys()) {
     333                Object value = dataProvider.getTemplateValue(key, false);
     334                if (value != null && GpxConstants.WPT_KEYS.contains(key)) {
     335                    wpt.put(key, value);
     336                }
     337            }
     338        }
     339        return wpt;
     340    }
     341
    298342    public final void setCoor(LatLon coor) {
    299343        if(this.coor == null) {
     
    343387     * @param mv map view
    344388     * @param mousePressed true if the left mouse button is pressed
     389     * @param showTextOrIcon true if text and icon shall be drawn
    345390     */
    346391    public void paint(Graphics g, MapView mv, boolean mousePressed, boolean showTextOrIcon) {
     
    398443    }
    399444
    400     private String formatOffset () {
     445    private String formatOffset() {
    401446        int wholeSeconds = (int)(offset + 0.5);
    402447        if (wholeSeconds < 60)
Note: See TracChangeset for help on using the changeset viewer.