001    package echopoint.style;
002    
003    import static echopoint.util.FontKit.makeFont;
004    import nextapp.echo.app.Font;
005    
006    /**
007     * A font utility class that is used to enforce an application wide font face
008     * policy.  The default font face, font style and font size to use may be
009     * specified as system properties with the following keys:
010     *
011     * <ul>
012     *   <li>{@code echopoint.style.DefaultFont.typeface} - Example
013     *     {@code "'Verdana, Times New Roman'"}</li>
014     *   <li>{@code echopoint.style.DefaultFont.style} - Example {@code BOLD}</li>
015     *   <li>{@code echopoint.style.DefaultFont.size} - Example {@code 12pt}</li>
016     * </ul>
017     *
018     * <p>Alternatively, the default font properties may be specified in your
019     * {@code web.xml} file as servlet {@code init-param} values if extending
020     * from the {@link echopoint.Servlet} class (in the webcontainer tree).</p>
021     *
022     * <p><b>Note:</b> Please note that the fonts are created using {@link
023     * echopoint.util.FontKit#makeFont(String)}, so please follow the same
024     * conventions when configuring the font properties.</p>
025     *
026     * @author Rakesh 2009-05-13
027     * @version $Id: DefaultFont.java 208 2009-05-25 02:40:35Z sptrakesh $
028     */
029    public class DefaultFont
030    {
031      /** The system property used to configure the font typeface. */
032      public static final String TYPEFACE_KEY =
033          DefaultFont.class.getName() + ".typeface";
034    
035      /**
036       * The default font face to use if the typeface system property is not set.
037       *
038       * {@value}
039       */
040      public static final String TYPEFACE_VALUE =
041          "'Verdana, Times New Roman, Lucida Grande'";
042    
043      /** The system property used to configure the default font style. */
044      public static final String STYLE_KEY =
045          DefaultFont.class.getName() + ".style";
046    
047      /**
048       * The default font style to use for the application if not configured.
049       *
050       * {@value}
051       */
052      public static final String STYLE_VALUE = "PLAIN";
053    
054      /** The system property used to configure the default font size. */
055      public static final String SIZE_KEY =
056          DefaultFont.class.getName() + ".size";
057    
058      /**
059       * The default font size used if the property is not configured.
060       *
061       * {@value}
062       */
063      public static final String SIZE_VALUE = "10pt";
064    
065      private static String typeface;
066      private static String style;
067      private static String size;
068    
069      /** Static initialiser to read the system defaults. */
070      static
071      {
072        typeface = System.getProperty( TYPEFACE_KEY, TYPEFACE_VALUE );
073        style = System.getProperty( STYLE_KEY, STYLE_VALUE );
074        size = System.getProperty( SIZE_KEY, SIZE_VALUE );
075      }
076    
077      private DefaultFont() {}
078    
079      /**
080       * Return the default font for the application.
081       *
082       * @return The default font to use for the application.
083       */
084      public static Font getInstance()
085      {
086        return makeFont( DefaultFont.typeface + "," +
087            DefaultFont.style + "," + DefaultFont.size );
088      }
089    
090      public static Font getInstance( String style, String size )
091      {
092        return makeFont( DefaultFont.typeface + "," + style + "," + size );
093      }
094    
095      /**
096       * Return the default font (typeface and style) for the application with
097       * the specified size.
098       *
099       * @param size The size for the requested font.
100       * @return The standard font of requested size.
101       */
102      public static Font fontWithSize( final String size )
103      {
104        return makeFont( DefaultFont.typeface + "," +
105            DefaultFont.style + "," + size );
106      }
107    
108      /**
109       * Return the standard font (typeface and size) with the specified style.
110       *
111       * @param style The style to use for the font.
112       * @return The standard font with the specified style.
113       */
114      public static Font fontWithStyle( final String style )
115      {
116        return makeFont( DefaultFont.typeface + "," +
117            style + "," + DefaultFont.size );
118      }
119    
120      /**
121       * Set the default typeface to use.  Note that this method will need to be
122       * invoked before the stylesheet is loaded.
123       *
124       * @see echopoint.util.FontKit#makeFont(String)
125       * @param typeface The typeface to use as default.
126       */
127      public static void setTypeFace( final String typeface )
128      {
129        DefaultFont.typeface = typeface;
130      }
131    
132      /**
133       * Set the default style to use.  Note that this method will need to be
134       * invoked before the stylesheet is loaded.
135       *
136       * @see echopoint.util.FontKit#makeFont(String)
137       * @param style The style to use as default.
138       */
139      public static void setStyle( final String style )
140      {
141        DefaultFont.style = style;
142      }
143    
144      /**
145       * Set the default size to use.  Note that this method will need to be
146       * invoked before the stylesheet is loaded.
147       *
148       * @see echopoint.util.FontKit#makeFont(String)
149       * @param size The size to use as default.
150       */
151      public static void setSize( final String size )
152      {
153        DefaultFont.size = size;
154      }
155    }