package envoy.client.ui.controller; import java.util.stream.Collectors; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; import envoy.client.data.Chat; import envoy.client.data.LocalDB; import envoy.client.event.SendEvent; import envoy.client.ui.ClearableTextField; import envoy.client.ui.SceneContext; import envoy.client.ui.listcell.ContactListCellFactory; import envoy.data.Group; import envoy.event.EventBus; import envoy.event.GroupCreation; import envoy.util.Bounds; /** * Project: envoy-client
* File: ContactSearchSceneController.java
* Created: 07.06.2020
* * @author Maximilian Käfer * @since Envoy Client v0.1-beta */ public class GroupCreationScene { @FXML private Button createButton; @FXML private ClearableTextField groupNameField; @FXML private ListView chatList; private SceneContext sceneContext; private static final EventBus eventBus = EventBus.getInstance(); @FXML private void initialize() { chatList.setCellFactory(ContactListCellFactory::new); chatList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); groupNameField.setClearButtonListener(e -> { groupNameField.getTextField().clear(); createButton.setDisable(true); }); } /** * @param sceneContext enables the user to return to the chat scene * @param localDB the local database from which potential group members can * be selected * @since Envoy Client v0.1-beta */ public void initializeData(SceneContext sceneContext, LocalDB localDB) { this.sceneContext = sceneContext; Platform.runLater(() -> chatList.getItems() .addAll(localDB.getChats() .stream() .filter(c -> !(c.getRecipient() instanceof Group)) .filter(c -> c.getRecipient().getID() != localDB.getUser().getID()) .collect(Collectors.toList()))); } /** * Enables the {@code createButton} if at least one contact is selected. * * @since Envoy Client v0.1-beta */ @FXML private void chatListClicked() { createButton.setDisable(chatList.getSelectionModel().isEmpty() || groupNameField.getTextField().getText().isBlank()); } /** * Checks, whether the {@code createButton} can be enabled because text is * present in the textfield. * * @since Envoy Client v0.1-beta */ @FXML private void textUpdated() { createButton.setDisable(groupNameField.getTextField().getText().isBlank()); } /** * Sends a {@link GroupCreation} to the server and closes this scene. *

* If the given group name is not valid, an error is displayed instead. * * @since Envoy Client v0.1-beta */ @FXML private void createButtonClicked() { final var name = groupNameField.getTextField().getText(); if (!Bounds.isValidContactName(name)) { new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); groupNameField.getTextField().clear(); } else { eventBus.dispatch(new SendEvent(new GroupCreation(name, chatList.getSelectionModel().getSelectedItems().stream().map(c -> c.getRecipient().getID()).collect(Collectors.toSet())))); new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait(); sceneContext.pop(); } } @FXML private void backButtonClicked() { sceneContext.pop(); } }