source: josm/trunk/src/org/openstreetmap/josm/gui/history/TagInfoViewer.java@ 17921

Last change on this file since 17921 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: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import java.awt.event.FocusEvent;
5import java.awt.event.FocusListener;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Map;
9import java.util.function.IntFunction;
10import java.util.function.Supplier;
11
12import javax.swing.JPopupMenu;
13import javax.swing.JTable;
14import javax.swing.ListSelectionModel;
15
16import org.openstreetmap.josm.actions.RestorePropertyAction;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Tagged;
19import org.openstreetmap.josm.gui.dialogs.properties.CopyAllKeyValueAction;
20import org.openstreetmap.josm.gui.dialogs.properties.CopyKeyValueAction;
21import org.openstreetmap.josm.gui.dialogs.properties.CopyValueAction;
22import org.openstreetmap.josm.gui.dialogs.properties.HelpTagAction;
23import org.openstreetmap.josm.gui.dialogs.properties.TaginfoAction;
24import org.openstreetmap.josm.gui.util.TableHelper;
25import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
26
27/**
28 * TagInfoViewer is a UI component which displays the list of tags of two
29 * version of a {@link org.openstreetmap.josm.data.osm.OsmPrimitive} in a {@link org.openstreetmap.josm.data.osm.history.History}.
30 *
31 * <ul>
32 * <li>on the left, it displays the list of tags for the version at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}</li>
33 * <li>on the right, it displays the list of tags for the version at {@link PointInTimeType#CURRENT_POINT_IN_TIME}</li>
34 * </ul>
35 * @since 1709
36 */
37public class TagInfoViewer extends HistoryViewerPanel {
38 private static final class RepaintOnFocusChange implements FocusListener {
39 @Override
40 public void focusLost(FocusEvent e) {
41 repaintSelected(e);
42 }
43
44 @Override
45 public void focusGained(FocusEvent e) {
46 repaintSelected(e);
47 }
48
49 private static void repaintSelected(FocusEvent e) {
50 // we would only need the selected rows, but this is easier:
51 e.getComponent().repaint();
52 }
53 }
54
55 /**
56 * Constructs a new {@code TagInfoViewer}.
57 * @param model The history browsing model
58 */
59 public TagInfoViewer(HistoryBrowserModel model) {
60 super(model);
61 }
62
63 @Override
64 protected JTable buildReferenceTable() {
65 return buildTable(PointInTimeType.REFERENCE_POINT_IN_TIME);
66 }
67
68 @Override
69 protected JTable buildCurrentTable() {
70 return buildTable(PointInTimeType.CURRENT_POINT_IN_TIME);
71 }
72
73 private JTable buildTable(PointInTimeType pointInTime) {
74 TagTableModel tagTableModel = model.getTagTableModel(pointInTime);
75 JTable table = new JTable(tagTableModel, new TagTableColumnModel());
76 TableHelper.setFont(table, getClass());
77 table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
78 selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
79 table.getTableHeader().setReorderingAllowed(false);
80 table.setTransferHandler(new TagInfoTransferHandler());
81 table.addFocusListener(new RepaintOnFocusChange());
82 JPopupMenu tagMenu = new JPopupMenu();
83
84 IntFunction<String> tagKeyFn = x -> (String) table.getValueAt(x, 0);
85 IntFunction<String> tagValueFn = x -> tagTableModel.getValue(tagKeyFn.apply(x));
86 IntFunction<Map<String, Integer>> tagValuesFn = x -> {
87 String value = tagValueFn.apply(x);
88 return value != null ? Collections.singletonMap(value, 1) : Collections.emptyMap();
89 };
90 Supplier<Collection<? extends Tagged>> objectSp = () -> Collections.singletonList(model.getPointInTime(pointInTime));
91 Supplier<OsmPrimitive> primitiveSupplier = () -> getPrimitiveFromDataSet(pointInTime);
92
93 tagMenu.add(trackJosmAction(new CopyValueAction(table, tagKeyFn, objectSp)));
94 final CopyKeyValueAction copyKeyValueAction = new CopyKeyValueAction(table, tagKeyFn, objectSp);
95 tagMenu.add(trackJosmAction(copyKeyValueAction));
96 tagMenu.addPopupMenuListener(copyKeyValueAction);
97 tagMenu.add(trackJosmAction(new CopyAllKeyValueAction(table, tagKeyFn, objectSp)));
98 tagMenu.add(new RestorePropertyAction(tagKeyFn, tagValueFn, primitiveSupplier, table.getSelectionModel()));
99 tagMenu.addSeparator();
100 tagMenu.add(trackJosmAction(new HelpTagAction(table, tagKeyFn, tagValuesFn)));
101 TaginfoAction taginfoAction = new TaginfoAction(table, tagKeyFn, tagValuesFn, null, null);
102 tagMenu.add(trackJosmAction(taginfoAction.toTagHistoryAction()));
103 tagMenu.add(trackJosmAction(taginfoAction));
104
105 table.addMouseListener(new PopupMenuLauncher(tagMenu));
106 return table;
107 }
108}
Note: See TracBrowser for help on using the repository browser.