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.actions.RestorePropertyAction;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
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.util.TableHelper;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * TagInfoViewer is a UI component which displays the list of tags of two
|
---|
29 | * version of a {@link org.openstreetmap.josm.data.osm.OsmPrimitive} in a {@link org.openstreetmap.josm.data.osm.history.History}.
|
---|
30 | *
|
---|
31 | * <ul>
|
---|
32 | * <li>on the left, it displays the list of tags for the version at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}</li>
|
---|
33 | * <li>on the right, it displays the list of tags for the version at {@link PointInTimeType#CURRENT_POINT_IN_TIME}</li>
|
---|
34 | * </ul>
|
---|
35 | * @since 1709
|
---|
36 | */
|
---|
37 | public class TagInfoViewer extends HistoryViewerPanel {
|
---|
38 | private static final class RepaintOnFocusChange implements FocusListener {
|
---|
39 | @Override
|
---|
40 | public void focusLost(FocusEvent e) {
|
---|
41 | repaintSelected(e);
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void focusGained(FocusEvent e) {
|
---|
46 | repaintSelected(e);
|
---|
47 | }
|
---|
48 |
|
---|
49 | private static void repaintSelected(FocusEvent e) {
|
---|
50 | // we would only need the selected rows, but this is easier:
|
---|
51 | e.getComponent().repaint();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Constructs a new {@code TagInfoViewer}.
|
---|
57 | * @param model The history browsing model
|
---|
58 | */
|
---|
59 | public TagInfoViewer(HistoryBrowserModel model) {
|
---|
60 | super(model);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | protected JTable buildTable(PointInTimeType pointInTime) {
|
---|
65 | TagTableModel tagTableModel = model.getTagTableModel(pointInTime);
|
---|
66 | JTable table = new JTable(tagTableModel, new TagTableColumnModel());
|
---|
67 | TableHelper.setFont(table, getClass());
|
---|
68 | table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
69 | selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
|
---|
70 | table.getTableHeader().setReorderingAllowed(false);
|
---|
71 | table.setTransferHandler(new TagInfoTransferHandler());
|
---|
72 | table.addFocusListener(new RepaintOnFocusChange());
|
---|
73 | JPopupMenu tagMenu = new JPopupMenu();
|
---|
74 |
|
---|
75 | IntFunction<String> tagKeyFn = x -> (String) table.getValueAt(x, 0);
|
---|
76 | IntFunction<String> tagValueFn = x -> tagTableModel.getValue(tagKeyFn.apply(x));
|
---|
77 | IntFunction<Map<String, Integer>> tagValuesFn = x -> {
|
---|
78 | String value = tagValueFn.apply(x);
|
---|
79 | return value != null ? Collections.singletonMap(value, 1) : Collections.emptyMap();
|
---|
80 | };
|
---|
81 | Supplier<Collection<? extends Tagged>> objectSp = () -> Collections.singletonList(model.getPointInTime(pointInTime));
|
---|
82 | Supplier<OsmPrimitive> primitiveSupplier = () -> getPrimitiveFromDataSet(pointInTime);
|
---|
83 |
|
---|
84 | tagMenu.add(trackJosmAction(new CopyValueAction(table, tagKeyFn, objectSp)));
|
---|
85 | final CopyKeyValueAction copyKeyValueAction = new CopyKeyValueAction(table, tagKeyFn, objectSp);
|
---|
86 | tagMenu.add(trackJosmAction(copyKeyValueAction));
|
---|
87 | tagMenu.addPopupMenuListener(copyKeyValueAction);
|
---|
88 | tagMenu.add(trackJosmAction(new CopyAllKeyValueAction(table, tagKeyFn, objectSp)));
|
---|
89 | tagMenu.add(new RestorePropertyAction(tagKeyFn, tagValueFn, primitiveSupplier, table.getSelectionModel()));
|
---|
90 | tagMenu.addSeparator();
|
---|
91 | tagMenu.add(trackJosmAction(new HelpTagAction(table, tagKeyFn, tagValuesFn)));
|
---|
92 | TaginfoAction taginfoAction = new TaginfoAction(table, tagKeyFn, tagValuesFn, null, null);
|
---|
93 | tagMenu.add(trackJosmAction(taginfoAction.toTagHistoryAction()));
|
---|
94 | tagMenu.add(trackJosmAction(taginfoAction));
|
---|
95 |
|
---|
96 | table.addMouseListener(new PopupMenuLauncher(tagMenu));
|
---|
97 | return table;
|
---|
98 | }
|
---|
99 | }
|
---|