Add total progress of task execution

This commit is contained in:
huangyuhui
2018-09-07 15:12:41 +08:00
parent 84c18c8693
commit ce9d882c9f
4 changed files with 47 additions and 10 deletions

View File

@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.ui.construct;
import com.jfoenix.concurrency.JFXUtilities; import com.jfoenix.concurrency.JFXUtilities;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXProgressBar; import com.jfoenix.controls.JFXProgressBar;
import javafx.beans.binding.Bindings;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Label; import javafx.scene.control.Label;
@@ -42,6 +43,8 @@ public class TaskExecutorDialogPane extends StackPane {
@FXML @FXML
private Label lblSubtitle; private Label lblSubtitle;
@FXML @FXML
private Label lblProgress;
@FXML
private JFXButton btnCancel; private JFXButton btnCancel;
@FXML @FXML
private TaskListPane taskListPane; private TaskListPane taskListPane;
@@ -55,6 +58,11 @@ public class TaskExecutorDialogPane extends StackPane {
Optional.ofNullable(executor).ifPresent(TaskExecutor::cancel); Optional.ofNullable(executor).ifPresent(TaskExecutor::cancel);
onCancel.accept(this); onCancel.accept(this);
}); });
lblProgress.textProperty().bind(Bindings.createStringBinding(
() -> taskListPane.finishedTasksProperty().get() + "/" + taskListPane.totTasksProperty().get(),
taskListPane.finishedTasksProperty(), taskListPane.totTasksProperty()
));
} }
public void setExecutor(TaskExecutor executor) { public void setExecutor(TaskExecutor executor) {

View File

@@ -19,6 +19,8 @@ package org.jackhuang.hmcl.ui.construct;
import com.jfoenix.controls.JFXProgressBar; import com.jfoenix.controls.JFXProgressBar;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
@@ -42,6 +44,8 @@ import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public final class TaskListPane extends StackPane { public final class TaskListPane extends StackPane {
private final AdvancedListBox listBox = new AdvancedListBox(); private final AdvancedListBox listBox = new AdvancedListBox();
private final Map<Task, ProgressListNode> nodes = new HashMap<>(); private final Map<Task, ProgressListNode> nodes = new HashMap<>();
private final ReadOnlyIntegerWrapper finishedTasks = new ReadOnlyIntegerWrapper();
private final ReadOnlyIntegerWrapper totTasks = new ReadOnlyIntegerWrapper();
public TaskListPane() { public TaskListPane() {
listBox.setSpacing(0); listBox.setSpacing(0);
@@ -49,11 +53,28 @@ public final class TaskListPane extends StackPane {
getChildren().setAll(listBox); getChildren().setAll(listBox);
} }
public ReadOnlyIntegerProperty finishedTasksProperty() {
return finishedTasks.getReadOnlyProperty();
}
public ReadOnlyIntegerProperty totTasksProperty() {
return totTasks.getReadOnlyProperty();
}
public void setExecutor(TaskExecutor executor) { public void setExecutor(TaskExecutor executor) {
executor.addTaskListener(new TaskListener() { executor.addTaskListener(new TaskListener() {
@Override @Override
public void onStart() { 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 @Override
@@ -97,7 +118,10 @@ public final class TaskListPane extends StackPane {
if (node == null) if (node == null)
return; return;
node.unbind(); node.unbind();
Platform.runLater(() -> listBox.remove(node)); Platform.runLater(() -> {
listBox.remove(node);
finishedTasks.set(finishedTasks.getValue() + 1);
});
} }
@Override @Override
@@ -105,7 +129,10 @@ public final class TaskListPane extends StackPane {
ProgressListNode node = nodes.remove(task); ProgressListNode node = nodes.remove(task);
if (node == null) if (node == null)
return; return;
Platform.runLater(() -> node.setThrowable(throwable)); Platform.runLater(() -> {
node.setThrowable(throwable);
finishedTasks.set(finishedTasks.getValue() + 1);
});
} }
}); });
} }

View File

@@ -25,9 +25,10 @@
</VBox> </VBox>
</center> </center>
<bottom> <bottom>
<HBox alignment="CENTER_RIGHT" style="-fx-padding: 0 8px 8px 0;"> <StackPane style="-fx-padding: 0 8px 8px 8px;">
<JFXButton fx:id="btnCancel" text="%button.cancel" /> <Label StackPane.alignment="CENTER_LEFT" fx:id="lblProgress" />
</HBox> <JFXButton StackPane.alignment="CENTER_RIGHT" fx:id="btnCancel" text="%button.cancel" />
</StackPane>
</bottom> </bottom>
</BorderPane> </BorderPane>
</fx:root> </fx:root>

View File

@@ -159,11 +159,12 @@ public final class TaskExecutor {
if (doDependentsSucceeded) if (doDependentsSucceeded)
task.setDependentsSucceeded(); task.setDependentsSucceeded();
task.setState(Task.TaskState.RUNNING);
taskListeners.forEach(it -> it.onRunning(task));
try { 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) { } catch (ExecutionException e) {
if (e.getCause() instanceof Exception) if (e.getCause() instanceof Exception)
throw (Exception) e.getCause(); throw (Exception) e.getCause();