From 64f733610d4b28ef9133805acc01fa8f93932c7f Mon Sep 17 00:00:00 2001
From: Brian Hatchl <brian.hatchl@radiantsolutions.com>
Date: Wed, 13 Jun 2018 17:38:37 -0400
Subject: [PATCH] Squashed commit of the following:

commit f52b00c1ceeed2070570b7895548a256292acb5a
Author: Brian Hatchl <brian.hatchl@radiantsolutions.com>
Date:   Wed Jun 13 17:13:41 2018 -0400

    need to always include selected

    so we don't miss features that don't belong to relations
    in the selection

commit 115a3f7c1e267eec25dce3e0329394eda9dda640
Author: Brian Hatchl <brian.hatchl@radiantsolutions.com>
Date:   Tue Jun 12 13:21:27 2018 -0400

    make the relations selected to give a visual cue to what is copied

commit e5218b2c7df76cad2aa3424eed97769984be2273
Author: Brian Hatchl <brian.hatchl@radiantsolutions.com>
Date:   Tue Jun 12 11:50:36 2018 -0400

    Rename class and change shortcut to Ctrl+K so avoid conflict

    with opening Conflict Dialog.

commit 9e92930179246ad0b3971784e533650200b5d84a
Author: Brian Hatchl <brian.hatchl@radiantsolutions.com>
Date:   Mon Jun 11 15:07:01 2018 -0400

    My suggested edits

commit bdd2cb52a3dc5cc79caeac0ba69c73ec67f8d8df
Author: montrosemotel@hotmail.com <montrosemotel@hotmail.com>
Date:   Wed May 30 11:35:40 2018 -0600

    Fixes to 321.
---
 .../josm/plugins/utilsplugin2/UtilsPlugin2.java    |  4 +
 .../actions/CopyWithRelationsAction.java           | 86 ++++++++++++++++++++++
 2 files changed, 90 insertions(+)
 create mode 100644 utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/CopyWithRelationsAction.java

diff --git a/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/UtilsPlugin2.java b/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/UtilsPlugin2.java
index 7f2a4df..48ca905 100644
--- a/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/UtilsPlugin2.java
+++ b/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/UtilsPlugin2.java
@@ -14,6 +14,7 @@ import org.openstreetmap.josm.plugins.PluginInformation;
 import org.openstreetmap.josm.plugins.utilsplugin2.actions.AddIntersectionsAction;
 import org.openstreetmap.josm.plugins.utilsplugin2.actions.AlignWayNodesAction;
 import org.openstreetmap.josm.plugins.utilsplugin2.actions.CopyTagsAction;
+import org.openstreetmap.josm.plugins.utilsplugin2.actions.CopyWithRelationsAction;
 import org.openstreetmap.josm.plugins.utilsplugin2.actions.ExtractPointAction;
 import org.openstreetmap.josm.plugins.utilsplugin2.actions.PasteRelationsAction;
 import org.openstreetmap.josm.plugins.utilsplugin2.actions.SplitObjectAction;
@@ -84,6 +85,8 @@ public class UtilsPlugin2 extends Plugin {
     JMenuItem selectHighway;
     JMenuItem selectAreaBoundary;
 
+    JMenuItem copyWithRelations;
+
     JMenuItem selectURL;
 
     JMenuItem drawArc;
@@ -115,6 +118,7 @@ public class UtilsPlugin2 extends Plugin {
         wiki = MainMenu.add(dataMenu, new OpenPageAction());
         latlon = MainMenu.add(toolsMenu, new LatLonAction());
         drawArc = MainMenu.add(toolsMenu, new CurveAction());
+        copyWithRelations = MainMenu.add(toolsMenu, new CopyWithRelationsAction());
 
         selectionMenu.addSeparator();
 
diff --git a/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/CopyWithRelationsAction.java b/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/CopyWithRelationsAction.java
new file mode 100644
index 0000000..24dfac9
--- /dev/null
+++ b/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/CopyWithRelationsAction.java
@@ -0,0 +1,86 @@
+// License: GPL. For details, see LICENSE file.
+// Author: Nick Tobin
+package org.openstreetmap.josm.plugins.utilsplugin2.actions;
+
+import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.swing.JOptionPane;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.actions.JosmAction;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
+import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable;
+import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
+import org.openstreetmap.josm.gui.layer.OsmDataLayer;
+import org.openstreetmap.josm.tools.Shortcut;
+
+/**
+ * Copy OSM primitives and any parent relations and members to clipboard in order to paste them, or their tags, somewhere else.
+ */
+public class CopyWithRelationsAction extends JosmAction {
+
+	private static final String label = "Copy with relations";
+    /**
+     * Constructs a new {@code CopyWithRelationsAction}.
+     */
+    public CopyWithRelationsAction() {
+        super(tr(label), "copy",
+                tr("Select and copy parent relations of selected objects to paste buffer."),
+                Shortcut.registerShortcut("system:copywithrelations", tr("Edit: {0}", tr(label)), KeyEvent.VK_K, Shortcut.CTRL),
+                true, CopyWithRelationsAction.class.getName(), true);
+        putValue("help", ht("/Action/CopyWithRelations"));
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        DataSet set = getLayerManager().getEditDataSet();
+        Collection<OsmPrimitive> selection = set.getSelected();
+        Collection<Relation> relations = OsmPrimitive.getParentRelations(selection);
+        Collection<OsmPrimitive> copies = new ArrayList<>();
+        copies.addAll(selection); //include selection so something is copied if no relations
+        if (!relations.isEmpty()) {
+            copies.addAll(relations);
+            set.setSelected(copies);
+        }
+        copy(getLayerManager().getEditLayer(), copies);
+    }
+
+    /**
+     * Copies the given primitive ids to the clipboard. The output by this function
+     * looks similar to: node 1089302677,node 1089303458,way 93793372
+     * @param source The OSM data layer source
+     * @param primitives The OSM primitives to copy
+     */
+    public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
+        // copy ids to the clipboard
+        ClipboardUtils.copy(new PrimitiveTransferable(PrimitiveTransferData.getDataWithReferences(primitives), source));
+    }
+
+    @Override
+    protected void updateEnabledState() {
+        updateEnabledStateOnCurrentSelection();
+    }
+
+    @Override
+    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
+        setEnabled(selection != null && !selection.isEmpty());
+    }
+
+    protected void showEmptySelectionWarning() {
+        JOptionPane.showMessageDialog(
+                Main.parent,
+                tr("Please select something to copy."),
+                tr("Information"),
+                JOptionPane.INFORMATION_MESSAGE
+        );
+    }
+}
\ No newline at end of file
-- 
2.7.4

