blob: 7b37f3ed0c4645a58e11556e3df0a368db59febf [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package java.awt.event;
27
28import java.awt.AWTEvent;
29import java.awt.ItemSelectable;
30
31/**
32 * A semantic event which indicates that an item was selected or deselected.
33 * This high-level event is generated by an ItemSelectable object (such as a
34 * List) when an item is selected or deselected by the user.
35 * The event is passed to every <code>ItemListener</code> object which
36 * registered to receive such events using the component's
37 * <code>addItemListener</code> method.
38 * <P>
39 * The object that implements the <code>ItemListener</code> interface gets
40 * this <code>ItemEvent</code> when the event occurs. The listener is
41 * spared the details of processing individual mouse movements and mouse
42 * clicks, and can instead process a "meaningful" (semantic) event like
43 * "item selected" or "item deselected".
44 *
45 * @author Carl Quinn
46 *
47 * @see java.awt.ItemSelectable
48 * @see ItemListener
49 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/itemlistener.html">Tutorial: Writing an Item Listener</a>
50 *
51 * @since 1.1
52 */
53public class ItemEvent extends AWTEvent {
54
55 /**
56 * The first number in the range of ids used for item events.
57 */
58 public static final int ITEM_FIRST = 701;
59
60 /**
61 * The last number in the range of ids used for item events.
62 */
63 public static final int ITEM_LAST = 701;
64
65 /**
66 * This event id indicates that an item's state changed.
67 */
68 public static final int ITEM_STATE_CHANGED = ITEM_FIRST; //Event.LIST_SELECT
69
70 /**
71 * This state-change value indicates that an item was selected.
72 */
73 public static final int SELECTED = 1;
74
75 /**
76 * This state-change-value indicates that a selected item was deselected.
77 */
78 public static final int DESELECTED = 2;
79
80 /**
81 * The item whose selection state has changed.
82 *
83 * @serial
84 * @see #getItem()
85 */
86 Object item;
87
88 /**
89 * <code>stateChange</code> indicates whether the <code>item</code>
90 * was selected or deselected.
91 *
92 * @serial
93 * @see #getStateChange()
94 */
95 int stateChange;
96
97 /*
98 * JDK 1.1 serialVersionUID
99 */
100 private static final long serialVersionUID = -608708132447206933L;
101
102 /**
103 * Constructs an <code>ItemEvent</code> object.
104 * <p>Note that passing in an invalid <code>id</code> results in
105 * unspecified behavior. This method throws an
106 * <code>IllegalArgumentException</code> if <code>source</code>
107 * is <code>null</code>.
108 *
109 * @param source the <code>ItemSelectable</code> object
110 * that originated the event
111 * @param id an integer that identifies the event type
112 * @param item an object -- the item affected by the event
113 * @param stateChange
114 * an integer that indicates whether the item was
115 * selected or deselected
116 * @throws IllegalArgumentException if <code>source</code> is null
117 */
118 public ItemEvent(ItemSelectable source, int id, Object item, int stateChange) {
119 super(source, id);
120 this.item = item;
121 this.stateChange = stateChange;
122 }
123
124 /**
125 * Returns the originator of the event.
126 *
127 * @return the ItemSelectable object that originated the event.
128 */
129 public ItemSelectable getItemSelectable() {
130 return (ItemSelectable)source;
131 }
132
133 /**
134 * Returns the item affected by the event.
135 *
136 * @return the item (object) that was affected by the event
137 */
138 public Object getItem() {
139 return item;
140 }
141
142 /**
143 * Returns the type of state change (selected or deselected).
144 *
145 * @return an integer that indicates whether the item was selected
146 * or deselected
147 *
148 * @see #SELECTED
149 * @see #DESELECTED
150 */
151 public int getStateChange() {
152 return stateChange;
153 }
154
155 /**
156 * Returns a parameter string identifying this item event.
157 * This method is useful for event-logging and for debugging.
158 *
159 * @return a string identifying the event and its attributes
160 */
161 public String paramString() {
162 String typeStr;
163 switch(id) {
164 case ITEM_STATE_CHANGED:
165 typeStr = "ITEM_STATE_CHANGED";
166 break;
167 default:
168 typeStr = "unknown type";
169 }
170
171 String stateStr;
172 switch(stateChange) {
173 case SELECTED:
174 stateStr = "SELECTED";
175 break;
176 case DESELECTED:
177 stateStr = "DESELECTED";
178 break;
179 default:
180 stateStr = "unknown type";
181 }
182 return typeStr + ",item="+item + ",stateChange="+stateStr;
183 }
184
185}