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

Enable groups drag'n'drop to new library #9460

Merged
Merged
Show file tree
Hide file tree
Changes from 17 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
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/gui/DragAndDropHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ public static List<Path> getBibFiles(Dragboard dragboard) {
return dragboard.getFiles().stream().map(File::toPath).filter(FileUtil::isBibFile).collect(Collectors.toList());
}
}

public static boolean hasGroups(Dragboard dragboard) {
return !getGroups(dragboard).isEmpty();
}

public static List<String> getGroups(Dragboard dragboard) {
if (!dragboard.hasContent(DragAndDropDataFormats.GROUP)) {
return Collections.emptyList();
} else {
return (List<String>) dragboard.getContent(DragAndDropDataFormats.GROUP);
}
}
}
123 changes: 89 additions & 34 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TimerTask;
Expand Down Expand Up @@ -38,6 +39,7 @@
import javafx.scene.control.ToolBar;
import javafx.scene.control.Tooltip;
import javafx.scene.control.skin.TabPaneSkin;
import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
Expand Down Expand Up @@ -137,6 +139,7 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.TelemetryPreferences;

Expand Down Expand Up @@ -203,6 +206,7 @@ public JabRefFrame(Stage mainStage) {
});
}

@SuppressWarnings("checkstyle:SingleSpaceSeparator")
private void initDragAndDrop() {
Tab dndIndicator = new Tab(Localization.lang("Open files..."), null);
dndIndicator.getStyleClass().add("drop");
Expand Down Expand Up @@ -232,9 +236,9 @@ private void initDragAndDrop() {
this.getScene().setOnDragEntered(event -> {
// It is necessary to setOnDragOver for newly opened tabs
// drag'n'drop on tabs covered dnd on tabbedPane, so dnd on tabs should contain all dnds on tabbedPane
tabbedPane.lookupAll(".tab").forEach(tab -> {
tab.setOnDragOver(tabDragEvent -> {
if (DragAndDropHelper.hasBibFiles(tabDragEvent.getDragboard())) {
tabbedPane.lookupAll(".tab").forEach(destinationTabNode -> {
destinationTabNode.setOnDragOver(tabDragEvent -> {
if (DragAndDropHelper.hasBibFiles(tabDragEvent.getDragboard()) || DragAndDropHelper.hasGroups(tabDragEvent.getDragboard())) {
tabDragEvent.acceptTransferModes(TransferMode.ANY);
if (!tabbedPane.getTabs().contains(dndIndicator)) {
tabbedPane.getTabs().add(dndIndicator);
Expand All @@ -249,8 +253,10 @@ private void initDragAndDrop() {
tabDragEvent.consume();
}
});
tab.setOnDragExited(event1 -> tabbedPane.getTabs().remove(dndIndicator));
tab.setOnDragDropped(tabDragEvent -> {
destinationTabNode.setOnDragExited(event1 -> tabbedPane.getTabs().remove(dndIndicator));
destinationTabNode.setOnDragDropped(tabDragEvent -> {
Dragboard dragboard = tabDragEvent.getDragboard();

if (DragAndDropHelper.hasBibFiles(tabDragEvent.getDragboard())) {
tabbedPane.getTabs().remove(dndIndicator);
List<Path> bibFiles = DragAndDropHelper.getBibFiles(tabDragEvent.getDragboard());
Expand All @@ -260,9 +266,35 @@ private void initDragAndDrop() {
tabDragEvent.consume();
} else {
for (Tab libraryTab : tabbedPane.getTabs()) {
if (libraryTab.getId().equals(tab.getId()) &&
if (libraryTab.getId().equals(destinationTabNode.getId()) &&
!tabbedPane.getSelectionModel().getSelectedItem().equals(libraryTab)) {
((LibraryTab) libraryTab).dropEntry(stateManager.getLocalDragboard().getBibEntries());
LibraryTab destinationLibraryTab = (LibraryTab) libraryTab;
if (DragAndDropHelper.hasGroups(tabDragEvent.getDragboard())) {
List<String> groupPathToSources = DragAndDropHelper.getGroups(tabDragEvent.getDragboard());

copyRootNode(destinationLibraryTab);

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

for (String pathToSource : groupPathToSources) {
GroupTreeNode groupTreeNodeToCopy = getCurrentLibraryTab()
.getBibDatabaseContext()
.getMetaData()
.getGroups()
.get()
.getChildByPath(pathToSource)
.get();

// call the copy function
copyGroupTreeNode((LibraryTab) libraryTab, destinationLibraryGroupRoot, groupTreeNodeToCopy);
}

return;
}
destinationLibraryTab.dropEntry(stateManager.getLocalDragboard().getBibEntries());
}
}
tabDragEvent.consume();
Expand Down Expand Up @@ -1128,10 +1160,13 @@ public void addTab(LibraryTab libraryTab, boolean raisePanel) {
libraryTab.getUndoManager().registerListener(new UndoRedoEventManager());
}

/**
* Opens a new tab with existing data.
* Asynchronous loading is done at {@link #createLibraryTab(BackgroundTask, Path, PreferencesService, StateManager, JabRefFrame, ThemeManager)}.
*/
private void trackOpenNewDatabase(LibraryTab libraryTab) {
Globals.getTelemetryClient().ifPresent(client -> client.trackEvent(
"OpenNewDatabase",
Map.of(),
Map.of("NumberOfEntries", (double) libraryTab.getBibDatabaseContext().getDatabase().getEntryCount())));
}

public LibraryTab addTab(BibDatabaseContext databaseContext, boolean raisePanel) {
Objects.requireNonNull(databaseContext);

Expand Down Expand Up @@ -1160,10 +1195,9 @@ public FileHistoryMenu getFileHistory() {
}

/**
* Ask if the user really wants to close the given database.
* Offers to save or discard the changes -- or return to the library
* Ask if the user really wants to close the given database
*
* @return <code>true</code> if the user choose to close the database
* @return true if the user choose to close the database
*/
private boolean confirmClose(LibraryTab libraryTab) {
String filename = libraryTab.getBibDatabaseContext()
Expand All @@ -1174,24 +1208,15 @@ private boolean confirmClose(LibraryTab libraryTab) {

ButtonType saveChanges = new ButtonType(Localization.lang("Save changes"), ButtonBar.ButtonData.YES);
ButtonType discardChanges = new ButtonType(Localization.lang("Discard changes"), ButtonBar.ButtonData.NO);
ButtonType returnToLibrary = new ButtonType(Localization.lang("Return to library"), ButtonBar.ButtonData.CANCEL_CLOSE);
ButtonType cancel = new ButtonType(Localization.lang("Return to library"), ButtonBar.ButtonData.CANCEL_CLOSE);

Optional<ButtonType> response = dialogService.showCustomButtonDialogAndWait(Alert.AlertType.CONFIRMATION,
Localization.lang("Save before closing"),
Localization.lang("Library '%0' has changed.", filename),
saveChanges, discardChanges, returnToLibrary);

if (response.isEmpty()) {
return true;
}

ButtonType buttonType = response.get();

if (buttonType.equals(returnToLibrary)) {
return false;
}
saveChanges, discardChanges, cancel);

if (buttonType.equals(saveChanges)) {
if (response.isPresent() && response.get().equals(saveChanges)) {
// The user wants to save.
try {
SaveDatabaseAction saveAction = new SaveDatabaseAction(libraryTab, prefs, Globals.entryTypesManager);
if (saveAction.save()) {
Expand All @@ -1206,13 +1231,7 @@ private boolean confirmClose(LibraryTab libraryTab) {
// Save was cancelled or an error occurred.
return false;
}

if (buttonType.equals(discardChanges)) {
BackupManager.discardBackup(libraryTab.getBibDatabaseContext());
return true;
}

return false;
return response.isEmpty() || !response.get().equals(cancel);
}

/**
Expand Down Expand Up @@ -1422,4 +1441,40 @@ private void updateTexts(UndoChangeEvent event) {
*/
}
}

private void copyGroupTreeNode(LibraryTab destinationLibraryTab, GroupTreeNode parent, GroupTreeNode groupTreeNodeToCopy) {
List<BibEntry> allEntries = getCurrentLibraryTab()
.getBibDatabaseContext()
.getEntries();
// add groupTreeNodeToCopy to the parent-- in the first run that will the source/main GroupTreeNode
GroupTreeNode copiedNode = parent.addSubgroup(groupTreeNodeToCopy.copyNode().getGroup());
// add all entries of a groupTreeNode to the new library.
destinationLibraryTab.dropEntry(groupTreeNodeToCopy.getEntriesInGroup(allEntries));
// List of all children of groupTreeNodeToCopy
List<GroupTreeNode> children = groupTreeNodeToCopy.getChildren();

if (!children.isEmpty()) {
// use recursion to add all subgroups of the original groupTreeNodeToCopy
for (GroupTreeNode child : children) {
copyGroupTreeNode(destinationLibraryTab, copiedNode, child);
}
}
}

private void copyRootNode(LibraryTab destinationLibraryTab) {
if (!destinationLibraryTab.getBibDatabaseContext().getMetaData().getGroups().isEmpty()) {
return;
}
// a root (all entries) GroupTreeNode
GroupTreeNode currentLibraryGroupRoot = getCurrentLibraryTab().getBibDatabaseContext()
.getMetaData()
.getGroups()
.get()
.copyNode();

// add currentLibraryGroupRoot to the Library if it does not have a root.
destinationLibraryTab.getBibDatabaseContext()
.getMetaData()
.setGroups(currentLibraryGroupRoot);
}
}