1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import java.awt.event.FocusEvent;
|
---|
5 | import java.awt.event.FocusListener;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.function.IntFunction;
|
---|
11 | import java.util.function.Supplier;
|
---|
12 |
|
---|
13 | import javax.swing.JPopupMenu;
|
---|
14 | import javax.swing.JTable;
|
---|
15 | import javax.swing.ListSelectionModel;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
18 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.properties.CopyAllKeyValueAction;
|
---|
20 | import org.openstreetmap.josm.gui.dialogs.properties.CopyKeyValueAction;
|
---|
21 | import org.openstreetmap.josm.gui.dialogs.properties.CopyValueAction;
|
---|
22 | import org.openstreetmap.josm.gui.dialogs.properties.HelpAction;
|
---|
23 | import org.openstreetmap.josm.gui.dialogs.properties.TaginfoAction;
|
---|
24 | import 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 | */
|
---|
36 | public 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 | IntFunction<String> tagKeyFn = x -> (String) table.getValueAt(x, 0);
|
---|
83 | IntFunction<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 | }
|
---|