source: josm/trunk/src/org/openstreetmap/josm/gui/history/NodeListViewer.java

Last change on this file was 17921, checked in by simon04, 3 years ago

fix #18697 - Revert "Simplify HistoryViewerPanel.buildTable"

This reverts commit r15772

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