2020-06-09 21:22:45 +02:00
|
|
|
package envoy.client.ui.controller;
|
|
|
|
|
2020-07-13 19:02:40 +02:00
|
|
|
import static java.util.function.Predicate.not;
|
|
|
|
|
2020-06-09 21:22:45 +02:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import javafx.application.Platform;
|
|
|
|
import javafx.fxml.FXML;
|
|
|
|
import javafx.scene.control.*;
|
2020-06-27 09:03:59 +02:00
|
|
|
import javafx.scene.control.Alert.AlertType;
|
2020-06-09 21:22:45 +02:00
|
|
|
|
2020-07-10 22:41:59 +02:00
|
|
|
import envoy.client.data.Chat;
|
2020-06-09 21:22:45 +02:00
|
|
|
import envoy.client.data.LocalDB;
|
|
|
|
import envoy.client.event.SendEvent;
|
2020-06-28 22:30:14 +02:00
|
|
|
import envoy.client.ui.ClearableTextField;
|
2020-06-09 21:22:45 +02:00
|
|
|
import envoy.client.ui.SceneContext;
|
2020-07-13 22:08:08 +02:00
|
|
|
import envoy.client.ui.listcell.ContactControl;
|
|
|
|
import envoy.client.ui.listcell.ListCellFactory;
|
2020-07-16 17:47:59 +02:00
|
|
|
import envoy.data.Contact;
|
|
|
|
import envoy.data.Group;
|
2020-07-13 19:02:40 +02:00
|
|
|
import envoy.data.User;
|
2020-06-09 21:22:45 +02:00
|
|
|
import envoy.event.EventBus;
|
2020-06-20 10:00:38 +02:00
|
|
|
import envoy.event.GroupCreation;
|
2020-06-27 09:03:59 +02:00
|
|
|
import envoy.util.Bounds;
|
2020-06-09 21:22:45 +02:00
|
|
|
|
|
|
|
/**
|
2020-07-13 19:02:40 +02:00
|
|
|
* 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).
|
|
|
|
* <p>
|
|
|
|
* 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).
|
|
|
|
* <p>
|
2020-06-09 21:22:45 +02:00
|
|
|
* Project: <strong>envoy-client</strong><br>
|
2020-07-13 19:02:40 +02:00
|
|
|
* File: <strong>GroupCreationScene.java</strong><br>
|
2020-06-09 21:22:45 +02:00
|
|
|
* Created: <strong>07.06.2020</strong><br>
|
|
|
|
*
|
|
|
|
* @author Maximilian Käfer
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public class GroupCreationScene {
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private Button createButton;
|
|
|
|
|
|
|
|
@FXML
|
2020-06-28 22:30:14 +02:00
|
|
|
private ClearableTextField groupNameField;
|
2020-06-09 21:22:45 +02:00
|
|
|
|
|
|
|
@FXML
|
2020-07-13 19:02:40 +02:00
|
|
|
private ListView<User> userList;
|
2020-06-09 21:22:45 +02:00
|
|
|
|
|
|
|
private SceneContext sceneContext;
|
|
|
|
|
2020-07-16 17:47:59 +02:00
|
|
|
private LocalDB localDB;
|
|
|
|
|
2020-06-27 09:03:59 +02:00
|
|
|
private static final EventBus eventBus = EventBus.getInstance();
|
2020-06-09 21:22:45 +02:00
|
|
|
|
|
|
|
@FXML
|
|
|
|
private void initialize() {
|
2020-07-13 22:08:08 +02:00
|
|
|
userList.setCellFactory(new ListCellFactory<>(ContactControl::new));
|
2020-07-13 19:02:40 +02:00
|
|
|
userList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
|
2020-06-28 22:44:34 +02:00
|
|
|
groupNameField.setClearButtonListener(e -> { groupNameField.getTextField().clear(); createButton.setDisable(true); });
|
2020-06-09 21:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param sceneContext enables the user to return to the chat scene
|
2020-06-20 10:00:38 +02:00
|
|
|
* @param localDB the local database from which potential group members can
|
|
|
|
* be selected
|
2020-06-09 21:22:45 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public void initializeData(SceneContext sceneContext, LocalDB localDB) {
|
2020-07-16 17:47:59 +02:00
|
|
|
this.sceneContext = sceneContext;
|
|
|
|
this.localDB = localDB;
|
2020-07-13 19:02:40 +02:00
|
|
|
Platform.runLater(() -> userList.getItems()
|
2020-07-10 22:41:59 +02:00
|
|
|
.addAll(localDB.getChats()
|
|
|
|
.stream()
|
2020-07-13 19:02:40 +02:00
|
|
|
.map(Chat::getRecipient)
|
|
|
|
.filter(User.class::isInstance)
|
|
|
|
.filter(not(localDB.getUser()::equals))
|
|
|
|
.map(User.class::cast)
|
2020-07-10 22:41:59 +02:00
|
|
|
.collect(Collectors.toList())));
|
2020-06-09 21:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-27 09:03:59 +02:00
|
|
|
* Enables the {@code createButton} if at least one contact is selected.
|
2020-06-28 22:30:14 +02:00
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
2020-07-13 19:16:48 +02:00
|
|
|
private void userListClicked() {
|
2020-07-13 19:02:40 +02:00
|
|
|
createButton.setDisable(userList.getSelectionModel().isEmpty() || groupNameField.getTextField().getText().isBlank());
|
2020-06-28 22:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks, whether the {@code createButton} can be enabled because text is
|
2020-07-13 19:02:40 +02:00
|
|
|
* present in the text field.
|
2020-06-28 22:30:14 +02:00
|
|
|
*
|
2020-06-27 09:03:59 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
2020-06-28 22:30:14 +02:00
|
|
|
private void textUpdated() { createButton.setDisable(groupNameField.getTextField().getText().isBlank()); }
|
2020-06-27 09:03:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a {@link GroupCreation} to the server and closes this scene.
|
|
|
|
* <p>
|
|
|
|
* If the given group name is not valid, an error is displayed instead.
|
2020-06-09 21:22:45 +02:00
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
2020-06-27 09:03:59 +02:00
|
|
|
private void createButtonClicked() {
|
2020-06-28 22:30:14 +02:00
|
|
|
final var name = groupNameField.getTextField().getText();
|
2020-06-27 09:03:59 +02:00
|
|
|
if (!Bounds.isValidContactName(name)) {
|
|
|
|
new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait();
|
2020-06-28 22:30:14 +02:00
|
|
|
groupNameField.getTextField().clear();
|
2020-07-16 17:47:59 +02:00
|
|
|
} else if (groupNameAlreadyPresent(name)) {
|
2020-07-16 20:54:15 +02:00
|
|
|
final var alert = new Alert(AlertType.WARNING, "You already have a group with that name.", ButtonType.OK, ButtonType.CANCEL);
|
2020-07-16 17:47:59 +02:00
|
|
|
alert.setTitle("Create Group?");
|
|
|
|
alert.setHeaderText("Proceed?");
|
|
|
|
alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> createGroup(name));
|
2020-06-27 09:03:59 +02:00
|
|
|
} else {
|
|
|
|
new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait();
|
2020-07-16 17:47:59 +02:00
|
|
|
createGroup(name);
|
2020-06-27 09:03:59 +02:00
|
|
|
}
|
2020-06-09 21:22:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 17:47:59 +02:00
|
|
|
/**
|
|
|
|
* Creates a new group with the given name and all selected members.<br>
|
|
|
|
* 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()))));
|
|
|
|
sceneContext.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(groupName -> groupName.equals(newName));
|
|
|
|
}
|
|
|
|
|
2020-06-09 21:22:45 +02:00
|
|
|
@FXML
|
|
|
|
private void backButtonClicked() { sceneContext.pop(); }
|
|
|
|
}
|