This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/client/src/main/java/envoy/client/ui/controller/ContactSearchScene.java

138 lines
4.4 KiB
Java
Raw Normal View History

package envoy.client.ui.controller;
import java.util.logging.Level;
import java.util.logging.Logger;
2020-06-09 15:41:01 +02:00
import javafx.application.Platform;
import javafx.fxml.FXML;
2020-06-28 22:30:14 +02:00
import javafx.scene.control.Alert;
2020-06-09 15:41:01 +02:00
import javafx.scene.control.Alert.AlertType;
2020-06-28 22:30:14 +02:00
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;
import envoy.client.data.LocalDB;
import envoy.client.event.SendEvent;
2020-06-28 22:30:14 +02:00
import envoy.client.ui.ClearableTextField;
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;
import envoy.data.User;
import envoy.event.ElementOperation;
import envoy.event.EventBus;
import envoy.event.contact.ContactOperation;
import envoy.event.contact.UserSearchRequest;
import envoy.event.contact.UserSearchResult;
import envoy.util.EnvoyLog;
/**
* Provides a search bar in which a user name (substring) can be entered. The
* users with a matching name are then displayed inside a list view. A
* {@link UserSearchRequest} is sent on every keystroke.
* <p>
* <i>The actual search algorithm is implemented on the server.
* <p>
* To create a group, a button is available that loads the
* {@link GroupCreationScene}.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>ContactSearchScene.java</strong><br>
* Created: <strong>07.06.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Client v0.1-beta
*/
public class ContactSearchScene {
@FXML
2020-06-28 22:30:14 +02:00
private ClearableTextField searchBar;
@FXML
private ListView<User> userList;
private SceneContext sceneContext;
private LocalDB localDB;
private static EventBus eventBus = EventBus.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
/**
* @param sceneContext enables the user to return to the chat scene
* @param localDB the local database to which new contacts are added
* @since Envoy Client v0.1-beta
*/
public void initializeData(SceneContext sceneContext, LocalDB localDB) {
this.sceneContext = sceneContext;
this.localDB = localDB;
}
@FXML
private void initialize() {
2020-07-13 22:08:08 +02:00
userList.setCellFactory(new ListCellFactory<>(ContactControl::new));
searchBar.setClearButtonListener(e -> { searchBar.getTextField().clear(); userList.getItems().clear(); });
eventBus.register(UserSearchResult.class,
2020-07-13 19:16:48 +02:00
response -> Platform.runLater(() -> { userList.getItems().clear(); userList.getItems().addAll(response.get()); }));
}
/**
* Disables the clear and search button if no text is present in the search bar.
*
* @since Envoy Client v0.1-beta
*/
@FXML
2020-06-28 22:30:14 +02:00
private void sendRequest() {
final var text = searchBar.getTextField().getText().strip();
if (!text.isBlank()) eventBus.dispatch(new SendEvent(new UserSearchRequest(text)));
else userList.getItems().clear();
}
/**
* Clears the text in the search bar and the items shown in the list.
* Additionally disables both clear and search button.
*
* @since Envoy Client v0.1-beta
*/
@FXML
private void clear() {
2020-06-28 22:30:14 +02:00
searchBar.getTextField().setText(null);
userList.getItems().clear();
}
/**
2020-07-13 19:16:48 +02:00
* Sends an {@link ContactOperation} for the selected user to the
* server.
*
* @since Envoy Client v0.1-beta
*/
@FXML
2020-07-13 19:16:48 +02:00
private void userListClicked() {
final var user = userList.getSelectionModel().getSelectedItem();
if (user != null) {
2020-06-09 15:41:01 +02:00
final var alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Add Contact to Contact List");
alert.setHeaderText("Add the user " + user.getName() + " to your contact list?");
// Normally, this would be total BS (we are already on the FX Thread), however
// it could be proven that the creation of this dialog wrapped in
// Platform.runLater is less error-prone than without it
Platform.runLater(() -> alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> {
final var event = new ContactOperation(user, ElementOperation.ADD);
2020-06-09 15:41:01 +02:00
// Sends the event to the server
eventBus.dispatch(new SendEvent(event));
// Removes the chosen user and updates the UI
userList.getItems().remove(user);
2020-06-09 15:41:01 +02:00
eventBus.dispatch(event);
logger.log(Level.INFO, "Added user " + user);
}));
2020-06-09 15:41:01 +02:00
}
}
@FXML
private void newGroupButtonClicked() {
sceneContext.load(SceneContext.SceneInfo.GROUP_CREATION_SCENE);
sceneContext.<GroupCreationScene>getController().initializeData(sceneContext, localDB);
}
@FXML
private void backButtonClicked() { sceneContext.pop(); }
}