Changeset 16940 in josm


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

see #19716 - ChooseTrackVisibilityAction: replace TrackLength with custom renderer

File:
1 edited

Legend:

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

    r16625 r16940  
    1313import java.awt.event.MouseEvent;
    1414import java.awt.event.MouseListener;
    15 import java.io.Serializable;
    1615import java.util.Arrays;
    1716import java.util.Comparator;
     
    3433import javax.swing.ListSelectionModel;
    3534import javax.swing.event.TableModelEvent;
     35import javax.swing.table.DefaultTableCellRenderer;
    3636import javax.swing.table.DefaultTableModel;
    3737import javax.swing.table.TableCellRenderer;
     
    7575
    7676    /**
    77      * Class to format a length according to SystemOfMesurement.
    78      */
    79     private static final class TrackLength {
    80         private final double value;
    81 
    82         /**
    83          * Constructs a new {@code TrackLength} object with a given length.
    84          * @param value length of the track
    85          */
    86         TrackLength(double value) {
    87             this.value = value;
    88         }
    89 
    90         /**
    91          * Provides string representation.
    92          * @return String representation depending of SystemOfMeasurement
    93          */
    94         @Override
    95         public String toString() {
    96             return SystemOfMeasurement.getSystemOfMeasurement().getDistText(value);
    97         }
    98     }
    99 
    100     /**
    101      * Comparator for TrackLength objects
    102      */
    103     private static final class LengthContentComparator implements Comparator<TrackLength>, Serializable {
    104 
    105         private static final long serialVersionUID = 1L;
    106 
    107         /**
    108          * Compare 2 TrackLength objects relative to the real length
    109          */
    110         @Override
    111         public int compare(TrackLength l0, TrackLength l1) {
    112             return Double.compare(l0.value, l1.value);
    113         }
    114     }
    115 
    116     /**
    11777     * Gathers all available data for the tracks and returns them as array of arrays
    11878     * in the expected column order.
     
    12787            String desc = (String) Optional.ofNullable(attr.get(GpxConstants.GPX_DESC)).orElse("");
    12888            String time = GpxLayer.getTimespanForTrack(trk);
    129             TrackLength length = new TrackLength(trk.length());
    13089            String url = (String) Optional.ofNullable(attr.get("url")).orElse("");
    131             tracks[i] = new Object[]{name, desc, time, length, url, trk};
     90            tracks[i] = new Object[]{name, desc, time, trk.length(), url, trk};
    13291            i++;
    13392        }
     
    179138        t.setRowSorter(rowSorter);
    180139        rowSorter.setModel(model);
    181         rowSorter.setComparator(3, new LengthContentComparator());
     140        rowSorter.setComparator(3, Comparator.comparingDouble(length -> (double) length));
    182141        // default column widths
    183142        t.getColumnModel().getColumn(0).setPreferredWidth(220);
     
    185144        t.getColumnModel().getColumn(2).setPreferredWidth(200);
    186145        t.getColumnModel().getColumn(3).setPreferredWidth(50);
     146        t.getColumnModel().getColumn(3).setCellRenderer(new DefaultTableCellRenderer() {
     147            @Override
     148            public Component getTableCellRendererComponent(
     149                    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
     150                value = SystemOfMeasurement.getSystemOfMeasurement().getDistText((Double) value);
     151                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
     152            }
     153        });
    187154        t.getColumnModel().getColumn(4).setPreferredWidth(100);
    188155        // make the link clickable
Note: See TracChangeset for help on using the changeset viewer.