001: /*
002: * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * (C) Copyright IBM Corp. 1998 - All Rights Reserved
028: *
029: * The original version of this source code and documentation is copyrighted
030: * and owned by IBM, Inc. These materials are provided under terms of a
031: * License Agreement between IBM and Sun. This technology is protected by
032: * multiple US and International patents. This notice and attribution to IBM
033: * may not be removed.
034: *
035: */
036:
037: package java.awt;
038:
039: import java.util.Locale;
040: import java.util.ResourceBundle;
041:
042: /**
043: * The ComponentOrientation class encapsulates the language-sensitive
044: * orientation that is to be used to order the elements of a component
045: * or of text. It is used to reflect the differences in this ordering
046: * between Western alphabets, Middle Eastern (such as Hebrew), and Far
047: * Eastern (such as Japanese).
048: * <p>
049: * Fundamentally, this governs items (such as characters) which are laid out
050: * in lines, with the lines then laid out in a block. This also applies
051: * to items in a widget: for example, in a check box where the box is
052: * positioned relative to the text.
053: * <p>
054: * There are four different orientations used in modern languages
055: * as in the following table.<br>
056: * <pre>
057: * LT RT TL TR
058: * A B C C B A A D G G D A
059: * D E F F E D B E H H E B
060: * G H I I H G C F I I F C
061: * </pre><br>
062: * (In the header, the two-letter abbreviation represents the item direction
063: * in the first letter, and the line direction in the second. For example,
064: * LT means "items left-to-right, lines top-to-bottom",
065: * TL means "items top-to-bottom, lines left-to-right", and so on.)
066: * <p>
067: * The orientations are:
068: * <ul>
069: * <li>LT - Western Europe (optional for Japanese, Chinese, Korean)
070: * <li>RT - Middle East (Arabic, Hebrew)
071: * <li>TR - Japanese, Chinese, Korean
072: * <li>TL - Mongolian
073: * </ul>
074: * Components whose view and controller code depends on orientation
075: * should use the <code>isLeftToRight()</code> and
076: * <code>isHorizontal()</code> methods to
077: * determine their behavior. They should not include switch-like
078: * code that keys off of the constants, such as:
079: * <pre>
080: * if (orientation == LEFT_TO_RIGHT) {
081: * ...
082: * } else if (orientation == RIGHT_TO_LEFT) {
083: * ...
084: * } else {
085: * // Oops
086: * }
087: * </pre>
088: * This is unsafe, since more constants may be added in the future and
089: * since it is not guaranteed that orientation objects will be unique.
090: */
091: public final class ComponentOrientation implements java.io.Serializable {
092: /*
093: * serialVersionUID
094: */
095: private static final long serialVersionUID = -4113291392143563828L;
096:
097: // Internal constants used in the implementation
098: private static final int UNK_BIT = 1;
099: private static final int HORIZ_BIT = 2;
100: private static final int LTR_BIT = 4;
101:
102: /**
103: * Items run left to right and lines flow top to bottom
104: * Examples: English, French.
105: */
106: public static final ComponentOrientation LEFT_TO_RIGHT = new ComponentOrientation(
107: HORIZ_BIT | LTR_BIT);
108:
109: /**
110: * Items run right to left and lines flow top to bottom
111: * Examples: Arabic, Hebrew.
112: */
113: public static final ComponentOrientation RIGHT_TO_LEFT = new ComponentOrientation(
114: HORIZ_BIT);
115:
116: /**
117: * Indicates that a component's orientation has not been set.
118: * To preserve the behavior of existing applications,
119: * isLeftToRight will return true for this value.
120: */
121: public static final ComponentOrientation UNKNOWN = new ComponentOrientation(
122: HORIZ_BIT | LTR_BIT | UNK_BIT);
123:
124: /**
125: * Are lines horizontal?
126: * This will return true for horizontal, left-to-right writing
127: * systems such as Roman.
128: */
129: public boolean isHorizontal() {
130: return (orientation & HORIZ_BIT) != 0;
131: }
132:
133: /**
134: * HorizontalLines: Do items run left-to-right?<br>
135: * Vertical Lines: Do lines run left-to-right?<br>
136: * This will return true for horizontal, left-to-right writing
137: * systems such as Roman.
138: */
139: public boolean isLeftToRight() {
140: return (orientation & LTR_BIT) != 0;
141: }
142:
143: /**
144: * Returns the orientation that is appropriate for the given locale.
145: * @param locale the specified locale
146: */
147: public static ComponentOrientation getOrientation(Locale locale) {
148: // A more flexible implementation would consult a ResourceBundle
149: // to find the appropriate orientation. Until pluggable locales
150: // are introduced however, the flexiblity isn't really needed.
151: // So we choose efficiency instead.
152: String lang = locale.getLanguage();
153: if ("iw".equals(lang) || "ar".equals(lang) || "fa".equals(lang)
154: || "ur".equals(lang)) {
155: return RIGHT_TO_LEFT;
156: } else {
157: return LEFT_TO_RIGHT;
158: }
159: }
160:
161: /**
162: * Returns the orientation appropriate for the given ResourceBundle's
163: * localization. Three approaches are tried, in the following order:
164: * <ol>
165: * <li>Retrieve a ComponentOrientation object from the ResourceBundle
166: * using the string "Orientation" as the key.
167: * <li>Use the ResourceBundle.getLocale to determine the bundle's
168: * locale, then return the orientation for that locale.
169: * <li>Return the default locale's orientation.
170: * </ol>
171: *
172: * @deprecated As of J2SE 1.4, use {@link #getOrientation(java.util.Locale)}.
173: */
174: @Deprecated
175: public static ComponentOrientation getOrientation(ResourceBundle bdl) {
176: ComponentOrientation result = null;
177:
178: try {
179: result = (ComponentOrientation) bdl
180: .getObject("Orientation");
181: } catch (Exception e) {
182: }
183:
184: if (result == null) {
185: result = getOrientation(bdl.getLocale());
186: }
187: if (result == null) {
188: result = getOrientation(Locale.getDefault());
189: }
190: return result;
191: }
192:
193: private int orientation;
194:
195: private ComponentOrientation(int value) {
196: orientation = value;
197: }
198: }
|