Ticket #12357: CopyTagsAction.patch

File CopyTagsAction.patch, 10.0 KB (added by kolesar, 8 years ago)
  • new file src/org/openstreetmap/josm/actions/CopyTagsAction.java

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

    diff --git a/src/org/openstreetmap/josm/actions/HelpAction.java b/src/org/openstreetmap/josm/actions/HelpAction.java
    index 7874504..f936f25 100644
    a b public class HelpAction extends JosmAction {  
    3131
    3232    private HelpAction(boolean shortcut) {
    3333        super(tr("Help"), "help", null,
    34                 shortcut ? Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.DIRECT) : null,
     34                shortcut ? createShortcut() : null,
    3535                true);
    3636        setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE));
    3737    }
    3838
     39    public static Shortcut createShortcut() {
     40        return Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.DIRECT);
     41    }
     42
    3943    /**
    4044     * Constructs a new {@code HelpAction} without assigning a shortcut.
    4145     * @return a new {@code HelpAction}
  • src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    diff --git a/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java b/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java
    index 090fbd6..d855daa 100644
    a b import java.util.Collections;  
    1010import java.util.Date;
    1111import java.util.HashMap;
    1212import java.util.HashSet;
     13import java.util.LinkedList;
     14import java.util.List;
    1315import java.util.Map;
    1416import java.util.Map.Entry;
    1517import java.util.Objects;
    import java.util.concurrent.atomic.AtomicLong;  
    1820
    1921import org.openstreetmap.josm.tools.LanguageInfo;
    2022import org.openstreetmap.josm.tools.Utils;
     23import org.openstreetmap.josm.data.osm.Tag;
    2124
    2225/**
    2326* Abstract class to represent common features of the datatypes primitives.
    public abstract class AbstractPrimitive implements IPrimitive {  
    746749        return result;
    747750    }
    748751
     752    public Collection<Tag> getTags() {
     753        List<Tag> tags = new LinkedList<>();
     754        for (Entry<String, String> kv : getKeys().entrySet()) {
     755            tags.add(new Tag(kv.getKey(), kv.getValue()));
     756        }
     757        return tags;
     758    }
     759
     760    public Collection<String> getTagsAsString() {
     761        List<String> tagStrings = new LinkedList<>();
     762        for (Tag tag : getTags()) {
     763            tagStrings.add(tag.toString());
     764        }
     765        return tagStrings;
     766    }
     767
    749768    /**
    750769     * Replies true, if the map of key/value pairs of this primitive is not empty.
    751770     *
  • src/org/openstreetmap/josm/gui/MainMenu.java

    diff --git a/src/org/openstreetmap/josm/gui/MainMenu.java b/src/org/openstreetmap/josm/gui/MainMenu.java
    index 40ec6d0..aa7eadd 100644
    a b import org.openstreetmap.josm.actions.ChangesetManagerToggleAction;  
    3434import org.openstreetmap.josm.actions.CloseChangesetAction;
    3535import org.openstreetmap.josm.actions.CombineWayAction;
    3636import org.openstreetmap.josm.actions.CopyAction;
     37import org.openstreetmap.josm.actions.CopyTagsAction;
    3738import org.openstreetmap.josm.actions.CopyCoordinatesAction;
    3839import org.openstreetmap.josm.actions.CreateCircleAction;
    3940import org.openstreetmap.josm.actions.CreateMultipolygonAction;
    public class MainMenu extends JMenuBar {  
    188189    public final RedoAction redo = new RedoAction();
    189190    /** Edit / Copy */
    190191    public final CopyAction copy = new CopyAction();
     192    /** Edit / Copy Tags */
     193    public final JosmAction copyTags = new CopyTagsAction();
    191194    /** Edit / Copy Coordinates */
    192195    public final JosmAction copyCoordinates = new CopyCoordinatesAction();
    193196    /** Edit / Paste */
    public class MainMenu extends JMenuBar {  
    669672        Main.main.undoRedo.addCommandQueueListener(redo);
    670673        editMenu.addSeparator();
    671674        add(editMenu, copy);
     675        add(editMenu, copyTags, true);
    672676        add(editMenu, copyCoordinates, true);
    673677        add(editMenu, paste);
    674678        add(editMenu, pasteTags);
  • src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java b/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java
    index 0ea557b..212ba1d 100644
    a b public class LayerListDialog extends ToggleDialog {  
    285285            }
    286286        });
    287287
    288         // Show/Activate layer on Enter key press
     288        // Show/Activate layer on Space key press
    289289        InputMapUtils.addSpacebarAction(layerList, showHideLayerAction);
    290290
    291291        createLayout(layerList, true, Arrays.asList(
  • src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java b/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
    index efcf1ec..2b334a1 100644
    a b import javax.swing.table.TableModel;  
    5252import javax.swing.table.TableRowSorter;
    5353
    5454import org.openstreetmap.josm.Main;
     55import org.openstreetmap.josm.actions.CopyTagsAction;
     56import org.openstreetmap.josm.actions.HelpAction;
    5557import org.openstreetmap.josm.actions.JosmAction;
    5658import org.openstreetmap.josm.actions.relation.DownloadMembersAction;
    5759import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
    implements SelectionChangedListener, MapView.EditLayerChangeListener, DataSetLis  
    11221124
    11231125    class HelpAction extends AbstractAction {
    11241126        HelpAction() {
    1125             putValue(NAME, tr("Go to OSM wiki for tag help (F1)"));
     1127            putValue(NAME, tr("Go to OSM wiki for tag help"));
    11261128            putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help for selected object"));
    11271129            putValue(SMALL_ICON, ImageProvider.get("dialogs", "search"));
     1130            org.openstreetmap.josm.actions.HelpAction.createShortcut().setAccelerator(this);
    11281131        }
    11291132
    11301133        @Override
    implements SelectionChangedListener, MapView.EditLayerChangeListener, DataSetLis  
    13281331        CopyAllKeyValueAction() {
    13291332            putValue(NAME, tr("Copy all Keys/Values"));
    13301333            putValue(SHORT_DESCRIPTION, tr("Copy the key and value of all the tags to clipboard"));
     1334            CopyTagsAction.createShortcut().setAccelerator(this);
    13311335        }
    13321336
    13331337        @Override
    13341338        protected Collection<String> getString(OsmPrimitive p, String key) {
    1335             List<String> r = new LinkedList<>();
    1336             for (Entry<String, String> kv : p.getKeys().entrySet()) {
    1337                 r.add(new Tag(kv.getKey(), kv.getValue()).toString());
    1338             }
    1339             return r;
     1339            return p.getTagsAsString();
    13401340        }
    13411341    }
    13421342