Changeset 16275 in josm for trunk


Ignore:
Timestamp:
2020-04-12T16:21:16+02:00 (4 years ago)
Author:
simon04
Message:

see #19075 - Generify TaginfoAction

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
3 edited

Legend:

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

    r16189 r16275  
    192192    private final HelpAction helpTagAction = new HelpTagAction(tagTable, editHelper::getDataKey, editHelper::getDataValues);
    193193    private final HelpAction helpRelAction = new HelpMembershipAction(membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0));
    194     private final TaginfoAction taginfoAction = new TaginfoAction(tagTable, editHelper::getDataKey, editHelper::getDataValues,
    195             membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0));
     194    private final TaginfoAction taginfoAction = new TaginfoAction(tr("Go to Taginfo"),
     195            tagTable, editHelper::getDataKey, editHelper::getDataValues,
     196            membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0), null);
    196197    private final Collection<TaginfoAction> taginfoNationalActions = new ArrayList<>();
    197198    private final PasteValueAction pasteValueAction = new PasteValueAction();
     
    365366        tagMenuTagInfoNatItems.forEach(tagMenu::remove);
    366367        tagMenuTagInfoNatItems.clear();
    367         taginfoNationalActions.forEach(JosmAction::destroy);
    368368        taginfoNationalActions.clear();
    369369    }
     
    603603    @Override
    604604    public void destroy() {
    605         taginfoAction.destroy();
    606605        destroyTaginfoNationalActions();
    607606        super.destroy();
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TaginfoAction.java

    r16083 r16275  
    88import java.util.Objects;
    99import java.util.function.IntFunction;
     10import java.util.function.Supplier;
    1011
     12import javax.swing.AbstractAction;
    1113import javax.swing.JTable;
    1214
    13 import org.openstreetmap.josm.actions.JosmAction;
    1415import org.openstreetmap.josm.data.osm.IRelation;
     16import org.openstreetmap.josm.data.osm.Tag;
    1517import org.openstreetmap.josm.data.preferences.StringProperty;
     18import org.openstreetmap.josm.tools.ImageProvider;
    1619import org.openstreetmap.josm.tools.OpenBrowser;
    1720import org.openstreetmap.josm.tools.Utils;
     
    2124 * @since 13521
    2225 */
    23 public class TaginfoAction extends JosmAction {
     26public class TaginfoAction extends AbstractAction {
    2427
    2528    private static final StringProperty TAGINFO_URL_PROP = new StringProperty("taginfo.url", "https://taginfo.openstreetmap.org/");
    2629
    27     private final JTable tagTable;
    28     private final IntFunction<String> tagKeySupplier;
    29     private final IntFunction<Map<String, Integer>> tagValuesSupplier;
    30 
     30    private final Supplier<Tag> tagSupplier;
     31    private final Supplier<String> relationTypeSupplier;
    3132    private final String taginfoUrl;
    32     private final JTable membershipTable;
    33     private final IntFunction<IRelation<?>> memberValueSupplier;
    3433
    3534    /**
    3635     * Constructs a new {@code TaginfoAction}.
    37      * @param tagTable The tag table. Cannot be null
    38      * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
    39      * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null
    40      * @param membershipTable The membership table. Can be null
    41      * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
    42      * @since 13959 (signature)
     36     * @param tagSupplier Supplies the tag for which Taginfo should be opened
     37     * @param relationTypeSupplier Supplies a relation type for which Taginfo should be opened
     38     * @since 16275
    4339     */
    44     public TaginfoAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
    45             JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) {
    46         this(tr("Go to Taginfo"), tagTable, tagKeySupplier, tagValuesSupplier, membershipTable, memberValueSupplier, TAGINFO_URL_PROP.get());
     40    public TaginfoAction(Supplier<Tag> tagSupplier, Supplier<String> relationTypeSupplier) {
     41        super(tr("Go to Taginfo"));
     42        new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(this, true);
     43        putValue(SHORT_DESCRIPTION, tr("Launch browser with Taginfo statistics for selected object"));
     44        this.tagSupplier = Objects.requireNonNull(tagSupplier);
     45        this.relationTypeSupplier = Objects.requireNonNull(relationTypeSupplier);
     46        this.taginfoUrl = getTaginfoUrl(null);
    4747    }
    4848
     
    5555     * @param membershipTable The membership table. Can be null
    5656     * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
    57      * @param taginfoUrl Taginfo URL. Cannot be null
     57     * @param taginfoUrl Taginfo URL. Can be null
    5858     * @since 15565
    5959     */
    6060    public TaginfoAction(String name, JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
    6161                         JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier, String taginfoUrl) {
    62         super(name, /* ICON */ "dialogs/taginfo",
    63                 tr("Launch browser with Taginfo statistics for selected object"), null, false);
    64         this.taginfoUrl = taginfoUrl.endsWith("/") ? taginfoUrl : taginfoUrl + '/';
    65         this.tagTable = Objects.requireNonNull(tagTable);
    66         this.tagKeySupplier = Objects.requireNonNull(tagKeySupplier);
    67         this.tagValuesSupplier = Objects.requireNonNull(tagValuesSupplier);
    68         this.membershipTable = membershipTable;
    69         this.memberValueSupplier = memberValueSupplier;
     62        super(name);
     63        new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(this, true);
     64        putValue(SHORT_DESCRIPTION, tr("Launch browser with Taginfo statistics for selected object"));
     65        this.taginfoUrl = getTaginfoUrl(taginfoUrl);
     66        Objects.requireNonNull(tagTable);
     67        Objects.requireNonNull(tagKeySupplier);
     68        Objects.requireNonNull(tagValuesSupplier);
     69        this.tagSupplier = () -> {
     70            if (tagTable.getSelectedRowCount() == 1) {
     71                final int row = tagTable.getSelectedRow();
     72                final String key = Utils.encodeUrl(tagKeySupplier.apply(row)).replaceAll("\\+", "%20");
     73                Map<String, Integer> values = tagValuesSupplier.apply(row);
     74                String value = values.size() == 1 ? values.keySet().iterator().next() : null;
     75                return new Tag(key, value);
     76            }
     77            return null;
     78        };
     79        this.relationTypeSupplier = () -> membershipTable != null && membershipTable.getSelectedRowCount() == 1
     80                ? memberValueSupplier.apply(membershipTable.getSelectedRow()).get("type") : null;
    7081    }
    7182
    7283    @Override
    7384    public void actionPerformed(ActionEvent e) {
    74         final String url;
    75         if (tagTable.getSelectedRowCount() == 1) {
    76             final int row = tagTable.getSelectedRow();
    77             final String key = Utils.encodeUrl(tagKeySupplier.apply(row)).replaceAll("\\+", "%20");
    78             Map<String, Integer> values = tagValuesSupplier.apply(row);
    79             if (values.size() == 1) {
    80                 url = taginfoUrl + "tags/" + key
    81                         + '=' + Utils.encodeUrl(values.keySet().iterator().next()).replaceAll("\\+", "%20");
    82             } else {
    83                 url = taginfoUrl + "keys/" + key;
    84             }
    85         } else if (membershipTable != null && membershipTable.getSelectedRowCount() == 1) {
    86             final String type = (memberValueSupplier.apply(membershipTable.getSelectedRow())).get("type");
    87             url = taginfoUrl + "relations/" + type;
    88         } else {
     85        Tag tag = tagSupplier.get();
     86        if (tag != null) {
     87            openTaginfoForTag(tag, taginfoUrl);
    8988            return;
    9089        }
    91         OpenBrowser.displayUrl(url);
     90        String type = relationTypeSupplier.get();
     91        if (type != null) {
     92            openTaginfoForRelationType(type, taginfoUrl);
     93        }
     94    }
     95
     96    private static String getTaginfoUrl(String taginfoUrl) {
     97        if (taginfoUrl == null) {
     98            taginfoUrl = TAGINFO_URL_PROP.get();
     99        }
     100        return taginfoUrl.endsWith("/") ? taginfoUrl : taginfoUrl + '/';
     101    }
     102
     103    /**
     104     * Opens Taginfo for the given tag or key (if the tag value is null)
     105     * @param tag the tag
     106     * @param taginfoUrl Taginfo URL (may be null)
     107     * @since 16275
     108     */
     109    public static void openTaginfoForTag(Tag tag, String taginfoUrl) {
     110        taginfoUrl = getTaginfoUrl(taginfoUrl);
     111        if (tag.getValue().isEmpty()) {
     112            OpenBrowser.displayUrl(taginfoUrl + "keys/" + tag.getKey());
     113        } else {
     114            OpenBrowser.displayUrl(taginfoUrl + "tags/" + tag.getKey() + '=' + Utils.encodeUrl(tag.getValue()).replaceAll("\\+", "%20"));
     115        }
     116    }
     117
     118    /**
     119     * Opens Taginfo for the given relation type
     120     * @param type the relation type
     121     * @param taginfoUrl Taginfo URL (may be null)
     122     * @since 16275
     123     */
     124    public static void openTaginfoForRelationType(String type, String taginfoUrl) {
     125        taginfoUrl = getTaginfoUrl(taginfoUrl);
     126        OpenBrowser.displayUrl(taginfoUrl + "relations/" + type);
    92127    }
    93128}
  • trunk/src/org/openstreetmap/josm/gui/history/TagInfoViewer.java

    r15772 r16275  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.history;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.awt.event.FocusEvent;
     
    8587        tagMenu.addSeparator();
    8688        tagMenu.add(trackJosmAction(new HelpTagAction(table, tagKeyFn, tagValuesFn)));
    87         tagMenu.add(trackJosmAction(new TaginfoAction(table, tagKeyFn, tagValuesFn, null, null)));
     89        tagMenu.add(trackJosmAction(new TaginfoAction(tr("Go to Taginfo"), table, tagKeyFn, tagValuesFn, null, null, null)));
    8890
    8991        table.addMouseListener(new PopupMenuLauncher(tagMenu));
Note: See TracChangeset for help on using the changeset viewer.