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

[WIP] Implement copying cross-references #11228

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
34 changes: 34 additions & 0 deletions src/main/java/org/jabref/gui/CopyEntryToDialogView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jabref.gui;

import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.Region;

import org.jabref.gui.util.BaseDialog;

Check failure on line 7 in src/main/java/org/jabref/gui/CopyEntryToDialogView.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 Unused import - org.jabref.gui.util.BaseDialog. Raw Output: /github/workspace/./src/main/java/org/jabref/gui/CopyEntryToDialogView.java:7:8: error: Unused import - org.jabref.gui.util.BaseDialog. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)
import org.jabref.logic.l10n.Localization;

public class CopyEntryToDialogView extends FXDialog {
ButtonType copySelectedEntryOnly = new ButtonType("Copy selected entry only", ButtonBar.ButtonData.OK_DONE);
ButtonType copySelectedEntryWithCrossReferencedEntries = new ButtonType("Copy cross references", ButtonBar.ButtonData.OK_DONE);
ButtonType copySelectedEntryWithCrossReferencedEntriesRecursively = new ButtonType("Copy cross references recursively", ButtonBar.ButtonData.OK_DONE);

public CopyEntryToDialogView() {
super(AlertType.CONFIRMATION, Localization.lang("Copy entry to library"), true);
setHeaderText(null);
getDialogPane().setMinHeight(170);
setResizable(true);
setContentText("""
The selected entry cross references 4 other entries.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, Localization.lang does not work with textblocks yet.... You need to use \n

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that I tried to switch to JavaParser or similar tooling for better localization handling. However, this did not work in all cases and lead to upstream issues.

I was thinking to use Kilt for JabRef. However, I did not see fast enough how to keep our localization checks. (Because I like that we have a check that the messages are verbatim included in the code). - And it seems, we would have to change all our parameterized strings (poiu-de/kilt#23). ( %0 to {0}). And some layer on top of Kilt (which is not that harmful, but effort. And thus, the effort added too much for me).

Refs #7279

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the implementation: c6c7edd (#8050)


Do you want to copy all of them or just the selected one?

* Copying cross-references recursively means copying references of the selected entry, and references of its references, until all dependant entries are included.""");
getDialogPane().getButtonTypes().setAll(copySelectedEntryOnly, copySelectedEntryWithCrossReferencedEntries, copySelectedEntryWithCrossReferencedEntriesRecursively, new ButtonType("Cancel", ButtonBar.ButtonData.LEFT));
getDialogPane().getButtonTypes().stream()
.map(getDialogPane()::lookupButton)
.forEach(btn-> {
ButtonBar.setButtonUniformSize(btn, false);
((Region) btn).setMinWidth(120);
});
}
}
39 changes: 39 additions & 0 deletions src/main/java/org/jabref/gui/CopyEntryToLibraryAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.jabref.gui;

import java.util.List;

Check failure on line 3 in src/main/java/org/jabref/gui/CopyEntryToLibraryAction.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 Unused import - java.util.List. Raw Output: /github/workspace/./src/main/java/org/jabref/gui/CopyEntryToLibraryAction.java:3:8: error: Unused import - java.util.List. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)

import javafx.scene.control.TabPane;

import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;

public class CopyEntryToLibraryAction extends SimpleCommand {
private final DialogService dialogService;
private final StateManager stateManager;
private LibraryTab source;
private LibraryTab destination;
private final TabPane tabPane;

public CopyEntryToLibraryAction(DialogService dialogService, StateManager stateManager, LibraryTab source, LibraryTab destination, TabPane tabPane) {
this.dialogService = dialogService;
this.stateManager = stateManager;
this.source = source;
this.destination = destination;
this.tabPane = tabPane;

this.executable.bind(ActionHelper.needsEntriesSelected(1, stateManager));
}

@Override
public void execute() {
BibEntry selectedEntry = stateManager.getSelectedEntries().getFirst();
System.out.println(selectedEntry.getField(StandardField.EDITORA));
dialogService.showCustomDialogAndWait(new CopyEntryToDialogView());

// tabPane.getSelectionModel().select(destination);
// destination.dropEntry(List.of((BibEntry) selectedEntry.clone()));
// destination.dropEntry();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ private void createMainTable() {
Globals.getClipboardManager(),
entryTypesManager,
taskExecutor,
fileUpdateMonitor);
fileUpdateMonitor, getTabPane());
// Add the listener that binds selection to state manager (TODO: should be replaced by proper JavaFX binding as soon as table is implemented in JavaFX)
// content binding between StateManager#getselectedEntries and mainTable#getSelectedEntries does not work here as it does not trigger the ActionHelper#needsEntriesSelected checker for the menubar
mainTable.addSelectionListener(event -> {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/actions/StandardActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public enum StandardActions implements Action {

COPY_MORE(Localization.lang("Copy") + "..."),
COPY_TO(Localization.lang("Copy to") + "..."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this ... should be part of the localization string. I would change the above one as well

COPY_TITLE(Localization.lang("Copy title"), KeyBinding.COPY_TITLE),
COPY_KEY(Localization.lang("Copy citation key"), KeyBinding.COPY_CITATION_KEY),
COPY_CITE_KEY(Localization.lang("Copy citation key with configured cite command"), KeyBinding.COPY_CITE_CITATION_KEY),
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import javafx.collections.ListChangeListener;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
Expand Down Expand Up @@ -89,7 +90,7 @@ public MainTable(MainTableDataModel model,
ClipBoardManager clipBoardManager,
BibEntryTypesManager entryTypesManager,
TaskExecutor taskExecutor,
FileUpdateMonitor fileUpdateMonitor) {
FileUpdateMonitor fileUpdateMonitor, TabPane tabPane) {
super();

this.libraryTab = libraryTab;
Expand Down Expand Up @@ -145,7 +146,7 @@ public MainTable(MainTableDataModel model,
clipBoardManager,
taskExecutor,
Globals.journalAbbreviationRepository,
entryTypesManager))
entryTypesManager, tabPane))
.setOnDragDetected(this::handleOnDragDetected)
.setOnDragDropped(this::handleOnDragDropped)
.setOnDragOver(this::handleOnDragOver)
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.jabref.gui.maintable;

import java.util.List;

import javax.swing.undo.UndoManager;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.CopyEntryToLibraryAction;
import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.SendAsKindleEmailAction;
Expand All @@ -29,6 +34,7 @@
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.preferences.PreferencesService;
Expand All @@ -46,13 +52,14 @@ public static ContextMenu create(BibEntryTableViewModel entry,
ClipBoardManager clipBoardManager,
TaskExecutor taskExecutor,
JournalAbbreviationRepository abbreviationRepository,
BibEntryTypesManager entryTypesManager) {
BibEntryTypesManager entryTypesManager, TabPane tabPane) {
ActionFactory factory = new ActionFactory(keyBindingRepository);
ContextMenu contextMenu = new ContextMenu();

contextMenu.getItems().addAll(
factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, () -> libraryTab, stateManager, undoManager)),
createCopySubMenu(factory, dialogService, stateManager, preferencesService, clipBoardManager, abbreviationRepository, taskExecutor),
createCopyToSubmenu(dialogService, factory, stateManager, libraryTab, tabPane),
factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, () -> libraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, () -> libraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.MERGE_ENTRIES, new MergeEntriesAction(dialogService, stateManager, preferencesService)),
Expand Down Expand Up @@ -126,6 +133,38 @@ private static Menu createCopySubMenu(ActionFactory factory,
return copySpecialMenu;
}

/**
* Creates a submenu of all possible libraries that we can copy the selected entry to.
* */
private static Menu createCopyToSubmenu(DialogService dialogService, ActionFactory factory, StateManager stateManager, LibraryTab currLibraryTab, TabPane tabPane) {
BibDatabaseContext activeDatabase = stateManager.getActiveDatabase().orElseThrow();
List<BibDatabaseContext> possibleDestinations = stateManager.getOpenDatabases()
.stream().filter(database -> database != activeDatabase)
.toList();

Menu copyToMenu = factory.createMenu(StandardActions.COPY_TO);
for (BibDatabaseContext destDatabase : possibleDestinations) {
LibraryTab destLibraryTab = null;
for (Tab tab : tabPane.getTabs()) {
LibraryTab libraryTab = (LibraryTab) tab;
if (libraryTab.getBibDatabaseContext() == destDatabase) {
destLibraryTab = libraryTab;
break;
}
}

if (destLibraryTab == null) {
// TODO: log a warning
break;
}

String destLibraryName = destDatabase.getDatabasePath().orElseThrow().getFileName().toString();
copyToMenu.getItems().add(factory.createMenuItem(() -> destLibraryName, new CopyEntryToLibraryAction(dialogService, stateManager, currLibraryTab, destLibraryTab, tabPane)));
}

return copyToMenu;
}

private static Menu createSendSubMenu(ActionFactory factory,
DialogService dialogService,
StateManager stateManager,
Expand Down
Loading