2020-06-09 21:22:45 +02:00
|
|
|
package envoy.client.ui.controller;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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-01 19:35:15 +02:00
|
|
|
import envoy.client.ui.listcell.ContactListCellFactory;
|
2020-06-09 21:22:45 +02:00
|
|
|
import envoy.data.Contact;
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>ContactSearchSceneController.java</strong><br>
|
|
|
|
* 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
|
|
|
|
private ListView<Contact> contactList;
|
|
|
|
|
|
|
|
private SceneContext sceneContext;
|
|
|
|
|
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-01 19:35:15 +02:00
|
|
|
contactList.setCellFactory(ContactListCellFactory::new);
|
2020-06-09 21:22:45 +02:00
|
|
|
contactList.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-06-20 10:00:38 +02:00
|
|
|
this.sceneContext = sceneContext;
|
2020-06-09 21:22:45 +02:00
|
|
|
Platform.runLater(() -> contactList.getItems()
|
2020-06-27 09:03:59 +02:00
|
|
|
.addAll(localDB.getUsers().values().stream().filter(c -> c.getID() != localDB.getUser().getID()).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
|
|
|
|
private void contactListClicked() {
|
|
|
|
createButton.setDisable(contactList.getSelectionModel().isEmpty() || groupNameField.getTextField().getText().isBlank());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks, whether the {@code createButton} can be enabled because text is
|
|
|
|
* present in the textfield.
|
|
|
|
*
|
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-06-27 09:03:59 +02:00
|
|
|
} else {
|
|
|
|
eventBus.dispatch(new SendEvent(new GroupCreation(name,
|
|
|
|
contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet()))));
|
|
|
|
new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait();
|
|
|
|
sceneContext.pop();
|
|
|
|
}
|
2020-06-09 21:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private void backButtonClicked() { sceneContext.pop(); }
|
|
|
|
}
|