| 1 | /* ComponentOrientation.java -- describes a component's orientation
|
|---|
| 2 | Copyright (C) 2000, 2001, 2002 Free Software Foundation
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.io.Serializable;
|
|---|
| 42 | import java.util.Locale;
|
|---|
| 43 | import java.util.MissingResourceException;
|
|---|
| 44 | import java.util.ResourceBundle;
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * This class is used to differentiate different orientations for text layout.
|
|---|
| 48 | * It controls whether text flows left-to-right or right-to-left, and whether
|
|---|
| 49 | * lines are horizontal or vertical, as in this table:<br>
|
|---|
| 50 | * <pre>
|
|---|
| 51 | * LT RT TL TR
|
|---|
| 52 | * A B C C B A A D G G D A
|
|---|
| 53 | * D E F F E D B E H H E B
|
|---|
| 54 | * G H I I H G C F I I F C
|
|---|
| 55 | * </pre>
|
|---|
| 56 | * <b>LT</b> languages are most common (left-to-right lines, top-to-bottom).
|
|---|
| 57 | * This includes Western European languages, and optionally includes Japanese,
|
|---|
| 58 | * Chinese, and Korean. <b>RT</b> languages (right-to-left lines,
|
|---|
| 59 | * top-to-bottom) are mainly middle eastern, such as Hebrew and Arabic.
|
|---|
| 60 | * <b>TR</b> languages flow top-to-bottom in a line, right-to-left, and are
|
|---|
| 61 | * the basis of Japanese, Chinese, and Korean. Finally, <b>TL</b> languages
|
|---|
| 62 | * flow top-to-bottom in a line, left-to-right, as in Mongolian.
|
|---|
| 63 | *
|
|---|
| 64 | * <p>This is a pretty poor excuse for a type-safe enum, since it is not
|
|---|
| 65 | * guaranteed that orientation objects are unique (thanks to serialization),
|
|---|
| 66 | * yet there is no equals() method. You would be wise to compare the output
|
|---|
| 67 | * of isHorizontal() and isLeftToRight() rather than comparing objects with
|
|---|
| 68 | * ==, especially since more constants may be added in the future.
|
|---|
| 69 | *
|
|---|
| 70 | * @author Bryce McKinlay <[email protected]>
|
|---|
| 71 | * @since 1.0
|
|---|
| 72 | * @status updated to 1.4
|
|---|
| 73 | */
|
|---|
| 74 | public final class ComponentOrientation implements Serializable
|
|---|
| 75 | {
|
|---|
| 76 | /**
|
|---|
| 77 | * Compatible with JDK 1.0+.
|
|---|
| 78 | */
|
|---|
| 79 | private static final long serialVersionUID = -4113291392143563828L;
|
|---|
| 80 |
|
|---|
| 81 | /** Constant for unknown orientation. */
|
|---|
| 82 | private static final int UNKNOWN_ID = 1;
|
|---|
| 83 |
|
|---|
| 84 | /** Constant for horizontal line orientation. */
|
|---|
| 85 | private static final int HORIZONTAL_ID = 2;
|
|---|
| 86 |
|
|---|
| 87 | /** Constant for left-to-right orientation. */
|
|---|
| 88 | private static final int LEFT_TO_RIGHT_ID = 4;
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * Items run left to right, and lines flow top to bottom. Examples: English,
|
|---|
| 92 | * French.
|
|---|
| 93 | */
|
|---|
| 94 | public static final ComponentOrientation LEFT_TO_RIGHT
|
|---|
| 95 | = new ComponentOrientation(HORIZONTAL_ID | LEFT_TO_RIGHT_ID);
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Items run right to left, and lines flow top to bottom. Examples: Arabic,
|
|---|
| 99 | * Hebrew.
|
|---|
| 100 | */
|
|---|
| 101 | public static final ComponentOrientation RIGHT_TO_LEFT
|
|---|
| 102 | = new ComponentOrientation(HORIZONTAL_ID);
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * The orientation is unknown for the locale. For backwards compatibility,
|
|---|
| 106 | * this behaves like LEFT_TO_RIGHT in the instance methods.
|
|---|
| 107 | */
|
|---|
| 108 | public static final ComponentOrientation UNKNOWN
|
|---|
| 109 | = new ComponentOrientation(UNKNOWN_ID | HORIZONTAL_ID | LEFT_TO_RIGHT_ID);
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * The orientation of this object; bitwise-or of unknown (1), horizontal (2),
|
|---|
| 113 | * and left-to-right (4).
|
|---|
| 114 | *
|
|---|
| 115 | * @serial the orientation
|
|---|
| 116 | */
|
|---|
| 117 | private final int orientation;
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Construct a given orientation.
|
|---|
| 121 | *
|
|---|
| 122 | * @param orientation the orientation
|
|---|
| 123 | */
|
|---|
| 124 | private ComponentOrientation(int orientation)
|
|---|
| 125 | {
|
|---|
| 126 | this.orientation = orientation;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * Returns true if the lines are horizontal, in which case lines flow
|
|---|
| 131 | * top-to-bottom. For example, English, Hebrew. Counterexamples: Japanese,
|
|---|
| 132 | * Chinese, Korean, Mongolian.
|
|---|
| 133 | *
|
|---|
| 134 | * @return true if this orientation has horizontal lines
|
|---|
| 135 | */
|
|---|
| 136 | public boolean isHorizontal()
|
|---|
| 137 | {
|
|---|
| 138 | return (orientation & HORIZONTAL_ID) != 0;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /**
|
|---|
| 142 | * If isHorizontal() returns true, then this determines whether items in
|
|---|
| 143 | * the line flow left-to-right. If isHorizontal() returns false, items in
|
|---|
| 144 | * a line flow top-to-bottom, and this determines if lines flow
|
|---|
| 145 | * left-to-right.
|
|---|
| 146 | *
|
|---|
| 147 | * @return true if this orientation flows left-to-right
|
|---|
| 148 | */
|
|---|
| 149 | public boolean isLeftToRight()
|
|---|
| 150 | {
|
|---|
| 151 | return (orientation & LEFT_TO_RIGHT_ID) != 0;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Gets an orientation appropriate for the locale.
|
|---|
| 156 | *
|
|---|
| 157 | * @param locale the locale
|
|---|
| 158 | * @return the orientation for that locale
|
|---|
| 159 | * @throws NullPointerException if locale is null
|
|---|
| 160 | */
|
|---|
| 161 | public static ComponentOrientation getOrientation(Locale locale)
|
|---|
| 162 | {
|
|---|
| 163 | // Based on iterating over all languages defined in JDK 1.4, this behavior
|
|---|
| 164 | // matches Sun's. However, it makes me wonder if any non-horizontal
|
|---|
| 165 | // orientations even exist, as it sure contradicts their documentation.
|
|---|
| 166 | String language = locale.getLanguage();
|
|---|
| 167 | if ("ar".equals(language) || "fa".equals(language) || "iw".equals(language)
|
|---|
| 168 | || "ur".equals(language))
|
|---|
| 169 | return RIGHT_TO_LEFT;
|
|---|
| 170 | return LEFT_TO_RIGHT;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * Gets an orientation from a resource bundle. This tries the following:<ol>
|
|---|
| 175 | * <li>Use the key "Orientation" to find an instance of ComponentOrientation
|
|---|
| 176 | * in the bundle.</li>
|
|---|
| 177 | * <li>Get the locale of the resource bundle, and get the orientation of
|
|---|
| 178 | * that locale.</li>
|
|---|
| 179 | * <li>Give up and get the orientation of the default locale.<li>
|
|---|
| 180 | * <ol>
|
|---|
| 181 | *
|
|---|
| 182 | * @param bdl the bundle to use
|
|---|
| 183 | * @return the orientation
|
|---|
| 184 | * @throws NullPointerException if bdl is null
|
|---|
| 185 | * @deprecated use {@link #getOrientation(Locale)} instead
|
|---|
| 186 | */
|
|---|
| 187 | public static ComponentOrientation getOrientation(ResourceBundle bdl)
|
|---|
| 188 | {
|
|---|
| 189 | ComponentOrientation r;
|
|---|
| 190 | try
|
|---|
| 191 | {
|
|---|
| 192 | r = (ComponentOrientation) bdl.getObject("Orientation");
|
|---|
| 193 | if (r != null)
|
|---|
| 194 | return r;
|
|---|
| 195 | }
|
|---|
| 196 | catch (MissingResourceException ignored)
|
|---|
| 197 | {
|
|---|
| 198 | }
|
|---|
| 199 | catch (ClassCastException ignored)
|
|---|
| 200 | {
|
|---|
| 201 | }
|
|---|
| 202 | try
|
|---|
| 203 | {
|
|---|
| 204 | r = getOrientation(bdl.getLocale());
|
|---|
| 205 | if (r != null)
|
|---|
| 206 | return r;
|
|---|
| 207 | }
|
|---|
| 208 | catch (Exception ignored)
|
|---|
| 209 | {
|
|---|
| 210 | }
|
|---|
| 211 | return getOrientation(Locale.getDefault());
|
|---|
| 212 | }
|
|---|
| 213 | } // class ComponentOrientation
|
|---|