1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.FocusEvent;
|
---|
7 | import java.awt.event.FocusListener;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.function.IntFunction;
|
---|
12 | import java.util.function.Supplier;
|
---|
13 |
|
---|
14 | import javax.swing.JPopupMenu;
|
---|
15 | import javax.swing.JTable;
|
---|
16 | import javax.swing.ListSelectionModel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
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.HelpTagAction;
|
---|
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 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 | }
|
---|