Add validation to actions panel

This commit is contained in:
0xboobface 2018-12-09 21:35:35 +01:00
parent 86ae660218
commit 878b25c55c
2 changed files with 28 additions and 2 deletions

View File

@ -23,9 +23,11 @@ public class Wizard extends BorderPane {
private Button prev;
private Button finish;
private boolean cancelled = true;
private Runnable validator;
public Wizard(Stage stage, Pane... pages) {
public Wizard(Stage stage, Runnable validator, Pane... pages) {
this.stage = stage;
this.validator = validator;
this.pages = pages;
if (pages.length == 0) {
@ -49,6 +51,14 @@ public class Wizard extends BorderPane {
cancel.setOnAction(evt -> stage.close());
finish = new Button("Finish");
finish.setOnAction(evt -> {
if(validator != null) {
try {
validator.run();
} catch(IllegalStateException e) {
Dialogs.showError("Settings invalid", e.getMessage(), null);
return;
}
}
cancelled = false;
stage.close();
});

View File

@ -16,6 +16,7 @@ import ctbrec.Config;
import ctbrec.Model;
import ctbrec.OS;
import ctbrec.Recording;
import ctbrec.StringUtil;
import ctbrec.event.Event;
import ctbrec.event.EventBusHolder;
import ctbrec.event.EventHandler;
@ -128,7 +129,7 @@ public class ActionSettingsPanel extends TitledPane {
dialog.setTitle("New Action");
InputStream icon = getClass().getResourceAsStream("/icon.png");
dialog.getIcons().add(new Image(icon));
Wizard root = new Wizard(dialog, actionPane);
Wizard root = new Wizard(dialog, this::validateSettings, actionPane);
Scene scene = new Scene(root, 800, 540);
scene.getStylesheets().addAll(getScene().getStylesheets());
dialog.setScene(scene);
@ -193,6 +194,21 @@ public class ActionSettingsPanel extends TitledPane {
LOG.debug("Registered event handler for {} {}", config.getEvent(), config.getName());
}
private void validateSettings() {
if(StringUtil.isBlank(name.getText())) {
throw new IllegalStateException("Name cannot be empty");
}
if(event.getValue() == Event.Type.MODEL_STATUS_CHANGED && modelState.getValue() == null) {
throw new IllegalStateException("Select a state");
}
if(modelSelectionPane.getSelectedItems().isEmpty() && !modelSelectionPane.isAllSelected()) {
throw new IllegalStateException("Select one or more models or tick off \"all\"");
}
if(!(showNotification.isSelected() || playSound.isSelected() || executeProgram.isSelected())) {
throw new IllegalStateException("No action selected");
}
}
private void delete(ActionEvent evt) {
List<EventHandlerConfiguration> selected = new ArrayList<>(actionTable.getSelectionModel().getSelectedItems());
for (EventHandlerConfiguration config : selected) {