Source Code Cross Referenced for Choice.java in  » 6.0-JDK-Core » AWT » java » awt » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Core » AWT » java.awt 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:        package java.awt;
026:
027:        import java.util.*;
028:        import java.awt.peer.ChoicePeer;
029:        import java.awt.event.*;
030:        import java.util.EventListener;
031:        import java.io.ObjectOutputStream;
032:        import java.io.ObjectInputStream;
033:        import java.io.IOException;
034:
035:        import javax.accessibility.*;
036:
037:        /**
038:         * The <code>Choice</code> class presents a pop-up menu of choices.
039:         * The current choice is displayed as the title of the menu.
040:         * <p>
041:         * The following code example produces a pop-up menu:
042:         * <p>
043:         * <hr><blockquote><pre>
044:         * Choice ColorChooser = new Choice();
045:         * ColorChooser.add("Green");
046:         * ColorChooser.add("Red");
047:         * ColorChooser.add("Blue");
048:         * </pre></blockquote><hr>
049:         * <p>
050:         * After this choice menu has been added to a panel,
051:         * it appears as follows in its normal state:
052:         * <p>
053:         * <img src="doc-files/Choice-1.gif" alt="The following text describes the graphic"
054:         * ALIGN=center HSPACE=10 VSPACE=7>
055:         * <p>
056:         * In the picture, <code>"Green"</code> is the current choice.
057:         * Pushing the mouse button down on the object causes a menu to
058:         * appear with the current choice highlighted.
059:         * <p>
060:         * Some native platforms do not support arbitrary resizing of 
061:         * <code>Choice</code> components and the behavior of 
062:         * <code>setSize()/getSize()</code> is bound by 
063:         * such limitations.
064:         * Native GUI <code>Choice</code> components' size are often bound by such
065:         * attributes as font size and length of items contained within 
066:         * the <code>Choice</code>.
067:         * <p>
068:         * @version	1.100 05/05/07
069:         * @author 	Sami Shaio
070:         * @author 	Arthur van Hoff
071:         * @since       JDK1.0
072:         */
073:        public class Choice extends Component implements  ItemSelectable,
074:                Accessible {
075:            /**
076:             * The items for the <code>Choice</code>.
077:             * This can be a <code>null</code> value.
078:             * @serial
079:             * @see #add(String)
080:             * @see #addItem(String)
081:             * @see #getItem(int)
082:             * @see #getItemCount()
083:             * @see #insert(String, int)
084:             * @see #remove(String)
085:             */
086:            Vector pItems;
087:
088:            /**
089:             * The index of the current choice for this <code>Choice</code>
090:             * or -1 if nothing is selected.
091:             * @serial
092:             * @see #getSelectedItem()
093:             * @see #select(int)
094:             */
095:            int selectedIndex = -1;
096:
097:            transient ItemListener itemListener;
098:
099:            private static final String base = "choice";
100:            private static int nameCounter = 0;
101:
102:            /*
103:             * JDK 1.1 serialVersionUID
104:             */
105:            private static final long serialVersionUID = -4075310674757313071L;
106:
107:            /**
108:             * Creates a new choice menu. The menu initially has no items in it.
109:             * <p>
110:             * By default, the first item added to the choice menu becomes the
111:             * selected item, until a different selection is made by the user
112:             * by calling one of the <code>select</code> methods.
113:             * @exception HeadlessException if GraphicsEnvironment.isHeadless()
114:             * returns true
115:             * @see       java.awt.GraphicsEnvironment#isHeadless
116:             * @see       #select(int)
117:             * @see       #select(java.lang.String)
118:             */
119:            public Choice() throws HeadlessException {
120:                GraphicsEnvironment.checkHeadless();
121:                pItems = new Vector();
122:            }
123:
124:            /**
125:             * Constructs a name for this component.  Called by 
126:             * <code>getName</code> when the name is <code>null</code>.
127:             */
128:            String constructComponentName() {
129:                synchronized (Choice.class) {
130:                    return base + nameCounter++;
131:                }
132:            }
133:
134:            /**
135:             * Creates the <code>Choice</code>'s peer.  This peer allows us
136:             * to change the look
137:             * of the <code>Choice</code> without changing its functionality.
138:             * @see     java.awt.Toolkit#createChoice(java.awt.Choice)
139:             * @see     java.awt.Component#getToolkit()
140:             */
141:            public void addNotify() {
142:                synchronized (getTreeLock()) {
143:                    if (peer == null)
144:                        peer = getToolkit().createChoice(this );
145:                    super .addNotify();
146:                }
147:            }
148:
149:            /**
150:             * Returns the number of items in this <code>Choice</code> menu.
151:             * @return the number of items in this <code>Choice</code> menu
152:             * @see     #getItem
153:             * @since   JDK1.1
154:             */
155:            public int getItemCount() {
156:                return countItems();
157:            }
158:
159:            /**
160:             * @deprecated As of JDK version 1.1,
161:             * replaced by <code>getItemCount()</code>.
162:             */
163:            @Deprecated
164:            public int countItems() {
165:                return pItems.size();
166:            }
167:
168:            /**
169:             * Gets the string at the specified index in this
170:             * <code>Choice</code> menu.
171:             * @param      index the index at which to begin
172:             * @see        #getItemCount
173:             */
174:            public String getItem(int index) {
175:                return getItemImpl(index);
176:            }
177:
178:            /*
179:             * This is called by the native code, so client code can't
180:             * be called on the toolkit thread.
181:             */
182:            final String getItemImpl(int index) {
183:                return (String) pItems.elementAt(index);
184:            }
185:
186:            /**
187:             * Adds an item to this <code>Choice</code> menu.
188:             * @param      item    the item to be added
189:             * @exception  NullPointerException   if the item's value is
190:             *			<code>null</code>
191:             * @since      JDK1.1
192:             */
193:            public void add(String item) {
194:                addItem(item);
195:            }
196:
197:            /**
198:             * Obsolete as of Java 2 platform v1.1.  Please use the
199:             * <code>add</code> method instead.
200:             * <p>
201:             * Adds an item to this <code>Choice</code> menu.
202:             * @param item the item to be added
203:             * @exception NullPointerException if the item's value is equal to
204:             *		<code>null</code>
205:             */
206:            public void addItem(String item) {
207:                synchronized (this ) {
208:                    insertNoInvalidate(item, pItems.size());
209:                }
210:
211:                // This could change the preferred size of the Component.
212:                if (valid) {
213:                    invalidate();
214:                }
215:            }
216:
217:            /**
218:             * Inserts an item to this <code>Choice</code>,
219:             * but does not invalidate the <code>Choice</code>.
220:             * Client methods must provide their own synchronization before
221:             * invoking this method.
222:             * @param item the item to be added
223:             * @param index the new item position
224:             * @exception NullPointerException if the item's value is equal to
225:             *		<code>null</code>
226:             */
227:            private void insertNoInvalidate(String item, int index) {
228:                if (item == null) {
229:                    throw new NullPointerException(
230:                            "cannot add null item to Choice");
231:                }
232:                pItems.insertElementAt(item, index);
233:                ChoicePeer peer = (ChoicePeer) this .peer;
234:                if (peer != null) {
235:                    peer.addItem(item, index);
236:                }
237:                // no selection or selection shifted up
238:                if (selectedIndex < 0 || selectedIndex >= index) {
239:                    select(0);
240:                }
241:            }
242:
243:            /**
244:             * Inserts the item into this choice at the specified position.
245:             * Existing items at an index greater than or equal to
246:             * <code>index</code> are shifted up by one to accommodate
247:             * the new item.  If <code>index</code> is greater than or
248:             * equal to the number of items in this choice,
249:             * <code>item</code> is added to the end of this choice.
250:             * <p>
251:             * If the item is the first one being added to the choice,
252:             * then the item becomes selected.  Otherwise, if the
253:             * selected item was one of the items shifted, the first
254:             * item in the choice becomes the selected item.  If the
255:             * selected item was no among those shifted, it remains
256:             * the selected item.
257:             * @param item the non-<code>null</code> item to be inserted
258:             * @param index the position at which the item should be inserted
259:             * @exception IllegalArgumentException if index is less than 0
260:             */
261:            public void insert(String item, int index) {
262:                synchronized (this ) {
263:                    if (index < 0) {
264:                        throw new IllegalArgumentException(
265:                                "index less than zero.");
266:                    }
267:                    /* if the index greater than item count, add item to the end */
268:                    index = Math.min(index, pItems.size());
269:
270:                    insertNoInvalidate(item, index);
271:                }
272:
273:                // This could change the preferred size of the Component.
274:                if (valid) {
275:                    invalidate();
276:                }
277:            }
278:
279:            /**
280:             * Removes the first occurrence of <code>item</code>
281:             * from the <code>Choice</code> menu.  If the item
282:             * being removed is the currently selected item,
283:             * then the first item in the choice becomes the
284:             * selected item.  Otherwise, the currently selected
285:             * item remains selected (and the selected index is
286:             * updated accordingly).
287:             * @param      item  the item to remove from this <code>Choice</code> menu
288:             * @exception  IllegalArgumentException  if the item doesn't
289:             *                     exist in the choice menu
290:             * @since      JDK1.1
291:             */
292:            public void remove(String item) {
293:                synchronized (this ) {
294:                    int index = pItems.indexOf(item);
295:                    if (index < 0) {
296:                        throw new IllegalArgumentException("item " + item
297:                                + " not found in choice");
298:                    } else {
299:                        removeNoInvalidate(index);
300:                    }
301:                }
302:
303:                // This could change the preferred size of the Component.
304:                if (valid) {
305:                    invalidate();
306:                }
307:            }
308:
309:            /**
310:             * Removes an item from the choice menu
311:             * at the specified position.  If the item
312:             * being removed is the currently selected item,
313:             * then the first item in the choice becomes the
314:             * selected item.  Otherwise, the currently selected
315:             * item remains selected (and the selected index is
316:             * updated accordingly).
317:             * @param      position the position of the item
318:             * @throws IndexOutOfBoundsException if the specified 
319:             * 		position is out of bounds
320:             * @since      JDK1.1
321:             */
322:            public void remove(int position) {
323:                synchronized (this ) {
324:                    removeNoInvalidate(position);
325:                }
326:
327:                // This could change the preferred size of the Component.
328:                if (valid) {
329:                    invalidate();
330:                }
331:            }
332:
333:            /**
334:             * Removes an item from the <code>Choice</code> at the
335:             * specified position, but does not invalidate the <code>Choice</code>.
336:             * Client methods must provide their
337:             * own synchronization before invoking this method.
338:             * @param      position   the position of the item
339:             */
340:            private void removeNoInvalidate(int position) {
341:                pItems.removeElementAt(position);
342:                ChoicePeer peer = (ChoicePeer) this .peer;
343:                if (peer != null) {
344:                    peer.remove(position);
345:                }
346:                /* Adjust selectedIndex if selected item was removed. */
347:                if (pItems.size() == 0) {
348:                    selectedIndex = -1;
349:                } else if (selectedIndex == position) {
350:                    select(0);
351:                } else if (selectedIndex > position) {
352:                    select(selectedIndex - 1);
353:                }
354:            }
355:
356:            /**
357:             * Removes all items from the choice menu.
358:             * @see       #remove
359:             * @since     JDK1.1
360:             */
361:            public void removeAll() {
362:                synchronized (this ) {
363:                    if (peer != null) {
364:                        ((ChoicePeer) peer).removeAll();
365:                    }
366:                    pItems.removeAllElements();
367:                    selectedIndex = -1;
368:                }
369:
370:                // This could change the preferred size of the Component.
371:                if (valid) {
372:                    invalidate();
373:                }
374:            }
375:
376:            /**
377:             * Gets a representation of the current choice as a string.
378:             * @return    a string representation of the currently
379:             *                     selected item in this choice menu
380:             * @see       #getSelectedIndex
381:             */
382:            public synchronized String getSelectedItem() {
383:                return (selectedIndex >= 0) ? getItem(selectedIndex) : null;
384:            }
385:
386:            /**
387:             * Returns an array (length 1) containing the currently selected
388:             * item.  If this choice has no items, returns <code>null</code>.
389:             * @see ItemSelectable
390:             */
391:            public synchronized Object[] getSelectedObjects() {
392:                if (selectedIndex >= 0) {
393:                    Object[] items = new Object[1];
394:                    items[0] = getItem(selectedIndex);
395:                    return items;
396:                }
397:                return null;
398:            }
399:
400:            /**
401:             * Returns the index of the currently selected item.
402:             * If nothing is selected, returns -1.
403:             *
404:             * @return the index of the currently selected item, or -1 if nothing
405:             *  is currently selected
406:             * @see #getSelectedItem
407:             */
408:            public int getSelectedIndex() {
409:                return selectedIndex;
410:            }
411:
412:            /**
413:             * Sets the selected item in this <code>Choice</code> menu to be the
414:             * item at the specified position.
415:             *
416:             * <p>Note that this method should be primarily used to
417:             * initially select an item in this component.  
418:             * Programmatically calling this method will <i>not</i> trigger
419:             * an <code>ItemEvent</code>.  The only way to trigger an
420:             * <code>ItemEvent</code> is by user interaction.
421:             *
422:             * @param      pos      the positon of the selected item
423:             * @exception  IllegalArgumentException if the specified
424:             *                            position is greater than the
425:             *				  number of items or less than zero
426:             * @see        #getSelectedItem
427:             * @see        #getSelectedIndex
428:             */
429:            public synchronized void select(int pos) {
430:                if ((pos >= pItems.size()) || (pos < 0)) {
431:                    throw new IllegalArgumentException(
432:                            "illegal Choice item position: " + pos);
433:                }
434:                if (pItems.size() > 0) {
435:                    selectedIndex = pos;
436:                    ChoicePeer peer = (ChoicePeer) this .peer;
437:                    if (peer != null) {
438:                        peer.select(pos);
439:                    }
440:                }
441:            }
442:
443:            /**
444:             * Sets the selected item in this <code>Choice</code> menu
445:             * to be the item whose name is equal to the specified string.
446:             * If more than one item matches (is equal to) the specified string,
447:             * the one with the smallest index is selected.
448:             *
449:             * <p>Note that this method should be primarily used to
450:             * initially select an item in this component.  
451:             * Programmatically calling this method will <i>not</i> trigger
452:             * an <code>ItemEvent</code>.  The only way to trigger an
453:             * <code>ItemEvent</code> is by user interaction.
454:             *
455:             * @param       str     the specified string
456:             * @see         #getSelectedItem
457:             * @see         #getSelectedIndex
458:             */
459:            public synchronized void select(String str) {
460:                int index = pItems.indexOf(str);
461:                if (index >= 0) {
462:                    select(index);
463:                }
464:            }
465:
466:            /**
467:             * Adds the specified item listener to receive item events from
468:             * this <code>Choice</code> menu.  Item events are sent in response
469:             * to user input, but not in response to calls to <code>select</code>.
470:             * If l is <code>null</code>, no exception is thrown and no action
471:             * is performed.
472:             * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
473:             * >AWT Threading Issues</a> for details on AWT's threading model.
474:             * @param         l    the item listener
475:             * @see           #removeItemListener
476:             * @see           #getItemListeners
477:             * @see           #select
478:             * @see           java.awt.event.ItemEvent
479:             * @see           java.awt.event.ItemListener
480:             * @since         JDK1.1
481:             */
482:            public synchronized void addItemListener(ItemListener l) {
483:                if (l == null) {
484:                    return;
485:                }
486:                itemListener = AWTEventMulticaster.add(itemListener, l);
487:                newEventsOnly = true;
488:            }
489:
490:            /**
491:             * Removes the specified item listener so that it no longer receives
492:             * item events from this <code>Choice</code> menu.
493:             * If l is <code>null</code>, no exception is thrown and no
494:             * action is performed.
495:             * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
496:             * >AWT Threading Issues</a> for details on AWT's threading model.
497:             * @param         l    the item listener
498:             * @see           #addItemListener
499:             * @see           #getItemListeners
500:             * @see           java.awt.event.ItemEvent
501:             * @see           java.awt.event.ItemListener
502:             * @since         JDK1.1
503:             */
504:            public synchronized void removeItemListener(ItemListener l) {
505:                if (l == null) {
506:                    return;
507:                }
508:                itemListener = AWTEventMulticaster.remove(itemListener, l);
509:            }
510:
511:            /**
512:             * Returns an array of all the item listeners
513:             * registered on this choice.
514:             *
515:             * @return all of this choice's <code>ItemListener</code>s
516:             *         or an empty array if no item 
517:             *         listeners are currently registered
518:             *
519:             * @see           #addItemListener
520:             * @see           #removeItemListener
521:             * @see           java.awt.event.ItemEvent
522:             * @see           java.awt.event.ItemListener
523:             * @since 1.4
524:             */
525:            public synchronized ItemListener[] getItemListeners() {
526:                return (ItemListener[]) (getListeners(ItemListener.class));
527:            }
528:
529:            /**
530:             * Returns an array of all the objects currently registered
531:             * as <code><em>Foo</em>Listener</code>s
532:             * upon this <code>Choice</code>.
533:             * <code><em>Foo</em>Listener</code>s are registered using the
534:             * <code>add<em>Foo</em>Listener</code> method.
535:             *
536:             * <p>
537:             * You can specify the <code>listenerType</code> argument
538:             * with a class literal, such as
539:             * <code><em>Foo</em>Listener.class</code>.
540:             * For example, you can query a
541:             * <code>Choice</code> <code>c</code>
542:             * for its item listeners with the following code:
543:             *
544:             * <pre>ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));</pre>
545:             *
546:             * If no such listeners exist, this method returns an empty array.
547:             *
548:             * @param listenerType the type of listeners requested; this parameter
549:             *          should specify an interface that descends from
550:             *          <code>java.util.EventListener</code>
551:             * @return an array of all objects registered as
552:             *          <code><em>Foo</em>Listener</code>s on this choice,
553:             *          or an empty array if no such
554:             *          listeners have been added
555:             * @exception ClassCastException if <code>listenerType</code>
556:             *          doesn't specify a class or interface that implements
557:             *          <code>java.util.EventListener</code>
558:             *
559:             * @see #getItemListeners
560:             * @since 1.3
561:             */
562:            public <T extends EventListener> T[] getListeners(
563:                    Class<T> listenerType) {
564:                EventListener l = null;
565:                if (listenerType == ItemListener.class) {
566:                    l = itemListener;
567:                } else {
568:                    return super .getListeners(listenerType);
569:                }
570:                return AWTEventMulticaster.getListeners(l, listenerType);
571:            }
572:
573:            // REMIND: remove when filtering is done at lower level
574:            boolean eventEnabled(AWTEvent e) {
575:                if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
576:                    if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0
577:                            || itemListener != null) {
578:                        return true;
579:                    }
580:                    return false;
581:                }
582:                return super .eventEnabled(e);
583:            }
584:
585:            /**
586:             * Processes events on this choice. If the event is an
587:             * instance of <code>ItemEvent</code>, it invokes the
588:             * <code>processItemEvent</code> method. Otherwise, it calls its
589:             * superclass's <code>processEvent</code> method.
590:             * <p>Note that if the event parameter is <code>null</code>
591:             * the behavior is unspecified and may result in an
592:             * exception.
593:             *
594:             * @param      e the event
595:             * @see        java.awt.event.ItemEvent
596:             * @see        #processItemEvent
597:             * @since      JDK1.1
598:             */
599:            protected void processEvent(AWTEvent e) {
600:                if (e instanceof  ItemEvent) {
601:                    processItemEvent((ItemEvent) e);
602:                    return;
603:                }
604:                super .processEvent(e);
605:            }
606:
607:            /**
608:             * Processes item events occurring on this <code>Choice</code>
609:             * menu by dispatching them to any registered
610:             * <code>ItemListener</code> objects.
611:             * <p>
612:             * This method is not called unless item events are
613:             * enabled for this component. Item events are enabled
614:             * when one of the following occurs:
615:             * <p><ul>
616:             * <li>An <code>ItemListener</code> object is registered
617:             * via <code>addItemListener</code>.
618:             * <li>Item events are enabled via <code>enableEvents</code>.
619:             * </ul>
620:             * <p>Note that if the event parameter is <code>null</code>
621:             * the behavior is unspecified and may result in an
622:             * exception.
623:             *
624:             * @param       e the item event
625:             * @see         java.awt.event.ItemEvent
626:             * @see         java.awt.event.ItemListener
627:             * @see         #addItemListener(ItemListener)
628:             * @see         java.awt.Component#enableEvents
629:             * @since       JDK1.1
630:             */
631:            protected void processItemEvent(ItemEvent e) {
632:                ItemListener listener = itemListener;
633:                if (listener != null) {
634:                    listener.itemStateChanged(e);
635:                }
636:            }
637:
638:            /**
639:             * Returns a string representing the state of this <code>Choice</code>
640:             * menu. This method is intended to be used only for debugging purposes,
641:             * and the content and format of the returned string may vary between
642:             * implementations. The returned string may be empty but may not be
643:             * <code>null</code>.
644:             *
645:             * @return    the parameter string of this <code>Choice</code> menu
646:             */
647:            protected String paramString() {
648:                return super .paramString() + ",current=" + getSelectedItem();
649:            }
650:
651:            /* Serialization support.
652:             */
653:
654:            /*
655:             * Choice Serial Data Version.
656:             * @serial
657:             */
658:            private int choiceSerializedDataVersion = 1;
659:
660:            /**
661:             * Writes default serializable fields to stream.  Writes
662:             * a list of serializable <code>ItemListeners</code>
663:             * as optional data. The non-serializable
664:             * <code>ItemListeners</code> are detected and
665:             * no attempt is made to serialize them.
666:             *
667:             * @param s the <code>ObjectOutputStream</code> to write
668:             * @serialData <code>null</code> terminated sequence of 0 
669:             *   or more pairs; the pair consists of a <code>String</code> 
670:             *   and an <code>Object</code>; the <code>String</code> indicates
671:             *   the type of object and is one of the following:
672:             *   <code>itemListenerK</code> indicating an 
673:             *     <code>ItemListener</code> object
674:             *
675:             * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
676:             * @see java.awt.Component#itemListenerK
677:             * @see #readObject(ObjectInputStream)
678:             */
679:            private void writeObject(ObjectOutputStream s)
680:                    throws java.io.IOException {
681:                s.defaultWriteObject();
682:
683:                AWTEventMulticaster.save(s, itemListenerK, itemListener);
684:                s.writeObject(null);
685:            }
686:
687:            /**
688:             * Reads the <code>ObjectInputStream</code> and if it
689:             * isn't <code>null</code> adds a listener to receive
690:             * item events fired by the <code>Choice</code> item.
691:             * Unrecognized keys or values will be ignored.
692:             *
693:             * @param s the <code>ObjectInputStream</code> to read
694:             * @exception HeadlessException if
695:             *   <code>GraphicsEnvironment.isHeadless</code> returns
696:             *   <code>true</code>
697:             * @serial
698:             * @see #removeItemListener(ItemListener)
699:             * @see #addItemListener(ItemListener)
700:             * @see java.awt.GraphicsEnvironment#isHeadless
701:             * @see #writeObject(ObjectOutputStream)
702:             */
703:            private void readObject(ObjectInputStream s)
704:                    throws ClassNotFoundException, IOException,
705:                    HeadlessException {
706:                GraphicsEnvironment.checkHeadless();
707:                s.defaultReadObject();
708:
709:                Object keyOrNull;
710:                while (null != (keyOrNull = s.readObject())) {
711:                    String key = ((String) keyOrNull).intern();
712:
713:                    if (itemListenerK == key)
714:                        addItemListener((ItemListener) (s.readObject()));
715:
716:                    else
717:                        // skip value for unrecognized key
718:                        s.readObject();
719:                }
720:            }
721:
722:            /////////////////
723:            // Accessibility support
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:                 * JDK 1.3 serialVersionUID
754:                 */
755:                private static final long serialVersionUID = 7175603582428509322L;
756:
757:                public AccessibleAWTChoice() {
758:                    super ();
759:                }
760:
761:                /**
762:                 * Get the AccessibleAction associated with this object.  In the
763:                 * implementation of the Java Accessibility API for this class, 
764:                 * return this object, which is responsible for implementing the
765:                 * AccessibleAction interface on behalf of itself.
766:                 * 
767:                 * @return this object
768:                 * @see AccessibleAction
769:                 */
770:                public AccessibleAction getAccessibleAction() {
771:                    return this ;
772:                }
773:
774:                /**
775:                 * Get the role of this object.
776:                 *
777:                 * @return an instance of AccessibleRole describing the role of the 
778:                 * object
779:                 * @see AccessibleRole
780:                 */
781:                public AccessibleRole getAccessibleRole() {
782:                    return AccessibleRole.COMBO_BOX;
783:                }
784:
785:                /**
786:                 * Returns the number of accessible actions available in this object
787:                 * If there are more than one, the first one is considered the "default"
788:                 * action of the object.
789:                 *
790:                 * @return the zero-based number of Actions in this object
791:                 */
792:                public int getAccessibleActionCount() {
793:                    return 0; //  To be fully implemented in a future release
794:                }
795:
796:                /**
797:                 * Returns a description of the specified action of the object.
798:                 *
799:                 * @param i zero-based index of the actions
800:                 * @return a String description of the action
801:                 * @see #getAccessibleActionCount
802:                 */
803:                public String getAccessibleActionDescription(int i) {
804:                    return null; //  To be fully implemented in a future release
805:                }
806:
807:                /**
808:                 * Perform the specified Action on the object
809:                 *
810:                 * @param i zero-based index of actions
811:                 * @return true if the action was performed; otherwise false.
812:                 * @see #getAccessibleActionCount
813:                 */
814:                public boolean doAccessibleAction(int i) {
815:                    return false; //  To be fully implemented in a future release
816:                }
817:
818:            } // inner class AccessibleAWTChoice
819:
820:        }
w_w__w___.j__a_v__a__2_s.__c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.