1. /*
  2. * @(#)ItemEvent.java 1.28 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.event;
  8. import java.awt.Component;
  9. import java.awt.AWTEvent;
  10. import java.awt.Event;
  11. import java.awt.ItemSelectable;
  12. /**
  13. * A semantic event which indicates that an item was selected or deselected.
  14. * This high-level event is generated by an ItemSelectable object (such as a
  15. * List) when an item is selected or deselected by the user.
  16. * The event is passed to every <code>ItemListener</code> object which
  17. * registered to receive such events using the component's
  18. * <code>addItemListener</code> method.
  19. * <P>
  20. * The object that implements the <code>ItemListener</code> interface gets
  21. * this <code>ItemEvent</code> when the event occurs. The listener is
  22. * spared the details of processing individual mouse movements and mouse
  23. * clicks, and can instead process a "meaningful" (semantic) event like
  24. * "item selected" or "item deselected".
  25. *
  26. * @version 1.28 12/19/03
  27. * @author Carl Quinn
  28. *
  29. * @see java.awt.ItemSelectable
  30. * @see ItemListener
  31. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/itemlistener.html">Tutorial: Writing an Item Listener</a>
  32. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  33. *
  34. * @since 1.1
  35. */
  36. public class ItemEvent extends AWTEvent {
  37. /**
  38. * The first number in the range of ids used for item events.
  39. */
  40. public static final int ITEM_FIRST = 701;
  41. /**
  42. * The last number in the range of ids used for item events.
  43. */
  44. public static final int ITEM_LAST = 701;
  45. /**
  46. * This event id indicates that an item's state changed.
  47. */
  48. public static final int ITEM_STATE_CHANGED = ITEM_FIRST; //Event.LIST_SELECT
  49. /**
  50. * This state-change value indicates that an item was selected.
  51. */
  52. public static final int SELECTED = 1;
  53. /**
  54. * This state-change-value indicates that a selected item was deselected.
  55. */
  56. public static final int DESELECTED = 2;
  57. /**
  58. * The item whose selection state has changed.
  59. *
  60. * @serial
  61. * @see #getItem()
  62. */
  63. Object item;
  64. /**
  65. * <code>stateChange</code> indicates whether the <code>item</code>
  66. * was selected or deselected.
  67. *
  68. * @serial
  69. * @see #getStateChange()
  70. */
  71. int stateChange;
  72. /*
  73. * JDK 1.1 serialVersionUID
  74. */
  75. private static final long serialVersionUID = -608708132447206933L;
  76. /**
  77. * Constructs an <code>ItemEvent</code> object.
  78. * <p>Note that passing in an invalid <code>id</code> results in
  79. * unspecified behavior. This method throws an
  80. * <code>IllegalArgumentException</code> if <code>source</code>
  81. * is <code>null</code>.
  82. *
  83. * @param source the <code>ItemSelectable</code> object
  84. * that originated the event
  85. * @param id an integer that identifies the event type
  86. * @param item an object -- the item affected by the event
  87. * @param stateChange
  88. * an integer that indicates whether the item was
  89. * selected or deselected
  90. * @throws IllegalArgumentException if <code>source</code> is null
  91. */
  92. public ItemEvent(ItemSelectable source, int id, Object item, int stateChange) {
  93. super(source, id);
  94. this.item = item;
  95. this.stateChange = stateChange;
  96. }
  97. /**
  98. * Returns the originator of the event.
  99. *
  100. * @return the ItemSelectable object that originated the event.
  101. */
  102. public ItemSelectable getItemSelectable() {
  103. return (ItemSelectable)source;
  104. }
  105. /**
  106. * Returns the item affected by the event.
  107. *
  108. * @return the item (object) that was affected by the event
  109. */
  110. public Object getItem() {
  111. return item;
  112. }
  113. /**
  114. * Returns the type of state change (selected or deselected).
  115. *
  116. * @return an integer that indicates whether the item was selected
  117. * or deselected
  118. *
  119. * @see #SELECTED
  120. * @see #DESELECTED
  121. */
  122. public int getStateChange() {
  123. return stateChange;
  124. }
  125. /**
  126. * Returns a parameter string identifying this item event.
  127. * This method is useful for event-logging and for debugging.
  128. *
  129. * @return a string identifying the event and its attributes
  130. */
  131. public String paramString() {
  132. String typeStr;
  133. switch(id) {
  134. case ITEM_STATE_CHANGED:
  135. typeStr = "ITEM_STATE_CHANGED";
  136. break;
  137. default:
  138. typeStr = "unknown type";
  139. }
  140. String stateStr;
  141. switch(stateChange) {
  142. case SELECTED:
  143. stateStr = "SELECTED";
  144. break;
  145. case DESELECTED:
  146. stateStr = "DESELECTED";
  147. break;
  148. default:
  149. stateStr = "unknown type";
  150. }
  151. return typeStr + ",item="+item + ",stateChange="+stateStr;
  152. }
  153. }