001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.properties;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.Collections;
008import java.util.function.Function;
009import java.util.function.Supplier;
010
011import javax.swing.JTable;
012
013import org.openstreetmap.josm.data.osm.Tag;
014import org.openstreetmap.josm.data.osm.Tagged;
015
016/**
017 * Copy the key and value of the selected tag(s) to clipboard.
018 * @since 13521
019 */
020public class CopyKeyValueAction extends AbstractCopyAction {
021
022    /**
023     * Constructs a new {@code CopyKeyValueAction}.
024     * @param tagTable the tag table
025     * @param keyFn a function which returns the selected key for a given row index
026     * @param objectSp a supplier which returns the selected tagged object(s)
027     */
028    public CopyKeyValueAction(JTable tagTable, Function<Integer, String> keyFn, Supplier<Collection<? extends Tagged>> objectSp) {
029        super(tagTable, keyFn, objectSp);
030        putValue(NAME, tr("Copy selected Key(s)/Value(s)"));
031        putValue(SHORT_DESCRIPTION, tr("Copy the key and value of the selected tag(s) to clipboard"));
032    }
033
034    @Override
035    protected Collection<String> getString(Tagged p, String key) {
036        String v = p.get(key);
037        return v == null ? null : Collections.singleton(new Tag(key, v).toString());
038    }
039}