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

Last change on this file since 16457 was 16275, checked in by simon04, 4 years ago

see #19075 - Generify TaginfoAction

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