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-09 15:41:01 +02:00
|
|
|
import javafx.scene.control.*;
|
|
|
|
import javafx.scene.control.Alert.AlertType;
|
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-09 15:49:11 +02:00
|
|
|
import envoy.client.ui.ContactListCell;
|
2020-06-09 21:22:45 +02:00
|
|
|
import envoy.client.ui.SceneContext;
|
2020-06-07 20:46:04 +02:00
|
|
|
import envoy.data.Contact;
|
|
|
|
import envoy.event.ElementOperation;
|
|
|
|
import envoy.event.EventBus;
|
|
|
|
import envoy.event.contact.ContactOperationEvent;
|
|
|
|
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
|
|
|
|
private Button backButton;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private Button clearButton;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private Button searchButton;
|
|
|
|
|
2020-06-09 21:22:45 +02:00
|
|
|
@FXML
|
|
|
|
private Button newGroupButton;
|
|
|
|
|
2020-06-07 20:46:04 +02:00
|
|
|
@FXML
|
|
|
|
private TextField searchBar;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private ListView<Contact> contactList;
|
|
|
|
|
|
|
|
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
|
|
|
|
* @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-06-09 15:49:11 +02:00
|
|
|
contactList.setCellFactory(e -> new ContactListCell());
|
2020-06-09 15:41:01 +02:00
|
|
|
eventBus.register(ContactSearchResult.class, response -> Platform.runLater(() -> {
|
|
|
|
contactList.getItems().clear();
|
|
|
|
contactList.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
|
|
|
|
private void checkClearButton() {
|
|
|
|
final var containsContent = searchBar.getText().strip().isEmpty();
|
|
|
|
clearButton.setDisable(containsContent);
|
|
|
|
searchButton.setDisable(containsContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a {@link ContactSearchRequest} to the server.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
|
|
|
private void suggestContacts() { eventBus.dispatch(new SendEvent(new ContactSearchRequest(searchBar.getText()))); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
searchBar.setText(null);
|
|
|
|
contactList.getItems().clear();
|
|
|
|
clearButton.setDisable(true);
|
|
|
|
searchButton.setDisable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an {@link ContactOperationEvent} for every selected contact to the
|
|
|
|
* server.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
|
|
|
private void contactListClicked() {
|
2020-06-09 15:41:01 +02:00
|
|
|
final var contact = contactList.getSelectionModel().getSelectedItem();
|
|
|
|
if (contact != null) {
|
|
|
|
final var alert = new Alert(AlertType.CONFIRMATION);
|
|
|
|
alert.setTitle("Add Contact to Contact List");
|
|
|
|
alert.setHeaderText("Add the user " + contact.getName() + " to your contact list?");
|
|
|
|
alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> {
|
|
|
|
final var event = new ContactOperationEvent(contact, ElementOperation.ADD);
|
|
|
|
// Sends the event to the server
|
|
|
|
eventBus.dispatch(new SendEvent(event));
|
|
|
|
// Updates the UI
|
|
|
|
eventBus.dispatch(event);
|
|
|
|
logger.log(Level.INFO, "Added contact " + contact);
|
|
|
|
});
|
|
|
|
}
|
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(); }
|
|
|
|
}
|