From 4c6d1a3ed9332039d437627ec86eb06d23d90569 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 7 Sep 2024 21:19:58 +0200 Subject: [PATCH] Seperated notifications --- src/main/java/org/jabref/gui/LibraryTab.java | 158 +++++++++--------- .../java/org/jabref/gui/edit/EditAction.java | 8 +- .../jabref/gui/importer/NewEntryAction.java | 2 +- 3 files changed, 86 insertions(+), 82 deletions(-) diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index de371909e5c..0ce7d557b77 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -75,7 +75,6 @@ import org.jabref.logic.pdf.FileAnnotationCache; import org.jabref.logic.search.LuceneManager; import org.jabref.logic.shared.DatabaseLocation; -import org.jabref.logic.util.UpdateField; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.FieldChange; import org.jabref.model.database.BibDatabase; @@ -461,63 +460,6 @@ public SuggestionProviders getSuggestionProviders() { return suggestionProviders; } - /** - * Removes the selected entries and files linked to selected entries from the database - * - * @param mode If DELETE_ENTRY the user will get asked if he really wants to delete the entries, and it will be localized as "deleted". If true the action will be localized as "cut" - */ - public void deleteEntry(StandardActions mode) { - deleteEntry(mode, mainTable.getSelectedEntries()); - } - - /** - * Removes the selected entries and files linked to selected entries from the database - * - * @param mode If DELETE_ENTRY the user will get asked if he really wants to delete the entries, and it will be localized as "deleted". If true the action will be localized as "cut" - */ - private void deleteEntry(StandardActions mode, List entries) { - if (entries.isEmpty()) { - return; - } - if (mode == StandardActions.DELETE_ENTRY && !showDeleteConfirmationDialog(entries.size())) { - return; - } - - // Delete selected entries - getUndoManager().addEdit(new UndoableRemoveEntries(bibDatabaseContext.getDatabase(), entries, mode == StandardActions.CUT)); - bibDatabaseContext.getDatabase().removeEntries(entries); - - if (mode != StandardActions.CUT) { - List linkedFileList = entries.stream() - .flatMap(entry -> entry.getFiles().stream()) - .distinct() - .toList(); - - if (!linkedFileList.isEmpty()) { - List viewModels = linkedFileList.stream() - .map(linkedFile -> linkedFile.toModel(null, bibDatabaseContext, null, null, preferencesService)) - .collect(Collectors.toList()); - - new DeleteFileAction(dialogService, preferencesService.getFilePreferences(), bibDatabaseContext, viewModels).execute(); - } - } - - ensureNotShowingBottomPanel(entries); - - this.changedProperty.setValue(true); - switch (mode) { - case StandardActions.CUT -> dialogService.notify(Localization.lang("Cut %0 entry(ies)", entries.size())); - case StandardActions.DELETE_ENTRY -> dialogService.notify(Localization.lang("Deleted %0 entry(ies)", entries.size())); - } - - // prevent the main table from loosing focus - mainTable.requestFocus(); - } - - public void deleteEntry(BibEntry entry) { - deleteEntry(StandardActions.DELETE_ENTRY, Collections.singletonList(entry)); - } - public void registerUndoableChanges(List changes) { NamedCompound ce = new NamedCompound(Localization.lang("Save actions")); for (FieldChange change : changes) { @@ -537,16 +479,12 @@ public void insertEntry(final BibEntry bibEntry) { public void insertEntries(final List entries) { if (!entries.isEmpty()) { - bibDatabaseContext.getDatabase().insertEntries(entries); + importHandler.importCleanedEntries(entries); - // Set owner and timestamp - UpdateField.setAutomaticFields(entries, - preferencesService.getOwnerPreferences(), - preferencesService.getTimestampPreferences()); // Create an UndoableInsertEntries object. getUndoManager().addEdit(new UndoableInsertEntries(bibDatabaseContext.getDatabase(), entries)); - this.changedProperty.setValue(true); // The database just changed. + markBaseChanged(); if (preferencesService.getEntryEditorPreferences().shouldOpenOnNewEntry()) { showAndEdit(entries.getFirst()); } @@ -959,30 +897,35 @@ public void resetChangeMonitor() { stateManager)); } - public void copyEntry() { - List selectedEntries = getSelectedEntries(); + public void copy() { + int entriesCopied = doCopy(getSelectedEntries()); + if (entriesCopied >= 0) { + dialogService.notify(Localization.lang("Copied %0 entry(ies)", entriesCopied)); + } else { + dialogService.notify(Localization.lang("Copy failed", entriesCopied)); + } + } + private int doCopy(List selectedEntries) { if (!selectedEntries.isEmpty()) { - List stringConstants = getUsedStringValues(selectedEntries); + List stringConstants = bibDatabaseContext.getDatabase().getUsedStrings(selectedEntries); try { if (stringConstants.isEmpty()) { clipBoardManager.setContent(selectedEntries, entryTypesManager); } else { clipBoardManager.setContent(selectedEntries, entryTypesManager, stringConstants); } - dialogService.notify(Localization.lang("Copied %0 entry(ies)", selectedEntries.size())); - } catch ( - IOException e) { + return selectedEntries.size(); + } catch (IOException e) { LOGGER.error("Error while copying selected entries to clipboard.", e); + return -1; } } - } - private List getUsedStringValues(List entries) { - return bibDatabaseContext.getDatabase().getUsedStrings(entries); + return 0; } - public void pasteEntry() { + public void paste() { List entriesToAdd; String content = ClipBoardManager.getContents(); entriesToAdd = importHandler.handleBibTeXData(content); @@ -1016,9 +959,70 @@ public void dropEntry(List entriesToAdd) { importHandler.importEntriesWithDuplicateCheck(bibDatabaseContext, entriesToAdd); } - public void cutEntry() { - copyEntry(); - deleteEntry(StandardActions.CUT); + public void cut() { + int entriesCopied = doCopy(getSelectedEntries()); + int entriesDeleted = doDeleteEntry(StandardActions.CUT, mainTable.getSelectedEntries()); + + if (entriesCopied == entriesDeleted) { + dialogService.notify(Localization.lang("Cut %0 entry(ies)", entriesCopied)); + } else { + dialogService.notify(Localization.lang("Cut failed", entriesCopied)); + undoManager.undo(); + clipBoardManager.setContent(""); + } + } + + /** + * Removes the selected entries and files linked to selected entries from the database + */ + public void delete() { + int entriesDeleted = doDeleteEntry(StandardActions.DELETE_ENTRY, mainTable.getSelectedEntries()); + dialogService.notify(Localization.lang("Deleted %0 entry(ies)", entriesDeleted)); + } + + public void deleteEntry(BibEntry entry) { + doDeleteEntry(StandardActions.DELETE_ENTRY, Collections.singletonList(entry)); + } + + /** + * Removes the selected entries and files linked to selected entries from the database + * + * @param mode If DELETE_ENTRY the user will get asked if he really wants to delete the entries, and it will be localized as "deleted". If true the action will be localized as "cut" + */ + private int doDeleteEntry(StandardActions mode, List entries) { + if (entries.isEmpty()) { + return 0; + } + if (mode == StandardActions.DELETE_ENTRY && !showDeleteConfirmationDialog(entries.size())) { + return -1; + } + + // Delete selected entries + getUndoManager().addEdit(new UndoableRemoveEntries(bibDatabaseContext.getDatabase(), entries, mode == StandardActions.CUT)); + bibDatabaseContext.getDatabase().removeEntries(entries); + + if (mode != StandardActions.CUT) { + List linkedFileList = entries.stream() + .flatMap(entry -> entry.getFiles().stream()) + .distinct() + .toList(); + + if (!linkedFileList.isEmpty()) { + List viewModels = linkedFileList.stream() + .map(linkedFile -> linkedFile.toModel(null, bibDatabaseContext, null, null, preferencesService)) + .collect(Collectors.toList()); + + new DeleteFileAction(dialogService, preferencesService.getFilePreferences(), bibDatabaseContext, viewModels).execute(); + } + } + + ensureNotShowingBottomPanel(entries); + markBaseChanged(); + + // prevent the main table from loosing focus + mainTable.requestFocus(); + + return entries.size(); } public boolean isModified() { diff --git a/src/main/java/org/jabref/gui/edit/EditAction.java b/src/main/java/org/jabref/gui/edit/EditAction.java index 096539e8574..e0f3663f029 100644 --- a/src/main/java/org/jabref/gui/edit/EditAction.java +++ b/src/main/java/org/jabref/gui/edit/EditAction.java @@ -78,10 +78,10 @@ public void execute() { // Not sure what is selected -> copy/paste/cut selected entries except for Preview and CodeArea switch (action) { - case COPY -> tabSupplier.get().copyEntry(); - case CUT -> tabSupplier.get().cutEntry(); - case PASTE -> tabSupplier.get().pasteEntry(); - case DELETE_ENTRY -> tabSupplier.get().deleteEntry(StandardActions.DELETE_ENTRY); + case COPY -> tabSupplier.get().copy(); + case CUT -> tabSupplier.get().cut(); + case PASTE -> tabSupplier.get().paste(); + case DELETE_ENTRY -> tabSupplier.get().delete(); case UNDO -> { if (undoManager.canUndo()) { undoManager.undo(); diff --git a/src/main/java/org/jabref/gui/importer/NewEntryAction.java b/src/main/java/org/jabref/gui/importer/NewEntryAction.java index a47e1a9f6c6..6a3e70cc249 100644 --- a/src/main/java/org/jabref/gui/importer/NewEntryAction.java +++ b/src/main/java/org/jabref/gui/importer/NewEntryAction.java @@ -43,7 +43,7 @@ public NewEntryAction(Supplier tabSupplier, DialogService dialogServ public NewEntryAction(Supplier tabSupplier, EntryType type, DialogService dialogService, PreferencesService preferences, StateManager stateManager) { this(tabSupplier, dialogService, preferences, stateManager); - this.type = Optional.of(type); + this.type = Optional.ofNullable(type); } @Override