mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 11:43:53 +00:00
feat(Room): Récupéraiton des salons depuis le serveur, usine à cellule et affichage dans le client
This commit is contained in:
parent
d99405160d
commit
29cf0a3b43
6 changed files with 149 additions and 1 deletions
|
|
@ -26,6 +26,7 @@ import net.synedra.validatorfx.Validator;
|
|||
import org.json.JSONObject;
|
||||
import rtgre.chat.graphisme.ContactListViewCell;
|
||||
import rtgre.chat.graphisme.PostListViewCell;
|
||||
import rtgre.chat.graphisme.RoomListViewCell;
|
||||
import rtgre.chat.net.ChatClient;
|
||||
import rtgre.modeles.*;
|
||||
|
||||
|
|
@ -68,9 +69,11 @@ public class ChatController implements Initializable {
|
|||
private ContactMap contactMap = new ContactMap();
|
||||
private ObservableList<Contact> contactObservableList = FXCollections.observableArrayList();
|
||||
private ObservableList<Post> postsObservableList = FXCollections.observableArrayList();
|
||||
Validator validatorLogin = new Validator();
|
||||
private Validator validatorLogin = new Validator();
|
||||
private ChatClient client = null;
|
||||
private PostVector postVector;
|
||||
private RoomMap roomMap = new RoomMap();
|
||||
private ObservableList<Room> roomObservableList = FXCollections.observableArrayList();
|
||||
|
||||
|
||||
@Override
|
||||
|
|
@ -103,8 +106,11 @@ public class ChatController implements Initializable {
|
|||
|
||||
initContactListView();
|
||||
initPostListView();
|
||||
initRoomListView();
|
||||
contactsListView.getSelectionModel().selectedItemProperty().addListener(
|
||||
(observableValue, previous, selected) -> handleContactSelection((Contact) selected));
|
||||
roomsListView.getSelectionModel().selectedItemProperty().addListener(
|
||||
(observableValue, previous, selected) -> handleContactSelection((Contact) selected));
|
||||
|
||||
validatorLogin.createCheck()
|
||||
.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) {
|
||||
String login = getSelectedContactLogin();
|
||||
if (login != null) {
|
||||
|
|
@ -173,6 +188,7 @@ public class ChatController implements Initializable {
|
|||
this.contact.setConnected(true);
|
||||
|
||||
client.sendAuthEvent(contact);
|
||||
client.sendListRoomEvent();
|
||||
client.sendEvent(new rtgre.modeles.Event(rtgre.modeles.Event.LIST_CONTACTS, new JSONObject()));
|
||||
|
||||
initContactListView();
|
||||
|
|
@ -291,12 +307,22 @@ public class ChatController implements Initializable {
|
|||
handleContEvent(event.getContent());
|
||||
} else if (event.getType().equals(rtgre.modeles.Event.POST)) {
|
||||
handlePostEvent(event.getContent());
|
||||
} else if (event.getType().equals(rtgre.modeles.Event.ROOM)) {
|
||||
handleRoomEvent(event.getContent());
|
||||
} else {
|
||||
LOGGER.warning("Unhandled event type: " + event.getType());
|
||||
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) {
|
||||
System.out.println(content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()));
|
||||
if (content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()) ||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -68,6 +68,11 @@ public class ChatClient extends ClientTCP {
|
|||
sendEvent(listPostEvent);
|
||||
}
|
||||
|
||||
public void sendListRoomEvent() {
|
||||
Event listRoomEvent = new Event(Event.LIST_ROOMS, new JSONObject());
|
||||
sendEvent(listRoomEvent);
|
||||
}
|
||||
|
||||
public void sendQuitEvent() {
|
||||
Event quitEvent = new Event(Event.QUIT, new JSONObject());
|
||||
sendEvent(quitEvent);
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ public class Event {
|
|||
public static final String LIST_CONTACTS = "LSTC";
|
||||
public static final String LIST_POSTS = "LSTP";
|
||||
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 JSONObject content;
|
||||
|
||||
|
|
|
|||
|
|
@ -56,4 +56,8 @@ public class Room {
|
|||
public String toJson() {
|
||||
return this.toJsonObject().toString();
|
||||
}
|
||||
|
||||
public int getUnreadCount() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public class ChatServer {
|
|||
private Vector<ChatClientHandler> clientList;
|
||||
private PostVector postVector;
|
||||
private ContactMap contactMap;
|
||||
private RoomMap roomMap;
|
||||
|
||||
static {
|
||||
try {
|
||||
|
|
@ -53,7 +54,9 @@ public class ChatServer {
|
|||
clientList = new Vector<>();
|
||||
contactMap = new ContactMap();
|
||||
postVector = new PostVector();
|
||||
roomMap = new RoomMap();
|
||||
contactMap.loadDefaultContacts();
|
||||
roomMap.loadDefaultRooms();
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
|
|
@ -254,6 +257,10 @@ public class ChatServer {
|
|||
doListPost(event.getContent());
|
||||
LOGGER.info("Sending Posts");
|
||||
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)) {
|
||||
LOGGER.info("Déconnexion");
|
||||
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 {
|
||||
|
||||
if (contactMap.getContact(user.getLogin()).isConnected()) {
|
||||
|
|
@ -371,6 +394,7 @@ public class ChatServer {
|
|||
sock.close();
|
||||
removeClient(this);
|
||||
user.setConnected(false);
|
||||
contactMap.get(user.getLogin()).setConnected(false);
|
||||
sendEventToAllContacts(new Event(Event.CONT, user.toJsonObject()));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue