feat(Room): Récupéraiton des salons depuis le serveur, usine à cellule et affichage dans le client

This commit is contained in:
Emi Boucly 2025-01-08 16:07:18 +01:00
parent d99405160d
commit 29cf0a3b43
6 changed files with 149 additions and 1 deletions

View file

@ -26,6 +26,7 @@ import net.synedra.validatorfx.Validator;
import org.json.JSONObject; import org.json.JSONObject;
import rtgre.chat.graphisme.ContactListViewCell; import rtgre.chat.graphisme.ContactListViewCell;
import rtgre.chat.graphisme.PostListViewCell; import rtgre.chat.graphisme.PostListViewCell;
import rtgre.chat.graphisme.RoomListViewCell;
import rtgre.chat.net.ChatClient; import rtgre.chat.net.ChatClient;
import rtgre.modeles.*; import rtgre.modeles.*;
@ -68,9 +69,11 @@ public class ChatController implements Initializable {
private ContactMap contactMap = new ContactMap(); private ContactMap contactMap = new ContactMap();
private ObservableList<Contact> contactObservableList = FXCollections.observableArrayList(); private ObservableList<Contact> contactObservableList = FXCollections.observableArrayList();
private ObservableList<Post> postsObservableList = FXCollections.observableArrayList(); private ObservableList<Post> postsObservableList = FXCollections.observableArrayList();
Validator validatorLogin = new Validator(); private Validator validatorLogin = new Validator();
private ChatClient client = null; private ChatClient client = null;
private PostVector postVector; private PostVector postVector;
private RoomMap roomMap = new RoomMap();
private ObservableList<Room> roomObservableList = FXCollections.observableArrayList();
@Override @Override
@ -103,8 +106,11 @@ public class ChatController implements Initializable {
initContactListView(); initContactListView();
initPostListView(); initPostListView();
initRoomListView();
contactsListView.getSelectionModel().selectedItemProperty().addListener( contactsListView.getSelectionModel().selectedItemProperty().addListener(
(observableValue, previous, selected) -> handleContactSelection((Contact) selected)); (observableValue, previous, selected) -> handleContactSelection((Contact) selected));
roomsListView.getSelectionModel().selectedItemProperty().addListener(
(observableValue, previous, selected) -> handleContactSelection((Contact) selected));
validatorLogin.createCheck() validatorLogin.createCheck()
.dependsOn("login", loginTextField.textProperty()) .dependsOn("login", loginTextField.textProperty())
@ -124,6 +130,15 @@ public class ChatController implements Initializable {
/* -------------------------------------- */ /* -------------------------------------- */
} }
private void initRoomListView() {
try {
roomsListView.setCellFactory(roomListView -> new RoomListViewCell());
roomsListView.setItems(roomObservableList);
} catch (Exception e) {
LOGGER.severe(e.getMessage());
}
}
private void onActionSend(ActionEvent actionEvent) { private void onActionSend(ActionEvent actionEvent) {
String login = getSelectedContactLogin(); String login = getSelectedContactLogin();
if (login != null) { if (login != null) {
@ -173,6 +188,7 @@ public class ChatController implements Initializable {
this.contact.setConnected(true); this.contact.setConnected(true);
client.sendAuthEvent(contact); client.sendAuthEvent(contact);
client.sendListRoomEvent();
client.sendEvent(new rtgre.modeles.Event(rtgre.modeles.Event.LIST_CONTACTS, new JSONObject())); client.sendEvent(new rtgre.modeles.Event(rtgre.modeles.Event.LIST_CONTACTS, new JSONObject()));
initContactListView(); initContactListView();
@ -291,12 +307,22 @@ public class ChatController implements Initializable {
handleContEvent(event.getContent()); handleContEvent(event.getContent());
} else if (event.getType().equals(rtgre.modeles.Event.POST)) { } else if (event.getType().equals(rtgre.modeles.Event.POST)) {
handlePostEvent(event.getContent()); handlePostEvent(event.getContent());
} else if (event.getType().equals(rtgre.modeles.Event.ROOM)) {
handleRoomEvent(event.getContent());
} else { } else {
LOGGER.warning("Unhandled event type: " + event.getType()); LOGGER.warning("Unhandled event type: " + event.getType());
this.client.close(); this.client.close();
} }
} }
private void handleRoomEvent(JSONObject content) {
LOGGER.info(content.toString());
Room room = new Room(content.getString("room"));
roomMap.add(room);
roomObservableList.add(room);
roomsListView.refresh();
}
private void handlePostEvent(JSONObject content) { private void handlePostEvent(JSONObject content) {
System.out.println(content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin())); System.out.println(content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()));
if (content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()) || if (content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()) ||

View file

@ -0,0 +1,87 @@
package rtgre.chat.graphisme;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.control.ListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import rtgre.modeles.Contact;
import rtgre.modeles.Room;
import java.awt.image.BufferedImage;
import static rtgre.chat.ChatApplication.LOGGER;
public class RoomListViewCell extends ListCell<Room> {
@Override
protected void updateItem(Room room, boolean empty) {
super.updateItem(room, empty);
if (empty) {
setGraphic(null);
}
else {
// Cas d'un contact
updateRoom(room);
}
}
public Color colorFromName(String roomName) {
switch (roomName) {
case "#all":
return Color.CADETBLUE;
case "#juniors":
return Color.FORESTGREEN;
case "#ducks":
return Color.GOLD;
case "#mice":
return Color.LIGHTPINK;
default:
return Color.GRAY;
}
}
private void updateRoom(Room room) {
LOGGER.finest("Mise à jour de " + room);
String unreadCountNotif = (room.getUnreadCount() == 0) ? "" : " (%d)".formatted(room.getUnreadCount());
LOGGER.finest("unread: %s %s".formatted(room.getRoomName(), unreadCountNotif));
Text roomText = new Text(room.getRoomName() + unreadCountNotif);
roomText.setFont(Font.font(null, 12)); // FontWeight.BOLD, 14));
roomText.setFill(Color.BLACK);
ImageView view = new ImageView();
Rectangle rectangle = new Rectangle(15, 15, colorFromName(room.getRoomName()));
rectangle.setArcHeight(8.0d);
rectangle.setArcWidth(8.0d);
StackPane stack = new StackPane();
stack.getChildren().addAll(rectangle, new Text(room.abbreviation()));
/*
if (contact.getAvatar() != null) {
avatar = SwingFXUtils.toFXImage((BufferedImage) contact.getAvatar(), null);
view = new ImageView(avatar);
}*/
HBox temp = new HBox(stack);
temp.setAlignment(Pos.CENTER_LEFT);
HBox.setHgrow(roomText, Priority.ALWAYS);
HBox hBox = new HBox(temp, roomText, view);
hBox.setSpacing(5.0);
hBox.setAlignment(Pos.CENTER_LEFT);
setGraphic(hBox);
}
}

View file

@ -68,6 +68,11 @@ public class ChatClient extends ClientTCP {
sendEvent(listPostEvent); sendEvent(listPostEvent);
} }
public void sendListRoomEvent() {
Event listRoomEvent = new Event(Event.LIST_ROOMS, new JSONObject());
sendEvent(listRoomEvent);
}
public void sendQuitEvent() { public void sendQuitEvent() {
Event quitEvent = new Event(Event.QUIT, new JSONObject()); Event quitEvent = new Event(Event.QUIT, new JSONObject());
sendEvent(quitEvent); sendEvent(quitEvent);

View file

@ -13,6 +13,8 @@ public class Event {
public static final String LIST_CONTACTS = "LSTC"; public static final String LIST_CONTACTS = "LSTC";
public static final String LIST_POSTS = "LSTP"; public static final String LIST_POSTS = "LSTP";
public static final String SYSTEM = "SYST"; public static final String SYSTEM = "SYST";
public static final String LIST_ROOMS = "LSTR";
public static final String ROOM = "ROOM";
private final String type; private final String type;
private final JSONObject content; private final JSONObject content;

View file

@ -56,4 +56,8 @@ public class Room {
public String toJson() { public String toJson() {
return this.toJsonObject().toString(); return this.toJsonObject().toString();
} }
public int getUnreadCount() {
return 0;
}
} }

View file

@ -24,6 +24,7 @@ public class ChatServer {
private Vector<ChatClientHandler> clientList; private Vector<ChatClientHandler> clientList;
private PostVector postVector; private PostVector postVector;
private ContactMap contactMap; private ContactMap contactMap;
private RoomMap roomMap;
static { static {
try { try {
@ -53,7 +54,9 @@ public class ChatServer {
clientList = new Vector<>(); clientList = new Vector<>();
contactMap = new ContactMap(); contactMap = new ContactMap();
postVector = new PostVector(); postVector = new PostVector();
roomMap = new RoomMap();
contactMap.loadDefaultContacts(); contactMap.loadDefaultContacts();
roomMap.loadDefaultRooms();
} }
public void close() throws IOException { public void close() throws IOException {
@ -254,6 +257,10 @@ public class ChatServer {
doListPost(event.getContent()); doListPost(event.getContent());
LOGGER.info("Sending Posts"); LOGGER.info("Sending Posts");
return true; return true;
} else if (event.getType().equals(Event.LIST_ROOMS)) {
doListRoom(event.getContent());
LOGGER.info("Sending Rooms");
return true;
} else if (event.getType().equals(Event.QUIT)) { } else if (event.getType().equals(Event.QUIT)) {
LOGGER.info("Déconnexion"); LOGGER.info("Déconnexion");
return false; return false;
@ -263,6 +270,22 @@ public class ChatServer {
} }
} }
private void doListRoom(JSONObject content) {
System.out.println(contactMap.getContact(user.getLogin()).isConnected());
if (contactMap.getContact(user.getLogin()).isConnected()) {
for (Room room: roomMap.values()) {
System.out.println(room);
try {
System.out.println(new Event("ROOM", room.toJsonObject()).toJson());
send(new Event("ROOM", room.toJsonObject()).toJson());
} catch (IOException e) {
System.out.println(e.getMessage());
throw new IllegalStateException();
}
}
}
}
private void doListPost(JSONObject content) throws JSONException, IllegalStateException { private void doListPost(JSONObject content) throws JSONException, IllegalStateException {
if (contactMap.getContact(user.getLogin()).isConnected()) { if (contactMap.getContact(user.getLogin()).isConnected()) {
@ -371,6 +394,7 @@ public class ChatServer {
sock.close(); sock.close();
removeClient(this); removeClient(this);
user.setConnected(false); user.setConnected(false);
contactMap.get(user.getLogin()).setConnected(false);
sendEventToAllContacts(new Event(Event.CONT, user.toJsonObject())); sendEventToAllContacts(new Event(Event.CONT, user.toJsonObject()));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);