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

Last change on this file since 16604 was 16604, checked in by simon04, 4 years ago

Checkstyle

  • Property svn:eol-style set to native
File size: 6.5 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 = Utils.encodeUrl(tagKeySupplier.apply(row)).replaceAll("\\+", "%20");
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 * Opens Taginfo for the given tag or key (if the tag value is null)
109 * @param tag the tag
110 * @since 16596
111 */
112 public String getTaginfoUrlForTag(Tag tag) {
113 if (tag.getValue().isEmpty()) {
114 return taginfoUrl + "/keys/" + tag.getKey();
115 } else {
116 return taginfoUrl + "/tags/" + tag.getKey() + '=' + Utils.encodeUrl(tag.getValue()).replaceAll("\\+", "%20");
117 }
118 }
119
120 /**
121 * Opens Taginfo for the given relation type
122 * @param type the relation type
123 * @since 16596
124 */
125 public String getTaginfoUrlForRelationType(String type) {
126 return taginfoUrl + "/relations/" + type;
127 }
128
129 /**
130 * Returns a new action which launches the Taginfo instance from the given URL
131 * @param name the action's text as displayed on the menu (if it is added to a menu)
132 * @param taginfoUrl Taginfo URL
133 * @since 16597
134 */
135 public TaginfoAction withTaginfoUrl(String name, String taginfoUrl) {
136 TaginfoAction action = new TaginfoAction(name, tagSupplier, relationTypeSupplier, taginfoUrl);
137 new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(action, true);
138 return action;
139 }
140
141 /**
142 * Returns a new action which launches https://taghistory.raifer.tech/ for the given tag
143 * @return a new action
144 * @since 16596
145 */
146 public TaginfoAction toTagHistoryAction() {
147 String url = TAG_HISTORY_URL_PROP.get();
148 return new TaginfoAction(tr("Go to OSM Tag History"), tagSupplier, relationTypeSupplier, url) {
149 @Override
150 public String getTaginfoUrlForTag(Tag tag) {
151 return String.join("/", taginfoUrl, tag.getKey(), tag.getValue());
152 }
153
154 @Override
155 public String getTaginfoUrlForRelationType(String type) {
156 return null;
157 }
158 };
159 }
160}
Note: See TracBrowser for help on using the repository browser.