Message.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package common;
  2. import java.awt.Color;
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import javax.swing.text.SimpleAttributeSet;
  7. import javax.swing.text.StyleConstants;
  8. import server.Server;
  9. @SuppressWarnings("serial")
  10. public class Message implements java.io.Serializable {
  11. public enum MessageType {
  12. PM, NORMAL, WARNING, ERROR, COMMAND, INFO
  13. }
  14. public MessageType messType = MessageType.NORMAL;
  15. public String content = "", channel = "", sender = "";
  16. public SimpleAttributeSet style = new SimpleAttributeSet();
  17. public String[] usersOnline;
  18. public boolean preInfo = true;
  19. public Message(String channel, String send, String con, MessageType messType) {
  20. this.sender = send;
  21. this.channel = channel;
  22. this.content = con;
  23. this.messType = messType;
  24. usersOnline = Server.getUsersOnline();
  25. }
  26. public Message(String sender, String con) {
  27. this("", sender, con, MessageType.NORMAL);
  28. }
  29. public Message(String con) {
  30. this("Info", "SERVER", con, MessageType.INFO);
  31. }
  32. public Message(String con, MessageType messType, boolean preInfo) { //TODO Needs to include Server.getUsersOnline() to prevent NullPointerException
  33. this.content = con;
  34. this.preInfo = preInfo;
  35. this.messType = messType;
  36. if (preInfo)
  37. usersOnline = Server.getUsersOnline();
  38. else
  39. usersOnline = null;
  40. }
  41. public boolean validate() {
  42. if (content.equals("") || content == null) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. @Override
  48. public String toString() {
  49. switch (messType) {
  50. case COMMAND:
  51. StyleConstants.setForeground(style, Color.GREEN);
  52. break;
  53. case ERROR:
  54. StyleConstants.setForeground(style, Color.RED);
  55. break;
  56. case INFO:
  57. StyleConstants.setForeground(style, Color.BLUE);
  58. break;
  59. case NORMAL:
  60. break;
  61. case PM:
  62. StyleConstants.setForeground(style, Color.GRAY);
  63. break;
  64. case WARNING:
  65. StyleConstants.setForeground(style, Color.YELLOW);
  66. break;
  67. default:
  68. break;
  69. }
  70. DateFormat dateFormat = new SimpleDateFormat("[HH:mm:ss]");
  71. Date time = new Date();
  72. String timestamp = dateFormat.format(time);
  73. String preInfoStr = timestamp + "<" + channel + ">" + sender + ": ";
  74. if (preInfo)
  75. return preInfoStr + content;
  76. else
  77. return content;
  78. }
  79. }