source: josm/trunk/src/org/openstreetmap/josm/gui/history/HistoryViewerPanel.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: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import java.awt.GridBagConstraints;
5import java.awt.Insets;
6import java.awt.event.ActionEvent;
7import java.util.function.BiPredicate;
8import java.util.stream.IntStream;
9
10import javax.swing.AbstractAction;
11import javax.swing.JPopupMenu;
12import javax.swing.JScrollPane;
13import javax.swing.JTable;
14import javax.swing.ListSelectionModel;
15
16import org.openstreetmap.josm.actions.AutoScaleAction;
17import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
18import org.openstreetmap.josm.data.osm.IPrimitive;
19import org.openstreetmap.josm.data.osm.OsmData;
20import org.openstreetmap.josm.data.osm.PrimitiveId;
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.util.AdjustmentSynchronizer;
23import org.openstreetmap.josm.tools.ImageProvider;
24
25/**
26 * Base class of {@link TagInfoViewer} and {@link RelationMemberListViewer}.
27 * @since 6207
28 */
29public abstract class HistoryViewerPanel extends HistoryBrowserPanel {
30
31 protected transient AdjustmentSynchronizer adjustmentSynchronizer;
32 protected transient SelectionSynchronizer selectionSynchronizer;
33
34 protected HistoryViewerPanel(HistoryBrowserModel model) {
35 setModel(model);
36 build();
37 }
38
39 private JScrollPane embedInScrollPane(JTable table) {
40 JScrollPane pane = new JScrollPane(table);
41 adjustmentSynchronizer.participateInSynchronizedScrolling(pane.getVerticalScrollBar());
42 return pane;
43 }
44
45 protected abstract JTable buildReferenceTable();
46
47 protected abstract JTable buildCurrentTable();
48
49 private void build() {
50 GridBagConstraints gc = new GridBagConstraints();
51
52 // ---------------------------
53 gc.gridx = 0;
54 gc.gridy = 0;
55 gc.gridwidth = 1;
56 gc.gridheight = 1;
57 gc.weightx = 0.5;
58 gc.weighty = 0.0;
59 gc.insets = new Insets(5, 5, 5, 0);
60 gc.fill = GridBagConstraints.HORIZONTAL;
61 gc.anchor = GridBagConstraints.FIRST_LINE_START;
62 referenceInfoPanel = new VersionInfoPanel(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
63 add(referenceInfoPanel, gc);
64
65 gc.gridx = 1;
66 gc.gridy = 0;
67 gc.gridwidth = 1;
68 gc.gridheight = 1;
69 gc.fill = GridBagConstraints.HORIZONTAL;
70 gc.weightx = 0.5;
71 gc.weighty = 0.0;
72 gc.anchor = GridBagConstraints.FIRST_LINE_START;
73 currentInfoPanel = new VersionInfoPanel(model, PointInTimeType.CURRENT_POINT_IN_TIME);
74 add(currentInfoPanel, gc);
75
76 adjustmentSynchronizer = new AdjustmentSynchronizer();
77 selectionSynchronizer = new SelectionSynchronizer();
78
79 // ---------------------------
80 gc.gridx = 0;
81 gc.gridy = 1;
82 gc.gridwidth = 1;
83 gc.gridheight = 1;
84 gc.weightx = 0.5;
85 gc.weighty = 1.0;
86 gc.fill = GridBagConstraints.BOTH;
87 gc.anchor = GridBagConstraints.NORTHWEST;
88 add(embedInScrollPane(buildReferenceTable()), gc);
89
90 gc.gridx = 1;
91 gc.gridy = 1;
92 gc.gridwidth = 1;
93 gc.gridheight = 1;
94 gc.weightx = 0.5;
95 gc.weighty = 1.0;
96 gc.fill = GridBagConstraints.BOTH;
97 gc.anchor = GridBagConstraints.NORTHWEST;
98 add(embedInScrollPane(buildCurrentTable()), gc);
99 }
100
101 /**
102 * Enables semantic highlighting for the {@link org.openstreetmap.josm.data.StructUtils.SerializeOptions}
103 * @param thisSelectionModel selection model
104 * @param thisModel table model (corresponding to the selection model)
105 * @param otherModel table model for the other point in time
106 * @param isSemanticallyEquivalent predicate to determine whether the items should be highlighted
107 */
108 protected void enableSemanticSelectionSynchronization(ListSelectionModel thisSelectionModel,
109 DiffTableModel thisModel, DiffTableModel otherModel,
110 BiPredicate<TwoColumnDiff.Item, TwoColumnDiff.Item> isSemanticallyEquivalent) {
111 selectionSynchronizer.setSelectionIndexMapper((selection, sourceSelectionModel) -> {
112 DiffTableModel sourceModel = sourceSelectionModel == thisSelectionModel ? thisModel : otherModel;
113 DiffTableModel destinationModel = sourceSelectionModel == thisSelectionModel ? otherModel : thisModel;
114 return IntStream.range(0, destinationModel.getRowCount())
115 .filter(i -> isSemanticallyEquivalent.test(sourceModel.getValueAt(selection, 0), destinationModel.getValueAt(i, 0)));
116 });
117 }
118
119 static class ListPopupMenu extends JPopupMenu {
120 private final ZoomToObjectAction zoomToObjectAction;
121 private final ShowHistoryAction showHistoryAction;
122
123 ListPopupMenu(String name, String shortDescription) {
124 zoomToObjectAction = new ZoomToObjectAction(name, shortDescription);
125 add(zoomToObjectAction);
126 showHistoryAction = new ShowHistoryAction();
127 add(showHistoryAction);
128 }
129
130 void prepare(PrimitiveId pid) {
131 zoomToObjectAction.setPrimitiveId(pid);
132 zoomToObjectAction.updateEnabledState();
133
134 showHistoryAction.setPrimitiveId(pid);
135 showHistoryAction.updateEnabledState();
136 }
137 }
138
139 static class ZoomToObjectAction extends AbstractAction {
140 private transient PrimitiveId primitiveId;
141
142 /**
143 * Constructs a new {@code ZoomToObjectAction}.
144 * @param name name for the action
145 * @param shortDescription The key used for storing a short <code>String</code> description for the action, used for tooltip text.
146 */
147 ZoomToObjectAction(String name, String shortDescription) {
148 putValue(NAME, name);
149 putValue(SHORT_DESCRIPTION, shortDescription);
150 new ImageProvider("dialogs", "zoomin").getResource().attachImageIcon(this, true);
151 }
152
153 @Override
154 public void actionPerformed(ActionEvent e) {
155 if (!isEnabled())
156 return;
157 IPrimitive p = getPrimitiveToZoom();
158 if (p != null && p.isSelectable()) {
159 p.getDataSet().setSelected(p);
160 AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
161 }
162 }
163
164 public void setPrimitiveId(PrimitiveId pid) {
165 this.primitiveId = pid;
166 updateEnabledState();
167 }
168
169 protected IPrimitive getPrimitiveToZoom() {
170 if (primitiveId == null)
171 return null;
172 OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData();
173 if (ds == null)
174 return null;
175 return ds.getPrimitiveById(primitiveId);
176 }
177
178 public void updateEnabledState() {
179 setEnabled(MainApplication.getLayerManager().getActiveData() != null && getPrimitiveToZoom() != null);
180 }
181 }
182}
Note: See TracBrowser for help on using the repository browser.