KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > componentxml > propertypeer > FontPeer


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.app.componentxml.propertypeer;
31
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.w3c.dom.Element JavaDoc;
37
38 import nextapp.echo2.app.Extent;
39 import nextapp.echo2.app.Font;
40 import nextapp.echo2.app.componentxml.InvalidPropertyException;
41 import nextapp.echo2.app.componentxml.PropertyXmlPeer;
42 import nextapp.echo2.app.util.DomUtil;
43
44 /**
45  * <code>PropertyXmlPeer</code> implementation for
46  * <code>nextapp.echo2.app.Font</code> properties.
47  */

48 public class FontPeer
49 implements PropertyXmlPeer {
50
51     private static final Map JavaDoc TYPEFACE_TEXT_TO_CONSTANT;
52     static {
53         Map JavaDoc constantMap = new HashMap JavaDoc();
54         constantMap.put("Helvetica", Font.HELVETICA);
55         constantMap.put("Arial", Font.ARIAL);
56         constantMap.put("Verdana", Font.VERDANA);
57         constantMap.put("Times", Font.TIMES);
58         constantMap.put("Times Roman", Font.TIMES_ROMAN);
59         constantMap.put("Times New Roman", Font.TIMES_NEW_ROMAN);
60         constantMap.put("Courier", Font.COURIER);
61         constantMap.put("Courier New", Font.COURIER_NEW);
62         TYPEFACE_TEXT_TO_CONSTANT = Collections.unmodifiableMap(constantMap);
63     }
64
65     private Font.Typeface getSimpleTypeface(String JavaDoc name) {
66         if (TYPEFACE_TEXT_TO_CONSTANT.containsKey(name)) {
67             return (Font.Typeface) TYPEFACE_TEXT_TO_CONSTANT.get(name);
68         } else {
69             return new Font.Typeface(name);
70         }
71     }
72
73     /**
74      * @see nextapp.echo2.app.componentxml.PropertyXmlPeer#getValue(java.lang.ClassLoader,
75      * java.lang.Class, org.w3c.dom.Element)
76      */

77     public Object JavaDoc getValue(ClassLoader JavaDoc classLoader, Class JavaDoc objectClass, Element JavaDoc propertyElement)
78     throws InvalidPropertyException {
79         Element JavaDoc fontElement = DomUtil.getChildElementByTagName(propertyElement, "font");
80         Extent size = null;
81         if (fontElement.hasAttribute("size")) {
82             String JavaDoc sizeString = fontElement.getAttribute("size");
83             size = ExtentPeer.toExtent(sizeString);
84         }
85         int style = 0;
86         if ("true".equals(fontElement.getAttribute("bold"))) {
87             style |= Font.BOLD;
88         }
89         if ("true".equals(fontElement.getAttribute("italic"))) {
90             style |= Font.ITALIC;
91         }
92         if ("true".equals(fontElement.getAttribute("underline"))) {
93             style |= Font.UNDERLINE;
94         }
95         if ("true".equals(fontElement.getAttribute("overline"))) {
96             style |= Font.OVERLINE;
97         }
98         if ("true".equals(fontElement.getAttribute("line-through"))) {
99             style |= Font.LINE_THROUGH;
100         }
101         Element JavaDoc[] typefaces = DomUtil.getChildElementsByTagName(fontElement, "typeface");
102         Font.Typeface typeface;
103         if (typefaces.length == 0) {
104             if (fontElement.hasAttribute("typeface")) {
105                 typeface = getSimpleTypeface(fontElement.getAttribute("typeface"));
106             } else {
107                 typeface = null;
108             }
109         } else if (typefaces.length == 1) {
110             typeface = getSimpleTypeface(typefaces[0].getAttribute("name"));
111         } else {
112             typeface = new Font.Typeface(typefaces[typefaces.length - 1].getAttribute("name"));
113             for (int i = typefaces.length - 2; i >= 0; --i) {
114                 typeface = new Font.Typeface(typefaces[i].getAttribute("name"), typeface);
115             }
116         }
117         return new Font(typeface, style, size);
118     }
119 }
120
Popular Tags