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

Last change on this file since 13535 was 13521, checked in by Don-vip, 6 years ago

fix #16085 - add contextual menu in tag table of history dialog with following entries, similarly to properties dialog:

  • Copy Value
  • Copy selected Key(s)/Value(s)
  • Copy all Keys/Values
  • Go to OSM wiki for tag help
  • Go to Taginfo
  • Property svn:eol-style set to native
File size: 4.1 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.Arrays;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.Map;
10import java.util.function.Function;
11import java.util.function.Supplier;
12
13import javax.swing.JPopupMenu;
14import javax.swing.JTable;
15import javax.swing.ListSelectionModel;
16
17import org.openstreetmap.josm.data.osm.Tagged;
18import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
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.HelpAction;
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 buildReferenceTable() {
64 return buildTable(PointInTimeType.REFERENCE_POINT_IN_TIME, "table.referencetagtable", model::getReferencePointInTime);
65 }
66
67 @Override
68 protected JTable buildCurrentTable() {
69 return buildTable(PointInTimeType.CURRENT_POINT_IN_TIME, "table.currenttagtable", model::getCurrentPointInTime);
70 }
71
72 private JTable buildTable(PointInTimeType pointInTime, String name, Supplier<HistoryOsmPrimitive> histoSp) {
73 TagTableModel tagTableModel = model.getTagTableModel(pointInTime);
74 JTable table = new JTable(tagTableModel, new TagTableColumnModel());
75 table.setName(name);
76 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
77 selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
78 table.setTransferHandler(new TagInfoTransferHandler());
79 table.addFocusListener(new RepaintOnFocusChange());
80 JPopupMenu tagMenu = new JPopupMenu();
81
82 Function<Integer, String> tagKeyFn = x -> (String) table.getValueAt(x, 0);
83 Function<Integer, Map<String, Integer>> tagValuesFn = x -> {
84 Map<String, Integer> map = new HashMap<>();
85 String key = tagTableModel.getValue((String) table.getValueAt(x, 0));
86 if (key != null) {
87 map.put(key, 1);
88 }
89 return map;
90 };
91 Supplier<Collection<? extends Tagged>> objectSp = () -> Arrays.asList(histoSp.get());
92
93 tagMenu.add(new CopyValueAction(table, tagKeyFn, objectSp));
94 tagMenu.add(new CopyKeyValueAction(table, tagKeyFn, objectSp));
95 tagMenu.add(new CopyAllKeyValueAction(table, tagKeyFn, objectSp));
96 tagMenu.addSeparator();
97 tagMenu.add(new HelpAction(table, tagKeyFn, tagValuesFn, null, null));
98 tagMenu.add(new TaginfoAction(table, tagKeyFn, tagValuesFn, null, null));
99
100 table.addMouseListener(new PopupMenuLauncher(tagMenu));
101 return table;
102 }
103}
Note: See TracBrowser for help on using the repository browser.