home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / AccessibleStateSet.java < prev    next >
Text File  |  1998-02-26  |  6KB  |  195 lines

  1. /*
  2.  * @(#)AccessibleStateSet.java    1.7 98/02/04 
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.accessibility;
  22.  
  23. import java.util.Vector;
  24. import java.util.Locale;
  25. import java.util.MissingResourceException;
  26. import java.util.ResourceBundle;
  27.  
  28. /**
  29.  * Class AccessibleStateSet determines a components state set.  The state set
  30.  * of a component is a set of AccessibleState objects and descriptions the
  31.  * current overall state of the object, such as whether it is enabled, 
  32.  * has focus, etc.
  33.  *
  34.  * @see AccessibleState
  35.  *
  36.  * @version     1.7 02/04/98 11:13:02
  37.  * @author      Willie Walker
  38.  */
  39. public class AccessibleStateSet {
  40.  
  41.     /**
  42.      * Each entry in the Vector represents an AccessibleState.
  43.      * @see #add
  44.      * @see #addAll
  45.      * @see #remove
  46.      * @see #contains
  47.      * @see #toArray
  48.      * @see #clear
  49.      */
  50.     protected Vector states = null;
  51.  
  52.     /**
  53.      * Create a new empty state set.
  54.      */
  55.     public AccessibleStateSet() {
  56.         states = null;
  57.     }
  58.  
  59.     /**
  60.      * Create a new state with the initial set of states contained in 
  61.      * the array of states passed in.  Duplicate entries are ignored.
  62.      * @param state an array of AccessibleState describing the state set.
  63.      */
  64.     public AccessibleStateSet(AccessibleState[] states) {
  65.         if (states.length != 0) {
  66.             this.states = new Vector(states.length);
  67.             for (int i = 0; i < states.length; i++) {
  68.                 if (!this.states.contains(states[i])) {
  69.                     this.states.addElement(states[i]);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Add a new state to the current state set if it is not already
  77.      * present.  If the state is already in the state set, the state
  78.      * set is unchanged and the return value is false.  Otherwise, 
  79.      * the state is added to the state set and the return value is
  80.      * true.
  81.      * @param state the state to add to the state set
  82.      * @return true if state is added to the state set; false if the state set 
  83.      * is unchanged
  84.      */
  85.     public boolean add(AccessibleState state) {
  86.         // [[[ PENDING:  WDW - the implementation of this does not need
  87.         // to always use a vector of states.  It could be improved by
  88.         // caching the states as a bit set.]]]
  89.         if (states == null) {
  90.             states = new Vector();
  91.         }
  92.  
  93.         if (!states.contains(state)) {
  94.             states.addElement(state);
  95.             return true;
  96.         } else {
  97.             return false;
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Add all of the states to the existing state set.  Duplicate entries 
  103.      * are ignored.
  104.      * @param state  AccessibleState array describing the state set.
  105.      */
  106.     public void addAll(AccessibleState[] states) {
  107.         if (states.length != 0) {
  108.             if (this.states == null) {
  109.         this.states = new Vector(states.length);
  110.             }
  111.             for (int i = 0; i < states.length; i++) {
  112.                 if (!this.states.contains(states[i])) {
  113.                     this.states.addElement(states[i]);
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.     /**
  120.      * Remove a state from the current state set.  If the state is not
  121.      * in the set, the state set will be unchanged and the return value
  122.      * will be false.  If the state is in the state set, it will be removed
  123.      * from the set and the return value will be true.
  124.      *    
  125.      * @param state the state to remove from the state set
  126.      * @return true is the state is in the state set; false if the state set 
  127.      * will be unchanged
  128.      */
  129.     public boolean remove(AccessibleState state) {
  130.         if (states == null) {
  131.             return false;
  132.         } else {
  133.             return states.removeElement(state);
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * Remove all the states from the current state set.
  139.      */
  140.     public void clear() {
  141.         if (states != null) {
  142.             states.removeAllElements();
  143.         }
  144.     }
  145.  
  146.     /**
  147.      * Check if the current state is in the state set.
  148.      * @param state the state
  149.      * @return true if the state is in the state set; otherwise false
  150.      */
  151.     public boolean contains(AccessibleState state) {
  152.         if (states == null) {
  153.             return false;
  154.         } else {
  155.             return states.contains(state);
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Returns the current state set as an array of AccessibleState
  161.      * @return AccessibleState array conatining the current state.
  162.      */
  163.     public AccessibleState[] toArray() {
  164.         if (states == null) {
  165.             return new AccessibleState[0];
  166.         } else {
  167.             AccessibleState[] stateArray = new AccessibleState[states.size()];
  168.             for (int i = 0; i < stateArray.length; i++) {
  169.                 stateArray[i] = (AccessibleState) states.elementAt(i);
  170.             }
  171.             return stateArray;
  172.         }
  173.     }
  174.  
  175.     /**
  176.      * Create a localized String representing all the states in the set 
  177.      * using the default locale.
  178.      *
  179.      * @return comma separated localized String
  180.      * @see AccessibleBundle#toDisplayString
  181.      */
  182.     public String toString() {
  183.         String ret = null;
  184.         if ((states != null) && (states.size() > 0)) {
  185.             ret = ((AccessibleState) (states.elementAt(0))).toDisplayString();
  186.             for (int i = 1; i < states.size(); i++) {
  187.                 ret = ret + "," 
  188.                         + ((AccessibleState) (states.elementAt(i))).
  189.                           toDisplayString();
  190.             }
  191.         }
  192.         return ret;
  193.     }
  194. }
  195.