1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Point;
|
---|
7 |
|
---|
8 | import javax.swing.JTable;
|
---|
9 | import javax.swing.ListSelectionModel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
13 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
14 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
15 | import org.openstreetmap.josm.data.osm.history.History;
|
---|
16 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * NodeListViewer is a UI component which displays the node list of two
|
---|
20 | * version of a {@link OsmPrimitive} in a {@link History}.
|
---|
21 | *
|
---|
22 | * <ul>
|
---|
23 | * <li>on the left, it displays the node list for the version at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}</li>
|
---|
24 | * <li>on the right, it displays the node list for the version at {@link PointInTimeType#CURRENT_POINT_IN_TIME}</li>
|
---|
25 | * </ul>
|
---|
26 | * @since 1709
|
---|
27 | */
|
---|
28 | public class NodeListViewer extends HistoryViewerPanel {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Constructs a new {@code NodeListViewer}.
|
---|
32 | * @param model history browser model
|
---|
33 | */
|
---|
34 | public NodeListViewer(HistoryBrowserModel model) {
|
---|
35 | super(model);
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | protected JTable buildTable(PointInTimeType pointInTimeType) {
|
---|
40 | final DiffTableModel tableModel = model.getNodeListTableModel(pointInTimeType);
|
---|
41 | final NodeListTableColumnModel columnModel = new NodeListTableColumnModel();
|
---|
42 | final JTable table = new JTable(tableModel, columnModel);
|
---|
43 | tableModel.addTableModelListener(new ReversedChangeListener(table, columnModel, tr("The nodes of this way are in reverse order")));
|
---|
44 | table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
45 | selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
|
---|
46 | table.getTableHeader().setReorderingAllowed(false);
|
---|
47 | table.addMouseListener(new InternalPopupMenuLauncher());
|
---|
48 | table.addMouseListener(new ShowHistoryAction.DoubleClickAdapter(e -> {
|
---|
49 | int row = table.rowAtPoint(e.getPoint());
|
---|
50 | return primitiveIdAtRow(tableModel, row);
|
---|
51 | }));
|
---|
52 | enableSemanticSelectionSynchronization(table.getSelectionModel(),
|
---|
53 | tableModel, model.getNodeListTableModel(pointInTimeType.opposite()),
|
---|
54 | this::isSemanticallyEquivalent);
|
---|
55 | return table;
|
---|
56 | }
|
---|
57 |
|
---|
58 | private boolean isSemanticallyEquivalent(TwoColumnDiff.Item o1, TwoColumnDiff.Item o2) {
|
---|
59 | return o1.value != null && o1.value.equals(o2.value); //compare node IDs
|
---|
60 | }
|
---|
61 |
|
---|
62 | private static PrimitiveId primitiveIdAtRow(DiffTableModel model, int row) {
|
---|
63 | if (row < 0)
|
---|
64 | return null;
|
---|
65 | Long id = (Long) model.getValueAt(row, 0).value;
|
---|
66 | return id == null ? null : new SimplePrimitiveId(id, OsmPrimitiveType.NODE);
|
---|
67 | }
|
---|
68 |
|
---|
69 | static class InternalPopupMenuLauncher extends PopupMenuLauncher {
|
---|
70 | InternalPopupMenuLauncher() {
|
---|
71 | super(new ListPopupMenu(tr("Zoom to node"), tr("Zoom to this node in the current data layer")));
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | protected int checkTableSelection(JTable table, Point p) {
|
---|
76 | int row = super.checkTableSelection(table, p);
|
---|
77 | ((ListPopupMenu) menu).prepare(primitiveIdAtRow((DiffTableModel) table.getModel(), row));
|
---|
78 | return row;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|