Package client

Class ChatModel

java.lang.Object
client.ChatModel

public class ChatModel extends Object
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 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

      public void setUser(User user)
      Sets the current user and fetches the available chat rooms for that user.
      Parameters:
      user - The User to log in.
    • setChats

      public void setChats(ArrayList<String> chats)
      Updates the local list of chat rooms and notifies all listeners.
      Parameters:
      chats - An ArrayList of chat room names.
    • getChats

      public ArrayList<String> getChats()
      Fetches the latest list of chat rooms from the server for the current user.
      Returns:
      The updated list of chat room names.
    • setCurrentChat

      public void setCurrentChat(Chat currentChat)
      Sets the currently active chat room and notifies listeners to update the view.
      Parameters:
      currentChat - The Chat room to be displayed.
    • getCurrentChat

      public Chat getCurrentChat()
      Returns:
      The currently selected Chat room.
    • getUser

      public User getUser()
      Returns:
      The User currently using the application.
    • addChat

      public void addChat(String chat)
      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

      public void addListener(ChatModelListener listener)
      Registers a listener to receive model change notifications.
      Parameters:
      listener - The ChatModelListener to add.
    • removeListener

      public void removeListener(ChatModelListener listener)
      Removes a listener from receiving notifications.
      Parameters:
      listener - The ChatModelListener to remove.
    • addMessage

      public void addMessage(Message message, Chat chat)
      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

      public void sendMessage(String text, ClientWebSocketHandler ws)
      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

      public void sendImageMessage(String filepath, ClientWebSocketHandler ws)
      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

      public Message createMessage(String m)
      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

      public Message createImageMessage(String hash)
      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.