feat(post): refresh de la liste des messages on-cick, ajout classe PostVector

This commit is contained in:
bouclyma 2024-12-11 12:42:16 +01:00
parent e29b2c17b2
commit 3e352ef801
4 changed files with 238 additions and 1 deletions

View file

@ -23,6 +23,7 @@ import net.synedra.validatorfx.Check;
import net.synedra.validatorfx.TooltipWrapper;
import net.synedra.validatorfx.Validator;
import rtgre.chat.graphisme.ContactListViewCell;
import rtgre.chat.graphisme.PostListViewCell;
import rtgre.modeles.Contact;
import rtgre.modeles.ContactMap;
import rtgre.modeles.Message;
@ -196,6 +197,7 @@ public class ChatController implements Initializable {
}
private void initPostListView() {
try {
postListView.setCellFactory(postListView -> new PostListViewCell(this));
postListView.setItems(postsObservableList);
} catch (Exception e) {
LOGGER.severe(e.getMessage());
@ -216,12 +218,20 @@ public class ChatController implements Initializable {
return login;
}
public Contact getContact() {
return contact;
}
public ContactMap getContactsMap() {
return contactMap;
}
void handleContactSelection(Contact contactSelected) {
if (contactSelected != null) {
LOGGER.info("Clic sur " + contactSelected);
}
Post postSys = new Post("system", contactSelected.getLogin(), "Bienvenue dans la discussion avec " + contactSelected.getLogin());
postsObservableList.add(postSys);
postListView.refresh();
}
}

View file

@ -0,0 +1,88 @@
package rtgre.chat.graphisme;
import rtgre.chat.ChatController;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ListCell;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import rtgre.modeles.Contact;
import rtgre.modeles.Post;
import java.awt.image.BufferedImage;
import java.util.Date;
public class PostListViewCell extends ListCell<Post> {
/** Controller de l'application */
ChatController controller;
public PostListViewCell(ChatController controller) {
this.controller = controller;
}
/**
* Callback déclenchée à chaque modification d'un objet d'une liste d'observable.
*
* @param post Le post
* @param empty La liste de cellule doit-elle être complètement remise à zéro ?
*/
@Override
protected void updateItem(Post post, boolean empty) {
super.updateItem(post, empty);
if (empty) {
setGraphic(null);
}
else {
updatePost(post);
}
}
void updatePost(Post post) {
Text datetimeText = new Text("\n%1$td/%1$tm/%1$tY %1$tH:%1$tM:%1$tS\n".formatted(new Date(post.getTimestamp())));
datetimeText.setFont(Font.font(null, FontPosture.ITALIC, 8));
Text nicknameText = new Text(post.getFrom() + ": ");
nicknameText.setFill(Color.DARKBLUE);
nicknameText.setFont(Font.font(null, FontWeight.BOLD, 14));
nicknameText.setFill(Color.BLUEVIOLET);
Text msgText = new Text(post.getBody());
// L'émetteur du message
Contact c = this.controller.getContactsMap().get(post.getFrom());
ImageView avatar = new ImageView();
if (c != null) {
avatar = new ImageView(SwingFXUtils.toFXImage((BufferedImage) c.getAvatar(), null));
avatar.setFitWidth(20);
avatar.setFitHeight(20);
}
TextFlow tf = new TextFlow((Node) datetimeText, avatar, nicknameText, msgText);
tf.maxWidthProperty().bind(getListView().widthProperty().multiply(0.8));
HBox hBox = new HBox(tf);
hBox.maxWidthProperty().bind(getListView().widthProperty());
String login;
try {
login = this.controller.getContact().getLogin();
}
catch (Exception e) {
login = "???";
}
if (post.getFrom().equals(login)) {
tf.setBackground(Background.fill(Color.web("#EEF")));
hBox.setAlignment(Pos.CENTER_RIGHT);
} else {
tf.setBackground(Background.fill(Color.web("#FEE")));
hBox.setAlignment(Pos.CENTER_LEFT);
}
setGraphic(hBox);
getListView().scrollTo(getListView().getItems().size() - 1);
}
}

View file

@ -0,0 +1,27 @@
package rtgre.modeles;
import java.util.UUID;
import java.util.Vector;
public class PostVector extends Vector<Post> {
public Post getPostById(UUID uuid) {
for (Post post : this) {
if (post.id == uuid) {
return post;
}
}
return null;
}
public Vector<Post> getPostsSince(long timestamp) {
Vector<Post> posts = new Vector<>();
for (Post post : this) {
if (post.timestamp > timestamp) {
posts.add(post);
}
}
return posts;
}
}