001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.tagging.presets.items; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.event.MouseEvent; 007import java.util.Arrays; 008import java.util.Collection; 009import java.util.Optional; 010 011import javax.swing.JPanel; 012import javax.swing.SwingUtilities; 013 014import org.openstreetmap.josm.data.osm.OsmPrimitive; 015import org.openstreetmap.josm.gui.dialogs.properties.HelpAction; 016import org.openstreetmap.josm.gui.widgets.UrlLabel; 017import org.openstreetmap.josm.spi.preferences.Config; 018import org.openstreetmap.josm.tools.GBC; 019import org.openstreetmap.josm.tools.LanguageInfo; 020 021/** 022 * Hyperlink type. 023 * @since 8863 024 */ 025public class Link extends TextItem { 026 027 /** The OSM wiki page to display. */ 028 public String wiki; // NOSONAR 029 030 /** The link to display. */ 031 public String href; // NOSONAR 032 033 /** The localized version of {@link #href}. */ 034 public String locale_href; // NOSONAR 035 036 @Override 037 public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) { 038 initializeLocaleText(tr("More information about this feature")); 039 Optional.ofNullable(buildUrlLabel()).ifPresent(label -> p.add(label, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL))); 040 return false; 041 } 042 043 protected UrlLabel buildUrlLabel() { 044 final String url = getUrl(); 045 if (wiki != null) { 046 return new UrlLabel(url, locale_text, 2) { 047 @Override 048 public void mouseClicked(MouseEvent e) { 049 if (SwingUtilities.isLeftMouseButton(e)) { 050 // Open localized page if exists 051 HelpAction.displayHelp(Arrays.asList( 052 LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI) + wiki, 053 wiki)); 054 } else { 055 super.mouseClicked(e); 056 } 057 } 058 }; 059 } else if (href != null || locale_href != null) { 060 return new UrlLabel(url, locale_text, 2); 061 } 062 return null; 063 } 064 065 /** 066 * Returns the link URL. 067 * @return the link URL 068 * @since 15423 069 */ 070 public String getUrl() { 071 if (wiki != null) { 072 return Config.getUrls().getOSMWiki() + "/wiki/" + wiki; 073 } else if (href != null || locale_href != null) { 074 return Optional.ofNullable(locale_href).orElse(href); 075 } 076 return null; 077 } 078 079 @Override 080 protected String fieldsToString() { 081 return super.fieldsToString() 082 + (wiki != null ? "wiki=" + wiki + ", " : "") 083 + (href != null ? "href=" + href + ", " : "") 084 + (locale_href != null ? "locale_href=" + locale_href + ", " : ""); 085 } 086}