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