1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail.event;
  6. import java.util.*;
  7. import javax.mail.*;
  8. /**
  9. * This class notifies changes in the number of messages in a folder. <p>
  10. *
  11. * Note that some folder types may only deliver MessageCountEvents at
  12. * certain times or after certain operations. IMAP in particular will
  13. * only notify the client of MessageCountEvents when a client issues a
  14. * new command.
  15. * Refer to RFC 2060 <A HREF="http://www.ietf.org/rfc/rfc2060.txt">
  16. * http://www.ietf.org/rfc/rfc2060.txt</A> for details.
  17. * A client may want "poll" the folder by occasionally calling the
  18. * <code>getMessageCount</code> or <code>isConnected</code> methods
  19. * to solicit any such notifications.
  20. *
  21. * @author John Mani
  22. */
  23. public class MessageCountEvent extends MailEvent {
  24. /** The messages were added to their folder */
  25. public static final int ADDED = 1;
  26. /** The messages were removed from their folder */
  27. public static final int REMOVED = 2;
  28. /**
  29. * The event type.
  30. *
  31. * @serial
  32. */
  33. protected int type;
  34. /**
  35. * If true, this event is the result of an explicit
  36. * expunge by this client, and the messages in this
  37. * folder have been renumbered to account for this.
  38. * If false, this event is the result of an expunge
  39. * by external sources.
  40. *
  41. * @serial
  42. */
  43. protected boolean removed;
  44. /**
  45. * The messages.
  46. */
  47. transient protected Message[] msgs;
  48. /**
  49. * Constructor.
  50. * @param source The containing folder
  51. * @param type The event type
  52. * @param removed If true, this event is the result of an explicit
  53. * expunge by this client, and the messages in this
  54. * folder have been renumbered to account for this.
  55. * If false, this event is the result of an expunge
  56. * by external sources.
  57. *
  58. * @param msgs The messages added/removed
  59. */
  60. public MessageCountEvent(Folder folder, int type,
  61. boolean removed, Message[] msgs) {
  62. super(folder);
  63. this.type = type;
  64. this.removed = removed;
  65. this.msgs = msgs;
  66. }
  67. /**
  68. * Return the type of this event.
  69. * @return type
  70. */
  71. public int getType() {
  72. return type;
  73. }
  74. /**
  75. * Indicates whether this event is the result of an explicit
  76. * expunge by this client, or due to an expunge from external
  77. * sources. If <code>true</code>, this event is due to an
  78. * explicit expunge and hence all remaining messages in this
  79. * folder have been renumbered. If <code>false</code>, this event
  80. * is due to an external expunge. <p>
  81. *
  82. * Note that this method is valid only if the type of this event
  83. * is <code>REMOVED</code>
  84. */
  85. public boolean isRemoved() {
  86. return removed;
  87. }
  88. /**
  89. * Return the array of messages added or removed.
  90. * @return array of messages
  91. */
  92. public Message[] getMessages() {
  93. return msgs;
  94. }
  95. /**
  96. * Invokes the appropriate MessageCountListener method.
  97. */
  98. public void dispatch(Object listener) {
  99. if (type == ADDED)
  100. ((MessageCountListener)listener).messagesAdded(this);
  101. else // REMOVED
  102. ((MessageCountListener)listener).messagesRemoved(this);
  103. }
  104. }