2020-06-08 10:36:30 +02:00
|
|
|
package envoy.client.ui.controller;
|
2020-06-07 20:46:04 +02:00
|
|
|
|
2020-09-08 20:41:01 +02:00
|
|
|
import java.util.logging.*;
|
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-08-23 12:36:43 +02:00
|
|
|
import javafx.scene.control.*;
|
2020-06-09 15:41:01 +02:00
|
|
|
import javafx.scene.control.Alert.AlertType;
|
2020-06-07 20:46:04 +02:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import dev.kske.eventbus.*;
|
|
|
|
|
|
|
|
import envoy.data.User;
|
|
|
|
import envoy.event.ElementOperation;
|
|
|
|
import envoy.event.contact.*;
|
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.client.data.Context;
|
|
|
|
import envoy.client.event.BackEvent;
|
2020-09-23 17:03:32 +02:00
|
|
|
import envoy.client.helper.AlertHelper;
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.client.net.Client;
|
|
|
|
import envoy.client.ui.control.ContactControl;
|
|
|
|
import envoy.client.ui.listcell.ListCellFactory;
|
2020-09-08 20:41:01 +02:00
|
|
|
|
2020-06-07 20:46:04 +02:00
|
|
|
/**
|
2020-10-19 18:17:51 +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.
|
2020-07-13 19:02:40 +02:00
|
|
|
* <p>
|
|
|
|
* <i>The actual search algorithm is implemented on the server.
|
|
|
|
* <p>
|
2020-10-19 18:17:51 +02:00
|
|
|
* To create a group, a button is available that loads the {@link GroupCreationTab}.
|
2020-06-07 20:46:04 +02:00
|
|
|
*
|
|
|
|
* @author Leon Hofmeister
|
2020-08-23 12:36:43 +02:00
|
|
|
* @author Maximilian Käfer
|
2020-06-07 20:46:04 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-09-08 20:41:01 +02:00
|
|
|
public class ContactSearchTab implements EventListener {
|
2020-06-07 20:46:04 +02:00
|
|
|
|
|
|
|
@FXML
|
2020-08-23 12:36:43 +02:00
|
|
|
private TextArea 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
|
|
|
|
2020-07-18 18:20:52 +02:00
|
|
|
private User currentlySelectedUser;
|
|
|
|
|
2020-09-23 17:03:32 +02:00
|
|
|
private final Alert alert = new Alert(AlertType.CONFIRMATION);
|
|
|
|
|
2020-09-25 14:29:23 +02:00
|
|
|
private static final Client client = Context.getInstance().getClient();
|
2020-07-18 18:20:52 +02:00
|
|
|
private static final EventBus eventBus = EventBus.getInstance();
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
|
2020-06-07 20:46:04 +02:00
|
|
|
|
|
|
|
@FXML
|
|
|
|
private void initialize() {
|
2020-09-08 20:41:01 +02:00
|
|
|
eventBus.registerListener(this);
|
2020-07-13 22:08:08 +02:00
|
|
|
userList.setCellFactory(new ListCellFactory<>(ContactControl::new));
|
2020-09-23 17:03:32 +02:00
|
|
|
alert.setTitle("Add User?");
|
2020-09-08 20:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Event
|
|
|
|
private void onUserSearchResult(UserSearchResult result) {
|
2020-10-19 18:17:51 +02:00
|
|
|
Platform.runLater(() -> {
|
|
|
|
userList.getItems().clear();
|
|
|
|
userList.getItems().addAll(result.get());
|
|
|
|
});
|
2020-09-08 20:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Event
|
2020-10-11 23:04:25 +02:00
|
|
|
private void onUserOperation(UserOperation operation) {
|
2020-09-08 20:41:01 +02:00
|
|
|
final var contact = operation.get();
|
2020-10-19 18:17:51 +02:00
|
|
|
if (operation.getOperationType() == ElementOperation.ADD)
|
|
|
|
Platform.runLater(() -> {
|
|
|
|
userList.getItems().remove(contact);
|
|
|
|
if (currentlySelectedUser != null && currentlySelectedUser.equals(contact)
|
|
|
|
&& alert.isShowing())
|
|
|
|
alert.close();
|
|
|
|
});
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-25 22:00:34 +02:00
|
|
|
* If text is present, sends a request to the server.
|
2020-06-07 20:46:04 +02:00
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
2020-06-28 22:30:14 +02:00
|
|
|
private void sendRequest() {
|
2020-08-23 12:36:43 +02:00
|
|
|
final var text = searchBar.getText().strip();
|
2020-10-19 18:17:51 +02:00
|
|
|
if (!text.isBlank())
|
|
|
|
client.send(new UserSearchRequest(text));
|
|
|
|
else
|
|
|
|
userList.getItems().clear();
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Clears the text in the search bar and the items shown in the list. Additionally disables both
|
|
|
|
* clear and search button.
|
2020-06-07 20:46:04 +02:00
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
@FXML
|
|
|
|
private void clear() {
|
2020-08-23 12:36:43 +02:00
|
|
|
searchBar.setText(null);
|
2020-07-13 19:02:40 +02:00
|
|
|
userList.getItems().clear();
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Sends an {@link UserOperation} for the selected user to the server.
|
2020-06-07 20:46:04 +02:00
|
|
|
*
|
|
|
|
* @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-09-23 17:03:32 +02:00
|
|
|
currentlySelectedUser = user;
|
2020-10-19 18:17:51 +02:00
|
|
|
alert.setContentText(
|
|
|
|
"Add user " + currentlySelectedUser.getName() + " to your contacts?");
|
2020-09-23 17:03:32 +02:00
|
|
|
AlertHelper.confirmAction(alert, this::addAsContact);
|
2020-06-09 15:41:01 +02:00
|
|
|
}
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 22:00:34 +02:00
|
|
|
private void addAsContact() {
|
|
|
|
|
2020-09-23 17:03:32 +02:00
|
|
|
// Sends the event to the server
|
2020-10-11 23:04:25 +02:00
|
|
|
final var event = new UserOperation(currentlySelectedUser, ElementOperation.ADD);
|
2020-09-27 15:20:19 +02:00
|
|
|
client.send(event);
|
2020-09-25 22:00:34 +02:00
|
|
|
|
2020-09-23 17:03:32 +02:00
|
|
|
// Removes the chosen user and updates the UI
|
|
|
|
userList.getItems().remove(currentlySelectedUser);
|
|
|
|
eventBus.dispatch(event);
|
|
|
|
logger.log(Level.INFO, "Added user " + currentlySelectedUser);
|
|
|
|
}
|
|
|
|
|
2020-08-23 17:11:41 +02:00
|
|
|
@FXML
|
2020-10-13 23:51:02 +02:00
|
|
|
private void backButtonClicked() {
|
|
|
|
searchBar.setText("");
|
|
|
|
eventBus.dispatch(new BackEvent());
|
|
|
|
}
|
2020-06-07 20:46:04 +02:00
|
|
|
}
|