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