Added nice error handling when creating groups insted of alert

This commit is contained in:
Maximilian P. Käfer 2020-08-23 22:29:13 +02:00
parent 1d03128744
commit 46a883dda9
3 changed files with 98 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import javafx.application.Platform;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.HBox;
import envoy.client.data.Chat; import envoy.client.data.Chat;
import envoy.client.data.LocalDB; import envoy.client.data.LocalDB;
@ -44,15 +45,31 @@ public class GroupCreationTab {
@FXML @FXML
private Button createButton; private Button createButton;
@FXML
private Button cancelButton;
@FXML @FXML
private TextArea groupNameField; private TextArea groupNameField;
@FXML @FXML
private ListView<User> userList; private ListView<User> userList;
@FXML
private Label errorMessageLabel;
@FXML
private Button proceedDupButton;
@FXML
private Button cancelDupButton;
@FXML
private HBox errorProceedBox;
private LocalDB localDB; private LocalDB localDB;
private String name;
private static final EventBus eventBus = EventBus.getInstance(); private static final EventBus eventBus = EventBus.getInstance();
@FXML @FXML
@ -103,20 +120,37 @@ public class GroupCreationTab {
*/ */
@FXML @FXML
private void createButtonClicked() { private void createButtonClicked() {
final var name = groupNameField.getText(); name = groupNameField.getText();
if (!Bounds.isValidContactName(name)) { if (!Bounds.isValidContactName(name)) {
new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); errorMessageLabel.setPrefHeight(30);
errorMessageLabel.setMinHeight(30);
errorMessageLabel.setMaxHeight(30);
errorMessageLabel.setText("The group name is not valid!");
groupNameField.clear(); groupNameField.clear();
} else if (groupNameAlreadyPresent(name)) { } else if (groupNameAlreadyPresent(name)) {
final var alert = new Alert(AlertType.WARNING, "You already have a group with that name.", ButtonType.OK, ButtonType.CANCEL); errorMessageLabel.setPrefHeight(30);
alert.setTitle("Create Group?"); errorMessageLabel.setMinHeight(30);
alert.setHeaderText("Proceed?"); errorMessageLabel.setMaxHeight(30);
alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> createGroup(name)); errorMessageLabel.setText("Name does already exist! Proceed anyways?");
proceedDupButton.setPrefHeight(30);
proceedDupButton.setMinHeight(30);
proceedDupButton.setMaxHeight(30);
cancelDupButton.setPrefHeight(30);
cancelDupButton.setMinHeight(30);
cancelDupButton.setMaxHeight(30);
errorProceedBox.setPrefHeight(30);
errorProceedBox.setMinHeight(30);
errorProceedBox.setMaxHeight(30);
createButton.setDisable(true);
cancelButton.setDisable(true);
} else { } else {
new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait();
createGroup(name); createGroup(name);
eventBus.dispatch(new BackEvent());
errorMessageLabel.setPrefHeight(0);
errorMessageLabel.setMinHeight(0);
errorMessageLabel.setMaxHeight(0);
groupNameField.clear();
} }
eventBus.dispatch(new BackEvent());
} }
/** /**
@ -150,4 +184,44 @@ public class GroupCreationTab {
@FXML @FXML
private void backButtonClicked() { eventBus.dispatch(new BackEvent()); } private void backButtonClicked() { eventBus.dispatch(new BackEvent()); }
@FXML
private void proceedOnNameDuplication() {
createButton.setDisable(false);
cancelButton.setDisable(false);
createGroup(name);
eventBus.dispatch(new BackEvent());
errorMessageLabel.setPrefHeight(0);
errorMessageLabel.setMinHeight(0);
errorMessageLabel.setMaxHeight(0);
proceedDupButton.setPrefHeight(0);
proceedDupButton.setMinHeight(0);
proceedDupButton.setMaxHeight(0);
cancelDupButton.setPrefHeight(0);
cancelDupButton.setMinHeight(0);
cancelDupButton.setMaxHeight(0);
errorProceedBox.setPrefHeight(0);
errorProceedBox.setMinHeight(0);
errorProceedBox.setMaxHeight(0);
groupNameField.clear();
}
@FXML
private void cancelOnNameDuplication() {
createButton.setDisable(false);
cancelButton.setDisable(false);
errorMessageLabel.setPrefHeight(0);
errorMessageLabel.setMinHeight(0);
errorMessageLabel.setMaxHeight(0);
proceedDupButton.setPrefHeight(0);
proceedDupButton.setMinHeight(0);
proceedDupButton.setMaxHeight(0);
cancelDupButton.setPrefHeight(0);
cancelDupButton.setMinHeight(0);
cancelDupButton.setMaxHeight(0);
errorProceedBox.setPrefHeight(0);
errorProceedBox.setMinHeight(0);
errorProceedBox.setMaxHeight(0);
groupNameField.clear();
}
} }

View File

@ -82,3 +82,8 @@
-fx-background-insets : 4.0, 0.0, 0.0; -fx-background-insets : 4.0, 0.0, 0.0;
-fx-background-radius : 2.0em; -fx-background-radius : 2.0em;
} }
#proceedButton {
-fx-text-fill: white;
-fx-background-color: transparent;
}

View File

@ -37,16 +37,23 @@
<Insets left="10.0" right="10.0" top="5.0" /> <Insets left="10.0" right="10.0" top="5.0" />
</VBox.margin> </VBox.margin>
</Label> </Label>
<Label id="infoLabel-error" fx:id="errorMessageLabel" maxHeight="0.0" minHeight="0.0" prefHeight="0.0" textAlignment="CENTER" textFill="RED" VBox.vgrow="ALWAYS" />
<HBox fx:id="errorProceedBox" alignment="TOP_CENTER" maxHeight="0.0" minHeight="0.0" prefHeight="0.0" prefWidth="316.0" spacing="5.0">
<children>
<Button id="proceedButton" fx:id="proceedDupButton" maxHeight="0.0" minHeight="0.0" mnemonicParsing="false" onAction="#proceedOnNameDuplication" prefHeight="0.0" text="Proceed" />
<Button id="proceedButton" fx:id="cancelDupButton" maxHeight="0.0" minHeight="0.0" mnemonicParsing="false" onAction="#cancelOnNameDuplication" prefHeight="0.0" text="Cancel" />
</children>
</HBox>
<HBox id="underline" alignment="TOP_CENTER" prefWidth="200.0" spacing="5.0"> <HBox id="underline" alignment="TOP_CENTER" prefWidth="200.0" spacing="5.0">
<children> <children>
<Button fx:id="createButton" onAction="#createButtonClicked" maxHeight="30.0" maxWidth="-Infinity" minHeight="30.0" mnemonicParsing="false" prefHeight="30.0" text="Create" /> <Button fx:id="createButton" maxHeight="30.0" maxWidth="-Infinity" minHeight="30.0" mnemonicParsing="false" onAction="#createButtonClicked" prefHeight="30.0" text="Create" />
<Button maxHeight="30.0" maxWidth="-Infinity" minHeight="30.0" mnemonicParsing="false" onAction="#backButtonClicked" prefHeight="30.0" text="Cancel" /> <Button fx:id="cancelButton" maxHeight="30.0" maxWidth="-Infinity" minHeight="30.0" mnemonicParsing="false" onAction="#backButtonClicked" prefHeight="30.0" text="Cancel" />
</children> </children>
<VBox.margin> <VBox.margin>
<Insets left="10.0" right="10.0" /> <Insets left="10.0" right="10.0" />
</VBox.margin> </VBox.margin>
<padding> <padding>
<Insets bottom="17.0" top="5.0" /> <Insets bottom="10.0" top="5.0" />
</padding> </padding>
</HBox> </HBox>
<Label text="Select Group Members" textAlignment="CENTER" textFill="WHITE"> <Label text="Select Group Members" textAlignment="CENTER" textFill="WHITE">
@ -54,7 +61,7 @@
<Font size="15.0" /> <Font size="15.0" />
</font> </font>
<VBox.margin> <VBox.margin>
<Insets top="5" bottom="5.0" /> <Insets bottom="5.0" top="5" />
</VBox.margin> </VBox.margin>
</Label> </Label>
<ListView id="chatList" fx:id="userList" focusTraversable="false" onMouseClicked="#userListClicked" prefWidth="316.0" VBox.vgrow="ALWAYS"> <ListView id="chatList" fx:id="userList" focusTraversable="false" onMouseClicked="#userListClicked" prefWidth="316.0" VBox.vgrow="ALWAYS">