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.Collection;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.function.IntFunction;
|
---|
10 | import java.util.function.Supplier;
|
---|
11 |
|
---|
12 | import javax.swing.JPopupMenu;
|
---|
13 | import javax.swing.JTable;
|
---|
14 | import javax.swing.ListSelectionModel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
17 | import org.openstreetmap.josm.gui.dialogs.properties.CopyAllKeyValueAction;
|
---|
18 | import org.openstreetmap.josm.gui.dialogs.properties.CopyKeyValueAction;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.properties.CopyValueAction;
|
---|
20 | import org.openstreetmap.josm.gui.dialogs.properties.HelpTagAction;
|
---|
21 | import org.openstreetmap.josm.gui.dialogs.properties.TaginfoAction;
|
---|
22 | import 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 | */
|
---|
34 | public 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 | }
|
---|