Skip to content

Commit

Permalink
Merge pull request JabRef#11255 from HoussemNasri/fix-entry-drag-drop
Browse files Browse the repository at this point in the history
Fix drag and dropping entries not always working
  • Loading branch information
Siedlerchr authored Apr 30, 2024
2 parents 4f87740 + 9f259c9 commit 0046e98
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed crash on opening the entry editor when auto-completion is enabled. [#11188](https://github.com/JabRef/jabref/issues/11188)
- We fixed the usage of the key binding for "Clear search" (default: <kbd>Escape</kbd>). [#10764](https://github.com/JabRef/jabref/issues/10764)
- We fixed an issue where library shown as unsaved and marked (*) after accepting changes made externally to the file. [#11027](https://github.com/JabRef/jabref/issues/11027)
- We fixed an issue where drag and dropping entries from one library to another was not always working. [#11254](https://github.com/JabRef/jabref/issues/11254)
- We fixed an issue where drag and dropping entries created a shallow copy. [#11160](https://github.com/JabRef/jabref/issues/11160)

### Removed

Expand Down
78 changes: 51 additions & 27 deletions src/main/java/org/jabref/gui/frame/FrameDndHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import org.jabref.model.groups.GroupTreeNode;

import com.tobiasdiez.easybind.EasyBind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FrameDndHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(FrameDndHandler.class);

private final TabPane tabPane;
private final Supplier<Scene> scene;
private final Supplier<OpenDatabaseAction> openDatabaseAction;
Expand Down Expand Up @@ -81,45 +85,65 @@ private void onTabDragDropped(Node destinationTabNode, DragEvent tabDragEvent, T
tabDragEvent.setDropCompleted(true);
tabDragEvent.consume();
} else {
if (stateManager.getActiveDatabase().isEmpty()
|| stateManager.getActiveDatabase().get().getMetaData().getGroups().isEmpty()) {
if (stateManager.getActiveDatabase().isEmpty()) {
LOGGER.warn("Active library is empty when dropping entries");
return;
}

LibraryTab destinationLibraryTab = null;
for (Tab libraryTab : tabPane.getTabs()) {
if (libraryTab.getId().equals(destinationTabNode.getId()) &&
!tabPane.getSelectionModel().getSelectedItem().equals(libraryTab)) {
LibraryTab destinationLibraryTab = (LibraryTab) libraryTab;
if (hasGroups(dragboard)) {
List<String> groupPathToSources = getGroups(dragboard);

copyRootNode(destinationLibraryTab);

GroupTreeNode destinationLibraryGroupRoot = destinationLibraryTab
.getBibDatabaseContext()
.getMetaData()
.getGroups().get();

GroupTreeNode groupsTreeNode = stateManager.getActiveDatabase().get()
.getMetaData()
.getGroups()
.get();

for (String pathToSource : groupPathToSources) {
GroupTreeNode groupTreeNodeToCopy = groupsTreeNode
.getChildByPath(pathToSource)
.get();
copyGroupTreeNode((LibraryTab) libraryTab, destinationLibraryGroupRoot, groupTreeNodeToCopy);
}
return;
}
destinationLibraryTab.dropEntry(stateManager.getLocalDragboard().getBibEntries());
destinationLibraryTab = (LibraryTab) libraryTab;
break;
}
}

if (destinationLibraryTab == null) {
LOGGER.warn("Failed to find library tab to drop into");
return;
}

if (hasEntries(dragboard)) {
List<BibEntry> entryCopies = stateManager.getLocalDragboard().getBibEntries()
.stream().map(entry -> (BibEntry) entry.clone())
.toList();
destinationLibraryTab.dropEntry(entryCopies);
} else if (hasGroups(dragboard)) {
dropGroups(dragboard, destinationLibraryTab);
}

tabDragEvent.consume();
}
}

private void dropGroups(Dragboard dragboard, LibraryTab destinationLibraryTab) {
List<String> groupPathToSources = getGroups(dragboard);

copyRootNode(destinationLibraryTab);

GroupTreeNode destinationLibraryGroupRoot = destinationLibraryTab
.getBibDatabaseContext()
.getMetaData()
.getGroups().get();

GroupTreeNode groupsTreeNode = stateManager.getActiveDatabase().get()
.getMetaData()
.getGroups()
.get();

for (String pathToSource : groupPathToSources) {
GroupTreeNode groupTreeNodeToCopy = groupsTreeNode
.getChildByPath(pathToSource)
.get();
copyGroupTreeNode(destinationLibraryTab, destinationLibraryGroupRoot, groupTreeNodeToCopy);
}
}

private boolean hasEntries(Dragboard dragboard) {
return dragboard.hasContent(DragAndDropDataFormats.ENTRIES);
}

private void onTabDragOver(DragEvent event, DragEvent tabDragEvent, Tab dndIndicator) {
if (hasBibFiles(tabDragEvent.getDragboard()) || hasGroups(tabDragEvent.getDragboard())) {
tabDragEvent.acceptTransferModes(TransferMode.ANY);
Expand Down

0 comments on commit 0046e98

Please sign in to comment.