Package client
Class ChatModel
java.lang.Object
client.ChatModel
ChatModel - The Model component of the MVC pattern.
This class is responsible for the application's business logic and state management.
Communicates with the server via the ConnectionHandler.
As an Observable, it notifies registered ChatModelListeners
whenever the state changes (e.g., new messages or loaded chats).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates a new chat room and links the current user to it.voidaddListener(ChatModelListener listener) Registers a listener to receive model change notifications.voidaddMessage(Message message, Chat chat) Sends a message to a specific chat room.createImageMessage(String hash) Method to create an image-based message.Method to create a standard text message with the current timestamp and user.getChats()Fetches the latest list of chat rooms from the server for the current user.getUser()voidLeaves the currently active chat room by setting activeChat to null.voidremoveListener(ChatModelListener listener) Removes a listener from receiving notifications.voidsendImageMessage(String filepath, ClientWebSocketHandler ws) Sends an image message to the server for the currently active chat room.voidsendMessage(String text, ClientWebSocketHandler ws) Sends a message to the server for the currently active chat room.voidUpdates the local list of chat rooms and notifies all listeners.voidsetCurrentChat(Chat currentChat) Sets the currently active chat room and notifies listeners to update the view.voidSets the current user and fetches the available chat rooms for that user.
-
Constructor Details
-
ChatModel
public ChatModel()Constructs a new ChatModel. Initializes message and listener lists and sets up the server connection parameters.
-
-
Method Details
-
leaveChat
public void leaveChat()Leaves the currently active chat room by setting activeChat to null. -
setUser
Sets the current user and fetches the available chat rooms for that user.- Parameters:
user- The User to log in.
-
setChats
Updates the local list of chat rooms and notifies all listeners.- Parameters:
chats- An ArrayList of chat room names.
-
getChats
Fetches the latest list of chat rooms from the server for the current user.- Returns:
- The updated list of chat room names.
-
setCurrentChat
Sets the currently active chat room and notifies listeners to update the view.- Parameters:
currentChat- The Chat room to be displayed.
-
getCurrentChat
- Returns:
- The currently selected Chat room.
-
getUser
- Returns:
- The User currently using the application.
-
addChat
Creates a new chat room and links the current user to it. If a chat room with the specified name already exists, join instead. Automatically refreshes the chat list upon success.- Parameters:
chat- The name of the chat room to add/join.
-
addListener
Registers a listener to receive model change notifications.- Parameters:
listener- The ChatModelListener to add.
-
removeListener
Removes a listener from receiving notifications.- Parameters:
listener- The ChatModelListener to remove.
-
addMessage
Sends a message to a specific chat room. Updates the local message list and triggers a listener notification with the updated chat history from the server.- Parameters:
message- The Message object to send.chat- The Chat destination for the m: essage.
-
sendMessage
Sends a message to the server for the currently active chat room.- Parameters:
text- The text content of the message to send.ws- The ClientWebSocketHandler used to send the message to the server.
-
sendImageMessage
Sends an image message to the server for the currently active chat room.- Parameters:
filepath- The file path of the image to send.ws- The ClientWebSocketHandler used to send the message to the server.
-
createMessage
Method to create a standard text message with the current timestamp and user.- Parameters:
m- The text content of the message.- Returns:
- A new Message object configured for text.
-
createImageMessage
Method to create an image-based message.- Parameters:
hash- The unique identifier or path for the image file.- Returns:
- A new Message object configured as an image.
-