1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.Objects;
|
---|
9 | import java.util.function.IntFunction;
|
---|
10 |
|
---|
11 | import javax.swing.JTable;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
14 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
15 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
16 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Launch browser with Taginfo statistics for selected object.
|
---|
21 | * @since 13521
|
---|
22 | */
|
---|
23 | public class TaginfoAction extends JosmAction {
|
---|
24 |
|
---|
25 | private static final StringProperty TAGINFO_URL_PROP = new StringProperty("taginfo.url", "https://taginfo.openstreetmap.org/");
|
---|
26 |
|
---|
27 | private final JTable tagTable;
|
---|
28 | private final IntFunction<String> tagKeySupplier;
|
---|
29 | private final IntFunction<Map<String, Integer>> tagValuesSupplier;
|
---|
30 |
|
---|
31 | private final String taginfoUrl;
|
---|
32 | private final JTable membershipTable;
|
---|
33 | private final IntFunction<IRelation<?>> memberValueSupplier;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructs a new {@code TaginfoAction}.
|
---|
37 | * @param tagTable The tag table. Cannot be null
|
---|
38 | * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
|
---|
39 | * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null
|
---|
40 | * @param membershipTable The membership table. Can be null
|
---|
41 | * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
|
---|
42 | * @since 13959 (signature)
|
---|
43 | */
|
---|
44 | public TaginfoAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
|
---|
45 | JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) {
|
---|
46 | this(tagTable, tagKeySupplier, tagValuesSupplier, membershipTable, memberValueSupplier, TAGINFO_URL_PROP.get(), null);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Constructs a new {@code TaginfoAction} with a given URL and optional name suffix.
|
---|
51 | * @param tagTable The tag table. Cannot be null
|
---|
52 | * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
|
---|
53 | * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null
|
---|
54 | * @param membershipTable The membership table. Can be null
|
---|
55 | * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
|
---|
56 | * @param taginfoUrl Taginfo URL. Cannot be null
|
---|
57 | * @param suffix Optional name suffix, can be null
|
---|
58 | * @since 15565
|
---|
59 | */
|
---|
60 | public TaginfoAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
|
---|
61 | JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier, String taginfoUrl, String suffix) {
|
---|
62 | super(tr("Go to Taginfo") + (suffix != null ? " " + suffix : ""), "dialogs/taginfo",
|
---|
63 | tr("Launch browser with Taginfo statistics for selected object"), null, false);
|
---|
64 | this.taginfoUrl = taginfoUrl.endsWith("/") ? taginfoUrl : taginfoUrl + '/';
|
---|
65 | this.tagTable = Objects.requireNonNull(tagTable);
|
---|
66 | this.tagKeySupplier = Objects.requireNonNull(tagKeySupplier);
|
---|
67 | this.tagValuesSupplier = Objects.requireNonNull(tagValuesSupplier);
|
---|
68 | this.membershipTable = membershipTable;
|
---|
69 | this.memberValueSupplier = memberValueSupplier;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public void actionPerformed(ActionEvent e) {
|
---|
74 | final String url;
|
---|
75 | if (tagTable.getSelectedRowCount() == 1) {
|
---|
76 | final int row = tagTable.getSelectedRow();
|
---|
77 | final String key = Utils.encodeUrl(tagKeySupplier.apply(row)).replaceAll("\\+", "%20");
|
---|
78 | Map<String, Integer> values = tagValuesSupplier.apply(row);
|
---|
79 | if (values.size() == 1) {
|
---|
80 | url = taginfoUrl + "tags/" + key
|
---|
81 | + '=' + Utils.encodeUrl(values.keySet().iterator().next()).replaceAll("\\+", "%20");
|
---|
82 | } else {
|
---|
83 | url = taginfoUrl + "keys/" + key;
|
---|
84 | }
|
---|
85 | } else if (membershipTable != null && membershipTable.getSelectedRowCount() == 1) {
|
---|
86 | final String type = (memberValueSupplier.apply(membershipTable.getSelectedRow())).get("type");
|
---|
87 | url = taginfoUrl + "relations/" + type;
|
---|
88 | } else {
|
---|
89 | return;
|
---|
90 | }
|
---|
91 | OpenBrowser.displayUrl(url);
|
---|
92 | }
|
---|
93 | }
|
---|