source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TaginfoAction.java

Last change on this file was 17035, checked in by stoecker, 4 years ago

fix #19808 - add icon for taghistory

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.util.Map;
8import java.util.Objects;
9import java.util.function.IntFunction;
10import java.util.function.Supplier;
11
12import javax.swing.AbstractAction;
13import javax.swing.JTable;
14
15import org.openstreetmap.josm.data.osm.IRelation;
16import org.openstreetmap.josm.data.osm.Tag;
17import org.openstreetmap.josm.data.preferences.StringProperty;
18import org.openstreetmap.josm.tools.ImageProvider;
19import org.openstreetmap.josm.tools.OpenBrowser;
20import org.openstreetmap.josm.tools.Utils;
21
22/**
23 * Launch browser with Taginfo statistics for selected object.
24 * @since 13521
25 */
26public class TaginfoAction extends AbstractAction {
27
28 private static final StringProperty TAGINFO_URL_PROP = new StringProperty("taginfo.url", "https://taginfo.openstreetmap.org/");
29 private static final StringProperty TAG_HISTORY_URL_PROP = new StringProperty("taghistory.url", "https://taghistory.raifer.tech/#***");
30
31 private final Supplier<Tag> tagSupplier;
32 private final Supplier<String> relationTypeSupplier;
33 protected final String taginfoUrl;
34
35 private TaginfoAction(String name, Supplier<Tag> tagSupplier, Supplier<String> relationTypeSupplier, String taginfoUrl) {
36 super(name);
37 this.tagSupplier = Objects.requireNonNull(tagSupplier);
38 this.relationTypeSupplier = Objects.requireNonNull(relationTypeSupplier);
39 this.taginfoUrl = withoutTrailingSlash(Objects.requireNonNull(taginfoUrl));
40 }
41
42 /**
43 * Constructs a new {@code TaginfoAction}.
44 * @param tagSupplier Supplies the tag for which Taginfo should be opened
45 * @param relationTypeSupplier Supplies a relation type for which Taginfo should be opened
46 * @since 16275
47 */
48 public TaginfoAction(Supplier<Tag> tagSupplier, Supplier<String> relationTypeSupplier) {
49 this(tr("Go to Taginfo"), tagSupplier, relationTypeSupplier, TAGINFO_URL_PROP.get());
50 new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(this, true);
51 }
52
53 /**
54 * Constructs a new {@code TaginfoAction} with a given URL and optional name suffix.
55 * @param tagTable The tag table. Cannot be null
56 * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
57 * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null
58 * @param membershipTable The membership table. Can be null
59 * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
60 * @since 16597
61 */
62 public TaginfoAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
63 JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) {
64 this(getTagSupplier(tagTable, tagKeySupplier, tagValuesSupplier),
65 getRelationTypeSupplier(membershipTable, memberValueSupplier));
66 }
67
68 private static Supplier<Tag> getTagSupplier(JTable tagTable, IntFunction<String> tagKeySupplier,
69 IntFunction<Map<String, Integer>> tagValuesSupplier) {
70 Objects.requireNonNull(tagTable);
71 Objects.requireNonNull(tagKeySupplier);
72 Objects.requireNonNull(tagValuesSupplier);
73 return () -> {
74 if (tagTable.getSelectedRowCount() == 1) {
75 final int row = tagTable.getSelectedRow();
76 final String key = tagKeySupplier.apply(row);
77 Map<String, Integer> values = tagValuesSupplier.apply(row);
78 String value = values.size() == 1 ? values.keySet().iterator().next() : null;
79 return new Tag(key, value);
80 }
81 return null;
82 };
83 }
84
85 private static Supplier<String> getRelationTypeSupplier(JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) {
86 return () -> membershipTable != null && membershipTable.getSelectedRowCount() == 1
87 ? memberValueSupplier.apply(membershipTable.getSelectedRow()).get("type") : null;
88 }
89
90 @Override
91 public void actionPerformed(ActionEvent e) {
92 Tag tag = tagSupplier.get();
93 if (tag != null) {
94 OpenBrowser.displayUrl(getTaginfoUrlForTag(tag));
95 return;
96 }
97 String type = relationTypeSupplier.get();
98 if (type != null) {
99 OpenBrowser.displayUrl(getTaginfoUrlForRelationType(type));
100 }
101 }
102
103 private static String withoutTrailingSlash(String url) {
104 return Utils.strip(url, "/");
105 }
106
107 /**
108 * Returns the Taginfo URL for the given tag or key (if the tag value is null)
109 * @param tag the tag
110 * @return the Taginfo URL for the given tag or key
111 * @since 16596
112 */
113 public String getTaginfoUrlForTag(Tag tag) {
114 if (tag.getValue().isEmpty()) {
115 return taginfoUrl + "/keys/" + encodeKeyValue(tag.getKey());
116 } else {
117 return taginfoUrl + "/tags/" + encodeKeyValue(tag.getKey()) + '=' + encodeKeyValue(tag.getValue());
118 }
119 }
120
121 private static String encodeKeyValue(String string) {
122 return Utils.encodeUrl(string).replaceAll("\\+", "%20");
123 }
124
125 /**
126 * Returns the Taginfo URL for the given relation type
127 * @param type the relation type
128 * @return the Taginfo URL for the given relation type
129 * @since 16596
130 */
131 public String getTaginfoUrlForRelationType(String type) {
132 return taginfoUrl + "/relations/" + type;
133 }
134
135 /**
136 * Returns a new action which launches the Taginfo instance from the given URL
137 * @param name the action's text as displayed on the menu (if it is added to a menu)
138 * @param taginfoUrl Taginfo URL
139 * @return a new action which launches the Taginfo instance from the given URL
140 * @since 16597
141 */
142 public TaginfoAction withTaginfoUrl(String name, String taginfoUrl) {
143 TaginfoAction action = new TaginfoAction(name, tagSupplier, relationTypeSupplier, taginfoUrl);
144 new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(action, true);
145 return action;
146 }
147
148 /**
149 * Returns a new action which launches https://taghistory.raifer.tech/ for the given tag
150 * @return a new action
151 * @since 16596
152 */
153 public TaginfoAction toTagHistoryAction() {
154 String url = TAG_HISTORY_URL_PROP.get();
155 TaginfoAction action = new TaginfoAction(tr("Go to OSM Tag History"), tagSupplier, relationTypeSupplier, url) {
156 @Override
157 public String getTaginfoUrlForTag(Tag tag) {
158 return String.join("/", taginfoUrl, tag.getKey(), tag.getValue());
159 }
160
161 @Override
162 public String getTaginfoUrlForRelationType(String type) {
163 return null;
164 }
165 };
166 new ImageProvider("dialogs/taghistory").getResource().attachImageIcon(action, true);
167 return action;
168 }
169}
Note: See TracBrowser for help on using the repository browser.