2020-06-08 10:36:30 +02:00
|
|
|
package envoy.client.ui.controller;
|
2020-06-07 20:46:04 +02:00
|
|
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2020-06-09 15:41:01 +02:00
|
|
|
import javafx.application.Platform;
|
2020-06-07 20:46:04 +02:00
|
|
|
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;
|
2020-06-07 20:46:04 +02:00
|
|
|
|
2020-06-09 21:22:45 +02:00
|
|
|
import envoy.client.data.LocalDB;
|
2020-06-07 20:46:04 +02:00
|
|
|
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-07-13 19:02:40 +02:00
|
|
|
import envoy.data.User;
|
2020-06-07 20:46:04 +02:00
|
|
|
import envoy.event.ElementOperation;
|
|
|
|
import envoy.event.EventBus;
|
2020-06-20 10:00:38 +02:00
|
|
|
import envoy.event.contact.ContactOperation;
|
2020-07-13 19:02:40 +02:00
|
|
|
import envoy.event.contact.UserSearchRequest;
|
|
|
|
import envoy.event.contact.UserSearchResult;
|
2020-06-07 20:46:04 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
|
|
|
/**
|
2020-07-13 19:02:40 +02:00
|
|
|
* 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>
|
2020-06-07 20:46:04 +02:00
|
|
|
* Project: <strong>envoy-client</strong><br>
|
2020-07-13 19:02:40 +02:00
|
|
|
* File: <strong>ContactSearchScene.java</strong><br>
|
2020-06-07 20:46:04 +02:00
|
|
|
* Created: <strong>07.06.2020</strong><br>
|
|
|
|
*
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-08 10:36:30 +02:00
|
|
|
public class ContactSearchScene {
|
2020-06-07 20:46:04 +02:00
|
|
|
|
|
|
|
@FXML
|
2020-06-28 22:30:14 +02:00
|
|
|
private ClearableTextField searchBar;
|
2020-06-07 20:46:04 +02:00
|
|
|
|
|
|
|
@FXML
|
2020-07-13 19:02:40 +02:00
|
|
|
private ListView<User> userList;
|
2020-06-07 20:46:04 +02:00
|
|
|
|
|
|
|
private SceneContext sceneContext;
|
|
|
|
|
2020-06-09 21:22:45 +02:00
|
|
|
private LocalDB localDB;
|
|
|
|
|
2020-06-07 20:46:04 +02:00
|
|
|
private static EventBus eventBus = EventBus.getInstance();
|
2020-06-08 10:36:30 +02:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
|
2020-06-07 20:46:04 +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 to which new contacts are added
|
2020-06-07 20:46:04 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-09 21:22:45 +02:00
|
|
|
public void initializeData(SceneContext sceneContext, LocalDB localDB) {
|
|
|
|
this.sceneContext = sceneContext;
|
|
|
|
this.localDB = localDB;
|
|
|
|
}
|
2020-06-07 20:46:04 +02:00
|
|
|
|
|
|
|
@FXML
|
|
|
|
private void initialize() {
|
2020-07-13 19:02:40 +02:00
|
|
|
userList.setCellFactory(new ContactListCellFactory<>());
|
|
|
|
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()); }));
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2020-07-13 19:02:40 +02:00
|
|
|
if (!text.isBlank()) eventBus.dispatch(new SendEvent(new UserSearchRequest(text)));
|
|
|
|
else userList.getItems().clear();
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2020-07-13 19:02:40 +02:00
|
|
|
userList.getItems().clear();
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-13 19:16:48 +02:00
|
|
|
* Sends an {@link ContactOperation} for the selected user to the
|
2020-06-07 20:46:04 +02:00
|
|
|
* server.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
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");
|
2020-07-13 19:02:40 +02:00
|
|
|
alert.setHeaderText("Add the user " + user.getName() + " to your contact list?");
|
2020-07-04 15:27:42 +02:00
|
|
|
// 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 -> {
|
2020-07-13 19:02:40 +02:00
|
|
|
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));
|
|
|
|
// Updates the UI
|
|
|
|
eventBus.dispatch(event);
|
2020-07-13 19:02:40 +02:00
|
|
|
logger.log(Level.INFO, "Added user " + user);
|
2020-07-04 15:27:42 +02:00
|
|
|
}));
|
2020-06-09 15:41:01 +02:00
|
|
|
}
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 21:22:45 +02:00
|
|
|
@FXML
|
|
|
|
private void newGroupButtonClicked() {
|
|
|
|
sceneContext.load(SceneContext.SceneInfo.GROUP_CREATION_SCENE);
|
|
|
|
sceneContext.<GroupCreationScene>getController().initializeData(sceneContext, localDB);
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:46:04 +02:00
|
|
|
@FXML
|
|
|
|
private void backButtonClicked() { sceneContext.pop(); }
|
|
|
|
}
|