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.List;
010import java.util.Optional;
011
012import javax.swing.JPanel;
013import javax.swing.SwingUtilities;
014
015import org.openstreetmap.josm.data.osm.OsmPrimitive;
016import org.openstreetmap.josm.gui.dialogs.properties.HelpAction;
017import org.openstreetmap.josm.gui.widgets.UrlLabel;
018import org.openstreetmap.josm.spi.preferences.Config;
019import org.openstreetmap.josm.tools.GBC;
020import org.openstreetmap.josm.tools.LanguageInfo;
021
022/**
023 * Hyperlink type.
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        if (wiki != null) {
040            final String url = Config.getUrls().getOSMWiki() + "/wiki/" + wiki;
041            final UrlLabel label = new UrlLabel(url, locale_text, 2) {
042                @Override
043                public void mouseClicked(MouseEvent e) {
044                    if (SwingUtilities.isLeftMouseButton(e)) {
045                        // Open localized page if exists
046                        final List<String> pages = Arrays.asList(
047                                LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI) + wiki,
048                                wiki);
049                        HelpAction.displayHelp(pages);
050                    } else {
051                        super.mouseClicked(e);
052                    }
053                }
054            };
055            p.add(label, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL));
056        } else if (href != null || locale_href != null) {
057            final String url = Optional.ofNullable(locale_href).orElse(href);
058            final UrlLabel label = new UrlLabel(url, locale_text, 2);
059            p.add(label, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL));
060        }
061        return false;
062    }
063
064    @Override
065    protected String fieldsToString() {
066        return super.fieldsToString()
067                + (wiki != null ? "wiki=" + wiki + ", " : "")
068                + (href != null ? "href=" + href + ", " : "")
069                + (locale_href != null ? "locale_href=" + locale_href + ", " : "");
070    }
071}