1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Michael Danilov, Dmitry A. Durnev
19 * @version $Revision$
20 */
21
22package java.awt;
23
24import java.io.Serializable;
25import java.util.*;
26
27/**
28 * The ComponentOrientation class specifies the language-sensitive orientation
29 * of component's elements or text. It is used to reflect the differences in
30 * this ordering between different writing systems. The ComponentOrientation
31 * class indicates the orientation of the elements/text in the horizontal
32 * direction ("left to right" or "right to left") and in the vertical direction
33 * ("top to bottom" or "bottom to top").
34 *
35 * @since Android 1.0
36 */
37public final class ComponentOrientation implements Serializable {
38
39    /**
40     * The Constant serialVersionUID.
41     */
42    private static final long serialVersionUID = -4113291392143563828L;
43
44    /**
45     * The Constant LEFT_TO_RIGHT indicates that items run left to right.
46     */
47    public static final ComponentOrientation LEFT_TO_RIGHT = new ComponentOrientation(true, true);
48
49    /**
50     * The Constant RIGHT_TO_LEFT indicates that items run right to left.
51     */
52    public static final ComponentOrientation RIGHT_TO_LEFT = new ComponentOrientation(true, false);
53
54    /**
55     * The Constant UNKNOWN indicates that a component's orientation is not set.
56     */
57    public static final ComponentOrientation UNKNOWN = new ComponentOrientation(true, true);
58
59    /**
60     * The Constant rlLangs.
61     */
62    private static final Set<String> rlLangs = new HashSet<String>(); // RIGHT_TO_LEFT
63
64    // languages
65
66    /**
67     * The horizontal.
68     */
69    private final boolean horizontal;
70
71    /**
72     * The left2right.
73     */
74    private final boolean left2right;
75
76    static {
77        rlLangs.add("ar"); //$NON-NLS-1$
78        rlLangs.add("fa"); //$NON-NLS-1$
79        rlLangs.add("iw"); //$NON-NLS-1$
80        rlLangs.add("ur"); //$NON-NLS-1$
81    }
82
83    /**
84     * Gets the orientation for the given ResourceBundle's localization.
85     *
86     * @param bdl
87     *            the ResourceBundle.
88     * @return the ComponentOrientation.
89     * @deprecated Use getOrientation(java.util.Locale) method.
90     */
91    @Deprecated
92    public static ComponentOrientation getOrientation(ResourceBundle bdl) {
93        Object obj = null;
94        try {
95            obj = bdl.getObject("Orientation"); //$NON-NLS-1$
96        } catch (MissingResourceException mre) {
97            obj = null;
98        }
99        if (obj instanceof ComponentOrientation) {
100            return (ComponentOrientation)obj;
101        }
102        Locale locale = bdl.getLocale();
103        if (locale == null) {
104            locale = Locale.getDefault();
105        }
106        return getOrientation(locale);
107    }
108
109    /**
110     * Gets the orientation for the specified locale.
111     *
112     * @param locale
113     *            the specified Locale.
114     * @return the ComponentOrientation.
115     */
116    public static ComponentOrientation getOrientation(Locale locale) {
117        String lang = locale.getLanguage();
118        return rlLangs.contains(lang) ? RIGHT_TO_LEFT : LEFT_TO_RIGHT;
119    }
120
121    /**
122     * Instantiates a new component orientation.
123     *
124     * @param hor
125     *            whether the items should be arranged horizontally.
126     * @param l2r
127     *            whether this orientation specifies a left-to-right flow.
128     */
129    private ComponentOrientation(boolean hor, boolean l2r) {
130        horizontal = hor;
131        left2right = l2r;
132    }
133
134    /**
135     * Returns true if the text of the of writing systems arranged horizontally.
136     *
137     * @return true, if the text is written horizontally, false for a vertical
138     *         arrangement.
139     */
140    public boolean isHorizontal() {
141        return horizontal;
142    }
143
144    /**
145     * Returns true if the text is arranged from left to right.
146     *
147     * @return true, for writing systems written from left to right; false for
148     *         right-to-left.
149     */
150    public boolean isLeftToRight() {
151        return left2right;
152    }
153
154}
155