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

Commit

Permalink
ergonomics in contingencies editor : bind keys + fix save after rename (
Browse files Browse the repository at this point in the history
  • Loading branch information
miovd authored and geofjamg committed Oct 18, 2018
1 parent 102a76f commit 6af6c60
Showing 1 changed file with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
Expand All @@ -34,6 +36,8 @@ public class ContingencyStoreEditor extends BorderPane implements ProjectFileVie

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("lang.ContingencyStore");

private static final String REMOVE = RESOURCE_BUNDLE.getString("Remove");

private final ContingencyStore store;

private final TreeItem<Object> root = new TreeItem<>();
Expand Down Expand Up @@ -181,6 +185,8 @@ public ContingencyStoreEditor(ContingencyStore store) {
}
});

contingencyTree.addEventHandler(KeyEvent.KEY_PRESSED, this::keyBind);

setTop(toolBar);
setCenter(contingencyTree);
}
Expand All @@ -201,11 +207,22 @@ private void writeContingencies() {
private ContextMenu createContingencyMenu() {
MenuItem renameItem = new MenuItem(RESOURCE_BUNDLE.getString("Rename") + "...");
renameItem.setOnAction(event -> rename());
MenuItem removeItem = new MenuItem(RESOURCE_BUNDLE.getString("Remove"));
MenuItem removeItem = new MenuItem(REMOVE);
removeItem.setOnAction(event -> remove());
return new ContextMenu(renameItem, removeItem);
}

private void keyBind(KeyEvent e) {
if (e.getCode() == KeyCode.DELETE) {
alertRemove();
} else if (e.getCode() == KeyCode.F2) {
TreeItem<Object> item = contingencyTree.getSelectionModel().getSelectedItem();
if (item.getValue() instanceof Contingency) {
rename();
}
}
}

private void rename() {
TreeItem<Object> item = contingencyTree.getSelectionModel().getSelectedItem();
Contingency contingency = (Contingency) item.getValue();
Expand All @@ -215,9 +232,29 @@ private void rename() {
dialog.setContentText(RESOURCE_BUNDLE.getString("Name"));
Optional<String> result = dialog.showAndWait();
result.ifPresent(newName -> {
if (!newName.isEmpty()) {
if (!newName.isEmpty() && !newName.equals(contingency.getId())) {
contingency.setId(newName);
contingencyTree.refresh();
saved.set(false);
}
});
}

private void alertRemove() {
TreeItem<Object> item = contingencyTree.getSelectionModel().getSelectedItem();
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle(REMOVE);
if (item.getValue() instanceof Contingency) {
alert.setHeaderText(REMOVE + " " + ((Contingency) item.getValue()).getId() + " ?");
} else if (item.getValue() instanceof ContingencyElement) {
alert.setHeaderText(REMOVE + " " + ((ContingencyElement) item.getValue()).getId() + " ?");
}
Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(type -> {
if (type == ButtonType.OK) {
remove();
} else {
alert.close();
}
});
}
Expand All @@ -241,7 +278,7 @@ private void remove() {
}

private ContextMenu createContingencyElementMenu() {
MenuItem removeItem = new MenuItem(RESOURCE_BUNDLE.getString("Remove"));
MenuItem removeItem = new MenuItem(REMOVE);
removeItem.setOnAction(event -> remove());
return new ContextMenu(removeItem);
}
Expand Down

0 comments on commit 6af6c60

Please sign in to comment.