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

ergonomics in contingencies editor : bind keys + fix save after rename #76

Merged
merged 3 commits into from
Oct 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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