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-07-12 13:07:46 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-06-07 20:46:04 +02:00
|
|
|
|
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-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;
|
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-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-06-07 20:46:04 +02:00
|
|
|
import envoy.event.contact.ContactSearchRequest;
|
|
|
|
import envoy.event.contact.ContactSearchResult;
|
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>ContactSearchSceneController.java</strong><br>
|
|
|
|
* 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-12 17:10:22 +02:00
|
|
|
private ListView<Chat> chatList;
|
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-12 17:10:22 +02:00
|
|
|
chatList.setCellFactory(ContactListCellFactory::new);
|
|
|
|
searchBar.setClearButtonListener(e -> { searchBar.getTextField().clear(); chatList.getItems().clear(); });
|
2020-06-13 22:36:52 +02:00
|
|
|
eventBus.register(ContactSearchResult.class,
|
2020-07-10 22:41:59 +02:00
|
|
|
response -> Platform.runLater(() -> {
|
2020-07-12 17:10:22 +02:00
|
|
|
chatList.getItems().clear();
|
|
|
|
chatList.getItems().addAll(response.get().stream().map(Chat::new).collect(Collectors.toList()));
|
2020-07-10 22:41:59 +02:00
|
|
|
}));
|
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();
|
|
|
|
if (!text.isBlank()) eventBus.dispatch(new SendEvent(new ContactSearchRequest(text)));
|
2020-07-12 17:10:22 +02:00
|
|
|
else chatList.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-12 17:10:22 +02:00
|
|
|
chatList.getItems().clear();
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-20 10:00:38 +02:00
|
|
|
* Sends an {@link ContactOperation} for every selected contact to the
|
2020-06-07 20:46:04 +02:00
|
|
|
* server.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
2020-07-12 17:10:22 +02:00
|
|
|
private void chatListClicked() {
|
|
|
|
final var chat = chatList.getSelectionModel().getSelectedItem();
|
2020-07-10 22:41:59 +02:00
|
|
|
if (chat != 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-10 22:41:59 +02:00
|
|
|
alert.setHeaderText("Add the user " + chat.getRecipient().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-10 22:41:59 +02:00
|
|
|
final var event = new ContactOperation(chat.getRecipient(), 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-12 13:07:46 +02:00
|
|
|
logger.log(Level.INFO, "Added contact " + chat.getRecipient());
|
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(); }
|
|
|
|
}
|