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