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