jdk/src/share/classes/java/awt/ComponentOrientation.java
author tbell
Wed, 07 Oct 2009 14:15:01 -0700
changeset 3965 63aae8ce7f47
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright IBM Corp. 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * and owned by IBM, Inc. These materials are provided under terms of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * License Agreement between IBM and Sun. This technology is protected by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * multiple US and International patents. This notice and attribution to IBM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  * The ComponentOrientation class encapsulates the language-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * orientation that is to be used to order the elements of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * or of text. It is used to reflect the differences in this ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  * between Western alphabets, Middle Eastern (such as Hebrew), and Far
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * Eastern (such as Japanese).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  * Fundamentally, this governs items (such as characters) which are laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * in lines, with the lines then laid out in a block. This also applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  * to items in a widget: for example, in a check box where the box is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  * positioned relative to the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  * There are four different orientations used in modern languages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  * as in the following table.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  * LT          RT          TL          TR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  * A B C       C B A       A D G       G D A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  * D E F       F E D       B E H       H E B
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  * G H I       I H G       C F I       I F C
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  * </pre><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  * (In the header, the two-letter abbreviation represents the item direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  * in the first letter, and the line direction in the second. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  * LT means "items left-to-right, lines top-to-bottom",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  * TL means "items top-to-bottom, lines left-to-right", and so on.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  * The orientations are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  * <li>LT - Western Europe (optional for Japanese, Chinese, Korean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  * <li>RT - Middle East (Arabic, Hebrew)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  * <li>TR - Japanese, Chinese, Korean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  * <li>TL - Mongolian
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  * Components whose view and controller code depends on orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  * should use the <code>isLeftToRight()</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  * <code>isHorizontal()</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  * determine their behavior. They should not include switch-like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  * code that keys off of the constants, such as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  * if (orientation == LEFT_TO_RIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  * } else if (orientation == RIGHT_TO_LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  * } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  *   // Oops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  * This is unsafe, since more constants may be added in the future and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  * since it is not guaranteed that orientation objects will be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
public final class ComponentOrientation implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final long serialVersionUID = -4113291392143563828L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // Internal constants used in the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final int UNK_BIT      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final int HORIZ_BIT    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int LTR_BIT      = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Items run left to right and lines flow top to bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Examples: English, French.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final ComponentOrientation LEFT_TO_RIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    new ComponentOrientation(HORIZ_BIT|LTR_BIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Items run right to left and lines flow top to bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Examples: Arabic, Hebrew.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static final ComponentOrientation RIGHT_TO_LEFT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    new ComponentOrientation(HORIZ_BIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Indicates that a component's orientation has not been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * To preserve the behavior of existing applications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * isLeftToRight will return true for this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static final ComponentOrientation UNKNOWN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    new ComponentOrientation(HORIZ_BIT|LTR_BIT|UNK_BIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Are lines horizontal?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * This will return true for horizontal, left-to-right writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * systems such as Roman.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public boolean isHorizontal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return (orientation & HORIZ_BIT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * HorizontalLines: Do items run left-to-right?<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Vertical Lines:  Do lines run left-to-right?<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * This will return true for horizontal, left-to-right writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * systems such as Roman.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public boolean isLeftToRight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return (orientation & LTR_BIT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the orientation that is appropriate for the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param locale the specified locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static ComponentOrientation getOrientation(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // A more flexible implementation would consult a ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // to find the appropriate orientation.  Until pluggable locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // are introduced however, the flexiblity isn't really needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // So we choose efficiency instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String lang = locale.getLanguage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if( "iw".equals(lang) || "ar".equals(lang)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            || "fa".equals(lang) || "ur".equals(lang) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return RIGHT_TO_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return LEFT_TO_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns the orientation appropriate for the given ResourceBundle's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * localization.  Three approaches are tried, in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <li>Retrieve a ComponentOrientation object from the ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *      using the string "Orientation" as the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <li>Use the ResourceBundle.getLocale to determine the bundle's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *      locale, then return the orientation for that locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <li>Return the default locale's orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @deprecated As of J2SE 1.4, use {@link #getOrientation(java.util.Locale)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static ComponentOrientation getOrientation(ResourceBundle bdl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        ComponentOrientation result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            result = (ComponentOrientation)bdl.getObject("Orientation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            result = getOrientation(bdl.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            result = getOrientation(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private int orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private ComponentOrientation(int value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        orientation = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 }