Skip to content
New issue

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

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

Already on GitHub? # to your account

Only update auto completion suggestions upon major changes #3078

Merged
merged 2 commits into from
Aug 6, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
### Fixed

- We fixed an issue where the fetcher for the Astrophysics Data System (ADS) added some non-bibtex data to the entry returned from the search [#3035](https://github.com/JabRef/jabref/issues/3035)
- We improved the auto completion so that minor changes are not added as suggestions. [#2998](https://github.com/JabRef/jabref/issues/2998)
- We readded the undo mechanism for changes in the entry editor [#2973](https://github.com/JabRef/jabref/issues/2973)
- We fixed an issue where assigning an entry via drag and drop to a group caused JabRef to stop/freeze completely [#3036](https://github.com/JabRef/jabref/issues/3036)
- We fixed the shortcut <kbd>Ctrl</kbd>+<kbd>F</kbd> for the search field.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
import org.jabref.model.database.DatabaseLocation;
import org.jabref.model.database.KeyCollisionException;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.database.event.CoarseChangeFilter;
import org.jabref.model.database.event.EntryAddedEvent;
import org.jabref.model.database.event.EntryRemovedEvent;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -1396,7 +1397,8 @@ private void setupAutoCompletion() {
suggestionProviders = new SuggestionProviders(autoCompletePreferences, Globals.journalAbbreviationLoader);
suggestionProviders.indexDatabase(getDatabase());
// Ensure that the suggestion providers are in sync with entries
this.getDatabase().registerListener(new AutoCompleteUpdater(suggestionProviders));
CoarseChangeFilter changeFilter = new CoarseChangeFilter(bibDatabaseContext);
changeFilter.registerListener(new AutoCompleteUpdater(suggestionProviders));
} else {
// Create empty suggestion providers if auto completion is deactivated
suggestionProviders = new SuggestionProviders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.model.database.event.CoarseChangeFilter;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;
Expand All @@ -43,8 +43,6 @@ public class BackupManager {

private static Set<BackupManager> runningInstances = new HashSet<>();

private String lastFieldChanged;

private final BibDatabaseContext bibDatabaseContext;
private final JabRefPreferences preferences;
private final ExecutorService executor;
Expand All @@ -56,9 +54,8 @@ private BackupManager(BibDatabaseContext bibDatabaseContext) {
BlockingQueue<Runnable> workerQueue = new ArrayBlockingQueue<>(1);
this.executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, workerQueue);

// Listen for change events
bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);
CoarseChangeFilter changeFilter = new CoarseChangeFilter(bibDatabaseContext);
changeFilter.registerListener(this);
}

static Path getBackupPath(Path originalPath) {
Expand Down Expand Up @@ -131,18 +128,7 @@ private void performBackup(Path backupPath) {

@Subscribe
public synchronized void listen(@SuppressWarnings("unused") BibDatabaseContextChangedEvent event) {
if (!(event instanceof FieldChangedEvent)) {
startBackupTask();
} else {
// only do a backup if the field changes are more than one character or a new field is edited
FieldChangedEvent fieldChange = (FieldChangedEvent) event;
boolean isEditOnNewField = lastFieldChanged == null || !lastFieldChanged.equals(fieldChange.getFieldName());

if (fieldChange.getDelta() > 1 || isEditOnNewField) {
lastFieldChanged = fieldChange.getFieldName();
startBackupTask();
}
}
startBackupTask();
}

private void startBackupTask() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jabref.model.database.event;

import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.event.FieldChangedEvent;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

/**
* Filters change events and only relays major changes.
*/
public class CoarseChangeFilter {

private final EventBus eventBus = new EventBus();
private String lastFieldChanged;

public CoarseChangeFilter(BibDatabaseContext bibDatabaseContext) {
// Listen for change events
bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);
}

@Subscribe
public synchronized void listen(@SuppressWarnings("unused") BibDatabaseContextChangedEvent event) {
if (!(event instanceof FieldChangedEvent)) {
eventBus.post(event);
} else {
// Only relay event if the field changes are more than one character or a new field is edited
FieldChangedEvent fieldChange = (FieldChangedEvent) event;
boolean isEditOnNewField = lastFieldChanged == null || !lastFieldChanged.equals(fieldChange.getFieldName());

if (fieldChange.getDelta() > 1 || isEditOnNewField) {
lastFieldChanged = fieldChange.getFieldName();
eventBus.post(event);
}
}
}

public void registerListener(Object listener) {
eventBus.register(listener);
}
}