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

Last change on this file since 16465 was 16465, checked in by GerdP, 4 years ago

simplify code

  • Property svn:eol-style set to native
File size: 5.3 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;
7import java.awt.event.ActionEvent;
8
9import javax.swing.AbstractAction;
10import javax.swing.JPopupMenu;
11import javax.swing.JTable;
12import javax.swing.ListSelectionModel;
13
14import org.openstreetmap.josm.actions.AutoScaleAction;
15import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
16import org.openstreetmap.josm.data.osm.IPrimitive;
17import org.openstreetmap.josm.data.osm.OsmData;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
20import org.openstreetmap.josm.data.osm.PrimitiveId;
21import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
22import org.openstreetmap.josm.data.osm.history.History;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
25import org.openstreetmap.josm.tools.ImageProvider;
26
27/**
28 * NodeListViewer is a UI component which displays the node list of two
29 * version of a {@link OsmPrimitive} in a {@link History}.
30 *
31 * <ul>
32 * <li>on the left, it displays the node list for the version at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}</li>
33 * <li>on the right, it displays the node list for the version at {@link PointInTimeType#CURRENT_POINT_IN_TIME}</li>
34 * </ul>
35 * @since 1709
36 */
37public class NodeListViewer extends HistoryViewerPanel {
38
39 /**
40 * Constructs a new {@code NodeListViewer}.
41 * @param model history browser model
42 */
43 public NodeListViewer(HistoryBrowserModel model) {
44 super(model);
45 }
46
47 @Override
48 protected JTable buildTable(PointInTimeType pointInTimeType) {
49 final DiffTableModel tableModel = model.getNodeListTableModel(pointInTimeType);
50 final NodeListTableColumnModel columnModel = new NodeListTableColumnModel();
51 final JTable table = new JTable(tableModel, columnModel);
52 tableModel.addTableModelListener(new ReversedChangeListener(table, columnModel, tr("The nodes of this way are in reverse order")));
53 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
54 selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
55 table.addMouseListener(new InternalPopupMenuLauncher());
56 table.addMouseListener(new ShowHistoryAction.DoubleClickAdapter(e -> {
57 int row = table.rowAtPoint(e.getPoint());
58 return row < 0 ? null : primitiveIdAtRow(tableModel, row);
59 }));
60 return table;
61 }
62
63 static class NodeListPopupMenu extends JPopupMenu {
64 private final ZoomToNodeAction zoomToNodeAction;
65 private final ShowHistoryAction showHistoryAction;
66
67 NodeListPopupMenu() {
68 zoomToNodeAction = new ZoomToNodeAction();
69 add(zoomToNodeAction);
70 showHistoryAction = new ShowHistoryAction();
71 add(showHistoryAction);
72 }
73
74 void prepare(PrimitiveId pid) {
75 zoomToNodeAction.setPrimitiveId(pid);
76 zoomToNodeAction.updateEnabledState();
77
78 showHistoryAction.setPrimitiveId(pid);
79 showHistoryAction.updateEnabledState();
80 }
81 }
82
83 static class ZoomToNodeAction extends AbstractAction {
84 private transient PrimitiveId primitiveId;
85
86 /**
87 * Constructs a new {@code ZoomToNodeAction}.
88 */
89 ZoomToNodeAction() {
90 putValue(NAME, tr("Zoom to node"));
91 putValue(SHORT_DESCRIPTION, tr("Zoom to this node in the current data layer"));
92 new ImageProvider("dialogs", "zoomin").getResource().attachImageIcon(this, true);
93 }
94
95 @Override
96 public void actionPerformed(ActionEvent e) {
97 if (!isEnabled())
98 return;
99 IPrimitive p = getPrimitiveToZoom();
100 if (p != null && p.isSelectable()) {
101 p.getDataSet().setSelected(p);
102 AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
103 }
104 }
105
106 public void setPrimitiveId(PrimitiveId pid) {
107 this.primitiveId = pid;
108 updateEnabledState();
109 }
110
111 protected IPrimitive getPrimitiveToZoom() {
112 if (primitiveId == null)
113 return null;
114 OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData();
115 if (ds == null)
116 return null;
117 return ds.getPrimitiveById(primitiveId);
118 }
119
120 public void updateEnabledState() {
121 setEnabled(MainApplication.getLayerManager().getActiveData() != null && getPrimitiveToZoom() != null);
122 }
123 }
124
125 private static PrimitiveId primitiveIdAtRow(DiffTableModel model, int row) {
126 Long id = (Long) model.getValueAt(row, 0).value;
127 return id == null ? null : new SimplePrimitiveId(id, OsmPrimitiveType.NODE);
128 }
129
130 static class InternalPopupMenuLauncher extends PopupMenuLauncher {
131 InternalPopupMenuLauncher() {
132 super(new NodeListPopupMenu());
133 }
134
135 @Override
136 protected int checkTableSelection(JTable table, Point p) {
137 int row = super.checkTableSelection(table, p);
138 ((NodeListPopupMenu) menu).prepare(primitiveIdAtRow((DiffTableModel) table.getModel(), row));
139 return row;
140 }
141 }
142
143}
Note: See TracBrowser for help on using the repository browser.