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; 018import org.openstreetmap.josm.tools.ImageProvider; 019 020/** 021 * Copy the key and value of the selected tag(s) to clipboard. 022 * @since 13521 023 */ 024public class CopyKeyValueAction extends AbstractCopyAction implements PopupMenuListener { 025 026 /** 027 * Constructs a new {@code CopyKeyValueAction}. 028 * @param tagTable the tag table 029 * @param keyFn a function which returns the selected key for a given row index 030 * @param objectSp a supplier which returns the selected tagged object(s) 031 */ 032 public CopyKeyValueAction(JTable tagTable, IntFunction<String> keyFn, Supplier<Collection<? extends Tagged>> objectSp) { 033 super(tagTable, keyFn, objectSp); 034 setName(0); 035 putValue(SHORT_DESCRIPTION, tr("Copy the key and value of the selected tags to clipboard")); 036 new ImageProvider("copy").getResource().attachImageIcon(this, true); 037 } 038 039 private void setName(long n) { 040 putValue(NAME, trn("Copy selected {0} Key/Value", "Copy selected {0} Keys/Values", n, n)); 041 } 042 043 @Override 044 protected Collection<String> getString(Tagged p, String key) { 045 String v = p.get(key); 046 return v == null ? null : Collections.singleton(new Tag(key, v).toString()); 047 } 048 049 @Override 050 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 051 setName(valueStream().count()); 052 } 053 054 @Override 055 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 056 // Do nothing 057 } 058 059 @Override 060 public void popupMenuCanceled(PopupMenuEvent e) { 061 // Do nothing 062 } 063}