Add total progress of task execution
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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<Task, ProgressListNode> 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);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -25,9 +25,10 @@
|
||||
</VBox>
|
||||
</center>
|
||||
<bottom>
|
||||
<HBox alignment="CENTER_RIGHT" style="-fx-padding: 0 8px 8px 0;">
|
||||
<JFXButton fx:id="btnCancel" text="%button.cancel" />
|
||||
</HBox>
|
||||
<StackPane style="-fx-padding: 0 8px 8px 8px;">
|
||||
<Label StackPane.alignment="CENTER_LEFT" fx:id="lblProgress" />
|
||||
<JFXButton StackPane.alignment="CENTER_RIGHT" fx:id="btnCancel" text="%button.cancel" />
|
||||
</StackPane>
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
</fx:root>
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user