Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Add cancel button to cancelable tasks (#254)
Browse files Browse the repository at this point in the history
* Add cancel button to cancelable tasks

Signed-off-by: Paul Bui-Quang <paul.buiquang@rte-france.com>
  • Loading branch information
pl-buiquang authored and geofjamg committed Jan 9, 2020
1 parent 519a107 commit 27f8541
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
12 changes: 12 additions & 0 deletions gse-app/src/main/java/com/powsybl/gse/app/TaskItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.powsybl.gse.app;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

Expand All @@ -23,6 +25,8 @@ public class TaskItem {

private final StringProperty message = new SimpleStringProperty("");

private final BooleanProperty cancellable = new SimpleBooleanProperty(false);

public TaskItem(UUID id, String name, String message) {
this.id = Objects.requireNonNull(id);
this.name = Objects.requireNonNull(name);
Expand All @@ -41,6 +45,14 @@ public StringProperty getMessage() {
return message;
}

public boolean getCancellable() {
return cancellable.get();
}

public BooleanProperty cancellableProperty() {
return cancellable;
}

@Override
public String toString() {
return name;
Expand Down
22 changes: 19 additions & 3 deletions gse-app/src/main/java/com/powsybl/gse/app/TaskItemList.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class TaskItemList {

private final Lock initLock = new ReentrantLock();

private final ObservableList<TaskItem> items = FXCollections.observableArrayList(task -> new Observable[]{task.getMessage()});
private final ObservableList<TaskItem> items = FXCollections.observableArrayList(task -> new Observable[]{task.getMessage(), task.cancellableProperty()});

private final ObjectProperty<FilteredList<TaskItem>> displayItems = new SimpleObjectProperty<>();

Expand Down Expand Up @@ -91,7 +91,9 @@ private void reset() {
try {
items.clear();
for (TaskMonitor.Task task : snapshot.getTasks()) {
items.add(new TaskItem(task.getId(), task.getName(), task.getMessage()));
TaskItem taskItem = new TaskItem(task.getId(), task.getName(), task.getMessage());
taskItem.cancellableProperty().setValue(task.isCancellable());
items.add(taskItem);
}
} finally {
initLock.unlock();
Expand Down Expand Up @@ -130,7 +132,17 @@ private void processEvents() {
Platform.runLater(() -> items.stream()
.filter(task -> task.getId().equals(event.getTaskId()))
.findFirst()
.ifPresent(task -> task.getMessage().set(((UpdateTaskMessageEvent) event).getMessage())));
.ifPresent(task -> {
task.getMessage().set(((UpdateTaskMessageEvent) event).getMessage());
}));
} else if (event instanceof TaskCancellableStatusChangeEvent) {
Platform.runLater(() -> items.stream()
.filter(task -> task.getId().equals(event.getTaskId()))
.findFirst()
.ifPresent(task -> {
LOGGER.info("Task cancellable status has been update to {} for task {}", ((TaskCancellableStatusChangeEvent) event).isCancellable(), event.getTaskId());
task.cancellableProperty().set(((TaskCancellableStatusChangeEvent) event).isCancellable());
}));
} else {
throw new AssertionError();
}
Expand Down Expand Up @@ -166,6 +178,10 @@ public void hideTask(TaskItem taskItem) {
displayItems.get().setPredicate(el -> !config.getHiddenTasks().contains(el.getId().toString()));
}

public void cancelTask(TaskItem taskItem) {
taskMonitor.cancelTaskComputation(taskItem.getId());
}

private Config retrieveConfig() {
String serializedConfig = Preferences.userNodeForPackage(getClass()).get("config", null);
if (serializedConfig != null) {
Expand Down
53 changes: 36 additions & 17 deletions gse-app/src/main/java/com/powsybl/gse/app/TaskMonitorPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,12 +34,16 @@ private class TaskListCell extends ListCell<TaskItem> {

private final Label label = new Label("");
private final Pane content;
private final Button hideButton = new Button();
private final Button closeButton = new Button();
private final HBox actionBox = new HBox();
private final Consumer<TaskItem> onCloseAction;
private final Consumer<TaskItem> onHideAction;

public TaskListCell(Consumer<TaskItem> onClose) {
public TaskListCell(Consumer<TaskItem> onHide, Consumer<TaskItem> onClose) {
content = createLayout();
onCloseAction = onClose;
onHideAction = onHide;
}

@Override
Expand All @@ -51,10 +52,17 @@ protected void updateItem(TaskItem item, boolean empty) {
if (empty) {
setGraphic(null);
} else {
LOGGER.info("Updating task item display for {}", item);
label.setText(item.getName() + System.lineSeparator() + item.getMessage().getValue());
label.setTextOverrun(OverrunStyle.ELLIPSIS);
label.maxWidthProperty().bind(taskList.widthProperty().subtract(50.0));
hideButton.setOnAction(event -> onHideAction.accept(item));
closeButton.setOnAction(event -> onCloseAction.accept(item));
if (item.getCancellable() && !actionBox.getChildren().contains(closeButton)) {
actionBox.getChildren().add(closeButton);
} else if (!item.getCancellable()) {
actionBox.getChildren().remove(closeButton);
}
setGraphic(content);
}
}
Expand All @@ -68,24 +76,35 @@ private AnchorPane createLayout() {
root.getChildren().add(vBox);
AnchorPane.setLeftAnchor(vBox, 0.0);
AnchorPane.setTopAnchor(vBox, 0.0);
AnchorPane.setRightAnchor(vBox, 30.0);
AnchorPane.setRightAnchor(vBox, 45.0);
AnchorPane.setBottomAnchor(vBox, 0.0);

VBox box = new VBox();
box.setPrefWidth(15.0);
box.setAlignment(Pos.TOP_CENTER);
actionBox.setPrefWidth(30.0);
actionBox.setAlignment(Pos.TOP_CENTER);

Glyph graphic = Glyph.createAwesomeFont('\uf068');
graphic.color("-fx-mark-color");
closeButton.setGraphic(graphic);
hideButton.setGraphic(graphic);
hideButton.setPadding(new Insets(2, 5, 2, 5));
hideButton.setOnMouseEntered(event -> graphic.color("#eee"));
hideButton.setOnMouseExited(event -> graphic.color("-fx-mark-color"));
hideButton.getStyleClass().add("transparent-button");
hideButton.getStyleClass().add("hide-button");
actionBox.getChildren().add(hideButton);

Glyph closeGraphic = Glyph.createAwesomeFont('\uf00d');
closeGraphic.color("-fx-mark-color");
closeButton.setGraphic(closeGraphic);
closeButton.setPadding(new Insets(2, 5, 2, 5));
closeButton.setOnMouseEntered(event -> graphic.color("#eee"));
closeButton.setOnMouseExited(event -> graphic.color("-fx-mark-color"));
closeButton.setOnMouseEntered(event -> closeGraphic.color("#eee"));
closeButton.setOnMouseExited(event -> closeGraphic.color("-fx-mark-color"));
closeButton.getStyleClass().add("transparent-button");
closeButton.getStyleClass().add("hide-button");
box.getChildren().add(closeButton);
root.getChildren().add(box);
AnchorPane.setTopAnchor(box, 0.0);
AnchorPane.setRightAnchor(box, 0.0);
actionBox.getChildren().add(closeButton);

root.getChildren().add(actionBox);
AnchorPane.setTopAnchor(actionBox, 0.0);
AnchorPane.setRightAnchor(actionBox, 0.0);

return root;
}
Expand All @@ -97,7 +116,7 @@ public TaskMonitorPane(TaskItemList items) {
taskList.itemsProperty().bind(items.getDisplayItems());
taskList.setPlaceholder(new Label(RESOURCE_BUNDLE.getString("NoTaskRunning")));
setCenter(taskList);
taskList.setCellFactory(param -> new TaskListCell(items::hideTask));
taskList.setCellFactory(param -> new TaskListCell(items::hideTask, items::cancelTask));
renderHiddenItemsHint(items);
items.hiddenTaskCountProperty().addListener(new InvalidationListener() {
@Override
Expand Down
4 changes: 4 additions & 0 deletions gse-util/src/main/java/com/powsybl/gse/util/GseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.net.URLConnection;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -127,6 +128,9 @@ public static void execute(Executor executor, Runnable task) {
executor.execute(() -> {
try {
task.run();
} catch (CancellationException e) {
Platform.runLater(() -> GseAlerts.showDialogError("Task has been cancelled"));
LOGGER.warn("Task has been cancelled", e);
} catch (Throwable t) {
Platform.runLater(() -> GseUtil.showDialogError(t));
}
Expand Down

0 comments on commit 27f8541

Please sign in to comment.