KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > elem > FontElement


1 /*
2  * $Id: FontElement.java,v 1.1.1.1 2004/06/16 01:43:40 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup.elem;
9
10 import java.awt.Font JavaDoc;
11
12 import java.util.Hashtable JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.w3c.dom.Element JavaDoc;
16
17 import net.openmarkup.AttributeHandler;
18 import net.openmarkup.ElementType;
19 import net.openmarkup.Scribe;
20
21 import org.jdesktop.jdnc.markup.Attributes;
22 import org.jdesktop.jdnc.markup.Namespace;
23 import org.jdesktop.jdnc.markup.attr.*;
24
25 /**
26  *
27  * @author Amy Fowler
28  */

29 public class FontElement extends ElementProxy {
30
31     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
32
33     public FontElement(Element JavaDoc element, ElementType elementType) {
34         super(element, elementType);
35     }
36
37     public Object JavaDoc instantiate() {
38         String JavaDoc fontName = null; // default is "Default"
39
int fontSize = 12;
40         int fontStyle = Font.PLAIN;
41
42         // no zero arg constructor - need all attributes to instantiate
43

44         String JavaDoc attrValue = getAttributeNSOptional(Namespace.JDNC, Attributes.NAME);
45         if (attrValue.length() == 0) {
46         Scribe.getLogger().warning("Font has no name attribute defined, default to \"Default\"");
47         } else {
48             fontName = attrValue;
49         }
50         attrValue = getAttributeNSOptional(Namespace.JDNC, Attributes.SIZE);
51         if (attrValue.length() == 0) {
52         Scribe.getLogger().warning("Font has no size attribute defined, default to 12");
53         } else {
54             fontSize = Integer.parseInt(attrValue);
55         }
56         attrValue = getAttributeNSOptional(Namespace.JDNC, Attributes.STYLE);
57         if (attrValue.length() > 0) {
58             fontStyle = Decoder.decodeFontStyle(attrValue);
59         }
60
61         return new Font JavaDoc(fontName, fontStyle, fontSize);
62     }
63
64     protected Map JavaDoc registerAttributeHandlers() {
65         Map JavaDoc handlerMap = super.registerAttributeHandlers();
66         if (handlerMap != null) {
67         // Register null appliers. These attributes are handled elsewhere
68
handlerMap.put(Namespace.JDNC + ":" + Attributes.ID,
69                            NullAttribute.idHandler);
70             handlerMap.put(Namespace.JDNC + ":" + Attributes.NAME,
71                            nameHandler);
72             handlerMap.put(Namespace.JDNC + ":" + Attributes.SIZE,
73                            sizeHandler);
74             handlerMap.put(Namespace.JDNC + ":" + Attributes.STYLE,
75                            styleHandler);
76         }
77         return handlerMap;
78     }
79
80     private static final AttributeHandler nameHandler =
81         new AttributeHandler(Namespace.JDNC, Attributes.NAME, NullAttribute.nullApplier);
82
83     private static final AttributeHandler sizeHandler =
84         new AttributeHandler(Namespace.JDNC, Attributes.SIZE, NullAttribute.nullApplier);
85
86     private static final AttributeHandler styleHandler =
87         new AttributeHandler(Namespace.JDNC, Attributes.STYLE, NullAttribute.nullApplier);
88
89     protected Map JavaDoc getAttributeHandlerMap() {
90         return attrMap;
91     }
92
93 }
94
Popular Tags