Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix drag and dropping entries not always working #11255

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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
Loading