001package edu.pdx.cs410J.net;
002
003import java.io.Serializable;
004import java.text.*;
005import java.util.*;
006
007/**
008 * This class represents a message that is passed between two
009 * <code>ChatSession</code>s.
010 */
011public class ChatMessage implements Serializable {
012
013  private String sender;   // Name of the sender
014  private Date   date;     // When the messsage was sent
015  private String text;     // Contents of message
016
017  /**
018   * Creates a new <code>ChatMessage</code> with the current time.
019   */
020  public ChatMessage(String sender, String text) {
021    this.sender = sender;
022    this.date = new Date();
023    this.text = text;
024  }
025
026  /**
027   * Returns <code>true</code> if this is the last message sent
028   */
029  public boolean isLastMessage() {
030    return this.text.trim().equals("bye");
031  }
032
033  /**
034   * Returns a textual representation of this <code>ChatMessage</code>
035   * that is suitable for displaying in a <code>ChatSession</code>.
036   */
037  public String toString() {
038    DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM);
039    StringBuffer sb = new StringBuffer();
040    sb.append(this.sender);
041    sb.append(" [");
042    sb.append(df.format(this.date));
043    sb.append("]> ");
044    sb.append(this.text);
045    return sb.toString();
046  }
047
048}