124 lines
3.7 KiB
Java
124 lines
3.7 KiB
Java
package envoy.client.ui.controller;
|
|
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import javafx.application.Platform;
|
|
import javafx.fxml.FXML;
|
|
import javafx.scene.control.Alert;
|
|
import javafx.scene.control.Alert.AlertType;
|
|
import javafx.scene.control.ButtonType;
|
|
import javafx.scene.control.ListView;
|
|
|
|
import envoy.client.data.LocalDB;
|
|
import envoy.client.event.SendEvent;
|
|
import envoy.client.ui.ClearableTextField;
|
|
import envoy.client.ui.SceneContext;
|
|
import envoy.client.ui.listcell.ContactListCellFactory;
|
|
import envoy.data.Contact;
|
|
import envoy.event.ElementOperation;
|
|
import envoy.event.EventBus;
|
|
import envoy.event.contact.ContactOperation;
|
|
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
|
|
*/
|
|
public class ContactSearchScene {
|
|
|
|
@FXML
|
|
private ClearableTextField searchBar;
|
|
|
|
@FXML
|
|
private ListView<Contact> contactList;
|
|
|
|
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() {
|
|
contactList.setCellFactory(ContactListCellFactory::new);
|
|
searchBar.setClearButtonListener(e -> { searchBar.getTextField().clear(); contactList.getItems().clear(); });
|
|
eventBus.register(ContactSearchResult.class,
|
|
response -> Platform.runLater(() -> { contactList.getItems().clear(); contactList.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
|
|
private void sendRequest() {
|
|
final var text = searchBar.getTextField().getText().strip();
|
|
if (!text.isBlank()) eventBus.dispatch(new SendEvent(new ContactSearchRequest(text)));
|
|
else contactList.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() {
|
|
searchBar.getTextField().setText(null);
|
|
contactList.getItems().clear();
|
|
}
|
|
|
|
/**
|
|
* Sends an {@link ContactOperation} for every selected contact to the
|
|
* server.
|
|
*
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
@FXML
|
|
private void contactListClicked() {
|
|
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 ContactOperation(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);
|
|
});
|
|
}
|
|
}
|
|
|
|
@FXML
|
|
private void newGroupButtonClicked() {
|
|
sceneContext.load(SceneContext.SceneInfo.GROUP_CREATION_SCENE);
|
|
sceneContext.<GroupCreationScene>getController().initializeData(sceneContext, localDB);
|
|
}
|
|
|
|
@FXML
|
|
private void backButtonClicked() { sceneContext.pop(); }
|
|
}
|