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

Last change on this file since 16155 was 15772, checked in by simon04, 4 years ago

Simplify HistoryViewerPanel.buildTable

  • 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 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.data.osm.Tagged;
17import org.openstreetmap.josm.gui.dialogs.properties.CopyAllKeyValueAction;
18import org.openstreetmap.josm.gui.dialogs.properties.CopyKeyValueAction;
19import org.openstreetmap.josm.gui.dialogs.properties.CopyValueAction;
20import org.openstreetmap.josm.gui.dialogs.properties.HelpTagAction;
21import org.openstreetmap.josm.gui.dialogs.properties.TaginfoAction;
22import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
23
24/**
25 * TagInfoViewer is a UI component which displays the list of tags of two
26 * version of a {@link org.openstreetmap.josm.data.osm.OsmPrimitive} in a {@link org.openstreetmap.josm.data.osm.history.History}.
27 *
28 * <ul>
29 * <li>on the left, it displays the list of tags for the version at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}</li>
30 * <li>on the right, it displays the list of tags for the version at {@link PointInTimeType#CURRENT_POINT_IN_TIME}</li>
31 * </ul>
32 * @since 1709
33 */
34public class TagInfoViewer extends HistoryViewerPanel {
35 private static final class RepaintOnFocusChange implements FocusListener {
36 @Override
37 public void focusLost(FocusEvent e) {
38 repaintSelected(e);
39 }
40
41 @Override
42 public void focusGained(FocusEvent e) {
43 repaintSelected(e);
44 }
45
46 private static void repaintSelected(FocusEvent e) {
47 // we would only need the selected rows, but this is easier:
48 e.getComponent().repaint();
49 }
50 }
51
52 /**
53 * Constructs a new {@code TagInfoViewer}.
54 * @param model The history browsing model
55 */
56 public TagInfoViewer(HistoryBrowserModel model) {
57 super(model);
58 }
59
60 @Override
61 protected JTable buildTable(PointInTimeType pointInTime) {
62 TagTableModel tagTableModel = model.getTagTableModel(pointInTime);
63 JTable table = new JTable(tagTableModel, new TagTableColumnModel());
64 table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
65 selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
66 table.setTransferHandler(new TagInfoTransferHandler());
67 table.addFocusListener(new RepaintOnFocusChange());
68 JPopupMenu tagMenu = new JPopupMenu();
69
70 IntFunction<String> tagKeyFn = x -> (String) table.getValueAt(x, 0);
71 IntFunction<Map<String, Integer>> tagValuesFn = x -> {
72 String key = tagTableModel.getValue((String) table.getValueAt(x, 0));
73 if (key != null) {
74 return Collections.singletonMap(key, 1);
75 }
76 return Collections.emptyMap();
77 };
78 Supplier<Collection<? extends Tagged>> objectSp = () -> Collections.singletonList(model.getPointInTime(pointInTime));
79
80 tagMenu.add(trackJosmAction(new CopyValueAction(table, tagKeyFn, objectSp)));
81 final CopyKeyValueAction copyKeyValueAction = new CopyKeyValueAction(table, tagKeyFn, objectSp);
82 tagMenu.add(trackJosmAction(copyKeyValueAction));
83 tagMenu.addPopupMenuListener(copyKeyValueAction);
84 tagMenu.add(trackJosmAction(new CopyAllKeyValueAction(table, tagKeyFn, objectSp)));
85 tagMenu.addSeparator();
86 tagMenu.add(trackJosmAction(new HelpTagAction(table, tagKeyFn, tagValuesFn)));
87 tagMenu.add(trackJosmAction(new TaginfoAction(table, tagKeyFn, tagValuesFn, null, null)));
88
89 table.addMouseListener(new PopupMenuLauncher(tagMenu));
90 return table;
91 }
92}
Note: See TracBrowser for help on using the repository browser.