Changeset 8219 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2015-04-18T15:33:28+02:00 (10 years ago)
Author:
simon04
Message:

fix #6994 - History dialog: handle reversed ways better

This is achieved by comparing the reference elements in reversed order and
testing whether this diff requires less deletions+inserts

Location:
trunk/src/org/openstreetmap/josm/gui/history
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/history/DiffTableModel.java

    r7937 r8219  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.history;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.util.ArrayList;
     
    1416 */
    1517class DiffTableModel extends AbstractTableModel {
    16     private List<TwoColumnDiff.Item> rows;
     18    private List<TwoColumnDiff.Item> rows = new ArrayList<>();
     19    private boolean reversed = false;
    1720
    18     public void setRows(List<TwoColumnDiff.Item> rows) {
     21    public void setRows(List<TwoColumnDiff.Item> rows, boolean reversed) {
    1922        this.rows = rows;
    20     }
    21 
    22     public DiffTableModel(List<TwoColumnDiff.Item> rows) {
    23         this.rows = rows;
    24     }
    25     public DiffTableModel() {
    26         this.rows = new ArrayList<>();
     23        this.reversed = reversed;
     24        fireTableDataChanged();
    2725    }
    2826    @Override
     
    3432    public int getColumnCount() {
    3533        return 1;
     34    }
     35
     36    public boolean isReversed() {
     37        return reversed;
    3638    }
    3739
  • trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserModel.java

    r7299 r8219  
    8484    private HistoryOsmPrimitive latest;
    8585
    86     private VersionTableModel versionTableModel;
    87     private TagTableModel currentTagTableModel;
    88     private TagTableModel referenceTagTableModel;
    89     private DiffTableModel currentRelationMemberTableModel;
    90     private DiffTableModel referenceRelationMemberTableModel;
    91     private DiffTableModel referenceNodeListTableModel;
    92     private DiffTableModel currentNodeListTableModel;
     86    private final VersionTableModel versionTableModel;
     87    private final TagTableModel currentTagTableModel;
     88    private final TagTableModel referenceTagTableModel;
     89    private final DiffTableModel currentRelationMemberTableModel;
     90    private final DiffTableModel referenceRelationMemberTableModel;
     91    private final DiffTableModel referenceNodeListTableModel;
     92    private final DiffTableModel currentNodeListTableModel;
    9393
    9494    /**
     
    223223     */
    224224    protected void initNodeListTableModels() {
    225 
    226225        if(current.getType() != OsmPrimitiveType.WAY || reference.getType() != OsmPrimitiveType.WAY)
    227226            return;
     
    229228                ((HistoryWay)reference).getNodes().toArray(),
    230229                ((HistoryWay)current).getNodes().toArray());
    231         referenceNodeListTableModel.setRows(diff.referenceDiff);
    232         currentNodeListTableModel.setRows(diff.currentDiff);
    233 
    234         referenceNodeListTableModel.fireTableDataChanged();
    235         currentNodeListTableModel.fireTableDataChanged();
     230        referenceNodeListTableModel.setRows(diff.referenceDiff, diff.referenceReversed);
     231        currentNodeListTableModel.setRows(diff.currentDiff, false);
    236232    }
    237233
     
    239235        if(current.getType() != OsmPrimitiveType.RELATION || reference.getType() != OsmPrimitiveType.RELATION)
    240236            return;
    241 
    242237        TwoColumnDiff diff = new TwoColumnDiff(
    243238                ((HistoryRelation)reference).getMembers().toArray(),
    244239                ((HistoryRelation)current).getMembers().toArray());
    245 
    246         referenceRelationMemberTableModel.setRows(diff.referenceDiff);
    247         currentRelationMemberTableModel.setRows(diff.currentDiff);
    248 
    249         currentRelationMemberTableModel.fireTableDataChanged();
    250         referenceRelationMemberTableModel.fireTableDataChanged();
     240        referenceRelationMemberTableModel.setRows(diff.referenceDiff, diff.referenceReversed);
     241        currentRelationMemberTableModel.setRows(diff.currentDiff, false);
    251242    }
    252243
  • trunk/src/org/openstreetmap/josm/gui/history/NodeListViewer.java

    r6936 r8219  
    1818import javax.swing.JTable;
    1919import javax.swing.ListSelectionModel;
     20import javax.swing.event.TableModelEvent;
     21import javax.swing.event.TableModelListener;
    2022import javax.swing.table.TableModel;
    2123
     
    6062
    6163    protected JTable buildReferenceNodeListTable() {
    62         JTable table = new JTable(
    63                 model.getNodeListTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME),
    64                 new NodeListTableColumnModel()
    65         );
     64        final DiffTableModel tableModel = model.getNodeListTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME);
     65        final NodeListTableColumnModel columnModel = new NodeListTableColumnModel();
     66        final JTable table = new JTable(tableModel, columnModel);
     67        tableModel.addTableModelListener(newReversedChangeListener(table, columnModel));
    6668        table.setName("table.referencenodelisttable");
    6769        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
    7375
    7476    protected JTable buildCurrentNodeListTable() {
    75         JTable table = new JTable(
    76                 model.getNodeListTableModel(PointInTimeType.CURRENT_POINT_IN_TIME),
    77                 new NodeListTableColumnModel()
    78         );
     77        final DiffTableModel tableModel = model.getNodeListTableModel(PointInTimeType.CURRENT_POINT_IN_TIME);
     78        final NodeListTableColumnModel columnModel = new NodeListTableColumnModel();
     79        final JTable table = new JTable(tableModel, columnModel);
     80        tableModel.addTableModelListener(newReversedChangeListener(table, columnModel));
    7981        table.setName("table.currentnodelisttable");
    8082        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
    8385        table.addMouseListener(new DoubleClickAdapter(table));
    8486        return table;
     87    }
     88
     89    protected TableModelListener newReversedChangeListener(final JTable table, final NodeListTableColumnModel columnModel) {
     90        return new TableModelListener() {
     91            private Boolean reversed = null;
     92            final String nonReversedText = tr("Nodes") + (table.getFont().canDisplay('\u25bc') ? " \u25bc" : " (1-n)");
     93            final String reversedText = tr("Nodes") + (table.getFont().canDisplay('\u25b2') ? " \u25b2" : " (n-1)");
     94
     95            @Override
     96            public void tableChanged(TableModelEvent e) {
     97                if (e.getSource() instanceof DiffTableModel) {
     98                    final DiffTableModel model = (DiffTableModel) e.getSource();
     99                    if (reversed == null || reversed != model.isReversed()) {
     100                        reversed = model.isReversed();
     101                        columnModel.getColumn(0).setHeaderValue(reversed ? reversedText : nonReversedText);
     102                        table.getTableHeader().repaint();
     103                    }
     104                }
     105            }
     106        };
    85107    }
    86108
  • trunk/src/org/openstreetmap/josm/gui/history/TwoColumnDiff.java

    r7937 r8219  
    55import java.awt.Color;
    66import java.util.ArrayList;
     7import java.util.Arrays;
     8import java.util.Collections;
    79import java.util.List;
    810
     
    5557    Object[] reference;
    5658    Object[] current;
     59    boolean referenceReversed = false;
    5760
    5861    public TwoColumnDiff(Object[] reference, Object[] current) {
     
    6568
    6669    private void diff() {
    67         Diff diff = new Diff(reference, current);
    68         Diff.Change script = diff.diff_2(false);
    69         twoColumnDiffFromScript(script, reference, current);
     70        Diff.Change script = new Diff(reference, current).diff_2(false);
     71        // attempt diff with reference reversed and test whether less deletions+inserts are required
     72        Object[] referenceReversed = Utils.copyArray(reference);
     73        Collections.reverse(Arrays.asList(referenceReversed));
     74        Diff.Change scriptReversed = new Diff(referenceReversed, current).diff_2(false);
     75        if (scriptReversed == null /* reference and current are identical */
     76                || script != null && scriptReversed.deleted + scriptReversed.inserted < script.deleted + script.inserted) {
     77            this.referenceReversed = true;
     78            twoColumnDiffFromScript(scriptReversed, referenceReversed, current);
     79        } else {
     80            this.referenceReversed = false;
     81            twoColumnDiffFromScript(script, reference, current);
     82        }
    7083    }
    7184
Note: See TracChangeset for help on using the changeset viewer.