package envoy.client.ui.controller; import static java.util.function.Predicate.not; import java.util.stream.Collectors; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.layout.HBox; import envoy.client.data.*; import envoy.client.event.*; import envoy.client.ui.listcell.*; import envoy.data.*; import envoy.event.GroupCreation; import envoy.event.contact.ContactOperation; import envoy.util.Bounds; import dev.kske.eventbus.*; /** * Provides a group creation interface. A group name can be entered in the text * field at the top. Available users (local chat recipients) are displayed * inside a list and can be selected (multiple selection available). *
* When the group creation button is pressed, a {@link GroupCreation} is sent to * the server. This controller enforces a valid group name and a non-empty * member list (excluding the client user). *
* Project: envoy-client
* If the given group name is not valid, an error is displayed instead.
*
* @since Envoy Client v0.1-beta
*/
@FXML
private void createButtonClicked() {
name = groupNameField.getText();
if (!Bounds.isValidContactName(name)) {
setErrorMessageLabelSize(30);
errorMessageLabel.setText("The group name is not valid!");
groupNameField.clear();
} else if (groupNameAlreadyPresent(name)) {
setErrorMessageLabelSize(30);
errorMessageLabel.setText("Name does already exist! Proceed anyways?");
setProcessPaneSize(30);
createButton.setDisable(true);
cancelButton.setDisable(true);
} else {
createGroup(name);
eventBus.dispatch(new BackEvent());
// Restoring the original design as tabs will always be reused
setErrorMessageLabelSize(0);
groupNameField.clear();
}
}
/**
* Creates a new group with the given name and all selected members.
* File: GroupCreationScene.java
* Created: 07.06.2020
*
* @author Maximilian Käfer
* @since Envoy Client v0.1-beta
*/
public class GroupCreationTab implements EventListener {
@FXML
private Button createButton;
@FXML
private Button cancelButton;
@FXML
private TextArea groupNameField;
@FXML
private ListView
* Additionally pops the scene automatically.
*
* @param name the chosen group name
* @since Envoy Client v0.1-beta
*/
private void createGroup(String name) {
eventBus.dispatch(new SendEvent(
new GroupCreation(name, userList.getSelectionModel().getSelectedItems().stream().map(User::getID).collect(Collectors.toSet()))));
}
/**
* Returns true if the proposed group name is already present in the users
* {@code LocalDB}.
*
* @param newName the chosen group name
* @return true if this name is already present
* @since Envoy Client v0.1-beta
*/
public boolean groupNameAlreadyPresent(String newName) {
return localDB.getChats()
.stream()
.map(Chat::getRecipient)
.filter(Group.class::isInstance)
.map(Contact::getName)
.anyMatch(newName::equals);
}
@FXML
private void backButtonClicked() {
eventBus.dispatch(new BackEvent());
setErrorMessageLabelSize(0);
setProcessPaneSize(0);
}
@FXML
private void proceedOnNameDuplication() {
createButton.setDisable(false);
cancelButton.setDisable(false);
createGroup(name);
eventBus.dispatch(new BackEvent());
setErrorMessageLabelSize(0);
setProcessPaneSize(0);
groupNameField.clear();
}
@FXML
private void cancelOnNameDuplication() {
createButton.setDisable(false);
cancelButton.setDisable(false);
setErrorMessageLabelSize(0);
setProcessPaneSize(0);
groupNameField.clear();
}
private void setErrorMessageLabelSize(int value) {
errorMessageLabel.setPrefHeight(value);
errorMessageLabel.setMinHeight(value);
errorMessageLabel.setMaxHeight(value);
}
private void setProcessPaneSize(int value) {
proceedDuplicateButton.setPrefHeight(value);
proceedDuplicateButton.setMinHeight(value);
proceedDuplicateButton.setMaxHeight(value);
cancelDuplicateButton.setPrefHeight(value);
cancelDuplicateButton.setMinHeight(value);
cancelDuplicateButton.setMaxHeight(value);
errorProceedBox.setPrefHeight(value);
errorProceedBox.setMinHeight(value);
errorProceedBox.setMaxHeight(value);
}
@Event
private void onContactOperation(ContactOperation operation) {
if (operation.get() instanceof User) Platform.runLater(() -> {
switch (operation.getOperationType()) {
case ADD:
userList.getItems().add((User) operation.get());
break;
case REMOVE:
userList.getItems().removeIf(u -> u.equals(operation.get()));
break;
}
});
}
}