Ticket #12398: CopyTagsAction.patch

File CopyTagsAction.patch, 5.7 KB (added by kolesar, 10 years ago)
  • utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/UtilsPlugin2.java

    diff --git a/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/UtilsPlugin2.java b/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/UtilsPlugin2.java
    index 55da97e..80f2f13 100644
    a b import org.openstreetmap.josm.plugins.Plugin;  
    1313import org.openstreetmap.josm.plugins.PluginInformation;
    1414import org.openstreetmap.josm.plugins.utilsplugin2.actions.AddIntersectionsAction;
    1515import org.openstreetmap.josm.plugins.utilsplugin2.actions.AlignWayNodesAction;
     16import org.openstreetmap.josm.plugins.utilsplugin2.actions.CopyTagsAction;
    1617import org.openstreetmap.josm.plugins.utilsplugin2.actions.ExtractPointAction;
    1718import org.openstreetmap.josm.plugins.utilsplugin2.actions.PasteRelationsAction;
    1819import org.openstreetmap.josm.plugins.utilsplugin2.actions.SplitObjectAction;
    import org.openstreetmap.josm.plugins.utilsplugin2.selection.UnselectNodesAction  
    4748
    4849public class UtilsPlugin2 extends Plugin {
    4950
    50         private static UtilsPlugin2 instance;
     51    private static UtilsPlugin2 instance;
     52
     53    JMenuItem copyTags;
    5154
    5255    JMenuItem unglueRelation;
    5356    JMenuItem symmetry;
    public class UtilsPlugin2 extends Plugin {  
    8790        super(info);
    8891        instance = this;
    8992
     93        JMenu editMenu = Main.main.menu.editMenu;
    9094        JMenu toolsMenu = Main.main.menu.moreToolsMenu;
    9195        JMenu dataMenu = Main.main.menu.dataMenu;
    9296        JMenu selectionMenu = Main.main.menu.selectionMenu;
    9397
     98        copyTags = MainMenu.addAfter(editMenu, new CopyTagsAction(), false, Main.main.menu.copy);
     99
    94100        addIntersections = MainMenu.add(toolsMenu, new AddIntersectionsAction());
    95101        splitObject = MainMenu.add(toolsMenu, new SplitObjectAction());
    96102        alignWayNodes = MainMenu.add(toolsMenu, new AlignWayNodesAction());
    public class UtilsPlugin2 extends Plugin {  
    172178    }
    173179
    174180    public static final UtilsPlugin2 getInstance() {
    175         return instance;
     181        return instance;
    176182    }
    177183}
  • new file utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/CopyTagsAction.java

    diff --git a/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/CopyTagsAction.java b/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/CopyTagsAction.java
    new file mode 100644
    index 0000000..b851308
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.plugins.utilsplugin2.actions;
     3
     4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
     5import static org.openstreetmap.josm.tools.I18n.tr;
     6
     7import java.awt.event.ActionEvent;
     8import java.awt.event.KeyEvent;
     9
     10import java.util.Collection;
     11import java.util.Map;
     12import java.util.Map.Entry;
     13import java.util.Set;
     14import java.util.TreeSet;
     15
     16import javax.swing.JOptionPane;
     17
     18import org.openstreetmap.josm.Main;
     19import org.openstreetmap.josm.actions.JosmAction;
     20import org.openstreetmap.josm.data.osm.OsmPrimitive;
     21import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     22import org.openstreetmap.josm.data.osm.Tag;
     23import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     24import org.openstreetmap.josm.tools.Shortcut;
     25import 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 */
     31public 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}