/[classpath]/classpath/java/awt/ComponentOrientation.java
ViewVC logotype

Diff of /classpath/java/awt/ComponentOrientation.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by mark, Tue Jan 22 22:26:58 2002 UTC revision 1.3 by ericb, Mon May 6 02:43:17 2002 UTC
# Line 1  Line 1 
1  /* Copyright (C) 2000, 2001, 2002  Free Software Foundation  /* 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.  This file is part of GNU Classpath.
5    
# Line 34  this exception to your version of the li Line 35  this exception to your version of the li
35  obligated to do so.  If you do not wish to do so, delete this  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */  exception statement from your version. */
37    
 /**  
  * @author Bryce McKinlay  <bryce@albatross.co.nz>  
  */  
   
 /* Status: Incomplete. Needs a Locale lookup table. */  
38    
39  package java.awt;  package java.awt;
40    
41    import java.io.Serializable;
42  import java.util.Locale;  import java.util.Locale;
43    import java.util.MissingResourceException;
44  import java.util.ResourceBundle;  import java.util.ResourceBundle;
45    
46  public final class ComponentOrientation implements java.io.Serializable  /**
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 <bryce@albatross.co.nz>
71     * @since 1.0
72     * @status updated to 1.4
73     */
74    public final class ComponentOrientation implements Serializable
75  {  {
76    // Here is a wild guess.    /**
77    private static int HORIZONTAL_ID    = 1 << 0,     * Compatible with JDK 1.0+.
78                       LEFT_TO_RIGHT_ID = 1 << 1;     */
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    public static final ComponentOrientation LEFT_TO_RIGHT
95      = new ComponentOrientation(HORIZONTAL_ID & LEFT_TO_RIGHT_ID);      = 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    public static final ComponentOrientation RIGHT_TO_LEFT
102      = new ComponentOrientation(HORIZONTAL_ID);      = new ComponentOrientation(HORIZONTAL_ID);
   public static final ComponentOrientation UNKNOWN  
     = new ComponentOrientation(0);  
103    
104    // FIXME: This field is from the serialization spec, but what are the    /**
105    // correct values?     * The orientation is unknown for the locale. For backwards compatibility,
106    int orientation;     * 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    ComponentOrientation(int orientation)    /**
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;      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()    public boolean isHorizontal()
137    {    {
138      return ((orientation & HORIZONTAL_ID) != 0);      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()    public boolean isLeftToRight()
150    {    {
151      return ((orientation & LEFT_TO_RIGHT_ID) != 0);      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)    public static ComponentOrientation getOrientation(Locale locale)
162    {    {
163      // FIXME: Use a table to look this up.      // 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;      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)    public static ComponentOrientation getOrientation(ResourceBundle bdl)
188    {    {
189      ComponentOrientation r;      ComponentOrientation r;
   
190      try      try
191      {        {
192        Object obj = bdl.getObject("Orientation");          r = (ComponentOrientation) bdl.getObject("Orientation");
193        r = (ComponentOrientation) obj;          if (r != null)
194        if (r != null)            return r;
195          return r;          }
196      }      catch (MissingResourceException ignored)
197      catch (Exception x)        {
198      {        }
199        // Fall through      catch (ClassCastException ignored)
200      }        {
201          }
202      try      try
203      {        {
204        Locale l = bdl.getLocale();          r = getOrientation(bdl.getLocale());
205        r = getOrientation(l);          if (r != null)
206        if (r != null)            return r;
207          return r;        }
208      }      catch (Exception ignored)
209      catch (Exception x)        {
210      {        }
211        // Fall through        return getOrientation(Locale.getDefault());
     }  
   
     return (getOrientation (Locale.getDefault ()));  
212    }    }
213  }  } // class ComponentOrientation

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26