Ignore:
Timestamp:
2020-06-11T11:32:21+02:00 (4 years ago)
Author:
simon04
Message:

see #19369 - TaginfoAction.withTaginfoUrl: simplify construction of custom instances, share Consumer objects

Location:
trunk/src/org/openstreetmap/josm/gui/dialogs/properties
Files:
2 edited

Legend:

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

    r16596 r16597  
    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(tr("Go to Taginfo"),
     194    private final TaginfoAction taginfoAction = new TaginfoAction(
    195195            tagTable, editHelper::getDataKey, editHelper::getDataValues,
    196             membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0), null);
     196            membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0));
    197197    private final TaginfoAction tagHistoryAction = taginfoAction.toTagHistoryAction();
    198198    private final Collection<TaginfoAction> taginfoNationalActions = new ArrayList<>();
     
    375375            final LatLon center = newSel.iterator().next().getBBox().getCenter();
    376376            Territories.getRegionalTaginfoUrls(center).stream()
    377                     .map(taginfo -> new TaginfoAction(tr("Go to Taginfo ({0})", taginfo.toString()),
    378                             tagTable, editHelper::getDataKey, editHelper::getDataValues,
    379                             membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0), taginfo.getUrl())
     377                    .map(taginfo -> taginfoAction.withTaginfoUrl(tr("Go to Taginfo ({0})", taginfo.toString()), taginfo.getUrl())
    380378                    ).forEach(taginfoNationalActions::add);
    381379            taginfoNationalActions.stream().map(membershipMenu::add).forEach(membershipMenuTagInfoNatItems::add);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TaginfoAction.java

    r16596 r16597  
    3535    private TaginfoAction(String name, Supplier<Tag> tagSupplier, Supplier<String> relationTypeSupplier, String taginfoUrl) {
    3636        super(name);
    37         this.tagSupplier = tagSupplier;
    38         this.relationTypeSupplier = relationTypeSupplier;
    39         this.taginfoUrl = taginfoUrl;
     37        this.tagSupplier = Objects.requireNonNull(tagSupplier);
     38        this.relationTypeSupplier = Objects.requireNonNull(relationTypeSupplier);
     39        this.taginfoUrl = withoutTrailingSlash(Objects.requireNonNull(taginfoUrl));
    4040    }
    4141
     
    4747     */
    4848    public TaginfoAction(Supplier<Tag> tagSupplier, Supplier<String> relationTypeSupplier) {
    49         super(tr("Go to Taginfo"));
     49        this(tr("Go to Taginfo"), tagSupplier, relationTypeSupplier, TAGINFO_URL_PROP.get());
    5050        new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(this, true);
    51         putValue(SHORT_DESCRIPTION, tr("Launch browser with Taginfo statistics for selected object"));
    52         this.tagSupplier = Objects.requireNonNull(tagSupplier);
    53         this.relationTypeSupplier = Objects.requireNonNull(relationTypeSupplier);
    54         this.taginfoUrl = getTaginfoUrl(null);
    5551    }
    5652
    5753    /**
    5854     * Constructs a new {@code TaginfoAction} with a given URL and optional name suffix.
    59      * @param name the action's text as displayed on the menu (if it is added to a menu)
    6055     * @param tagTable The tag table. Cannot be null
    6156     * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
     
    6358     * @param membershipTable The membership table. Can be null
    6459     * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
    65      * @param taginfoUrl Taginfo URL. Can be null
    66      * @since 15565
     60     * @since 16597
    6761     */
    68     public TaginfoAction(String name, JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
    69                          JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier, String taginfoUrl) {
    70         super(name);
    71         new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(this, true);
    72         putValue(SHORT_DESCRIPTION, tr("Launch browser with Taginfo statistics for selected object"));
    73         this.taginfoUrl = getTaginfoUrl(taginfoUrl);
     62    public TaginfoAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
     63                         JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) {
     64        this(getTagSupplier(tagTable, tagKeySupplier, tagValuesSupplier),
     65                getRelationTypeSupplier(membershipTable, memberValueSupplier));
     66    }
     67
     68    private static Supplier<Tag> getTagSupplier(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier) {
    7469        Objects.requireNonNull(tagTable);
    7570        Objects.requireNonNull(tagKeySupplier);
    7671        Objects.requireNonNull(tagValuesSupplier);
    77         this.tagSupplier = () -> {
     72        return () -> {
    7873            if (tagTable.getSelectedRowCount() == 1) {
    7974                final int row = tagTable.getSelectedRow();
     
    8580            return null;
    8681        };
    87         this.relationTypeSupplier = () -> membershipTable != null && membershipTable.getSelectedRowCount() == 1
     82    }
     83
     84    private static Supplier<String> getRelationTypeSupplier(JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) {
     85        return () -> membershipTable != null && membershipTable.getSelectedRowCount() == 1
    8886                ? memberValueSupplier.apply(membershipTable.getSelectedRow()).get("type") : null;
    8987    }
     
    10098            OpenBrowser.displayUrl(getTaginfoUrlForRelationType(type));
    10199        }
    102     }
    103 
    104     private static String getTaginfoUrl(String taginfoUrl) {
    105         if (taginfoUrl == null) {
    106             taginfoUrl = TAGINFO_URL_PROP.get();
    107         }
    108         return withoutTrailingSlash(taginfoUrl);
    109100    }
    110101
     
    136127
    137128    /**
     129     * Returns a new action which launches the Taginfo instance from the given URL
     130     * @param name the action's text as displayed on the menu (if it is added to a menu)
     131     * @param taginfoUrl Taginfo URL
     132     * @since 16597
     133     */
     134    public TaginfoAction withTaginfoUrl(String name, String taginfoUrl) {
     135        TaginfoAction action = new TaginfoAction(name, tagSupplier, relationTypeSupplier, taginfoUrl);
     136        new ImageProvider("dialogs/taginfo").getResource().attachImageIcon(action, true);
     137        return action;
     138    }
     139
     140    /**
    138141     * Returns a new action which launches https://taghistory.raifer.tech/ for the given tag
    139142     * @return a new action
     
    141144     */
    142145    public TaginfoAction toTagHistoryAction() {
    143         String url = withoutTrailingSlash(TAG_HISTORY_URL_PROP.get());
     146        String url = TAG_HISTORY_URL_PROP.get();
    144147        return new TaginfoAction(tr("Go to OSM Tag History"), tagSupplier, relationTypeSupplier, url) {
    145148            @Override
Note: See TracChangeset for help on using the changeset viewer.