Skip to content

Commit

Permalink
Fix background task progress (#11822)
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb authored Sep 24, 2024
1 parent c766476 commit 5ccf3e0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
17 changes: 10 additions & 7 deletions src/main/java/org/jabref/gui/util/UiTaskExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ public static void runInJavaFXThread(Runnable runnable) {
@Override
public <V> Future<V> execute(BackgroundTask<V> task) {
Task<V> javafxTask = getJavaFXTask(task);
if (task.showToUser()) {
StateManager stateManager = Injector.instantiateModelOrService(StateManager.class);
if (stateManager != null) {
stateManager.addBackgroundTask(task, javafxTask);
} else {
LOGGER.info("Background task visible without GUI");

StateManager stateManager = Injector.instantiateModelOrService(StateManager.class);
task.showToUserProperty().subscribe(showToUser -> {
if (showToUser) {
if (stateManager != null) {
runInJavaFXThread(() -> stateManager.addBackgroundTask(task, javafxTask));
} else {
LOGGER.info("Background task visible without GUI");
}
}
}
});
return execute(javafxTask);
}

Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/jabref/logic/search/LuceneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Object call() {
linkedFilesIndexer.updateOnStart(this);
return null;
}
}.showToUser(true).executeWith(taskExecutor);
}.executeWith(taskExecutor);
} else {
linkedFilesIndexer.removeAllFromIndex();
}
Expand All @@ -81,8 +81,7 @@ public Object call() {
bibFieldsIndexer.updateOnStart(this);
return null;
}
}.showToUser(true)
.willBeRecoveredAutomatically(true)
}.willBeRecoveredAutomatically(true)
.onFinished(() -> this.databaseContext.getDatabase().postEvent(new IndexStartedEvent()))
.executeWith(taskExecutor);

Expand All @@ -93,7 +92,7 @@ public Object call() {
linkedFilesIndexer.updateOnStart(this);
return null;
}
}.showToUser(true).executeWith(taskExecutor);
}.executeWith(taskExecutor);
}
}

Expand All @@ -105,7 +104,7 @@ public Object call() {
return null;
}
}.onFinished(() -> this.databaseContext.getDatabase().postEvent(new IndexAddedOrUpdatedEvent(entries)))
.showToUser(true).executeWith(taskExecutor);
.executeWith(taskExecutor);

if (shouldIndexLinkedFiles.get() && !isLinkedFilesIndexerBlocked.get()) {
new BackgroundTask<>() {
Expand All @@ -114,7 +113,7 @@ public Object call() {
linkedFilesIndexer.addToIndex(entries, this);
return null;
}
}.showToUser(true).executeWith(taskExecutor);
}.executeWith(taskExecutor);
}
}

Expand All @@ -126,7 +125,7 @@ public Object call() {
return null;
}
}.onFinished(() -> this.databaseContext.getDatabase().postEvent(new IndexRemovedEvent(entries)))
.showToUser(true).executeWith(taskExecutor);
.executeWith(taskExecutor);

if (shouldIndexLinkedFiles.get()) {
new BackgroundTask<>() {
Expand All @@ -135,7 +134,7 @@ public Object call() {
linkedFilesIndexer.removeFromIndex(entries, this);
return null;
}
}.showToUser(true).executeWith(taskExecutor);
}.executeWith(taskExecutor);
}
}

Expand Down Expand Up @@ -177,7 +176,7 @@ public Object call() {
linkedFilesIndexer.addToIndex(List.of(entry), this);
return null;
}
}.showToUser(true).executeWith(taskExecutor);
}.executeWith(taskExecutor);
}
}

Expand All @@ -189,7 +188,7 @@ public Object call() {
return null;
}
}.onFinished(() -> this.databaseContext.getDatabase().postEvent(new IndexStartedEvent()))
.showToUser(true).executeWith(taskExecutor);
.executeWith(taskExecutor);

if (shouldIndexLinkedFiles.get()) {
new BackgroundTask<>() {
Expand All @@ -198,7 +197,7 @@ public Object call() {
linkedFilesIndexer.rebuildIndex(this);
return null;
}
}.showToUser(true).executeWith(taskExecutor);
}.executeWith(taskExecutor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public void updateOnStart(BackgroundTask<?> task) {

@Override
public void addToIndex(Collection<BibEntry> entries, BackgroundTask<?> task) {
task.setTitle(Localization.lang("Indexing bib fields for %0", libraryName));
if (entries.size() > 1) {
task.showToUser(true);
task.setTitle(Localization.lang("Indexing bib fields for %0", libraryName));
}
int i = 1;
long startTime = System.currentTimeMillis();
LOGGER.debug("Adding {} entries to index", entries.size());
Expand Down Expand Up @@ -99,7 +102,10 @@ private void addToIndex(BibEntry bibEntry) {

@Override
public void removeFromIndex(Collection<BibEntry> entries, BackgroundTask<?> task) {
task.setTitle(Localization.lang("Removing entries from index for %0", libraryName));
if (entries.size() > 1) {
task.showToUser(true);
task.setTitle(Localization.lang("Removing entries from index for %0", libraryName));
}
int i = 1;
for (BibEntry entry : entries) {
if (task.isCancelled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private void addToIndex(Map<String, Pair<Long, Path>> linkedFiles, BackgroundTas
}

task.setTitle(Localization.lang("Indexing PDF files for %0", libraryName));
task.showToUser(true);
LOGGER.debug("Adding {} files to index", linkedFiles.size());
int i = 1;
for (Map.Entry<String, Pair<Long, Path>> entry : linkedFiles.entrySet()) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/logic/util/BackgroundTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public boolean showToUser() {
return showToUser.get();
}

public BooleanProperty showToUserProperty() {
return showToUser;
}

public BackgroundTask<V> showToUser(boolean show) {
showToUser.set(show);
return this;
Expand Down

0 comments on commit 5ccf3e0

Please sign in to comment.