001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs.properties; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.event.ActionEvent; 007import java.util.Map; 008import java.util.Objects; 009import java.util.function.IntFunction; 010 011import javax.swing.JTable; 012 013import org.openstreetmap.josm.actions.JosmAction; 014import org.openstreetmap.josm.data.osm.IRelation; 015import org.openstreetmap.josm.data.preferences.StringProperty; 016import org.openstreetmap.josm.tools.OpenBrowser; 017import org.openstreetmap.josm.tools.Utils; 018 019/** 020 * Launch browser with Taginfo statistics for selected object. 021 * @since 13521 022 */ 023public class TaginfoAction extends JosmAction { 024 025 final transient StringProperty TAGINFO_URL_PROP = new StringProperty("taginfo.url", "https://taginfo.openstreetmap.org/"); 026 027 private final JTable tagTable; 028 private final IntFunction<String> tagKeySupplier; 029 private final IntFunction<Map<String, Integer>> tagValuesSupplier; 030 031 private final JTable membershipTable; 032 private final IntFunction<IRelation<?>> memberValueSupplier; 033 034 /** 035 * Constructs a new {@code TaginfoAction}. 036 * @param tagTable The tag table. Cannot be null 037 * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null 038 * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null 039 * @param membershipTable The membership table. Can be null 040 * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null 041 * @since 13959 (signature) 042 */ 043 public TaginfoAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier, 044 JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) { 045 super(tr("Go to Taginfo"), "dialogs/taginfo", tr("Launch browser with Taginfo statistics for selected object"), null, false); 046 this.tagTable = Objects.requireNonNull(tagTable); 047 this.tagKeySupplier = Objects.requireNonNull(tagKeySupplier); 048 this.tagValuesSupplier = Objects.requireNonNull(tagValuesSupplier); 049 this.membershipTable = membershipTable; 050 this.memberValueSupplier = memberValueSupplier; 051 } 052 053 @Override 054 public void actionPerformed(ActionEvent e) { 055 final String url; 056 if (tagTable.getSelectedRowCount() == 1) { 057 final int row = tagTable.getSelectedRow(); 058 final String key = Utils.encodeUrl(tagKeySupplier.apply(row)).replaceAll("\\+", "%20"); 059 Map<String, Integer> values = tagValuesSupplier.apply(row); 060 if (values.size() == 1) { 061 url = TAGINFO_URL_PROP.get() + "tags/" + key 062 + '=' + Utils.encodeUrl(values.keySet().iterator().next()).replaceAll("\\+", "%20"); 063 } else { 064 url = TAGINFO_URL_PROP.get() + "keys/" + key; 065 } 066 } else if (membershipTable != null && membershipTable.getSelectedRowCount() == 1) { 067 final String type = (memberValueSupplier.apply(membershipTable.getSelectedRow())).get("type"); 068 url = TAGINFO_URL_PROP.get() + "relations/" + type; 069 } else { 070 return; 071 } 072 OpenBrowser.displayUrl(url); 073 } 074}