Changeset 16839 in josm


Ignore:
Timestamp:
2020-08-03T22:06:42+02:00 (4 years ago)
Author:
simon04
Message:

fix #19622 - Tag2Link: show icons based on presets/styles

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OpenBrowserAction.java

    r16833 r16839  
    1818import org.openstreetmap.josm.spi.preferences.Config;
    1919import org.openstreetmap.josm.tools.ImageProvider;
     20import org.openstreetmap.josm.tools.ImageResource;
    2021import org.openstreetmap.josm.tools.OpenBrowser;
    2122import org.openstreetmap.josm.tools.Utils;
     
    3738     */
    3839    public OpenBrowserAction(String name, String url) {
    39         new ImageProvider("help/internet").getResource().attachImageIcon(this, true);
     40        this(name, url, null);
     41    }
     42
     43    /**
     44     * Constructs a new {@link OpenBrowserAction}.
     45     * @param name the name of this action
     46     * @param url the URL to launch
     47     * @param icon the action icon
     48     * @since 16839
     49     */
     50    public OpenBrowserAction(String name, String url, ImageResource icon) {
     51        if (icon == null) {
     52            new ImageProvider("help/internet").getResource().attachImageIcon(this, true);
     53        } else {
     54            icon.attachImageIcon(this, true);
     55        }
    4056        this.urls.add(url);
    4157        this.originalName = name;
  • trunk/src/org/openstreetmap/josm/gui/util/AbstractTag2LinkPopupListener.java

    r15724 r16839  
    4141
    4242    protected void addLinks(JPopupMenu popup, String key, String value) {
    43         Tag2Link.getLinksForTag(key, value, (name, url) -> {
     43        Tag2Link.getLinksForTag(key, value, (name, url, icon) -> {
    4444            if (itemList.isEmpty()) {
    4545                itemList.add(popup.add(new JPopupMenu.Separator()));
     
    4949                browserActions.get(name).addUrl(url);
    5050            } else {
    51                 final OpenBrowserAction action = new OpenBrowserAction(name, url);
     51                final OpenBrowserAction action = new OpenBrowserAction(name, url, icon);
    5252                browserActions.put(name, action);
    5353                itemList.add(popup.add(action));
  • trunk/src/org/openstreetmap/josm/tools/Tag2Link.java

    r16836 r16839  
    1111import java.util.Arrays;
    1212import java.util.Collections;
     13import java.util.HashMap;
    1314import java.util.List;
    1415import java.util.Map;
     16import java.util.Optional;
     17import java.util.function.Supplier;
    1518import java.util.function.UnaryOperator;
    1619import java.util.regex.Matcher;
     
    2326import javax.json.JsonValue;
    2427
     28import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2529import org.openstreetmap.josm.data.osm.OsmUtils;
    2630import org.openstreetmap.josm.data.preferences.CachingProperty;
     
    7781         * @param name the name/label of the link
    7882         * @param url the URL of the link
     83         * @param icon the icon to use
    7984         */
    80         void acceptLink(String name, String url);
     85        void acceptLink(String name, String url, ImageResource icon);
    8186    }
    8287
     
    142147        }
    143148
     149        final HashMap<OsmPrimitiveType, Optional<ImageResource>> memoize = new HashMap<>();
     150        final Supplier<ImageResource> imageResource = () -> memoize
     151                .computeIfAbsent(OsmPrimitiveType.NODE, type -> OsmPrimitiveImageProvider.getResource(key, value, type))
     152                .orElse(null);
     153
    144154        // Search
    145155        if (key.matches("^(.+[:_])?name([:_]" + languagePattern + ")?$")) {
     156            final ImageResource search = new ImageProvider("dialogs/search").getResource();
    146157            PREF_SEARCH_ENGINES.get().forEach(url ->
    147                     linkConsumer.acceptLink(tr("Search on {0}", getHost(url, url)), url.replace("$1", Utils.encodeUrl(value))));
     158                    linkConsumer.acceptLink(tr("Search on {0}", getHost(url, url)), url.replace("$1", Utils.encodeUrl(value)), search));
    148159        }
    149160
     
    155166                : null;
    156167        if (key.matches("^(.+[:_])?website([:_].+)?$") && validURL != null) {
    157             linkConsumer.acceptLink(getLinkName(validURL, key), validURL);
     168            linkConsumer.acceptLink(getLinkName(validURL, key), validURL, imageResource.get());
    158169        }
    159170        if (key.matches("^(.+[:_])?source([:_].+)?$") && validURL != null) {
    160             linkConsumer.acceptLink(getLinkName(validURL, key), validURL);
     171            linkConsumer.acceptLink(getLinkName(validURL, key), validURL, imageResource.get());
    161172        }
    162173        if (key.matches("^(.+[:_])?url([:_].+)?$") && validURL != null) {
    163             linkConsumer.acceptLink(getLinkName(validURL, key), validURL);
     174            linkConsumer.acceptLink(getLinkName(validURL, key), validURL, imageResource.get());
    164175        }
    165176        if (key.matches("image") && validURL != null) {
    166             linkConsumer.acceptLink(tr("View image"), validURL);
     177            linkConsumer.acceptLink(tr("View image"), validURL, imageResource.get());
    167178        }
    168179
     
    173184            final String lang = Utils.firstNotEmptyString("en", keyMatcher.group("lang"), valueMatcher.group("lang"));
    174185            final String url = "https://" + lang + ".wikipedia.org/wiki/" + valueMatcher.group("article").replace(' ', '_');
    175             linkConsumer.acceptLink(tr("View Wikipedia article"), url);
     186            linkConsumer.acceptLink(tr("View Wikipedia article"), url, imageResource.get());
    176187        }
    177188        if (key.matches("(.*:)?wikidata")) {
    178189            OsmUtils.splitMultipleValues(value)
    179                     .forEach(q -> linkConsumer.acceptLink(tr("View Wikidata item"), "https://www.wikidata.org/wiki/" + q));
     190                    .forEach(q -> linkConsumer.acceptLink(tr("View Wikidata item"), "https://www.wikidata.org/wiki/" + q, imageResource.get()));
    180191        }
    181192        if (key.matches("(.*:)?species")) {
    182193            final String url = "https://species.wikimedia.org/wiki/" + value;
    183             linkConsumer.acceptLink(getLinkName(url, key), url);
     194            linkConsumer.acceptLink(getLinkName(url, key), url, imageResource.get());
    184195        }
    185196        if (key.matches("wikimedia_commons|image") && value.matches("(?i:File):.*")) {
    186             linkConsumer.acceptLink(tr("View image on Wikimedia Commons"), "https://commons.wikimedia.org/wiki/" + value);
     197            String url = "https://commons.wikimedia.org/wiki/" + value;
     198            linkConsumer.acceptLink(tr("View image on Wikimedia Commons"), url, imageResource.get());
    187199        }
    188200        if (key.matches("wikimedia_commons|image") && value.matches("(?i:Category):.*")) {
    189             linkConsumer.acceptLink(tr("View category on Wikimedia Commons"), "https://commons.wikimedia.org/wiki/" + value);
     201            String url = "https://commons.wikimedia.org/wiki/" + value;
     202            linkConsumer.acceptLink(tr("View category on Wikimedia Commons"), url, imageResource.get());
    190203        }
    191204
     
    193206            final String formattedValue = valueFormatter.getOrDefault(key, x -> x).apply(value);
    194207            final String url = urlFormatter.replace("$1", formattedValue);
    195             linkConsumer.acceptLink(getLinkName(url, key), url);
     208            linkConsumer.acceptLink(getLinkName(url, key), url, imageResource.get());
    196209        });
    197210    }
  • trunk/test/unit/org/openstreetmap/josm/tools/Tag2LinkTest.java

    r16836 r16839  
    2020    List<String> links = new ArrayList<>();
    2121
    22     void addLink(String name, String url) {
     22    void addLink(String name, String url, ImageResource icon) {
    2323        links.add(name + " // " + url);
    2424    }
Note: See TracChangeset for help on using the changeset viewer.