| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.plugins.utilsplugin2.actions; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
| | 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 6 | |
| | 7 | import java.awt.event.ActionEvent; |
| | 8 | import java.awt.event.KeyEvent; |
| | 9 | |
| | 10 | import java.util.Collection; |
| | 11 | import java.util.Map; |
| | 12 | import java.util.Map.Entry; |
| | 13 | import java.util.Set; |
| | 14 | import java.util.TreeSet; |
| | 15 | |
| | 16 | import javax.swing.JOptionPane; |
| | 17 | |
| | 18 | import org.openstreetmap.josm.Main; |
| | 19 | import org.openstreetmap.josm.actions.JosmAction; |
| | 20 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 21 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType; |
| | 22 | import org.openstreetmap.josm.data.osm.Tag; |
| | 23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
| | 24 | import org.openstreetmap.josm.tools.Shortcut; |
| | 25 | import org.openstreetmap.josm.tools.Utils; |
| | 26 | |
| | 27 | /** |
| | 28 | * Copy OSM primitives to clipboard in order to paste them, or their tags, somewhere else. |
| | 29 | * @since 404 |
| | 30 | */ |
| | 31 | public final class CopyTagsAction extends JosmAction { |
| | 32 | |
| | 33 | /** |
| | 34 | * Constructs a new {@code CopyTagsAction}. |
| | 35 | */ |
| | 36 | public CopyTagsAction() { |
| | 37 | super(tr("Copy Tags"), "copy", |
| | 38 | tr("Copy all tags of selected objects to paste buffer."), |
| | 39 | createShortcut(), true); |
| | 40 | putValue("help", ht("/Action/CopyTags")); |
| | 41 | } |
| | 42 | |
| | 43 | public static Shortcut createShortcut() { |
| | 44 | return Shortcut.registerShortcut("system:copytags", tr("Edit: {0}", tr("Copy Tags")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE); |
| | 45 | } |
| | 46 | |
| | 47 | @Override |
| | 48 | public void actionPerformed(ActionEvent e) { |
| | 49 | if (isEmptySelection()) return; |
| | 50 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected(); |
| | 51 | copy(getEditLayer(), selection); |
| | 52 | } |
| | 53 | |
| | 54 | /** |
| | 55 | * Copies the tags of object to the clipboard. The output by this function |
| | 56 | * looks similar to: key=value\nkey=value |
| | 57 | * @param source The OSM data layer source |
| | 58 | * @param primitives The OSM primitives to copy |
| | 59 | */ |
| | 60 | public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) { |
| | 61 | Set<String> values = new TreeSet<>(); |
| | 62 | for (OsmPrimitive p : primitives) { |
| | 63 | for (Entry<String, String> kv : p.getKeys().entrySet()) { |
| | 64 | values.add(new Tag(kv.getKey(), kv.getValue()).toString()); |
| | 65 | } |
| | 66 | } |
| | 67 | if (!values.isEmpty()) Utils.copyToClipboard(Utils.join("\n", values)); |
| | 68 | } |
| | 69 | |
| | 70 | @Override |
| | 71 | protected void updateEnabledState() { |
| | 72 | if (getCurrentDataSet() == null) { |
| | 73 | setEnabled(false); |
| | 74 | } else { |
| | 75 | updateEnabledState(getCurrentDataSet().getSelected()); |
| | 76 | } |
| | 77 | } |
| | 78 | |
| | 79 | @Override |
| | 80 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
| | 81 | setEnabled(selection != null && !selection.isEmpty()); |
| | 82 | } |
| | 83 | |
| | 84 | private static boolean isEmptySelection() { |
| | 85 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected(); |
| | 86 | if (sel.isEmpty()) { |
| | 87 | JOptionPane.showMessageDialog( |
| | 88 | Main.parent, |
| | 89 | tr("Please select something to copy."), |
| | 90 | tr("Information"), |
| | 91 | JOptionPane.INFORMATION_MESSAGE |
| | 92 | ); |
| | 93 | return true; |
| | 94 | } |
| | 95 | return false; |
| | 96 | } |
| | 97 | } |