home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Choice.java < prev    next >
Text File  |  1996-05-03  |  4KB  |  153 lines

  1. /*
  2.  * @(#)Choice.java    1.16 95/12/14 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.util.*;
  22. import java.awt.peer.ChoicePeer;
  23.  
  24. /**
  25.  * The Choice class is a pop-up menu of choices. The current choice is
  26.  * displayed as the title of the menu.
  27.  *
  28.  * @version    1.16 14 Dec 1995
  29.  * @author     Sami Shaio
  30.  * @author     Arthur van Hoff
  31.  */
  32. public class Choice extends Component {
  33.     /**
  34.      * The items for the Choice.
  35.      */
  36.     Vector pItems;
  37.  
  38.     /** 
  39.      * The index of the current choice for this Choice.
  40.      */
  41.     int selectedIndex = -1;
  42.  
  43.     /** 
  44.      * Constructs a new Choice.
  45.      */
  46.     public Choice() {
  47.     pItems = new Vector();
  48.     }
  49.  
  50.     /**
  51.      * Creates the Choice's peer.  This peer allows us to change the look
  52.      * of the Choice without changing its functionality.
  53.      */
  54.     public synchronized void addNotify() {
  55.     peer = getToolkit().createChoice(this);
  56.     super.addNotify();
  57.     }
  58.  
  59.     /**
  60.      * Returns the number of items in this Choice.
  61.      * @see #getItem
  62.      */
  63.     public int countItems() {
  64.     return pItems.size();
  65.     }
  66.  
  67.     /**
  68.      * Returns the String at the specified index in the Choice.
  69.      * @param index the index at which to begin
  70.      * @see #countItems
  71.      */
  72.     public String getItem(int index) {
  73.     return (String)pItems.elementAt(index);
  74.     }
  75.  
  76.     /**
  77.      * Adds an item to this Choice.
  78.      * @param item the item to be added
  79.      * @exception NullPointerException If the item's value is equal to null.
  80.      */
  81.     public synchronized void addItem(String item) {
  82.     if (item == null) {
  83.         throw new NullPointerException();
  84.     }
  85.     pItems.addElement(item);
  86.     ChoicePeer peer = (ChoicePeer)this.peer;
  87.     if (peer != null) {
  88.         peer.addItem(item, pItems.size() - 1);
  89.     }
  90.     if (selectedIndex < 0) {
  91.         select(0);
  92.     }
  93.     }
  94.  
  95.     /**
  96.      * Returns a String representation of the current choice.
  97.      * @see #getSelectedIndex
  98.      */
  99.     public String getSelectedItem() {
  100.     int selectedIndex = this.selectedIndex;
  101.     return (selectedIndex >= 0) ? getItem(selectedIndex) : null;
  102.     }
  103.  
  104.     /**
  105.      * Returns the index of the currently selected item.
  106.      * @see #getSelectedItem
  107.      */
  108.     public int getSelectedIndex() {
  109.     return selectedIndex;
  110.     }
  111.  
  112.     /**
  113.      * Selects the item with the specified postion.
  114.      * @param pos the choice item position
  115.      * @exception IllegalArgumentException If the choice item position is 
  116.      * invalid.
  117.      * @see #getSelectedItem
  118.      * @see #getSelectedIndex
  119.      */
  120.     public synchronized void select(int pos) {
  121.     if (pos >= pItems.size()) {
  122.         throw new IllegalArgumentException("illegal Choice item position: " + pos);
  123.     }
  124.     if (pItems.size() > 0) {
  125.         selectedIndex = pos;
  126.         ChoicePeer peer = (ChoicePeer)this.peer;
  127.         if (peer != null) {
  128.         peer.select(pos);
  129.         }
  130.     }
  131.     }
  132.  
  133.     /**
  134.      * Selects the item with the specified String.
  135.      * @param str the specified String
  136.      * @see #getSelectedItem
  137.      * @see #getSelectedIndex
  138.      */
  139.     public void select(String str) {
  140.     int index = pItems.indexOf(str);
  141.     if (index >= 0) {
  142.         select(index);
  143.     }
  144.     }
  145.  
  146.     /**
  147.      * Returns the parameter String of this Choice.
  148.      */
  149.     protected String paramString() {
  150.     return super.paramString() + ",current=" + getSelectedItem();
  151.     }
  152. }
  153.