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/src/main/java/envoy/client/ui/ContactSearchSceneControlle...

118 lines
3.1 KiB
Java
Raw Normal View History

package envoy.client.ui;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import envoy.client.event.SendEvent;
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
*/
public class ContactSearchSceneController {
@FXML
private Button backButton;
@FXML
private Button clearButton;
@FXML
private Button searchButton;
@FXML
private TextField searchBar;
@FXML
private ListView<Contact> contactList;
private SceneContext sceneContext;
private static EventBus eventBus = EventBus.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ChatSceneController.class);
/**
* @param sceneContext enables the user to return to the chat scene
* @since Envoy Client v0.1-beta
*/
public void initializeData(SceneContext sceneContext) { this.sceneContext = sceneContext; }
@FXML
private void initialize() {
contactList.setCellFactory(e -> new UserListCell());
eventBus.register(ContactSearchResult.class, response -> Optional.of(response.get()).ifPresent(list -> contactList.getItems().addAll(list)));
}
/**
* 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() {
Optional.ofNullable(contactList.getSelectionModel().getSelectedItems()).ifPresent(contacts -> contacts.forEach(contact -> {
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);
}));
}
@FXML
private void backButtonClicked() { sceneContext.pop(); }
}