KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > event > ItemEvent


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
8 package java.awt.event;
9
10 import java.awt.Component JavaDoc;
11 import java.awt.AWTEvent JavaDoc;
12 import java.awt.Event JavaDoc;
13 import java.awt.ItemSelectable JavaDoc;
14
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 deselected by the user.
19  * The event is passed to every <code>ItemListener</code> object which
20  * registered to receive such events using the component's
21  * <code>addItemListener</code> method.
22  * <P>
23  * The object that implements the <code>ItemListener</code> interface gets
24  * this <code>ItemEvent</code> when the event occurs. The listener is
25  * spared the details of processing individual mouse movements and mouse
26  * clicks, and can instead process a "meaningful" (semantic) event like
27  * "item selected" or "item deselected".
28  *
29  * @version 1.28 12/19/03
30  * @author Carl Quinn
31  *
32  * @see java.awt.ItemSelectable
33  * @see ItemListener
34  * @see <a HREF="http://java.sun.com/docs/books/tutorial/post1.0/ui/itemlistener.html">Tutorial: Writing an Item Listener</a>
35  * @see <a HREF="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
36  *
37  * @since 1.1
38  */

39 public class ItemEvent extends AWTEvent JavaDoc {
40
41     /**
42      * The first number in the range of ids used for item events.
43      */

44     public static final int ITEM_FIRST = 701;
45
46     /**
47      * The last number in the range of ids used for item events.
48      */

49     public static final int ITEM_LAST = 701;
50
51     /**
52      * This event id indicates that an item's state changed.
53      */

54     public static final int ITEM_STATE_CHANGED = ITEM_FIRST; //Event.LIST_SELECT
55

56     /**
57      * This state-change value indicates that an item was selected.
58      */

59     public static final int SELECTED = 1;
60
61     /**
62      * This state-change-value indicates that a selected item was deselected.
63      */

64     public static final int DESELECTED = 2;
65
66     /**
67      * The item whose selection state has changed.
68      *
69      * @serial
70      * @see #getItem()
71      */

72     Object JavaDoc item;
73
74     /**
75      * <code>stateChange</code> indicates whether the <code>item</code>
76      * was selected or deselected.
77      *
78      * @serial
79      * @see #getStateChange()
80      */

81     int stateChange;
82
83     /*
84      * JDK 1.1 serialVersionUID
85      */

86     private static final long serialVersionUID = -608708132447206933L;
87
88     /**
89      * Constructs an <code>ItemEvent</code> object.
90      * <p>Note that passing in an invalid <code>id</code> results in
91      * unspecified behavior. This method throws an
92      * <code>IllegalArgumentException</code> if <code>source</code>
93      * is <code>null</code>.
94      *
95      * @param source the <code>ItemSelectable</code> object
96      * that originated the event
97      * @param id an integer that identifies the event type
98      * @param item an object -- the item affected by the event
99      * @param stateChange
100      * an integer that indicates whether the item was
101      * selected or deselected
102      * @throws IllegalArgumentException if <code>source</code> is null
103      */

104     public ItemEvent(ItemSelectable JavaDoc source, int id, Object JavaDoc item, int stateChange) {
105         super(source, id);
106     this.item = item;
107         this.stateChange = stateChange;
108     }
109
110     /**
111      * Returns the originator of the event.
112      *
113      * @return the ItemSelectable object that originated the event.
114      */

115     public ItemSelectable JavaDoc getItemSelectable() {
116         return (ItemSelectable JavaDoc)source;
117     }
118
119    /**
120     * Returns the item affected by the event.
121     *
122     * @return the item (object) that was affected by the event
123     */

124     public Object JavaDoc getItem() {
125         return item;
126     }
127
128    /**
129     * Returns the type of state change (selected or deselected).
130     *
131     * @return an integer that indicates whether the item was selected
132     * or deselected
133     *
134     * @see #SELECTED
135     * @see #DESELECTED
136     */

137     public int getStateChange() {
138         return stateChange;
139     }
140
141     /**
142      * Returns a parameter string identifying this item event.
143      * This method is useful for event-logging and for debugging.
144      *
145      * @return a string identifying the event and its attributes
146      */

147     public String JavaDoc paramString() {
148         String JavaDoc typeStr;
149         switch(id) {
150           case ITEM_STATE_CHANGED:
151               typeStr = "ITEM_STATE_CHANGED";
152               break;
153           default:
154               typeStr = "unknown type";
155         }
156
157         String JavaDoc stateStr;
158         switch(stateChange) {
159           case SELECTED:
160               stateStr = "SELECTED";
161               break;
162           case DESELECTED:
163               stateStr = "DESELECTED";
164               break;
165           default:
166               stateStr = "unknown type";
167         }
168         return typeStr + ",item="+item + ",stateChange="+stateStr;
169     }
170
171 }
172
Popular Tags