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