Changeset 16941 in josm


Ignore:
Timestamp:
2020-08-26T23:18:49+02:00 (4 years ago)
Author:
simon04
Message:

fix #19716 - ChooseTrackVisibilityAction: fix sorting by timespan

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

Legend:

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

    r16867 r16941  
    157157    public static String getTimespanForTrack(IGpxTrack trk) {
    158158        Date[] bounds = GpxData.getMinMaxTimeForTrack(trk);
     159        return bounds != null ? formatTimespan(bounds) : "";
     160    }
     161
     162    /**
     163     * Formats the timespan of the given track as a human readable string
     164     * @param bounds The bounds to format, i.e., an array containing the min/max date
     165     * @return The timespan as a string
     166     */
     167    public static String formatTimespan(Date[] bounds) {
    159168        String ts = "";
    160         if (bounds != null) {
    161             DateFormat df = DateUtils.getDateFormat(DateFormat.SHORT);
    162             String earliestDate = df.format(bounds[0]);
    163             String latestDate = df.format(bounds[1]);
    164 
    165             if (earliestDate.equals(latestDate)) {
    166                 DateFormat tf = DateUtils.getTimeFormat(DateFormat.SHORT);
    167                 ts += earliestDate + ' ';
    168                 ts += tf.format(bounds[0]) + " - " + tf.format(bounds[1]);
    169             } else {
    170                 DateFormat dtf = DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
    171                 ts += dtf.format(bounds[0]) + " - " + dtf.format(bounds[1]);
    172             }
    173 
    174             int diff = (int) (bounds[1].getTime() - bounds[0].getTime()) / 1000;
    175             ts += String.format(" (%d:%02d)", diff / 3600, (diff % 3600) / 60);
    176         }
     169        DateFormat df = DateUtils.getDateFormat(DateFormat.SHORT);
     170        String earliestDate = df.format(bounds[0]);
     171        String latestDate = df.format(bounds[1]);
     172
     173        if (earliestDate.equals(latestDate)) {
     174            DateFormat tf = DateUtils.getTimeFormat(DateFormat.SHORT);
     175            ts += earliestDate + ' ';
     176            ts += tf.format(bounds[0]) + " - " + tf.format(bounds[1]);
     177        } else {
     178            DateFormat dtf = DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
     179            ts += dtf.format(bounds[0]) + " - " + dtf.format(bounds[1]);
     180        }
     181
     182        int diff = (int) (bounds[1].getTime() - bounds[0].getTime()) / 1000;
     183        ts += String.format(" (%d:%02d)", diff / 3600, (diff % 3600) / 60);
    177184        return ts;
    178185    }
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java

    r16940 r16941  
    1515import java.util.Arrays;
    1616import java.util.Comparator;
     17import java.util.Date;
    1718import java.util.List;
    1819import java.util.Map;
     
    4243import org.openstreetmap.josm.data.SystemOfMeasurement;
    4344import org.openstreetmap.josm.data.gpx.GpxConstants;
     45import org.openstreetmap.josm.data.gpx.GpxData;
    4446import org.openstreetmap.josm.data.gpx.IGpxTrack;
    4547import org.openstreetmap.josm.gui.ExtendedDialog;
     
    8688            String name = (String) Optional.ofNullable(attr.get(GpxConstants.GPX_NAME)).orElse("");
    8789            String desc = (String) Optional.ofNullable(attr.get(GpxConstants.GPX_DESC)).orElse("");
    88             String time = GpxLayer.getTimespanForTrack(trk);
     90            Date[] time = GpxData.getMinMaxTimeForTrack(trk);
    8991            String url = (String) Optional.ofNullable(attr.get("url")).orElse("");
    9092            tracks[i] = new Object[]{name, desc, time, trk.length(), url, trk};
     
    138140        t.setRowSorter(rowSorter);
    139141        rowSorter.setModel(model);
     142        rowSorter.setComparator(2, Comparator.comparing((Date[] d) -> d == null ? Long.MIN_VALUE : d[0].getTime()));
    140143        rowSorter.setComparator(3, Comparator.comparingDouble(length -> (double) length));
    141144        // default column widths
     
    143146        t.getColumnModel().getColumn(1).setPreferredWidth(300);
    144147        t.getColumnModel().getColumn(2).setPreferredWidth(200);
     148        t.getColumnModel().getColumn(2).setCellRenderer(new DefaultTableCellRenderer() {
     149            @Override
     150            public Component getTableCellRendererComponent(
     151                    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
     152                if (value instanceof Date[]) {
     153                    value = GpxLayer.formatTimespan(((Date[]) value));
     154                }
     155                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
     156            }
     157        });
    145158        t.getColumnModel().getColumn(3).setPreferredWidth(50);
    146159        t.getColumnModel().getColumn(3).setCellRenderer(new DefaultTableCellRenderer() {
Note: See TracChangeset for help on using the changeset viewer.