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