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