From 6bf560abde07e614415ecfb86018835363ceb062 Mon Sep 17 00:00:00 2001
From: Jiri Vlasak <jiri.hubacek@gmail.com>
Date: Tue, 19 Sep 2023 01:02:05 +0200
Subject: [PATCH] Include changeset in note comment if feasible

When closing a note, check for the changeset in the changeset cache. If
there is a changeset with the comment that contains a link to the note
being closed, put the link to the changeset as the default text of the
closing note comment.
---
 .../josm/gui/NoteInputDialog.java              | 11 +++++++++++
 .../josm/gui/dialogs/NotesDialog.java          | 18 ++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/org/openstreetmap/josm/gui/NoteInputDialog.java b/src/org/openstreetmap/josm/gui/NoteInputDialog.java
index 779959b856..7c292353cb 100644
--- a/src/org/openstreetmap/josm/gui/NoteInputDialog.java
+++ b/src/org/openstreetmap/josm/gui/NoteInputDialog.java
@@ -44,6 +44,17 @@ public class NoteInputDialog extends ExtendedDialog {
      * @param icon Icon to display in the action button
      */
     public void showNoteDialog(String message, Icon icon) {
+        showNoteDialog(message, icon, "");
+    }
+
+    /**
+     * Displays the dialog to the user
+     * @param message Translated message to display to the user as input prompt
+     * @param icon Icon to display in the action button
+     * @param text Default text of the note's comment
+     */
+    public void showNoteDialog(String message, Icon icon, String text) {
+        textArea.setText(text);
         textArea.setRows(6);
         textArea.setColumns(30);
         textArea.setLineWrap(true);
diff --git a/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java b/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java
index 6e0fc7e8ff..a1c1b8600c 100644
--- a/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java
+++ b/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java
@@ -40,6 +40,8 @@ import org.openstreetmap.josm.actions.mapmode.AddNoteAction;
 import org.openstreetmap.josm.data.notes.Note;
 import org.openstreetmap.josm.data.notes.Note.State;
 import org.openstreetmap.josm.data.notes.NoteComment;
+import org.openstreetmap.josm.data.osm.Changeset;
+import org.openstreetmap.josm.data.osm.ChangesetCache;
 import org.openstreetmap.josm.data.osm.NoteData;
 import org.openstreetmap.josm.data.osm.NoteData.NoteDataUpdateListener;
 import org.openstreetmap.josm.gui.MainApplication;
@@ -422,12 +424,24 @@ public class NotesDialog extends ToggleDialog implements LayerChangeListener, No
 
         @Override
         public void actionPerformed(ActionEvent e) {
+            String note_url_short = "";
+            String note_url_long = "";
+            String changeset_url = "";
+            Note note = displayList.getSelectedValue();
+            if (note != null) {
+                note_url_short = "https://www.osm.org/note/" + note.getId();
+                note_url_long = "https://www.openstreetmap.org/note/" + note.getId();
+                for (Changeset cs: ChangesetCache.getInstance().getChangesets()) {
+                    if (cs.getComment().indexOf(note_url_short) > -1 || cs.getComment().indexOf(note_url_long) > -1) {
+                        changeset_url = "https://www.osm.org/changeset/" + cs.getId();
+                    }
+                }
+            }
             NoteInputDialog dialog = new NoteInputDialog(MainApplication.getMainFrame(), tr("Close note"), tr("Close note"));
-            dialog.showNoteDialog(tr("Close note with message:"), ImageProvider.get("dialogs/notes", "note_closed"));
+            dialog.showNoteDialog(tr("Close note with message:"), ImageProvider.get("dialogs/notes", "note_closed"), changeset_url);
             if (dialog.getValue() != 1) {
                 return;
             }
-            Note note = displayList.getSelectedValue();
             if (note != null) {
                 int selectedIndex = displayList.getSelectedIndex();
                 noteData.closeNote(note, dialog.getInputText());
-- 
2.30.2

