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 JTable reference;
|
---|
39 | private JTable current;
|
---|
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 buildReferenceTable() {
|
---|
67 | reference = buildTable(PointInTimeType.REFERENCE_POINT_IN_TIME);
|
---|
68 | return reference;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | protected JTable buildCurrentTable() {
|
---|
73 | current = buildTable(PointInTimeType.CURRENT_POINT_IN_TIME);
|
---|
74 | return current;
|
---|
75 | }
|
---|
76 |
|
---|
77 | private JTable buildTable(PointInTimeType pointInTime) {
|
---|
78 | TagTableModel tagTableModel = model.getTagTableModel(pointInTime);
|
---|
79 | JTable table = new JTable(tagTableModel, new TagTableColumnModel());
|
---|
80 | TableHelper.setFont(table, getClass());
|
---|
81 | table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
82 | selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
|
---|
83 | table.getTableHeader().setReorderingAllowed(false);
|
---|
84 | table.setTransferHandler(new TagInfoTransferHandler());
|
---|
85 | table.addFocusListener(new RepaintOnFocusChange());
|
---|
86 | JPopupMenu tagMenu = new JPopupMenu();
|
---|
87 |
|
---|
88 | IntFunction<String> tagKeyFn = x -> (String) table.getValueAt(x, 0);
|
---|
89 | IntFunction<String> tagValueFn = x -> tagTableModel.getValue(tagKeyFn.apply(x));
|
---|
90 | IntFunction<Map<String, Integer>> tagValuesFn = x -> {
|
---|
91 | String value = tagValueFn.apply(x);
|
---|
92 | return value != null ? Collections.singletonMap(value, 1) : Collections.emptyMap();
|
---|
93 | };
|
---|
94 | Supplier<Collection<? extends Tagged>> objectSp = () -> Collections.singletonList(model.getPointInTime(pointInTime));
|
---|
95 | Supplier<OsmPrimitive> primitiveSupplier = () -> getPrimitiveFromDataSet(pointInTime);
|
---|
96 |
|
---|
97 | tagMenu.add(trackJosmAction(new CopyValueAction(table, tagKeyFn, objectSp)));
|
---|
98 | final CopyKeyValueAction copyKeyValueAction = new CopyKeyValueAction(table, tagKeyFn, objectSp);
|
---|
99 | tagMenu.add(trackJosmAction(copyKeyValueAction));
|
---|
100 | tagMenu.addPopupMenuListener(copyKeyValueAction);
|
---|
101 | tagMenu.add(trackJosmAction(new CopyAllKeyValueAction(table, tagKeyFn, objectSp)));
|
---|
102 | tagMenu.add(new RestorePropertyAction(tagKeyFn, tagValueFn, primitiveSupplier, table.getSelectionModel()));
|
---|
103 | tagMenu.addSeparator();
|
---|
104 | tagMenu.add(trackJosmAction(new HelpTagAction(table, tagKeyFn, tagValuesFn)));
|
---|
105 | TaginfoAction taginfoAction = new TaginfoAction(table, tagKeyFn, tagValuesFn, null, null);
|
---|
106 | tagMenu.add(trackJosmAction(taginfoAction.toTagHistoryAction()));
|
---|
107 | tagMenu.add(trackJosmAction(taginfoAction));
|
---|
108 |
|
---|
109 | table.addMouseListener(new PopupMenuLauncher(tagMenu));
|
---|
110 | return table;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Use current data to adjust preferredWidth for both tables.
|
---|
115 | * @since 19013
|
---|
116 | */
|
---|
117 | public void adjustWidths() {
|
---|
118 | // We have two tables with 3 columns each. no column should get more than 1/4 of the size
|
---|
119 | int maxWidth = this.getWidth() / 4;
|
---|
120 | if (maxWidth == 0)
|
---|
121 | maxWidth = Integer.MAX_VALUE;
|
---|
122 | adjustWidths(reference, maxWidth);
|
---|
123 | adjustWidths(current, maxWidth);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private static void adjustWidths(JTable table, int maxWidth) {
|
---|
127 | for (int column = 0; column < table.getColumnCount(); column++) {
|
---|
128 | TableHelper.adjustColumnWidth(table, column, maxWidth);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|