Changeset 13522 in josm


Ignore:
Timestamp:
2018-03-13T01:07:46+01:00 (6 years ago)
Author:
Don-vip
Message:

see #16085 - rework HelpAction (copy/pasted in a plugin because it lacks public API)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/HelpAction.java

    r13521 r13522  
    1010import java.net.URISyntaxException;
    1111import java.util.ArrayList;
     12import java.util.Arrays;
    1213import java.util.List;
    1314import java.util.Map;
     
    7677            String lang = LanguageInfo.getWikiLanguagePrefix();
    7778            final List<URI> uris = new ArrayList<>();
    78             int row;
    7979            if (tagTable.getSelectedRowCount() == 1) {
    80                 row = tagTable.getSelectedRow();
     80                int row = tagTable.getSelectedRow();
    8181                String key = Utils.encodeUrl(tagKeySupplier.apply(row));
    8282                Map<String, Integer> m = tagValuesSupplier.apply(row);
    8383                if (!m.isEmpty()) {
    8484                    String val = Utils.encodeUrl(m.entrySet().iterator().next().getKey());
    85 
    86                     uris.add(new URI(String.format("%s%sTag:%s=%s", base, lang, key, val)));
    87                     uris.add(new URI(String.format("%sTag:%s=%s", base, key, val)));
    88                     uris.add(new URI(String.format("%s%sKey:%s", base, lang, key)));
    89                     uris.add(new URI(String.format("%sKey:%s", base, key)));
    90                     uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
    91                     uris.add(new URI(String.format("%sMap_Features", base)));
     85                    uris.addAll(getTagURIs(base, lang, key, val));
    9286                }
    9387            } else if (membershipTable != null && membershipTable.getSelectedRowCount() == 1) {
    94                 row = membershipTable.getSelectedRow();
    95                 String type = (memberValueSupplier.apply(row)).get("type");
    96                 if (type != null) {
    97                     type = Utils.encodeUrl(type);
    98                 }
    99 
    100                 if (type != null && !type.isEmpty()) {
    101                     uris.add(new URI(String.format("%s%sRelation:%s", base, lang, type)));
    102                     uris.add(new URI(String.format("%sRelation:%s", base, type)));
    103                 }
    104 
    105                 uris.add(new URI(String.format("%s%sRelations", base, lang)));
    106                 uris.add(new URI(String.format("%sRelations", base)));
     88                int row = membershipTable.getSelectedRow();
     89                uris.addAll(getRelationURIs(base, lang, memberValueSupplier.apply(row)));
    10790            } else {
    10891                // give the generic help page, if more than one element is selected
    109                 uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
    110                 uris.add(new URI(String.format("%sMap_Features", base)));
     92                uris.addAll(getGenericURIs(base, lang));
    11193            }
    11294
     
    11799    }
    118100
    119     private void displayHelp(final List<URI> uris) {
     101    /**
     102     * Returns a list of URIs for the given key/value.
     103     * @param base OSM wiki base URL
     104     * @param lang Language prefix
     105     * @param key Key
     106     * @param val Value
     107     * @return a list of URIs for the given key/value by order of relevance
     108     * @throws URISyntaxException in case of internal error
     109     * @since 13522
     110     */
     111    public static List<URI> getTagURIs(String base, String lang, String key, String val) throws URISyntaxException {
     112        return Arrays.asList(
     113            new URI(String.format("%s%sTag:%s=%s", base, lang, key, val)),
     114            new URI(String.format("%sTag:%s=%s", base, key, val)),
     115            new URI(String.format("%s%sKey:%s", base, lang, key)),
     116            new URI(String.format("%sKey:%s", base, key)),
     117            new URI(String.format("%s%sMap_Features", base, lang)),
     118            new URI(String.format("%sMap_Features", base))
     119        );
     120    }
     121
     122    /**
     123     * Returns a list of URIs for the given relation.
     124     * @param base OSM wiki base URL
     125     * @param lang Language prefix
     126     * @param rel Relation
     127     * @return a list of URIs for the given relation by order of relevance
     128     * @throws URISyntaxException in case of internal error
     129     * @since 13522
     130     */
     131    public static List<URI> getRelationURIs(String base, String lang, Relation rel) throws URISyntaxException {
     132        List<URI> uris = new ArrayList<>();
     133        String type = rel.get("type");
     134        if (type != null) {
     135            type = Utils.encodeUrl(type);
     136        }
     137
     138        if (type != null && !type.isEmpty()) {
     139            uris.add(new URI(String.format("%s%sRelation:%s", base, lang, type)));
     140            uris.add(new URI(String.format("%sRelation:%s", base, type)));
     141        }
     142
     143        uris.add(new URI(String.format("%s%sRelations", base, lang)));
     144        uris.add(new URI(String.format("%sRelations", base)));
     145        return uris;
     146    }
     147
     148    /**
     149     * Returns a list of generic URIs (Map Features).
     150     * @param base OSM wiki base URL
     151     * @param lang Language prefix
     152     * @return a list of generic URIs (Map Features)
     153     * @throws URISyntaxException in case of internal error
     154     * @since 13522
     155     */
     156    public static List<URI> getGenericURIs(String base, String lang) throws URISyntaxException {
     157        return Arrays.asList(
     158            new URI(String.format("%s%sMap_Features", base, lang)),
     159            new URI(String.format("%sMap_Features", base))
     160        );
     161    }
     162
     163    /**
     164     * Display help by searching the forst valid URI in the given list.
     165     * @param uris list of URIs to test
     166     * @since 13522
     167     */
     168    public static void displayHelp(final List<URI> uris) {
    120169        try {
    121170            // find a page that actually exists in the wiki
Note: See TracChangeset for help on using the changeset viewer.