From 7898b2ae94cd47eed5ecc1c747d39add317c1cd5 Mon Sep 17 00:00:00 2001 From: Bartosz Jaroszkowski Date: Sat, 6 Mar 2021 13:27:11 +0100 Subject: [PATCH 1/6] getting SuggestionProvider for a field now checks if user did not choose to ignore autocompletion for that particular field --- src/main/java/org/jabref/gui/LibraryTab.java | 129 ++++++++---------- .../autocompleter/SuggestionProviders.java | 7 +- 2 files changed, 60 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index 0aec18fde81..1d3dbc75721 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -71,34 +71,26 @@ public class LibraryTab extends Tab { private static final Logger LOGGER = LoggerFactory.getLogger(LibraryTab.class); - - private BibDatabaseContext bibDatabaseContext; - private MainTableDataModel tableModel; - - private CitationStyleCache citationStyleCache; - private FileAnnotationCache annotationCache; - private final JabRefFrame frame; private final CountingUndoManager undoManager; - private final SidePaneManager sidePaneManager; private final ExternalFileTypes externalFileTypes; - - private EntryEditor entryEditor; private final DialogService dialogService; private final PreferencesService preferencesService; - + private final BooleanProperty changedProperty = new SimpleBooleanProperty(false); + private final BooleanProperty nonUndoableChangeProperty = new SimpleBooleanProperty(false); + private BibDatabaseContext bibDatabaseContext; + private MainTableDataModel tableModel; + private CitationStyleCache citationStyleCache; + private FileAnnotationCache annotationCache; + private EntryEditor entryEditor; private MainTable mainTable; private BasePanelMode mode = BasePanelMode.SHOWING_NOTHING; private SplitPane splitPane; private DatabaseChangePane changePane; private boolean saving; private PersonNameSuggestionProvider searchAutoCompleter; - - private final BooleanProperty changedProperty = new SimpleBooleanProperty(false); - private final BooleanProperty nonUndoableChangeProperty = new SimpleBooleanProperty(false); // Used to track whether the base has changed since last save. - private BibEntry showing; private SuggestionProviders suggestionProviders; @SuppressWarnings({"FieldCanBeLocal"}) @@ -151,14 +143,32 @@ public LibraryTab(JabRefFrame frame, }); } - public void setDataLoadingTask(BackgroundTask dataLoadingTask) { - this.dataLoadingTask = dataLoadingTask; + private static void addChangedInformation(StringBuilder text, String fileName) { + text.append("\n"); + text.append(Localization.lang("Library '%0' has changed.", fileName)); + } + + private static void addModeInfo(StringBuilder text, BibDatabaseContext bibDatabaseContext) { + String mode = bibDatabaseContext.getMode().getFormattedName(); + String modeInfo = String.format("\n%s", Localization.lang("%0 mode", mode)); + text.append(modeInfo); + } + + private static void addSharedDbInformation(StringBuilder text, BibDatabaseContext bibDatabaseContext) { + text.append(bibDatabaseContext.getDBMSSynchronizer().getDBName()); + text.append(" ["); + text.append(Localization.lang("shared")); + text.append("]"); } public BackgroundTask getDataLoadingTask() { return dataLoadingTask; } + public void setDataLoadingTask(BackgroundTask dataLoadingTask) { + this.dataLoadingTask = dataLoadingTask; + } + /* The layout to display in the tab when it's loading*/ public Node createLoadingAnimationLayout() { ProgressIndicator progressIndicator = new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS); @@ -246,8 +256,7 @@ private boolean isDatabaseReadyForAutoSave(BibDatabaseContext context) { /** * Sets the title of the tab modification-asterisk filename – path-fragment *

- * The modification-asterisk (*) is shown if the file was modified since last save (path-fragment is only shown if - * filename is not (globally) unique) + * The modification-asterisk (*) is shown if the file was modified since last save (path-fragment is only shown if filename is not (globally) unique) *

* Example: *jabref-authors.bib – testbib */ @@ -322,25 +331,6 @@ public void updateTabTitle(boolean isChanged) { textProperty().setValue(tabTitle.toString()); setTooltip(new Tooltip(toolTipText.toString())); }); - - } - - private static void addChangedInformation(StringBuilder text, String fileName) { - text.append("\n"); - text.append(Localization.lang("Library '%0' has changed.", fileName)); - } - - private static void addModeInfo(StringBuilder text, BibDatabaseContext bibDatabaseContext) { - String mode = bibDatabaseContext.getMode().getFormattedName(); - String modeInfo = String.format("\n%s", Localization.lang("%0 mode", mode)); - text.append(modeInfo); - } - - private static void addSharedDbInformation(StringBuilder text, BibDatabaseContext bibDatabaseContext) { - text.append(bibDatabaseContext.getDBMSSynchronizer().getDBName()); - text.append(" ["); - text.append(Localization.lang("shared")); - text.append("]"); } private List collectAllDatabasePaths() { @@ -380,8 +370,7 @@ public JabRefFrame frame() { /** * Removes the selected entries from the database * - * @param cut If false 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" + * @param cut If false 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 delete(boolean cut) { delete(cut, mainTable.getSelectedEntries()); @@ -390,8 +379,7 @@ public void delete(boolean cut) { /** * Removes the selected entries from the database * - * @param cut If false 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" + * @param cut If false 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 delete(boolean cut, List entries) { if (entries.isEmpty()) { @@ -434,8 +422,7 @@ public void insertEntry(final BibEntry bibEntry) { } /** - * This method is called from JabRefFrame when the user wants to create a new entry or entries. It is necessary when - * the user would expect the added entry or one of the added entries to be selected in the entry editor + * This method is called from JabRefFrame when the user wants to create a new entry or entries. It is necessary when the user would expect the added entry or one of the added entries to be selected in the entry editor * * @param entries The new entries. */ @@ -531,7 +518,7 @@ public void setupMainPanel() { private void setupAutoCompletion() { AutoCompletePreferences autoCompletePreferences = preferencesService.getAutoCompletePreferences(); if (autoCompletePreferences.shouldAutoComplete()) { - suggestionProviders = new SuggestionProviders(getDatabase(), Globals.journalAbbreviationRepository); + suggestionProviders = new SuggestionProviders(getDatabase(), Globals.journalAbbreviationRepository, autoCompletePreferences); } else { // Create empty suggestion providers if auto completion is deactivated suggestionProviders = new SuggestionProviders(); @@ -548,8 +535,7 @@ public EntryEditor getEntryEditor() { } /** - * Sets the entry editor as the bottom component in the split pane. If an entry editor already was shown, makes sure - * that the divider doesn't move. Updates the mode to SHOWING_EDITOR. Then shows the given entry. + * Sets the entry editor as the bottom component in the split pane. If an entry editor already was shown, makes sure that the divider doesn't move. Updates the mode to SHOWING_EDITOR. Then shows the given entry. * * @param entry The entry to edit. */ @@ -588,8 +574,7 @@ public void closeBottomPane() { } /** - * This method selects the given entry, and scrolls it into view in the table. If an entryEditor is shown, it is - * given focus afterwards. + * This method selects the given entry, and scrolls it into view in the table. If an entryEditor is shown, it is given focus afterwards. */ public void clearAndSelect(final BibEntry bibEntry) { mainTable.clearAndSelect(bibEntry); @@ -604,8 +589,7 @@ public void selectNextEntry() { } /** - * This method is called from an EntryEditor when it should be closed. We relay to the selection listener, which - * takes care of the rest. + * This method is called from an EntryEditor when it should be closed. We relay to the selection listener, which takes care of the rest. */ public void entryEditorClosing() { closeBottomPane(); @@ -673,8 +657,7 @@ private boolean showDeleteConfirmationDialog(int numberOfEntries) { } /** - * Depending on whether a preview or an entry editor is showing, save the current divider location in the correct - * preference setting. + * Depending on whether a preview or an entry editor is showing, save the current divider location in the correct preference setting. */ private void saveDividerLocation(Number position) { if (mode == BasePanelMode.SHOWING_EDITOR) { @@ -693,8 +676,7 @@ public void cleanUp() { } /** - * Get an array containing the currently selected entries. The array is stable and not changed if the selection - * changes + * Get an array containing the currently selected entries. The array is stable and not changed if the selection changes * * @return A list containing the selected entries. Is never null. */ @@ -800,6 +782,23 @@ public void resetChangedProperties() { this.changedProperty.setValue(false); } + public static class Factory { + public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, Path file, BackgroundTask dataLoadingTask) { + BibDatabaseContext context = new BibDatabaseContext(); + context.setDatabasePath(file); + + LibraryTab newTab = new LibraryTab(frame, preferencesService, context, ExternalFileTypes.getInstance()); + newTab.setDataLoadingTask(dataLoadingTask); + + dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted) + .onSuccess(newTab::onDatabaseLoadingSucceed) + .onFailure(newTab::onDatabaseLoadingFailed) + .executeWith(Globals.TASK_EXECUTOR); + + return newTab; + } + } + private class GroupTreeListener { @Subscribe @@ -826,8 +825,7 @@ public void listen(EntriesRemovedEvent entriesRemovedEvent) { } /** - * Ensures that the results of the current search are updated when a new entry is inserted into the database Actual - * methods for performing search must run in javafx thread + * Ensures that the results of the current search are updated when a new entry is inserted into the database Actual methods for performing search must run in javafx thread */ private class SearchListener { @@ -847,21 +845,4 @@ public void listen(EntriesRemovedEvent removedEntriesEvent) { DefaultTaskExecutor.runInJavaFXThread(() -> frame.getGlobalSearchBar().performSearch()); } } - - public static class Factory { - public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, Path file, BackgroundTask dataLoadingTask) { - BibDatabaseContext context = new BibDatabaseContext(); - context.setDatabasePath(file); - - LibraryTab newTab = new LibraryTab(frame, preferencesService, context, ExternalFileTypes.getInstance()); - newTab.setDataLoadingTask(dataLoadingTask); - - dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted) - .onSuccess(newTab::onDatabaseLoadingSucceed) - .onFailure(newTab::onDatabaseLoadingFailed) - .executeWith(Globals.TASK_EXECUTOR); - - return newTab; - } - } } diff --git a/src/main/java/org/jabref/gui/autocompleter/SuggestionProviders.java b/src/main/java/org/jabref/gui/autocompleter/SuggestionProviders.java index 47e1309fc1c..073cce6acda 100644 --- a/src/main/java/org/jabref/gui/autocompleter/SuggestionProviders.java +++ b/src/main/java/org/jabref/gui/autocompleter/SuggestionProviders.java @@ -13,10 +13,12 @@ public class SuggestionProviders { private final boolean isEmpty; private BibDatabase database; private JournalAbbreviationRepository abbreviationRepository; + private AutoCompletePreferences autoCompletePreferences; - public SuggestionProviders(BibDatabase database, JournalAbbreviationRepository abbreviationRepository) { + public SuggestionProviders(BibDatabase database, JournalAbbreviationRepository abbreviationRepository, AutoCompletePreferences autoCompletePreferences) { this.database = database; this.abbreviationRepository = abbreviationRepository; + this.autoCompletePreferences = autoCompletePreferences; this.isEmpty = false; } @@ -25,7 +27,8 @@ public SuggestionProviders() { } public SuggestionProvider getForField(Field field) { - if (isEmpty) { + + if (isEmpty || !autoCompletePreferences.getCompleteFields().contains(field)) { return new EmptySuggestionProvider(); } From c727569d01f232ec5af032fea439c3f9ab92d305 Mon Sep 17 00:00:00 2001 From: Bartosz Jaroszkowski Date: Sat, 6 Mar 2021 14:05:47 +0100 Subject: [PATCH 2/6] added test to check if autocomplete is disabled on ignored fields --- .../FieldValueSuggestionProviderTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java index 99d5728fb03..746c7d2e1ac 100644 --- a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java +++ b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java @@ -3,7 +3,10 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import org.jabref.gui.Globals; import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; @@ -14,6 +17,8 @@ import static org.jabref.gui.autocompleter.AutoCompleterUtil.getRequest; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class FieldValueSuggestionProviderTest { @@ -56,6 +61,22 @@ void completeAfterAddingEntryWithoutFieldReturnsNothing() { assertEquals(Collections.emptyList(), result); } + @Test + void completeOnIgnoredFieldReturnsNothing() { + AutoCompletePreferences autoCompletePreferences = mock(AutoCompletePreferences.class); + when(autoCompletePreferences.getCompleteFields()).thenReturn(new HashSet<>(Set.of(StandardField.AUTHOR))); + SuggestionProviders suggestionProviders = new SuggestionProviders(database, Globals.journalAbbreviationRepository, autoCompletePreferences); + + SuggestionProvider autoCompleter = (SuggestionProvider) suggestionProviders.getForField(StandardField.TITLE); + + BibEntry entry = new BibEntry(); + entry.setField(StandardField.TITLE, "testValue"); + database.insertEntry(entry); + + Collection result = autoCompleter.provideSuggestions(getRequest(("testValue"))); + assertEquals(Collections.emptyList(), result); + } + @Test void completeValueReturnsValue() { BibEntry entry = new BibEntry(); From 3cb04b0362a898f0183a1fa38151ccbd2bc3b830 Mon Sep 17 00:00:00 2001 From: Bartosz Jaroszkowski Date: Sat, 6 Mar 2021 14:06:42 +0100 Subject: [PATCH 3/6] updated changelog to reflect the changes --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ec5e5f4e4..93e9f69a97c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Fixed +- We fixed an issue where choosing the fields on which autocompletion should not work in "Entry editor" preferences had no effect. [#7320](https://github.com/JabRef/jabref/issues/7320) - We fixed an issue where the "Normalize page numbers" formatter did not replace en-dashes or em-dashes with a hyphen-minus sign. [#7239](https://github.com/JabRef/jabref/issues/7239) - We fixed an issue with the style of highlighted check boxes while searching in preferences. [#7226](https://github.com/JabRef/jabref/issues/7226) - We fixed an issue where the option "Move file to file directory" was disabled in the entry editor for all files [#7194](https://github.com/JabRef/jabref/issues/7194) From 272d931f0adad738a78b8085f959450bb9968ece Mon Sep 17 00:00:00 2001 From: Bartosz Jaroszkowski Date: Sat, 6 Mar 2021 14:19:12 +0100 Subject: [PATCH 4/6] Globals no longer used in the test --- .../gui/autocompleter/FieldValueSuggestionProviderTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java index 746c7d2e1ac..582323d2691 100644 --- a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java +++ b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java @@ -6,7 +6,7 @@ import java.util.HashSet; import java.util.Set; -import org.jabref.gui.Globals; +import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; @@ -64,8 +64,9 @@ void completeAfterAddingEntryWithoutFieldReturnsNothing() { @Test void completeOnIgnoredFieldReturnsNothing() { AutoCompletePreferences autoCompletePreferences = mock(AutoCompletePreferences.class); + JournalAbbreviationRepository journalAbbreviationRepository = mock(JournalAbbreviationRepository.class); when(autoCompletePreferences.getCompleteFields()).thenReturn(new HashSet<>(Set.of(StandardField.AUTHOR))); - SuggestionProviders suggestionProviders = new SuggestionProviders(database, Globals.journalAbbreviationRepository, autoCompletePreferences); + SuggestionProviders suggestionProviders = new SuggestionProviders(database, journalAbbreviationRepository, autoCompletePreferences); SuggestionProvider autoCompleter = (SuggestionProvider) suggestionProviders.getForField(StandardField.TITLE); From 7ef8a0f279cc2339b01f4f360f7310db3510a696 Mon Sep 17 00:00:00 2001 From: BJaroszkowski <54515221+BJaroszkowski@users.noreply.github.com> Date: Sun, 7 Mar 2021 14:39:00 +0100 Subject: [PATCH 5/6] Simplify mock set in the test Co-authored-by: Christoph --- .../gui/autocompleter/FieldValueSuggestionProviderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java index 582323d2691..dee4a4714d5 100644 --- a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java +++ b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java @@ -65,7 +65,7 @@ void completeAfterAddingEntryWithoutFieldReturnsNothing() { void completeOnIgnoredFieldReturnsNothing() { AutoCompletePreferences autoCompletePreferences = mock(AutoCompletePreferences.class); JournalAbbreviationRepository journalAbbreviationRepository = mock(JournalAbbreviationRepository.class); - when(autoCompletePreferences.getCompleteFields()).thenReturn(new HashSet<>(Set.of(StandardField.AUTHOR))); + when(autoCompletePreferences.getCompleteFields()).thenReturn(Set.of(StandardField.AUTHOR)); SuggestionProviders suggestionProviders = new SuggestionProviders(database, journalAbbreviationRepository, autoCompletePreferences); SuggestionProvider autoCompleter = (SuggestionProvider) suggestionProviders.getForField(StandardField.TITLE); From 1fcce5faff3bb384710a794d1bdc9efe87d8c773 Mon Sep 17 00:00:00 2001 From: Bartosz Jaroszkowski Date: Sun, 7 Mar 2021 15:08:20 +0100 Subject: [PATCH 6/6] deleted unused imports --- .../gui/autocompleter/FieldValueSuggestionProviderTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java index dee4a4714d5..ecc0b77e806 100644 --- a/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java +++ b/src/test/java/org/jabref/gui/autocompleter/FieldValueSuggestionProviderTest.java @@ -3,7 +3,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.Set; import org.jabref.logic.journals.JournalAbbreviationRepository;