KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DataFieldElement.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 org.jdesktop.swing.data.EnumeratedMetaData;
11 import org.jdesktop.swing.data.MetaData;
12 import org.jdesktop.swing.data.NumberMetaData;
13 import org.jdesktop.swing.data.StringMetaData;
14
15 import org.jdesktop.jdnc.markup.Attributes;
16 import org.jdesktop.jdnc.markup.ElementTypes;
17 import org.jdesktop.jdnc.markup.Namespace;
18 import org.jdesktop.jdnc.markup.attr.DataFieldAttributes;
19 import org.jdesktop.jdnc.markup.attr.Decoder;
20
21 import java.util.Hashtable JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import net.openmarkup.AttributeHandler;
26 import net.openmarkup.ElementAssimilator;
27 import net.openmarkup.ElementHandler;
28 import net.openmarkup.ElementType;
29 import net.openmarkup.Realizable;
30
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 /**
36  *
37  * @author Amy Fowler
38  */

39 public class DataFieldElement extends ElementProxy {
40     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
41     private static final Map JavaDoc elementMap = new Hashtable JavaDoc();
42
43     public DataFieldElement(Element JavaDoc element, ElementType elementType) {
44         super(element, elementType);
45     }
46
47     protected Object JavaDoc instantiate() {
48         // determine which class of meta-data to instantiate....
49
NodeList JavaDoc children = getElementsByTagNameNS(Namespace.JDNC,
50             ElementTypes.FIELD_META_DATA_ENUM_VALUES.getLocalName());
51         if (children.getLength() > 0) {
52             return new EnumeratedMetaData();
53         }
54         String JavaDoc value = getAttributeNSOptional(Namespace.JDNC, Attributes.DATA_TYPE);
55         // if DATA_TYPE not set, then defaults to String
56
if (value.length() > 0) {
57             Class JavaDoc type = Decoder.decodeType(value);
58             if (type != null) {
59                 if (Number JavaDoc.class.isAssignableFrom(type)) {
60                     return new NumberMetaData();
61                 } else if (type != String JavaDoc.class) {
62                     return new MetaData();
63                 }
64             }
65         }
66         return new StringMetaData();
67     }
68
69     protected Map JavaDoc getAttributeHandlerMap() {
70         return attrMap;
71     }
72
73     protected Map JavaDoc getElementHandlerMap() {
74         return elementMap;
75     }
76
77     protected void applyAttributesAfter() {
78         MetaData metaData = (MetaData)getObject();
79         super.applyAttributesAfter();
80         applyAttribute(Namespace.JDNC, Attributes.DATA_TYPE);
81         applyAttribute(Namespace.JDNC, Attributes.TITLE);
82         applyAttribute(Namespace.JDNC, Attributes.NAME);
83         applyAttribute(Namespace.JDNC, Attributes.DECODE_FORMAT);
84         applyAttribute(Namespace.JDNC, Attributes.ENCODE_FORMAT);
85         applyAttribute(Namespace.JDNC, Attributes.IS_READ_ONLY);
86         applyAttribute(Namespace.JDNC, Attributes.IS_REQUIRED);
87         if (metaData instanceof NumberMetaData) {
88             applyAttribute(Namespace.JDNC, Attributes.MINIMUM);
89             applyAttribute(Namespace.JDNC, Attributes.MAXIMUM);
90         } else if (metaData instanceof StringMetaData) {
91             applyAttribute(Namespace.JDNC, Attributes.DISPLAY_SIZE);
92             applyAttribute(Namespace.JDNC, Attributes.MULTI_LINE);
93             applyAttribute(Namespace.JDNC, Attributes.MINIMUM_FIELD_SIZE);
94             applyAttribute(Namespace.JDNC, Attributes.MAXIMUM_FIELD_SIZE);
95         }
96     }
97
98     protected Map JavaDoc registerAttributeHandlers() {
99         Map JavaDoc handlerMap = super.registerAttributeHandlers();
100         if (handlerMap != null) {
101             handlerMap.put(Namespace.JDNC + ":" + Attributes.DATA_TYPE,
102                            typeHandler);
103             handlerMap.put(Namespace.JDNC + ":" + Attributes.TITLE,
104                            titleHandler);
105             handlerMap.put(Namespace.JDNC + ":" + Attributes.NAME, nameHandler);
106             handlerMap.put(Namespace.JDNC + ":" + Attributes.MAXIMUM,
107                            maximumHandler);
108             handlerMap.put(Namespace.JDNC + ":" + Attributes.MAXIMUM_FIELD_SIZE,
109                            maximumFieldSizeHandler);
110             handlerMap.put(Namespace.JDNC + ":" + Attributes.DECODE_FORMAT,
111                            decodeFormatHandler);
112             handlerMap.put(Namespace.JDNC + ":" + Attributes.ENCODE_FORMAT,
113                            encodeFormatHandler);
114             handlerMap.put(Namespace.JDNC + ":" + Attributes.DISPLAY_SIZE,
115                            displaySizeHandler);
116             handlerMap.put(Namespace.JDNC + ":" + Attributes.MINIMUM,
117                            minimumHandler);
118             handlerMap.put(Namespace.JDNC + ":" + Attributes.MINIMUM_FIELD_SIZE,
119                            maximumFieldSizeHandler);
120             handlerMap.put(Namespace.JDNC + ":" + Attributes.MULTI_LINE,
121                            multiLineHandler);
122             handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_READ_ONLY,
123                            isReadOnlyHandler);
124             handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_REQUIRED,
125                            isRequiredHandler);
126         }
127         return handlerMap;
128     }
129
130     protected Map JavaDoc registerElementHandlers() {
131         Map JavaDoc handlerMap = super.registerElementHandlers();
132         if (handlerMap != null) {
133             handlerMap.put(Namespace.JDNC + ":" +
134                            ElementTypes.FIELD_META_DATA_ENUM_VALUES.getLocalName(),
135                            dataEnumerationElementHandler);
136         }
137         return handlerMap;
138     }
139
140     public static final ElementAssimilator dataEnumerationAssimilator = new ElementAssimilator() {
141         public void assimilate(Realizable parent, Realizable child) {
142             EnumeratedMetaData metaData = (EnumeratedMetaData)parent.getObject();
143             List JavaDoc enumeration = (List JavaDoc)child.getObject();
144             metaData.setEnumeration(enumeration);
145         }
146     };
147
148
149     private static final AttributeHandler typeHandler =
150         new AttributeHandler(Namespace.JDNC, Attributes.DATA_TYPE,
151                              DataFieldAttributes.typeApplier);
152
153     private static final AttributeHandler displaySizeHandler =
154         new AttributeHandler(Namespace.JDNC, Attributes.DISPLAY_SIZE,
155                              DataFieldAttributes.displaySizeApplier);
156
157     private static final AttributeHandler decodeFormatHandler =
158         new AttributeHandler(Namespace.JDNC, Attributes.DECODE_FORMAT,
159                              DataFieldAttributes.decodeFormatApplier);
160
161     private static final AttributeHandler encodeFormatHandler =
162         new AttributeHandler(Namespace.JDNC, Attributes.ENCODE_FORMAT,
163                              DataFieldAttributes.encodeFormatApplier);
164
165     private static final AttributeHandler titleHandler =
166         new AttributeHandler(Namespace.JDNC, Attributes.TITLE,
167                              DataFieldAttributes.titleApplier);
168
169     private static final AttributeHandler maximumHandler =
170         new AttributeHandler(Namespace.JDNC, Attributes.MAXIMUM,
171                              DataFieldAttributes.maximumApplier);
172
173     private static final AttributeHandler maximumFieldSizeHandler =
174     new AttributeHandler(Namespace.JDNC, Attributes.MAXIMUM_FIELD_SIZE,
175                          DataFieldAttributes.maximumFieldSizeApplier);
176
177     private static final AttributeHandler minimumHandler =
178         new AttributeHandler(Namespace.JDNC, Attributes.MINIMUM,
179                              DataFieldAttributes.minimumApplier);
180
181     private static final AttributeHandler minimumFieldSizeHandler =
182         new AttributeHandler(Namespace.JDNC, Attributes.MINIMUM_FIELD_SIZE,
183                              DataFieldAttributes.minimumFieldSizeApplier);
184
185     private static final AttributeHandler multiLineHandler =
186         new AttributeHandler(Namespace.JDNC, Attributes.MULTI_LINE,
187                              DataFieldAttributes.multiLineApplier);
188
189     private static final AttributeHandler nameHandler =
190         new AttributeHandler(Namespace.JDNC, Attributes.NAME,
191                              DataFieldAttributes.nameApplier);
192
193     private static final AttributeHandler isReadOnlyHandler =
194         new AttributeHandler(Namespace.JDNC, Attributes.IS_READ_ONLY,
195                              DataFieldAttributes.isReadOnlyApplier);
196
197     private static final AttributeHandler isRequiredHandler =
198         new AttributeHandler(Namespace.JDNC, Attributes.IS_REQUIRED,
199                              DataFieldAttributes.isRequiredApplier);
200
201     private static final ElementHandler dataEnumerationElementHandler =
202          new ElementHandler(ElementTypes.FIELD_META_DATA_ENUM_VALUES,
203                             dataEnumerationAssimilator);
204
205
206 }
Popular Tags