KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webcontainer > propertyrender > FontRender


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webcontainer.propertyrender;
31
32 import nextapp.echo2.app.Component;
33 import nextapp.echo2.app.Font;
34 import nextapp.echo2.webrender.output.CssStyle;
35
36 /**
37  * Utility class for rendering <code>nextapp.echo2.app.Font</code>
38  * properties to CSS.
39  */

40 public class FontRender {
41
42     /**
43      * Renders a 'font-family' CSS attribute value based on the specified
44      * <code>Font.Typeface</code>.
45      *
46      * @param typeface the typeface
47      * @return the CSS attribute value
48      */

49     public static String JavaDoc renderFontFamilyCssAttributeValue(Font.Typeface typeface) {
50         StringBuffer JavaDoc out = new StringBuffer JavaDoc(typeface.getName());
51         typeface = typeface.getAlternate();
52         while (typeface != null) {
53             out.append(",");
54             out.append(typeface.getName());
55             typeface = typeface.getAlternate();
56         }
57         return out.toString();
58     }
59     
60     /**
61      * Renders a 'font-style' CSS attribute value for the specified
62      * <code>Font</code>.
63      *
64      * @param font the font
65      * @return the CSS attribute value
66      */

67     public static String JavaDoc renderFontStyleCssAttributeValue(Font font) {
68         return font.isItalic() ? "italic" : "normal";
69     }
70     
71     /**
72      * Renders a 'font-weight' CSS attribute value for the specified
73      * <code>Font</code>.
74      *
75      * @param font the font
76      * @return the CSS attribute value
77      */

78     public static String JavaDoc renderFontWeightCssAttributeValue(Font font) {
79         return font.isBold() ? "bold" : "normal";
80     }
81     
82     /**
83      * Renders a 'text-decoration' CSS attribute value based on the specified
84      * <code>Font</code>
85      *
86      * @param font the font
87      * @return the CSS attribute value
88      */

89     public static String JavaDoc renderTextDecorationCssAttributeValue(Font font) {
90         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
91         if (font.isUnderline()) {
92             out.append("underline");
93         }
94         if (font.isOverline()) {
95             if (out.length() > 0) {
96                 out.append(" ");
97             }
98             out.append("overline");
99         }
100         if (font.isLineThrough()) {
101             if (out.length() > 0) {
102                 out.append(" ");
103             }
104             out.append("line-through");
105         }
106         if (out.length() == 0) {
107             return "none";
108         } else {
109             return out.toString();
110         }
111     }
112     
113     /**
114      * Renders the <code>Font</code> properties of the provided
115      * <code>Component</code> to a CSS style.
116      *
117      * @param cssStyle the target <code>CssStyle</code>
118      * @param component the component
119      */

120     public static void renderToStyle(CssStyle cssStyle, Component component) {
121         renderToStyle(cssStyle, (Font) component.getRenderProperty(Component.PROPERTY_FONT));
122     }
123     
124     /**
125      * Renders a <code>Font</code> property to the given CSS style.
126      * Null property values are ignored.
127      *
128      * @param cssStyle the target <code>CssStyle</code>
129      * @param font the property value
130      */

131     public static void renderToStyle(CssStyle cssStyle, Font font) {
132         if (font == null) {
133             return;
134         }
135         Font.Typeface typeface = font.getTypeface();
136         if (typeface != null) {
137             cssStyle.setAttribute("font-family", renderFontFamilyCssAttributeValue(typeface));
138         }
139         if (font.getSize() != null) {
140             cssStyle.setAttribute("font-size", ExtentRender.renderCssAttributeValue(font.getSize()));
141         }
142         if (!font.isPlain()) {
143             if (font.isBold()) {
144                 cssStyle.setAttribute("font-weight", "bold");
145             }
146             if (font.isItalic()) {
147                 cssStyle.setAttribute("font-style", "italic");
148             }
149             if (font.isUnderline() || font.isOverline() || font.isLineThrough()) {
150                 StringBuffer JavaDoc out = new StringBuffer JavaDoc();
151                 if (font.isUnderline()) {
152                     out.append("underline");
153                 }
154                 if (font.isOverline()) {
155                     if (out.length() > 0) {
156                         out.append(" ");
157                     }
158                     out.append("overline");
159                 }
160                 if (font.isLineThrough()) {
161                     if (out.length() > 0) {
162                         out.append(" ");
163                     }
164                     out.append("line-through");
165                 }
166                 cssStyle.setAttribute("text-decoration", out.toString());
167             }
168         }
169     }
170     
171     /** Non-instantiable class. */
172     private FontRender() { }
173 }
174
Popular Tags