View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.context;
20  
21  import java.io.Serializable;
22  
23  /**
24   * Specifies a set of accessibility-related properties that are applied
25   * to the current request.
26   *
27   * AccessibilityProfile instances are obtained by the getInstance()
28   * factory method.
29   *
30   * AccessibilityProfile instances are immutable.
31   */
32  public class AccessibilityProfile 
33  {
34    /**
35     * Color contrast values
36     */
37    public enum ColorContrast
38    {
39      /**
40       * Color contrast value for users who prefer styles/content optimized
41       * for high contrast settings.
42       */
43      HIGH,
44      
45      /**
46       * Color contrast value for users who prefer the default
47       * (non-high contrast-optimized) styles/content.
48       */
49      STANDARD
50    }
51  
52    /**
53     * Font size values.
54     * 
55     * Note that the font size property is not used to define a specific
56     * (eg. pixel-based) font size, but rather to provide a hint that the
57     * use may prefer larger fonts.  The physical font size to use is specified
58     * by the skin.
59     */
60    public enum FontSize
61    {
62      /**
63       * Font size value for users who prefer larger font sizes.
64       */
65      LARGE,
66      
67      /**
68       * Font size value for users who prefer the default (medium) font sizes.
69       */
70      MEDIUM
71    }
72    
73    /**
74     * Returns an AccessibilityProfile instance with the specified properties.
75     * @param colorContrast Specifies the user's color contrast preference.  If 
76     *   null, defaults to ColorContrast.STANDARD.
77     * @param fontSize Specifies the user's font size preference.  If null, 
78     *   defaults to FontSize.MEDIUM.
79     */
80    public static AccessibilityProfile getInstance(
81      ColorContrast colorContrast,
82      FontSize      fontSize
83      )
84    {    
85      // Note: we could cache and share AccessibilityProfile instances
86      // here if that seems useful.
87      return new SerializableAccessibilityProfile(colorContrast, fontSize);
88    }
89  
90    /**
91     * Returns an AccessiblityProfile instance with the default preferences.
92     */
93    public static AccessibilityProfile getDefaultInstance()
94    {
95      return _sDefaultInstance;
96    }
97  
98    /**
99     * Returns the user's preferred color contrast setting.
100    */
101   public ColorContrast getColorContrast()
102   {
103     return _colorContrast;
104   }
105   
106   /**
107    * Returns the user's preferred font size setting.
108    */
109   public FontSize getFontSize()
110   {
111     return _fontSize;    
112   }
113 
114   /**
115    * Convenience method for testing whether high contrast content is required.
116    */
117   public final boolean isHighContrast()
118   {
119     return (_colorContrast == ColorContrast.HIGH);
120   }
121     
122   /**
123    * Convenience method for testing whether large fonts are required.
124    */
125   public final boolean isLargeFonts()
126   {
127     return (_fontSize == FontSize.LARGE);
128   }
129   
130   @Override
131   public final int hashCode()
132   {
133     return _hashCode;
134   }
135   
136   @Override
137   public boolean equals(Object o)
138   {
139     if (this == o)
140       return true;
141     else if (o instanceof AccessibilityProfile)
142     {
143       AccessibilityProfile otherProfile = (AccessibilityProfile)o;
144       
145       return (_hashCode == otherProfile._hashCode) &&
146               _colorContrast.equals(otherProfile._colorContrast) &&
147               _fontSize.equals(otherProfile._fontSize);
148     }
149     else
150     {
151       return false;
152     }
153   }
154   
155   // No need to support subclassing yet, so keep the constructor private.
156   // Clients should use the getInstance() factory method.
157   private AccessibilityProfile(
158     ColorContrast colorContrast,
159     FontSize      fontSize
160     )
161   {
162     _colorContrast = (colorContrast == null) ? ColorContrast.STANDARD : colorContrast;
163     _fontSize = (fontSize == null) ? FontSize.MEDIUM : fontSize;
164     _hashCode = _colorContrast.hashCode() * 37 + _fontSize.hashCode();
165   }
166 
167   //Serialization for SerializableAccessibilityProfile internal subclass requires no-arg constructor
168   //access of at least package security level.
169   AccessibilityProfile()
170   {
171     this(ColorContrast.STANDARD, FontSize.MEDIUM);
172   }
173 
174   private final ColorContrast _colorContrast;
175   private final FontSize      _fontSize;
176   
177   // hashCode could be transient, but then we would have to recalculate it when deserializing
178   // and it couldn't be final
179   private final int           _hashCode;
180 
181   // Default instance
182   private static final AccessibilityProfile _sDefaultInstance =
183     AccessibilityProfile.getInstance(ColorContrast.STANDARD, FontSize.MEDIUM);
184 
185   /**
186    * We maintain a private internal serializable class for our singleton instance.
187    */
188   private static final class SerializableAccessibilityProfile extends AccessibilityProfile implements Serializable
189   {
190     public SerializableAccessibilityProfile(
191       ColorContrast colorContrast,
192       FontSize      fontSize
193       )
194     {
195       super(colorContrast, fontSize);
196     }
197 
198     private static final long serialVersionUID = 1L;
199   }
200 }