2
|
1 |
/*
|
|
2 |
* Copyright 1995-2006 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 |
package java.awt;
|
|
26 |
|
|
27 |
import java.util.*;
|
|
28 |
import java.awt.peer.ChoicePeer;
|
|
29 |
import java.awt.event.*;
|
|
30 |
import java.util.EventListener;
|
|
31 |
import java.io.ObjectOutputStream;
|
|
32 |
import java.io.ObjectInputStream;
|
|
33 |
import java.io.IOException;
|
|
34 |
|
|
35 |
import javax.accessibility.*;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* The <code>Choice</code> class presents a pop-up menu of choices.
|
|
39 |
* The current choice is displayed as the title of the menu.
|
|
40 |
* <p>
|
|
41 |
* The following code example produces a pop-up menu:
|
|
42 |
* <p>
|
|
43 |
* <hr><blockquote><pre>
|
|
44 |
* Choice ColorChooser = new Choice();
|
|
45 |
* ColorChooser.add("Green");
|
|
46 |
* ColorChooser.add("Red");
|
|
47 |
* ColorChooser.add("Blue");
|
|
48 |
* </pre></blockquote><hr>
|
|
49 |
* <p>
|
|
50 |
* After this choice menu has been added to a panel,
|
|
51 |
* it appears as follows in its normal state:
|
|
52 |
* <p>
|
|
53 |
* <img src="doc-files/Choice-1.gif" alt="The following text describes the graphic"
|
|
54 |
* ALIGN=center HSPACE=10 VSPACE=7>
|
|
55 |
* <p>
|
|
56 |
* In the picture, <code>"Green"</code> is the current choice.
|
|
57 |
* Pushing the mouse button down on the object causes a menu to
|
|
58 |
* appear with the current choice highlighted.
|
|
59 |
* <p>
|
|
60 |
* Some native platforms do not support arbitrary resizing of
|
|
61 |
* <code>Choice</code> components and the behavior of
|
|
62 |
* <code>setSize()/getSize()</code> is bound by
|
|
63 |
* such limitations.
|
|
64 |
* Native GUI <code>Choice</code> components' size are often bound by such
|
|
65 |
* attributes as font size and length of items contained within
|
|
66 |
* the <code>Choice</code>.
|
|
67 |
* <p>
|
|
68 |
* @author Sami Shaio
|
|
69 |
* @author Arthur van Hoff
|
|
70 |
* @since JDK1.0
|
|
71 |
*/
|
|
72 |
public class Choice extends Component implements ItemSelectable, Accessible {
|
|
73 |
/**
|
|
74 |
* The items for the <code>Choice</code>.
|
|
75 |
* This can be a <code>null</code> value.
|
|
76 |
* @serial
|
|
77 |
* @see #add(String)
|
|
78 |
* @see #addItem(String)
|
|
79 |
* @see #getItem(int)
|
|
80 |
* @see #getItemCount()
|
|
81 |
* @see #insert(String, int)
|
|
82 |
* @see #remove(String)
|
|
83 |
*/
|
|
84 |
Vector pItems;
|
|
85 |
|
|
86 |
/**
|
|
87 |
* The index of the current choice for this <code>Choice</code>
|
|
88 |
* or -1 if nothing is selected.
|
|
89 |
* @serial
|
|
90 |
* @see #getSelectedItem()
|
|
91 |
* @see #select(int)
|
|
92 |
*/
|
|
93 |
int selectedIndex = -1;
|
|
94 |
|
|
95 |
transient ItemListener itemListener;
|
|
96 |
|
|
97 |
private static final String base = "choice";
|
|
98 |
private static int nameCounter = 0;
|
|
99 |
|
|
100 |
/*
|
|
101 |
* JDK 1.1 serialVersionUID
|
|
102 |
*/
|
|
103 |
private static final long serialVersionUID = -4075310674757313071L;
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Creates a new choice menu. The menu initially has no items in it.
|
|
107 |
* <p>
|
|
108 |
* By default, the first item added to the choice menu becomes the
|
|
109 |
* selected item, until a different selection is made by the user
|
|
110 |
* by calling one of the <code>select</code> methods.
|
|
111 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
|
|
112 |
* returns true
|
|
113 |
* @see java.awt.GraphicsEnvironment#isHeadless
|
|
114 |
* @see #select(int)
|
|
115 |
* @see #select(java.lang.String)
|
|
116 |
*/
|
|
117 |
public Choice() throws HeadlessException {
|
|
118 |
GraphicsEnvironment.checkHeadless();
|
|
119 |
pItems = new Vector();
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Constructs a name for this component. Called by
|
|
124 |
* <code>getName</code> when the name is <code>null</code>.
|
|
125 |
*/
|
|
126 |
String constructComponentName() {
|
|
127 |
synchronized (Choice.class) {
|
|
128 |
return base + nameCounter++;
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Creates the <code>Choice</code>'s peer. This peer allows us
|
|
134 |
* to change the look
|
|
135 |
* of the <code>Choice</code> without changing its functionality.
|
|
136 |
* @see java.awt.Toolkit#createChoice(java.awt.Choice)
|
|
137 |
* @see java.awt.Component#getToolkit()
|
|
138 |
*/
|
|
139 |
public void addNotify() {
|
|
140 |
synchronized (getTreeLock()) {
|
|
141 |
if (peer == null)
|
|
142 |
peer = getToolkit().createChoice(this);
|
|
143 |
super.addNotify();
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Returns the number of items in this <code>Choice</code> menu.
|
|
149 |
* @return the number of items in this <code>Choice</code> menu
|
|
150 |
* @see #getItem
|
|
151 |
* @since JDK1.1
|
|
152 |
*/
|
|
153 |
public int getItemCount() {
|
|
154 |
return countItems();
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* @deprecated As of JDK version 1.1,
|
|
159 |
* replaced by <code>getItemCount()</code>.
|
|
160 |
*/
|
|
161 |
@Deprecated
|
|
162 |
public int countItems() {
|
|
163 |
return pItems.size();
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Gets the string at the specified index in this
|
|
168 |
* <code>Choice</code> menu.
|
|
169 |
* @param index the index at which to begin
|
|
170 |
* @see #getItemCount
|
|
171 |
*/
|
|
172 |
public String getItem(int index) {
|
|
173 |
return getItemImpl(index);
|
|
174 |
}
|
|
175 |
|
|
176 |
/*
|
|
177 |
* This is called by the native code, so client code can't
|
|
178 |
* be called on the toolkit thread.
|
|
179 |
*/
|
|
180 |
final String getItemImpl(int index) {
|
|
181 |
return (String)pItems.elementAt(index);
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* Adds an item to this <code>Choice</code> menu.
|
|
186 |
* @param item the item to be added
|
|
187 |
* @exception NullPointerException if the item's value is
|
|
188 |
* <code>null</code>
|
|
189 |
* @since JDK1.1
|
|
190 |
*/
|
|
191 |
public void add(String item) {
|
|
192 |
addItem(item);
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Obsolete as of Java 2 platform v1.1. Please use the
|
|
197 |
* <code>add</code> method instead.
|
|
198 |
* <p>
|
|
199 |
* Adds an item to this <code>Choice</code> menu.
|
|
200 |
* @param item the item to be added
|
|
201 |
* @exception NullPointerException if the item's value is equal to
|
|
202 |
* <code>null</code>
|
|
203 |
*/
|
|
204 |
public void addItem(String item) {
|
|
205 |
synchronized (this) {
|
|
206 |
insertNoInvalidate(item, pItems.size());
|
|
207 |
}
|
|
208 |
|
|
209 |
// This could change the preferred size of the Component.
|
|
210 |
if (valid) {
|
|
211 |
invalidate();
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
|
216 |
* Inserts an item to this <code>Choice</code>,
|
|
217 |
* but does not invalidate the <code>Choice</code>.
|
|
218 |
* Client methods must provide their own synchronization before
|
|
219 |
* invoking this method.
|
|
220 |
* @param item the item to be added
|
|
221 |
* @param index the new item position
|
|
222 |
* @exception NullPointerException if the item's value is equal to
|
|
223 |
* <code>null</code>
|
|
224 |
*/
|
|
225 |
private void insertNoInvalidate(String item, int index) {
|
|
226 |
if (item == null) {
|
|
227 |
throw new
|
|
228 |
NullPointerException("cannot add null item to Choice");
|
|
229 |
}
|
|
230 |
pItems.insertElementAt(item, index);
|
|
231 |
ChoicePeer peer = (ChoicePeer)this.peer;
|
|
232 |
if (peer != null) {
|
|
233 |
peer.addItem(item, index);
|
|
234 |
}
|
|
235 |
// no selection or selection shifted up
|
|
236 |
if (selectedIndex < 0 || selectedIndex >= index) {
|
|
237 |
select(0);
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Inserts the item into this choice at the specified position.
|
|
244 |
* Existing items at an index greater than or equal to
|
|
245 |
* <code>index</code> are shifted up by one to accommodate
|
|
246 |
* the new item. If <code>index</code> is greater than or
|
|
247 |
* equal to the number of items in this choice,
|
|
248 |
* <code>item</code> is added to the end of this choice.
|
|
249 |
* <p>
|
|
250 |
* If the item is the first one being added to the choice,
|
|
251 |
* then the item becomes selected. Otherwise, if the
|
|
252 |
* selected item was one of the items shifted, the first
|
|
253 |
* item in the choice becomes the selected item. If the
|
|
254 |
* selected item was no among those shifted, it remains
|
|
255 |
* the selected item.
|
|
256 |
* @param item the non-<code>null</code> item to be inserted
|
|
257 |
* @param index the position at which the item should be inserted
|
|
258 |
* @exception IllegalArgumentException if index is less than 0
|
|
259 |
*/
|
|
260 |
public void insert(String item, int index) {
|
|
261 |
synchronized (this) {
|
|
262 |
if (index < 0) {
|
|
263 |
throw new IllegalArgumentException("index less than zero.");
|
|
264 |
}
|
|
265 |
/* if the index greater than item count, add item to the end */
|
|
266 |
index = Math.min(index, pItems.size());
|
|
267 |
|
|
268 |
insertNoInvalidate(item, index);
|
|
269 |
}
|
|
270 |
|
|
271 |
// This could change the preferred size of the Component.
|
|
272 |
if (valid) {
|
|
273 |
invalidate();
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
/**
|
|
278 |
* Removes the first occurrence of <code>item</code>
|
|
279 |
* from the <code>Choice</code> menu. If the item
|
|
280 |
* being removed is the currently selected item,
|
|
281 |
* then the first item in the choice becomes the
|
|
282 |
* selected item. Otherwise, the currently selected
|
|
283 |
* item remains selected (and the selected index is
|
|
284 |
* updated accordingly).
|
|
285 |
* @param item the item to remove from this <code>Choice</code> menu
|
|
286 |
* @exception IllegalArgumentException if the item doesn't
|
|
287 |
* exist in the choice menu
|
|
288 |
* @since JDK1.1
|
|
289 |
*/
|
|
290 |
public void remove(String item) {
|
|
291 |
synchronized (this) {
|
|
292 |
int index = pItems.indexOf(item);
|
|
293 |
if (index < 0) {
|
|
294 |
throw new IllegalArgumentException("item " + item +
|
|
295 |
" not found in choice");
|
|
296 |
} else {
|
|
297 |
removeNoInvalidate(index);
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
// This could change the preferred size of the Component.
|
|
302 |
if (valid) {
|
|
303 |
invalidate();
|
|
304 |
}
|
|
305 |
}
|
|
306 |
|
|
307 |
/**
|
|
308 |
* Removes an item from the choice menu
|
|
309 |
* at the specified position. If the item
|
|
310 |
* being removed is the currently selected item,
|
|
311 |
* then the first item in the choice becomes the
|
|
312 |
* selected item. Otherwise, the currently selected
|
|
313 |
* item remains selected (and the selected index is
|
|
314 |
* updated accordingly).
|
|
315 |
* @param position the position of the item
|
|
316 |
* @throws IndexOutOfBoundsException if the specified
|
|
317 |
* position is out of bounds
|
|
318 |
* @since JDK1.1
|
|
319 |
*/
|
|
320 |
public void remove(int position) {
|
|
321 |
synchronized (this) {
|
|
322 |
removeNoInvalidate(position);
|
|
323 |
}
|
|
324 |
|
|
325 |
// This could change the preferred size of the Component.
|
|
326 |
if (valid) {
|
|
327 |
invalidate();
|
|
328 |
}
|
|
329 |
}
|
|
330 |
|
|
331 |
/**
|
|
332 |
* Removes an item from the <code>Choice</code> at the
|
|
333 |
* specified position, but does not invalidate the <code>Choice</code>.
|
|
334 |
* Client methods must provide their
|
|
335 |
* own synchronization before invoking this method.
|
|
336 |
* @param position the position of the item
|
|
337 |
*/
|
|
338 |
private void removeNoInvalidate(int position) {
|
|
339 |
pItems.removeElementAt(position);
|
|
340 |
ChoicePeer peer = (ChoicePeer)this.peer;
|
|
341 |
if (peer != null) {
|
|
342 |
peer.remove(position);
|
|
343 |
}
|
|
344 |
/* Adjust selectedIndex if selected item was removed. */
|
|
345 |
if (pItems.size() == 0) {
|
|
346 |
selectedIndex = -1;
|
|
347 |
} else if (selectedIndex == position) {
|
|
348 |
select(0);
|
|
349 |
} else if (selectedIndex > position) {
|
|
350 |
select(selectedIndex-1);
|
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
|
|
355 |
/**
|
|
356 |
* Removes all items from the choice menu.
|
|
357 |
* @see #remove
|
|
358 |
* @since JDK1.1
|
|
359 |
*/
|
|
360 |
public void removeAll() {
|
|
361 |
synchronized (this) {
|
|
362 |
if (peer != null) {
|
|
363 |
((ChoicePeer)peer).removeAll();
|
|
364 |
}
|
|
365 |
pItems.removeAllElements();
|
|
366 |
selectedIndex = -1;
|
|
367 |
}
|
|
368 |
|
|
369 |
// This could change the preferred size of the Component.
|
|
370 |
if (valid) {
|
|
371 |
invalidate();
|
|
372 |
}
|
|
373 |
}
|
|
374 |
|
|
375 |
/**
|
|
376 |
* Gets a representation of the current choice as a string.
|
|
377 |
* @return a string representation of the currently
|
|
378 |
* selected item in this choice menu
|
|
379 |
* @see #getSelectedIndex
|
|
380 |
*/
|
|
381 |
public synchronized String getSelectedItem() {
|
|
382 |
return (selectedIndex >= 0) ? getItem(selectedIndex) : null;
|
|
383 |
}
|
|
384 |
|
|
385 |
/**
|
|
386 |
* Returns an array (length 1) containing the currently selected
|
|
387 |
* item. If this choice has no items, returns <code>null</code>.
|
|
388 |
* @see ItemSelectable
|
|
389 |
*/
|
|
390 |
public synchronized Object[] getSelectedObjects() {
|
|
391 |
if (selectedIndex >= 0) {
|
|
392 |
Object[] items = new Object[1];
|
|
393 |
items[0] = getItem(selectedIndex);
|
|
394 |
return items;
|
|
395 |
}
|
|
396 |
return null;
|
|
397 |
}
|
|
398 |
|
|
399 |
/**
|
|
400 |
* Returns the index of the currently selected item.
|
|
401 |
* If nothing is selected, returns -1.
|
|
402 |
*
|
|
403 |
* @return the index of the currently selected item, or -1 if nothing
|
|
404 |
* is currently selected
|
|
405 |
* @see #getSelectedItem
|
|
406 |
*/
|
|
407 |
public int getSelectedIndex() {
|
|
408 |
return selectedIndex;
|
|
409 |
}
|
|
410 |
|
|
411 |
/**
|
|
412 |
* Sets the selected item in this <code>Choice</code> menu to be the
|
|
413 |
* item at the specified position.
|
|
414 |
*
|
|
415 |
* <p>Note that this method should be primarily used to
|
|
416 |
* initially select an item in this component.
|
|
417 |
* Programmatically calling this method will <i>not</i> trigger
|
|
418 |
* an <code>ItemEvent</code>. The only way to trigger an
|
|
419 |
* <code>ItemEvent</code> is by user interaction.
|
|
420 |
*
|
|
421 |
* @param pos the positon of the selected item
|
|
422 |
* @exception IllegalArgumentException if the specified
|
|
423 |
* position is greater than the
|
|
424 |
* number of items or less than zero
|
|
425 |
* @see #getSelectedItem
|
|
426 |
* @see #getSelectedIndex
|
|
427 |
*/
|
|
428 |
public synchronized void select(int pos) {
|
|
429 |
if ((pos >= pItems.size()) || (pos < 0)) {
|
|
430 |
throw new IllegalArgumentException("illegal Choice item position: " + pos);
|
|
431 |
}
|
|
432 |
if (pItems.size() > 0) {
|
|
433 |
selectedIndex = pos;
|
|
434 |
ChoicePeer peer = (ChoicePeer)this.peer;
|
|
435 |
if (peer != null) {
|
|
436 |
peer.select(pos);
|
|
437 |
}
|
|
438 |
}
|
|
439 |
}
|
|
440 |
|
|
441 |
/**
|
|
442 |
* Sets the selected item in this <code>Choice</code> menu
|
|
443 |
* to be the item whose name is equal to the specified string.
|
|
444 |
* If more than one item matches (is equal to) the specified string,
|
|
445 |
* the one with the smallest index is selected.
|
|
446 |
*
|
|
447 |
* <p>Note that this method should be primarily used to
|
|
448 |
* initially select an item in this component.
|
|
449 |
* Programmatically calling this method will <i>not</i> trigger
|
|
450 |
* an <code>ItemEvent</code>. The only way to trigger an
|
|
451 |
* <code>ItemEvent</code> is by user interaction.
|
|
452 |
*
|
|
453 |
* @param str the specified string
|
|
454 |
* @see #getSelectedItem
|
|
455 |
* @see #getSelectedIndex
|
|
456 |
*/
|
|
457 |
public synchronized void select(String str) {
|
|
458 |
int index = pItems.indexOf(str);
|
|
459 |
if (index >= 0) {
|
|
460 |
select(index);
|
|
461 |
}
|
|
462 |
}
|
|
463 |
|
|
464 |
/**
|
|
465 |
* Adds the specified item listener to receive item events from
|
|
466 |
* this <code>Choice</code> menu. Item events are sent in response
|
|
467 |
* to user input, but not in response to calls to <code>select</code>.
|
|
468 |
* If l is <code>null</code>, no exception is thrown and no action
|
|
469 |
* is performed.
|
|
470 |
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
|
|
471 |
* >AWT Threading Issues</a> for details on AWT's threading model.
|
|
472 |
* @param l the item listener
|
|
473 |
* @see #removeItemListener
|
|
474 |
* @see #getItemListeners
|
|
475 |
* @see #select
|
|
476 |
* @see java.awt.event.ItemEvent
|
|
477 |
* @see java.awt.event.ItemListener
|
|
478 |
* @since JDK1.1
|
|
479 |
*/
|
|
480 |
public synchronized void addItemListener(ItemListener l) {
|
|
481 |
if (l == null) {
|
|
482 |
return;
|
|
483 |
}
|
|
484 |
itemListener = AWTEventMulticaster.add(itemListener, l);
|
|
485 |
newEventsOnly = true;
|
|
486 |
}
|
|
487 |
|
|
488 |
/**
|
|
489 |
* Removes the specified item listener so that it no longer receives
|
|
490 |
* item events from this <code>Choice</code> menu.
|
|
491 |
* If l is <code>null</code>, no exception is thrown and no
|
|
492 |
* action is performed.
|
|
493 |
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
|
|
494 |
* >AWT Threading Issues</a> for details on AWT's threading model.
|
|
495 |
* @param l the item listener
|
|
496 |
* @see #addItemListener
|
|
497 |
* @see #getItemListeners
|
|
498 |
* @see java.awt.event.ItemEvent
|
|
499 |
* @see java.awt.event.ItemListener
|
|
500 |
* @since JDK1.1
|
|
501 |
*/
|
|
502 |
public synchronized void removeItemListener(ItemListener l) {
|
|
503 |
if (l == null) {
|
|
504 |
return;
|
|
505 |
}
|
|
506 |
itemListener = AWTEventMulticaster.remove(itemListener, l);
|
|
507 |
}
|
|
508 |
|
|
509 |
/**
|
|
510 |
* Returns an array of all the item listeners
|
|
511 |
* registered on this choice.
|
|
512 |
*
|
|
513 |
* @return all of this choice's <code>ItemListener</code>s
|
|
514 |
* or an empty array if no item
|
|
515 |
* listeners are currently registered
|
|
516 |
*
|
|
517 |
* @see #addItemListener
|
|
518 |
* @see #removeItemListener
|
|
519 |
* @see java.awt.event.ItemEvent
|
|
520 |
* @see java.awt.event.ItemListener
|
|
521 |
* @since 1.4
|
|
522 |
*/
|
|
523 |
public synchronized ItemListener[] getItemListeners() {
|
|
524 |
return (ItemListener[])(getListeners(ItemListener.class));
|
|
525 |
}
|
|
526 |
|
|
527 |
/**
|
|
528 |
* Returns an array of all the objects currently registered
|
|
529 |
* as <code><em>Foo</em>Listener</code>s
|
|
530 |
* upon this <code>Choice</code>.
|
|
531 |
* <code><em>Foo</em>Listener</code>s are registered using the
|
|
532 |
* <code>add<em>Foo</em>Listener</code> method.
|
|
533 |
*
|
|
534 |
* <p>
|
|
535 |
* You can specify the <code>listenerType</code> argument
|
|
536 |
* with a class literal, such as
|
|
537 |
* <code><em>Foo</em>Listener.class</code>.
|
|
538 |
* For example, you can query a
|
|
539 |
* <code>Choice</code> <code>c</code>
|
|
540 |
* for its item listeners with the following code:
|
|
541 |
*
|
|
542 |
* <pre>ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));</pre>
|
|
543 |
*
|
|
544 |
* If no such listeners exist, this method returns an empty array.
|
|
545 |
*
|
|
546 |
* @param listenerType the type of listeners requested; this parameter
|
|
547 |
* should specify an interface that descends from
|
|
548 |
* <code>java.util.EventListener</code>
|
|
549 |
* @return an array of all objects registered as
|
|
550 |
* <code><em>Foo</em>Listener</code>s on this choice,
|
|
551 |
* or an empty array if no such
|
|
552 |
* listeners have been added
|
|
553 |
* @exception ClassCastException if <code>listenerType</code>
|
|
554 |
* doesn't specify a class or interface that implements
|
|
555 |
* <code>java.util.EventListener</code>
|
|
556 |
*
|
|
557 |
* @see #getItemListeners
|
|
558 |
* @since 1.3
|
|
559 |
*/
|
|
560 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
|
|
561 |
EventListener l = null;
|
|
562 |
if (listenerType == ItemListener.class) {
|
|
563 |
l = itemListener;
|
|
564 |
} else {
|
|
565 |
return super.getListeners(listenerType);
|
|
566 |
}
|
|
567 |
return AWTEventMulticaster.getListeners(l, listenerType);
|
|
568 |
}
|
|
569 |
|
|
570 |
// REMIND: remove when filtering is done at lower level
|
|
571 |
boolean eventEnabled(AWTEvent e) {
|
|
572 |
if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
|
|
573 |
if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
|
|
574 |
itemListener != null) {
|
|
575 |
return true;
|
|
576 |
}
|
|
577 |
return false;
|
|
578 |
}
|
|
579 |
return super.eventEnabled(e);
|
|
580 |
}
|
|
581 |
|
|
582 |
/**
|
|
583 |
* Processes events on this choice. If the event is an
|
|
584 |
* instance of <code>ItemEvent</code>, it invokes the
|
|
585 |
* <code>processItemEvent</code> method. Otherwise, it calls its
|
|
586 |
* superclass's <code>processEvent</code> method.
|
|
587 |
* <p>Note that if the event parameter is <code>null</code>
|
|
588 |
* the behavior is unspecified and may result in an
|
|
589 |
* exception.
|
|
590 |
*
|
|
591 |
* @param e the event
|
|
592 |
* @see java.awt.event.ItemEvent
|
|
593 |
* @see #processItemEvent
|
|
594 |
* @since JDK1.1
|
|
595 |
*/
|
|
596 |
protected void processEvent(AWTEvent e) {
|
|
597 |
if (e instanceof ItemEvent) {
|
|
598 |
processItemEvent((ItemEvent)e);
|
|
599 |
return;
|
|
600 |
}
|
|
601 |
super.processEvent(e);
|
|
602 |
}
|
|
603 |
|
|
604 |
/**
|
|
605 |
* Processes item events occurring on this <code>Choice</code>
|
|
606 |
* menu by dispatching them to any registered
|
|
607 |
* <code>ItemListener</code> objects.
|
|
608 |
* <p>
|
|
609 |
* This method is not called unless item events are
|
|
610 |
* enabled for this component. Item events are enabled
|
|
611 |
* when one of the following occurs:
|
|
612 |
* <p><ul>
|
|
613 |
* <li>An <code>ItemListener</code> object is registered
|
|
614 |
* via <code>addItemListener</code>.
|
|
615 |
* <li>Item events are enabled via <code>enableEvents</code>.
|
|
616 |
* </ul>
|
|
617 |
* <p>Note that if the event parameter is <code>null</code>
|
|
618 |
* the behavior is unspecified and may result in an
|
|
619 |
* exception.
|
|
620 |
*
|
|
621 |
* @param e the item event
|
|
622 |
* @see java.awt.event.ItemEvent
|
|
623 |
* @see java.awt.event.ItemListener
|
|
624 |
* @see #addItemListener(ItemListener)
|
|
625 |
* @see java.awt.Component#enableEvents
|
|
626 |
* @since JDK1.1
|
|
627 |
*/
|
|
628 |
protected void processItemEvent(ItemEvent e) {
|
|
629 |
ItemListener listener = itemListener;
|
|
630 |
if (listener != null) {
|
|
631 |
listener.itemStateChanged(e);
|
|
632 |
}
|
|
633 |
}
|
|
634 |
|
|
635 |
/**
|
|
636 |
* Returns a string representing the state of this <code>Choice</code>
|
|
637 |
* menu. This method is intended to be used only for debugging purposes,
|
|
638 |
* and the content and format of the returned string may vary between
|
|
639 |
* implementations. The returned string may be empty but may not be
|
|
640 |
* <code>null</code>.
|
|
641 |
*
|
|
642 |
* @return the parameter string of this <code>Choice</code> menu
|
|
643 |
*/
|
|
644 |
protected String paramString() {
|
|
645 |
return super.paramString() + ",current=" + getSelectedItem();
|
|
646 |
}
|
|
647 |
|
|
648 |
|
|
649 |
/* Serialization support.
|
|
650 |
*/
|
|
651 |
|
|
652 |
/*
|
|
653 |
* Choice Serial Data Version.
|
|
654 |
* @serial
|
|
655 |
*/
|
|
656 |
private int choiceSerializedDataVersion = 1;
|
|
657 |
|
|
658 |
/**
|
|
659 |
* Writes default serializable fields to stream. Writes
|
|
660 |
* a list of serializable <code>ItemListeners</code>
|
|
661 |
* as optional data. The non-serializable
|
|
662 |
* <code>ItemListeners</code> are detected and
|
|
663 |
* no attempt is made to serialize them.
|
|
664 |
*
|
|
665 |
* @param s the <code>ObjectOutputStream</code> to write
|
|
666 |
* @serialData <code>null</code> terminated sequence of 0
|
|
667 |
* or more pairs; the pair consists of a <code>String</code>
|
|
668 |
* and an <code>Object</code>; the <code>String</code> indicates
|
|
669 |
* the type of object and is one of the following:
|
|
670 |
* <code>itemListenerK</code> indicating an
|
|
671 |
* <code>ItemListener</code> object
|
|
672 |
*
|
|
673 |
* @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
|
|
674 |
* @see java.awt.Component#itemListenerK
|
|
675 |
* @see #readObject(ObjectInputStream)
|
|
676 |
*/
|
|
677 |
private void writeObject(ObjectOutputStream s)
|
|
678 |
throws java.io.IOException
|
|
679 |
{
|
|
680 |
s.defaultWriteObject();
|
|
681 |
|
|
682 |
AWTEventMulticaster.save(s, itemListenerK, itemListener);
|
|
683 |
s.writeObject(null);
|
|
684 |
}
|
|
685 |
|
|
686 |
/**
|
|
687 |
* Reads the <code>ObjectInputStream</code> and if it
|
|
688 |
* isn't <code>null</code> adds a listener to receive
|
|
689 |
* item events fired by the <code>Choice</code> item.
|
|
690 |
* Unrecognized keys or values will be ignored.
|
|
691 |
*
|
|
692 |
* @param s the <code>ObjectInputStream</code> to read
|
|
693 |
* @exception HeadlessException if
|
|
694 |
* <code>GraphicsEnvironment.isHeadless</code> returns
|
|
695 |
* <code>true</code>
|
|
696 |
* @serial
|
|
697 |
* @see #removeItemListener(ItemListener)
|
|
698 |
* @see #addItemListener(ItemListener)
|
|
699 |
* @see java.awt.GraphicsEnvironment#isHeadless
|
|
700 |
* @see #writeObject(ObjectOutputStream)
|
|
701 |
*/
|
|
702 |
private void readObject(ObjectInputStream s)
|
|
703 |
throws ClassNotFoundException, IOException, HeadlessException
|
|
704 |
{
|
|
705 |
GraphicsEnvironment.checkHeadless();
|
|
706 |
s.defaultReadObject();
|
|
707 |
|
|
708 |
Object keyOrNull;
|
|
709 |
while(null != (keyOrNull = s.readObject())) {
|
|
710 |
String key = ((String)keyOrNull).intern();
|
|
711 |
|
|
712 |
if (itemListenerK == key)
|
|
713 |
addItemListener((ItemListener)(s.readObject()));
|
|
714 |
|
|
715 |
else // skip value for unrecognized key
|
|
716 |
s.readObject();
|
|
717 |
}
|
|
718 |
}
|
|
719 |
|
|
720 |
|
|
721 |
/////////////////
|
|
722 |
// Accessibility support
|
|
723 |
////////////////
|
|
724 |
|
|
725 |
|
|
726 |
/**
|
|
727 |
* Gets the <code>AccessibleContext</code> associated with this
|
|
728 |
* <code>Choice</code>. For <code>Choice</code> components,
|
|
729 |
* the <code>AccessibleContext</code> takes the form of an
|
|
730 |
* <code>AccessibleAWTChoice</code>. A new <code>AccessibleAWTChoice</code>
|
|
731 |
* instance is created if necessary.
|
|
732 |
*
|
|
733 |
* @return an <code>AccessibleAWTChoice</code> that serves as the
|
|
734 |
* <code>AccessibleContext</code> of this <code>Choice</code>
|
|
735 |
* @since 1.3
|
|
736 |
*/
|
|
737 |
public AccessibleContext getAccessibleContext() {
|
|
738 |
if (accessibleContext == null) {
|
|
739 |
accessibleContext = new AccessibleAWTChoice();
|
|
740 |
}
|
|
741 |
return accessibleContext;
|
|
742 |
}
|
|
743 |
|
|
744 |
/**
|
|
745 |
* This class implements accessibility support for the
|
|
746 |
* <code>Choice</code> class. It provides an implementation of the
|
|
747 |
* Java Accessibility API appropriate to choice user-interface elements.
|
|
748 |
* @since 1.3
|
|
749 |
*/
|
|
750 |
protected class AccessibleAWTChoice extends AccessibleAWTComponent
|
|
751 |
implements AccessibleAction
|
|
752 |
{
|
|
753 |
/*
|
|
754 |
* JDK 1.3 serialVersionUID
|
|
755 |
*/
|
|
756 |
private static final long serialVersionUID = 7175603582428509322L;
|
|
757 |
|
|
758 |
public AccessibleAWTChoice() {
|
|
759 |
super();
|
|
760 |
}
|
|
761 |
|
|
762 |
/**
|
|
763 |
* Get the AccessibleAction associated with this object. In the
|
|
764 |
* implementation of the Java Accessibility API for this class,
|
|
765 |
* return this object, which is responsible for implementing the
|
|
766 |
* AccessibleAction interface on behalf of itself.
|
|
767 |
*
|
|
768 |
* @return this object
|
|
769 |
* @see AccessibleAction
|
|
770 |
*/
|
|
771 |
public AccessibleAction getAccessibleAction() {
|
|
772 |
return this;
|
|
773 |
}
|
|
774 |
|
|
775 |
/**
|
|
776 |
* Get the role of this object.
|
|
777 |
*
|
|
778 |
* @return an instance of AccessibleRole describing the role of the
|
|
779 |
* object
|
|
780 |
* @see AccessibleRole
|
|
781 |
*/
|
|
782 |
public AccessibleRole getAccessibleRole() {
|
|
783 |
return AccessibleRole.COMBO_BOX;
|
|
784 |
}
|
|
785 |
|
|
786 |
/**
|
|
787 |
* Returns the number of accessible actions available in this object
|
|
788 |
* If there are more than one, the first one is considered the "default"
|
|
789 |
* action of the object.
|
|
790 |
*
|
|
791 |
* @return the zero-based number of Actions in this object
|
|
792 |
*/
|
|
793 |
public int getAccessibleActionCount() {
|
|
794 |
return 0; // To be fully implemented in a future release
|
|
795 |
}
|
|
796 |
|
|
797 |
/**
|
|
798 |
* Returns a description of the specified action of the object.
|
|
799 |
*
|
|
800 |
* @param i zero-based index of the actions
|
|
801 |
* @return a String description of the action
|
|
802 |
* @see #getAccessibleActionCount
|
|
803 |
*/
|
|
804 |
public String getAccessibleActionDescription(int i) {
|
|
805 |
return null; // To be fully implemented in a future release
|
|
806 |
}
|
|
807 |
|
|
808 |
/**
|
|
809 |
* Perform the specified Action on the object
|
|
810 |
*
|
|
811 |
* @param i zero-based index of actions
|
|
812 |
* @return true if the action was performed; otherwise false.
|
|
813 |
* @see #getAccessibleActionCount
|
|
814 |
*/
|
|
815 |
public boolean doAccessibleAction(int i) {
|
|
816 |
return false; // To be fully implemented in a future release
|
|
817 |
}
|
|
818 |
|
|
819 |
} // inner class AccessibleAWTChoice
|
|
820 |
|
|
821 |
}
|