Path: blob/master/src/java.desktop/share/classes/java/awt/ComponentOrientation.java
41152 views
/*1* Copyright (c) 1998, 2021, 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.io.Serial;39import java.util.Locale;40import java.util.ResourceBundle;4142/**43* The ComponentOrientation class encapsulates the language-sensitive44* orientation that is to be used to order the elements of a component45* or of text. It is used to reflect the differences in this ordering46* between Western alphabets, Middle Eastern (such as Hebrew), and Far47* Eastern (such as Japanese).48* <p>49* Fundamentally, this governs items (such as characters) which are laid out50* in lines, with the lines then laid out in a block. This also applies51* to items in a widget: for example, in a check box where the box is52* positioned relative to the text.53* <p>54* There are four different orientations used in modern languages55* as in the following table.<br>56* <pre>57* LT RT TL TR58* A B C C B A A D G G D A59* D E F F E D B E H H E B60* G H I I H G C F I I F C61* </pre><br>62* (In the header, the two-letter abbreviation represents the item direction63* in the first letter, and the line direction in the second. For example,64* LT means "items left-to-right, lines top-to-bottom",65* TL means "items top-to-bottom, lines left-to-right", and so on.)66* <p>67* The orientations are:68* <ul>69* <li>LT - Western Europe (optional for Japanese, Chinese, Korean)70* <li>RT - Middle East (Arabic, Hebrew)71* <li>TR - Japanese, Chinese, Korean72* <li>TL - Mongolian73* </ul>74* Components whose view and controller code depends on orientation75* should use the {@code isLeftToRight()} and76* {@code isHorizontal()} methods to77* determine their behavior. They should not include switch-like78* code that keys off of the constants, such as:79* <pre>80* if (orientation == LEFT_TO_RIGHT) {81* ...82* } else if (orientation == RIGHT_TO_LEFT) {83* ...84* } else {85* // Oops86* }87* </pre>88* This is unsafe, since more constants may be added in the future and89* since it is not guaranteed that orientation objects will be unique.90*/91public final class ComponentOrientation implements java.io.Serializable92{93/**94* Use serialVersionUID from JDK 1.6 for interoperability.95*/96@Serial97private static final long serialVersionUID = -4113291392143563828L;9899// Internal constants used in the implementation100private static final int UNK_BIT = 1;101private static final int HORIZ_BIT = 2;102private static final int LTR_BIT = 4;103104/**105* Items run left to right and lines flow top to bottom106* Examples: English, French.107*/108public static final ComponentOrientation LEFT_TO_RIGHT =109new ComponentOrientation(HORIZ_BIT|LTR_BIT);110111/**112* Items run right to left and lines flow top to bottom113* Examples: Arabic, Hebrew.114*/115public static final ComponentOrientation RIGHT_TO_LEFT =116new ComponentOrientation(HORIZ_BIT);117118/**119* Indicates that a component's orientation has not been set.120* To preserve the behavior of existing applications,121* isLeftToRight will return true for this value.122*/123public static final ComponentOrientation UNKNOWN =124new ComponentOrientation(HORIZ_BIT|LTR_BIT|UNK_BIT);125126/**127* Are lines horizontal?128* This will return true for horizontal, left-to-right writing129* systems such as Roman.130*131* @return {@code true} if this orientation has horizontal lines132*/133public boolean isHorizontal() {134return (orientation & HORIZ_BIT) != 0;135}136137/**138* HorizontalLines: Do items run left-to-right?<br>139* Vertical Lines: Do lines run left-to-right?<br>140* This will return true for horizontal, left-to-right writing141* systems such as Roman.142*143* @return {@code true} if this orientation is left-to-right144*/145public boolean isLeftToRight() {146return (orientation & LTR_BIT) != 0;147}148149/**150* Returns the orientation that is appropriate for the given locale.151*152* @param locale the specified locale153* @return the orientation for the locale154*/155public static ComponentOrientation getOrientation(Locale locale) {156// A more flexible implementation would consult a ResourceBundle157// to find the appropriate orientation. Until pluggable locales158// are introduced however, the flexibility isn't really needed.159// So we choose efficiency instead.160return switch (locale.getLanguage()) {161case "ar", "fa", "he", "iw", "ji", "ur", "yi" -> RIGHT_TO_LEFT;162default -> LEFT_TO_RIGHT;163};164}165166/**167* Returns the orientation appropriate for the given ResourceBundle's168* localization. Three approaches are tried, in the following order:169* <ol>170* <li>Retrieve a ComponentOrientation object from the ResourceBundle171* using the string "Orientation" as the key.172* <li>Use the ResourceBundle.getLocale to determine the bundle's173* locale, then return the orientation for that locale.174* <li>Return the default locale's orientation.175* </ol>176*177* @param bdl the bundle to use178* @return the orientation179* @deprecated As of J2SE 1.4, use {@link #getOrientation(java.util.Locale)}.180*/181@Deprecated182public static ComponentOrientation getOrientation(ResourceBundle bdl)183{184ComponentOrientation result = null;185186try {187result = (ComponentOrientation)bdl.getObject("Orientation");188}189catch (Exception e) {190}191192if (result == null) {193result = getOrientation(bdl.getLocale());194}195if (result == null) {196result = getOrientation(Locale.getDefault());197}198return result;199}200201/**202* The bitwise-ored combination of flags.203*/204private int orientation;205206private ComponentOrientation(int value)207{208orientation = value;209}210}211212213