Changeset 6958 in josm for trunk


Ignore:
Timestamp:
2014-04-05T17:37:26+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #9827 - Sort track length by value instead of string representation (patch by Balaitous)

File:
1 edited

Legend:

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

    r6717 r6958  
    1414import java.awt.event.MouseListener;
    1515import java.util.Arrays;
     16import java.util.Comparator;
    1617import java.util.Map;
    1718
     
    2627import javax.swing.event.ListSelectionEvent;
    2728import javax.swing.event.ListSelectionListener;
     29import javax.swing.table.DefaultTableModel;
    2830import javax.swing.table.TableCellRenderer;
     31import javax.swing.table.TableRowSorter;
    2932
    3033import org.openstreetmap.josm.Main;
     
    5659        this.layer = layer;
    5760        putValue("help", ht("/Action/ChooseTrackVisibility"));
     61    }
     62
     63    /**
     64     * Class to format a length according to SystemOfMesurement.
     65     */
     66    private final class TrackLength {
     67        private double value;
     68
     69        /**
     70         * Constructs a new {@code TrackLength} object with a given length.
     71         * @param value length of the track
     72         */
     73        TrackLength(double value) {
     74            this.value = value;
     75        }
     76
     77        /**
     78         * Provides string representation.
     79         * @return String representation depending of SystemOfMeasurement
     80         */
     81        @Override
     82        public String toString() {
     83            return NavigatableComponent.getSystemOfMeasurement().getDistText(value);
     84        }
     85    }
     86
     87    /**
     88     * Comparator for TrackLength objects
     89     */
     90    private final class LengthContentComparator implements Comparator<TrackLength> {
     91
     92        /**
     93         * Compare 2 TrackLength objects relative to the real length
     94         */
     95        @Override
     96        public int compare(TrackLength l0, TrackLength l1) {
     97            if(l0.value < l1.value)
     98                return -1;
     99            else if(l0.value > l1.value)
     100                return 1;
     101            return 0;
     102        }
    58103    }
    59104
     
    69114            String desc = (String) (attr.containsKey("desc") ? attr.get("desc") : "");
    70115            String time = GpxLayer.getTimespanForTrack(trk);
    71             String length = NavigatableComponent.getSystemOfMeasurement().getDistText(trk.length());
     116            TrackLength length = new TrackLength(trk.length());
    72117            String url = (String) (attr.containsKey("url") ? attr.get("url") : "");
    73             tracks[i] = new String[]{name, desc, time, length, url};
     118            tracks[i] = new Object[]{name, desc, time, length, url};
    74119            i++;
    75120        }
     
    82127    private JTable buildTable(Object[][] content) {
    83128        final String[] headers = {tr("Name"), tr("Description"), tr("Timespan"), tr("Length"), tr("URL")};
    84         final JTable t = new JTable(content, headers) {
     129        DefaultTableModel model = new DefaultTableModel(content, headers);
     130        final JTable t = new JTable(model) {
    85131            @Override
    86132            public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
     
    88134                if (c instanceof JComponent) {
    89135                    JComponent jc = (JComponent) c;
    90                     jc.setToolTipText((String) getValueAt(row, col));
     136                    jc.setToolTipText(getValueAt(row, col).toString());
    91137                }
    92138                return c;
     
    98144            }
    99145        };
     146        // define how to sort row
     147        TableRowSorter<DefaultTableModel> rowSorter = new TableRowSorter<DefaultTableModel>();
     148        t.setRowSorter(rowSorter);
     149        rowSorter.setModel(model);
     150        rowSorter.setComparator(3, new LengthContentComparator());
    100151        // default column widths
    101152        t.getColumnModel().getColumn(0).setPreferredWidth(220);
     
    124175            }
    125176        };
    126         t.setAutoCreateRowSorter(true);
    127177        t.addMouseListener(urlOpener);
    128178        t.setFillsViewportHeight(true);
Note: See TracChangeset for help on using the changeset viewer.