001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs.properties; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005import static org.openstreetmap.josm.tools.I18n.trn; 006 007import java.util.Collection; 008import java.util.Collections; 009import java.util.function.IntFunction; 010import java.util.function.Supplier; 011 012import javax.swing.JTable; 013import javax.swing.event.PopupMenuEvent; 014import javax.swing.event.PopupMenuListener; 015 016import org.openstreetmap.josm.data.osm.Tag; 017import org.openstreetmap.josm.data.osm.Tagged; 018 019/** 020 * Copy the key and value of the selected tag(s) to clipboard. 021 * @since 13521 022 */ 023public class CopyKeyValueAction extends AbstractCopyAction implements PopupMenuListener { 024 025 /** 026 * Constructs a new {@code CopyKeyValueAction}. 027 * @param tagTable the tag table 028 * @param keyFn a function which returns the selected key for a given row index 029 * @param objectSp a supplier which returns the selected tagged object(s) 030 */ 031 public CopyKeyValueAction(JTable tagTable, IntFunction<String> keyFn, Supplier<Collection<? extends Tagged>> objectSp) { 032 super(tagTable, keyFn, objectSp); 033 setName(0); 034 putValue(SHORT_DESCRIPTION, tr("Copy the key and value of the selected tag(s) to clipboard")); 035 } 036 037 private void setName(long n) { 038 putValue(NAME, trn("Copy selected {0} Key/Value", "Copy selected {0} Keys/Values", n, n)); 039 } 040 041 @Override 042 protected Collection<String> getString(Tagged p, String key) { 043 String v = p.get(key); 044 return v == null ? null : Collections.singleton(new Tag(key, v).toString()); 045 } 046 047 @Override 048 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 049 setName(valueStream().count()); 050 } 051 052 @Override 053 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 054 // Do nothing 055 } 056 057 @Override 058 public void popupMenuCanceled(PopupMenuEvent e) { 059 // Do nothing 060 } 061}