KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > ComponentOrientation


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 /*
9  * (C) Copyright IBM Corp. 1998 - All Rights Reserved
10  *
11  * The original version of this source code and documentation is copyrighted
12  * and owned by IBM, Inc. These materials are provided under terms of a
13  * License Agreement between IBM and Sun. This technology is protected by
14  * multiple US and International patents. This notice and attribution to IBM
15  * may not be removed.
16  *
17  */

18
19 package java.awt;
20
21 import java.util.Locale JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 /**
25   * The ComponentOrientation class encapsulates the language-sensitive
26   * orientation that is to be used to order the elements of a component
27   * or of text. It is used to reflect the differences in this ordering
28   * between Western alphabets, Middle Eastern (such as Hebrew), and Far
29   * Eastern (such as Japanese).
30   * <p>
31   * Fundamentally, this governs items (such as characters) which are laid out
32   * in lines, with the lines then laid out in a block. This also applies
33   * to items in a widget: for example, in a check box where the box is
34   * positioned relative to the text.
35   * <p>
36   * There are four different orientations used in modern languages
37   * as in the following table.<br>
38   * <pre>
39   * LT RT TL TR
40   * A B C C B A A D G G D A
41   * D E F F E D B E H H E B
42   * G H I I H G C F I I F C
43   * </pre><br>
44   * (In the header, the two-letter abbreviation represents the item direction
45   * in the first letter, and the line direction in the second. For example,
46   * LT means "items left-to-right, lines top-to-bottom",
47   * BL means "items bottom-to-top, lines bottom-to-top", and so on.)
48   * <p>
49   * The orientations are:
50   * <ul>
51   * <li>LT - Western Europe (optional for Japanese, Chinese, Korean)
52   * <li>RT - Middle East (Arabic, Hebrew)
53   * <li>TR - Japanese, Chinese, Korean
54   * <li>TL - Mongolian
55   * </ul>
56   * Components whose view and controller code depends on orientation
57   * should use the <code>isLeftToRight()</code> and
58   * <code>isHorizontal()</code> methods to
59   * determine their behavior. They should not include switch-like
60   * code that keys off of the constants, such as:
61   * <pre>
62   * if (orientation == LEFT_TO_RIGHT) {
63   * ...
64   * } else if (orientation == RIGHT_TO_LEFT) {
65   * ...
66   * } else {
67   * // Oops
68   * }
69   * </pre>
70   * This is unsafe, since more constants may be added in the future and
71   * since it is not guaranteed that orientation objects will be unique.
72   */

73 public final class ComponentOrientation implements java.io.Serializable JavaDoc
74 {
75     // Internal constants used in the implementation
76
private static final int UNK_BIT = 1;
77     private static final int HORIZ_BIT = 2;
78     private static final int LTR_BIT = 4;
79
80     /**
81      * Items run left to right and lines flow top to bottom
82      * Examples: English, French.
83      */

84     public static final ComponentOrientation JavaDoc LEFT_TO_RIGHT =
85                     new ComponentOrientation JavaDoc(HORIZ_BIT|LTR_BIT);
86
87     /**
88      * Items run right to left and lines flow top to bottom
89      * Examples: Arabic, Hebrew.
90      */

91     public static final ComponentOrientation JavaDoc RIGHT_TO_LEFT =
92                     new ComponentOrientation JavaDoc(HORIZ_BIT);
93
94     /**
95      * Indicates that a component's orientation has not been set.
96      * To preserve the behavior of existing applications,
97      * isLeftToRight will return true for this value.
98      */

99     public static final ComponentOrientation JavaDoc UNKNOWN =
100                     new ComponentOrientation JavaDoc(HORIZ_BIT|LTR_BIT|UNK_BIT);
101
102     /**
103      * Are lines horizontal?
104      * This will return true for horizontal, left-to-right writing
105      * systems such as Roman.
106      */

107     public boolean isHorizontal() {
108         return (orientation & HORIZ_BIT) != 0;
109     }
110
111     /**
112      * HorizontalLines: Do items run left-to-right?<br>
113      * Vertical Lines: Do lines run left-to-right?<br>
114      * This will return true for horizontal, left-to-right writing
115      * systems such as Roman.
116      */

117     public boolean isLeftToRight() {
118         return (orientation & LTR_BIT) != 0;
119     }
120
121     /**
122      * Returns the orientation that is appropriate for the given locale.
123      * @param locale the specified locale
124      */

125     public static ComponentOrientation JavaDoc getOrientation(Locale JavaDoc locale) {
126         // A more flexible implementation would consult a ResourceBundle
127
// to find the appropriate orientation. Until pluggable locales
128
// are introduced however, the flexiblity isn't really needed.
129
// So we choose efficiency instead.
130
String JavaDoc lang = locale.getLanguage();
131         if( "iw".equals(lang) || "ar".equals(lang)
132             || "fa".equals(lang) || "ur".equals(lang) )
133         {
134             return RIGHT_TO_LEFT;
135         } else {
136             return LEFT_TO_RIGHT;
137         }
138     }
139
140     /**
141      * Returns the orientation appropriate for the given ResourceBundle's
142      * localization. Three approaches are tried, in the following order:
143      * <ol>
144      * <li>Retrieve a ComponentOrientation object from the ResourceBundle
145      * using the string "Orientation" as the key.
146      * <li>Use the ResourceBundle.getLocale to determine the bundle's
147      * locale, then return the orientation for that locale.
148      * <li>Return the default locale's orientation.
149      * </ol>
150      *
151      * @deprecated As of J2SE 1.4, use {@link #getOrientation(java.util.Locale)}.
152      */

153     @Deprecated JavaDoc
154     public static ComponentOrientation JavaDoc getOrientation(ResourceBundle JavaDoc bdl)
155     {
156         ComponentOrientation JavaDoc result = null;
157
158         try {
159             result = (ComponentOrientation JavaDoc)bdl.getObject("Orientation");
160         }
161         catch (Exception JavaDoc e) {
162         }
163
164         if (result == null) {
165             result = getOrientation(bdl.getLocale());
166         }
167         if (result == null) {
168             result = getOrientation(Locale.getDefault());
169         }
170         return result;
171     }
172
173     private int orientation;
174
175     private ComponentOrientation(int value)
176     {
177         orientation = value;
178     }
179  }
180
Popular Tags