Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/ComponentOrientation.java
38829 views
/*1* Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright IBM Corp. 1998 - All Rights Reserved27*28* The original version of this source code and documentation is copyrighted29* and owned by IBM, Inc. These materials are provided under terms of a30* License Agreement between IBM and Sun. This technology is protected by31* multiple US and International patents. This notice and attribution to IBM32* may not be removed.33*34*/3536package java.awt;3738import java.util.Locale;39import java.util.ResourceBundle;4041/**42* The ComponentOrientation class encapsulates the language-sensitive43* orientation that is to be used to order the elements of a component44* or of text. It is used to reflect the differences in this ordering45* between Western alphabets, Middle Eastern (such as Hebrew), and Far46* Eastern (such as Japanese).47* <p>48* Fundamentally, this governs items (such as characters) which are laid out49* in lines, with the lines then laid out in a block. This also applies50* to items in a widget: for example, in a check box where the box is51* positioned relative to the text.52* <p>53* There are four different orientations used in modern languages54* as in the following table.<br>55* <pre>56* LT RT TL TR57* A B C C B A A D G G D A58* D E F F E D B E H H E B59* G H I I H G C F I I F C60* </pre><br>61* (In the header, the two-letter abbreviation represents the item direction62* in the first letter, and the line direction in the second. For example,63* LT means "items left-to-right, lines top-to-bottom",64* TL means "items top-to-bottom, lines left-to-right", and so on.)65* <p>66* The orientations are:67* <ul>68* <li>LT - Western Europe (optional for Japanese, Chinese, Korean)69* <li>RT - Middle East (Arabic, Hebrew)70* <li>TR - Japanese, Chinese, Korean71* <li>TL - Mongolian72* </ul>73* Components whose view and controller code depends on orientation74* should use the <code>isLeftToRight()</code> and75* <code>isHorizontal()</code> methods to76* determine their behavior. They should not include switch-like77* code that keys off of the constants, such as:78* <pre>79* if (orientation == LEFT_TO_RIGHT) {80* ...81* } else if (orientation == RIGHT_TO_LEFT) {82* ...83* } else {84* // Oops85* }86* </pre>87* This is unsafe, since more constants may be added in the future and88* since it is not guaranteed that orientation objects will be unique.89*/90public final class ComponentOrientation implements java.io.Serializable91{92/*93* serialVersionUID94*/95private static final long serialVersionUID = -4113291392143563828L;9697// Internal constants used in the implementation98private static final int UNK_BIT = 1;99private static final int HORIZ_BIT = 2;100private static final int LTR_BIT = 4;101102/**103* Items run left to right and lines flow top to bottom104* Examples: English, French.105*/106public static final ComponentOrientation LEFT_TO_RIGHT =107new ComponentOrientation(HORIZ_BIT|LTR_BIT);108109/**110* Items run right to left and lines flow top to bottom111* Examples: Arabic, Hebrew.112*/113public static final ComponentOrientation RIGHT_TO_LEFT =114new ComponentOrientation(HORIZ_BIT);115116/**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*/121public static final ComponentOrientation UNKNOWN =122new ComponentOrientation(HORIZ_BIT|LTR_BIT|UNK_BIT);123124/**125* Are lines horizontal?126* This will return true for horizontal, left-to-right writing127* systems such as Roman.128*/129public boolean isHorizontal() {130return (orientation & HORIZ_BIT) != 0;131}132133/**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 writing137* systems such as Roman.138*/139public boolean isLeftToRight() {140return (orientation & LTR_BIT) != 0;141}142143/**144* Returns the orientation that is appropriate for the given locale.145* @param locale the specified locale146*/147public static ComponentOrientation getOrientation(Locale locale) {148// A more flexible implementation would consult a ResourceBundle149// to find the appropriate orientation. Until pluggable locales150// are introduced however, the flexiblity isn't really needed.151// So we choose efficiency instead.152String lang = locale.getLanguage();153if( "iw".equals(lang) || "ar".equals(lang)154|| "fa".equals(lang) || "ur".equals(lang) )155{156return RIGHT_TO_LEFT;157} else {158return LEFT_TO_RIGHT;159}160}161162/**163* Returns the orientation appropriate for the given ResourceBundle's164* localization. Three approaches are tried, in the following order:165* <ol>166* <li>Retrieve a ComponentOrientation object from the ResourceBundle167* using the string "Orientation" as the key.168* <li>Use the ResourceBundle.getLocale to determine the bundle's169* locale, then return the orientation for that locale.170* <li>Return the default locale's orientation.171* </ol>172*173* @deprecated As of J2SE 1.4, use {@link #getOrientation(java.util.Locale)}.174*/175@Deprecated176public static ComponentOrientation getOrientation(ResourceBundle bdl)177{178ComponentOrientation result = null;179180try {181result = (ComponentOrientation)bdl.getObject("Orientation");182}183catch (Exception e) {184}185186if (result == null) {187result = getOrientation(bdl.getLocale());188}189if (result == null) {190result = getOrientation(Locale.getDefault());191}192return result;193}194195private int orientation;196197private ComponentOrientation(int value)198{199orientation = value;200}201}202203204