home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / ComponentOrientation.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.8 KB  |  174 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ComponentOrientation.java    1.4 98/06/29
  3.  *
  4.  * (C) Copyright IBM Corp. 1998 - All Rights Reserved
  5.  *
  6.  * Portions copyright (c) 1998 Sun Microsystems, Inc.
  7.  * All Rights Reserved.
  8.  *
  9.  * The original version of this source code and documentation is copyrighted
  10.  * and owned by IBM, Inc. These materials are provided under terms of a
  11.  * License Agreement between IBM and Sun. This technology is protected by
  12.  * multiple US and International patents. This notice and attribution to IBM
  13.  * may not be removed.
  14.  *
  15.  */
  16.  
  17. package java.awt;
  18.  
  19. import java.util.Locale;
  20. import java.util.ResourceBundle;
  21.  
  22. /**
  23.   * The ComponentOrientation class encapsulates the language-sensitive
  24.   * orientation that is to be used to order the elements of a component
  25.   * or of text. It is used to reflect the differences in this ordering
  26.   * between Western alphabets, Middle Eastern (such as Hebrew), and Far
  27.   * Eastern (such as Japanese).
  28.   * <p>
  29.   * Fundamentally, this governs items (such as characters) which are laid out
  30.   * in lines, with the lines then laid out in a block. This also applies
  31.   * to items in a widget: for example, in a check box where the box is
  32.   * positioned relative to the text.
  33.   * <p>
  34.   * There are four different orientations used in modern languages
  35.   * as in the following table.<br>
  36.   * <pre>
  37.   * LT          RT          TL          TR   
  38.   * A B C       C B A       A D G       G D A
  39.   * D E F       F E D       B E H       H E B
  40.   * G H I       I H G       C F I       I F C
  41.   * </pre><br>
  42.   * (In the header, the two-letter abbreviation represents the item direction
  43.   * in the first letter, and the line direction in the second. For example,
  44.   * LT means "items left-to-right, lines top-to-bottom",
  45.   * BL means "items bottom-to-top, lines bottom-to-top", and so on.)
  46.   * <p>
  47.   * The orientations are:
  48.   * <ul>
  49.   * <li>LT - Western Europe (optional for Japanese, Chinese, Korean)
  50.   * <li>RT - Middle East (Arabic, Hebrew)
  51.   * <li>TR - Japanese, Chinese, Korean
  52.   * <li>TL - Mongolian
  53.   * </ul>
  54.   * Components whose view and controller code depends on orientation
  55.   * should use the <code>isLeftToRight()</code> and
  56.   * <code>isHorizontal()</code> methods to
  57.   * determine their behavior. They should not include switch-like
  58.   * code that keys off of the constants, such as:
  59.   * <pre>
  60.   * if (orientation == LEFT_TO_RIGHT) {
  61.   *   ...
  62.   * } else if (orientation == RIGHT_TO_LEFT) {
  63.   *   ...
  64.   * } else {
  65.   *   // Oops
  66.   * }
  67.   * </pre>
  68.   * This is unsafe, since more constants may be added in the future and
  69.   * since it is not guaranteed that orientation objects will be unique.
  70.   */
  71. public final class ComponentOrientation implements java.io.Serializable
  72. {
  73.     // Internal constants used in the implementation
  74.     private static final int UNK_BIT      = 1;
  75.     private static final int HORIZ_BIT    = 2;
  76.     private static final int LTR_BIT      = 4;
  77.  
  78.     /**
  79.      * Items run left to right and lines flow top to bottom
  80.      * Examples: English, French.
  81.      */
  82.     public static final ComponentOrientation LEFT_TO_RIGHT =
  83.                     new ComponentOrientation(HORIZ_BIT|LTR_BIT);
  84.  
  85.     /**
  86.      * Items run right to left and lines flow top to bottom
  87.      * Examples: Arabic, Hebrew.
  88.      */
  89.     public static final ComponentOrientation RIGHT_TO_LEFT =
  90.                     new ComponentOrientation(HORIZ_BIT);
  91.  
  92.     /**
  93.      * Indicates that a component's orientation has not been set.
  94.      * To preserve the behavior of existing applications,
  95.      * isLeftToRight will return true for this value.
  96.      */
  97.     public static final ComponentOrientation UNKNOWN =
  98.                     new ComponentOrientation(HORIZ_BIT|LTR_BIT|UNK_BIT);
  99.  
  100.     /**
  101.      * Are lines horizontal?
  102.      * This will return true for horizontal, left-to-right writing
  103.      * systems such as Roman.
  104.      */
  105.     public boolean isHorizontal() {
  106.         return (orientation & HORIZ_BIT) != 0;
  107.     }
  108.  
  109.     /**
  110.      * HorizontalLines: Do items run left-to-right?<br>
  111.      * Vertical Lines:  Do lines run left-to-right?<br>
  112.      * This will return true for horizontal, left-to-right writing
  113.      * systems such as Roman.
  114.      */
  115.     public boolean isLeftToRight() {
  116.         return (orientation & LTR_BIT) != 0;
  117.     }
  118.  
  119.     /**
  120.      * Return the orientation that is appropriate for the given locale
  121.      * @param locale used as a key to look up the orientation in an
  122.      *        internal resource bundle.
  123.      */
  124.     public static ComponentOrientation getOrientation(Locale locale) {
  125.         ComponentOrientation result = UNKNOWN;
  126.  
  127.         try {
  128.             ResourceBundle resource = ResourceBundle.getBundle
  129.                     ("java.text.resources.LocaleElements", locale);
  130.             result = (ComponentOrientation)resource.getObject("Orientation");
  131.         }
  132.         catch (Exception e) {
  133.         }
  134.         return result;
  135.     }
  136.  
  137.     /**
  138.      * Return the orientation appropriate for the given ResourceBundle's
  139.      * localization.  Three approaches are tried, inn the following order:
  140.      * <ol>
  141.      * <li>Retrieve a ComponentOrientation object from the ResourceBundle
  142.      *      using the string "Orientation" as the key.
  143.      * <li>Use the ResourceBundle.getLocale to determine the bundle's
  144.      *      locale, then return the orientation for that locale.
  145.      * <li>Return the default locale's orientation.
  146.      * </ol>
  147.      */
  148.     public static ComponentOrientation getOrientation(ResourceBundle bdl)
  149.     {
  150.         ComponentOrientation result = null;
  151.  
  152.         try {
  153.             result = (ComponentOrientation)bdl.getObject("Orientation");
  154.         }
  155.         catch (Exception e) {
  156.         }
  157.  
  158.         if (result == null) {
  159.             result = getOrientation(bdl.getLocale());
  160.         }
  161.         if (result == null) {
  162.             result = getOrientation(Locale.getDefault());
  163.         }
  164.         return result;
  165.     }
  166.  
  167.     private int orientation;
  168.  
  169.     private ComponentOrientation(int value)
  170.     {
  171.         orientation = value;
  172.     }
  173.  }
  174.