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