diff --git a/jabgui/src/main/java/org/jabref/gui/entryeditor/CommentsTab.java b/jabgui/src/main/java/org/jabref/gui/entryeditor/CommentsTab.java index c1bbee669bc..59aaf069791 100644 --- a/jabgui/src/main/java/org/jabref/gui/entryeditor/CommentsTab.java +++ b/jabgui/src/main/java/org/jabref/gui/entryeditor/CommentsTab.java @@ -1,7 +1,9 @@ package org.jabref.gui.entryeditor; +import java.io.File; import java.util.Comparator; import java.util.LinkedHashSet; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Optional; @@ -13,12 +15,15 @@ import javafx.collections.ObservableList; import javafx.geometry.VPos; import javafx.scene.control.Button; +import javafx.scene.input.Dragboard; +import javafx.scene.input.TransferMode; import javafx.scene.layout.Priority; import javafx.scene.layout.RowConstraints; import org.jabref.gui.StateManager; import org.jabref.gui.fieldeditors.FieldEditorFX; import org.jabref.gui.fieldeditors.FieldNameLabel; +import org.jabref.gui.fieldeditors.LinkedFilesEditorViewModel; import org.jabref.gui.fieldeditors.MarkdownEditor; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.preferences.GuiPreferences; @@ -33,8 +38,17 @@ import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.field.UserSpecificCommentField; +import java.io.IOException; +import java.nio.file.Path; + +import org.jabref.gui.frame.ExternalApplicationsPreferences; +import org.jabref.logic.util.io.FileUtil; +import org.jabref.model.entry.LinkedFile; + + public class CommentsTab extends FieldsEditorTab { public static final String NAME = "Comments"; + private final GuiPreferences preferences; private final String defaultOwner; private final UserSpecificCommentField userSpecificCommentField; @@ -42,6 +56,7 @@ public class CommentsTab extends FieldsEditorTab { private boolean isFieldCurrentlyVisible; private boolean shouldShowHideButton; + public CommentsTab(GuiPreferences preferences, UndoManager undoManager, UndoAction undoAction, @@ -58,6 +73,7 @@ public CommentsTab(GuiPreferences preferences, stateManager, previewPanel); this.defaultOwner = preferences.getOwnerPreferences().getDefaultOwner().toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]", "-"); + this.preferences = preferences; setText(Localization.lang("Comments")); setGraphic(IconTheme.JabRefIcons.COMMENT.getGraphicNode()); @@ -129,8 +145,56 @@ protected void setupPanel(BibDatabaseContext bibDatabaseContext, BibEntry entry, boolean isDefaultOwnerComment = field.equals(userSpecificCommentField); boolean shouldBeEnabled = isStandardBibtexComment || isDefaultOwnerComment; editor.setEditable(shouldBeEnabled); + + editor.setOnDragOver(event -> { + if (event.getDragboard().hasFiles()) { + event.acceptTransferModes(TransferMode.COPY); + } + event.consume(); + }); + + editor.setOnDragDropped(event -> { + Dragboard db = event.getDragboard(); + boolean success = false; + + if (db.hasFiles()) { + List files = db.getFiles(); + if (!files.isEmpty()) { + File file = files.get(0); + Optional fileDir = bibDatabaseContext.getFirstExistingFileDir(preferences.getFilePreferences()); + + if (fileDir.isEmpty()) { + System.err.println("No file directory found. Cannot copy file."); + } else { + Path destinationPath = fileDir.get().resolve(file.getName()); + + // We will assume the method returns 'true' on success + boolean copySuccessful = FileUtil.copyFile(file.toPath(), destinationPath, false); + + if (copySuccessful) { + List fileDirectories = bibDatabaseContext.getFileDirectories(preferences.getFilePreferences()); + ExternalApplicationsPreferences externalAppPrefs = preferences.getExternalApplicationsPreferences(); + + LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destinationPath, fileDirectories, externalAppPrefs); + + String relativePath = newLinkedFile.getLink(); + String markdownLink = "![](" + relativePath + ")"; + + editor.insertText(editor.getTextInputControl().getCaretPosition(), markdownLink); + success = true; + } else { + // The copy failed silently + System.err.println("File copy failed for an unknown reason."); + } + } + } + } + event.setDropCompleted(success); + event.consume(); + }); } + if (entryEditorPreferences.shouldShowUserCommentsFields()) { // Show "Hide" button only if user-specific comment field is empty if (!entry.hasField(userSpecificCommentField)) { diff --git a/jabgui/src/main/java/org/jabref/gui/fieldeditors/MarkdownEditor.java b/jabgui/src/main/java/org/jabref/gui/fieldeditors/MarkdownEditor.java index 7d230d3db2a..0568ebbd5b9 100644 --- a/jabgui/src/main/java/org/jabref/gui/fieldeditors/MarkdownEditor.java +++ b/jabgui/src/main/java/org/jabref/gui/fieldeditors/MarkdownEditor.java @@ -40,4 +40,12 @@ public void paste() { public void setEditable(boolean isEditable) { getTextInput().setEditable(isEditable); } + public TextInputControl getTextInputControl() { + return getTextInput(); + } + + public void insertText(int position, String text) { + getTextInput().insertText(position, text); + } + }