From ce9d882c9f583d252b0b2e6c27b8954c97bc3e47 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Fri, 7 Sep 2018 15:12:41 +0800 Subject: [PATCH] Add total progress of task execution --- .../ui/construct/TaskExecutorDialogPane.java | 8 +++++ .../hmcl/ui/construct/TaskListPane.java | 33 +++++++++++++++++-- .../resources/assets/fxml/task-dialog.fxml | 7 ++-- .../org/jackhuang/hmcl/task/TaskExecutor.java | 9 ++--- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java index 9b60e256a..4e89bcc0b 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java @@ -20,6 +20,7 @@ package org.jackhuang.hmcl.ui.construct; import com.jfoenix.concurrency.JFXUtilities; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXProgressBar; +import javafx.beans.binding.Bindings; import javafx.beans.property.StringProperty; import javafx.fxml.FXML; import javafx.scene.control.Label; @@ -42,6 +43,8 @@ public class TaskExecutorDialogPane extends StackPane { @FXML private Label lblSubtitle; @FXML + private Label lblProgress; + @FXML private JFXButton btnCancel; @FXML private TaskListPane taskListPane; @@ -55,6 +58,11 @@ public class TaskExecutorDialogPane extends StackPane { Optional.ofNullable(executor).ifPresent(TaskExecutor::cancel); onCancel.accept(this); }); + + lblProgress.textProperty().bind(Bindings.createStringBinding( + () -> taskListPane.finishedTasksProperty().get() + "/" + taskListPane.totTasksProperty().get(), + taskListPane.finishedTasksProperty(), taskListPane.totTasksProperty() + )); } public void setExecutor(TaskExecutor executor) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java index bf558cebb..243aff1d1 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskListPane.java @@ -19,6 +19,8 @@ package org.jackhuang.hmcl.ui.construct; import com.jfoenix.controls.JFXProgressBar; import javafx.application.Platform; +import javafx.beans.property.ReadOnlyIntegerProperty; +import javafx.beans.property.ReadOnlyIntegerWrapper; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; @@ -42,6 +44,8 @@ import static org.jackhuang.hmcl.util.i18n.I18n.i18n; public final class TaskListPane extends StackPane { private final AdvancedListBox listBox = new AdvancedListBox(); private final Map nodes = new HashMap<>(); + private final ReadOnlyIntegerWrapper finishedTasks = new ReadOnlyIntegerWrapper(); + private final ReadOnlyIntegerWrapper totTasks = new ReadOnlyIntegerWrapper(); public TaskListPane() { listBox.setSpacing(0); @@ -49,11 +53,28 @@ public final class TaskListPane extends StackPane { getChildren().setAll(listBox); } + public ReadOnlyIntegerProperty finishedTasksProperty() { + return finishedTasks.getReadOnlyProperty(); + } + + public ReadOnlyIntegerProperty totTasksProperty() { + return totTasks.getReadOnlyProperty(); + } + public void setExecutor(TaskExecutor executor) { executor.addTaskListener(new TaskListener() { @Override public void onStart() { - Platform.runLater(listBox::clear); + Platform.runLater(() -> { + listBox.clear(); + finishedTasks.set(0); + totTasks.set(0); + }); + } + + @Override + public void onReady(Task task) { + Platform.runLater(() -> totTasks.set(totTasks.getValue() + 1)); } @Override @@ -97,7 +118,10 @@ public final class TaskListPane extends StackPane { if (node == null) return; node.unbind(); - Platform.runLater(() -> listBox.remove(node)); + Platform.runLater(() -> { + listBox.remove(node); + finishedTasks.set(finishedTasks.getValue() + 1); + }); } @Override @@ -105,7 +129,10 @@ public final class TaskListPane extends StackPane { ProgressListNode node = nodes.remove(task); if (node == null) return; - Platform.runLater(() -> node.setThrowable(throwable)); + Platform.runLater(() -> { + node.setThrowable(throwable); + finishedTasks.set(finishedTasks.getValue() + 1); + }); } }); } diff --git a/HMCL/src/main/resources/assets/fxml/task-dialog.fxml b/HMCL/src/main/resources/assets/fxml/task-dialog.fxml index 18ccb8cf4..6e2eb50d3 100644 --- a/HMCL/src/main/resources/assets/fxml/task-dialog.fxml +++ b/HMCL/src/main/resources/assets/fxml/task-dialog.fxml @@ -25,9 +25,10 @@ - - - + + diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java index db542d959..fcbd2925e 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java @@ -159,11 +159,12 @@ public final class TaskExecutor { if (doDependentsSucceeded) task.setDependentsSucceeded(); - task.setState(Task.TaskState.RUNNING); - - taskListeners.forEach(it -> it.onRunning(task)); try { - task.getScheduler().schedule(task::execute).get(); + task.getScheduler().schedule(() -> { + task.setState(Task.TaskState.RUNNING); + taskListeners.forEach(it -> it.onRunning(task)); + task.execute(); + }).get(); } catch (ExecutionException e) { if (e.getCause() instanceof Exception) throw (Exception) e.getCause();