KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > HierarchicalDataMetaData


1 /*
2  * $Id: HierarchicalDataMetaData.java,v 1.1.1.1 2004/06/16 01:43:39 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.swing.data;
9
10 import java.util.HashMap JavaDoc;
11
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.Node JavaDoc;
14 import org.w3c.dom.NodeList JavaDoc;
15
16 import org.jdesktop.swing.table.TabularDataMetaData;
17
18 import org.jdesktop.swing.data.Converters;
19 import org.jdesktop.swing.data.Converter;
20
21 /**
22  * @author Ramesh Gupta
23  */

24 public class HierarchicalDataMetaData extends TabularDataMetaData {
25     private Element JavaDoc columns = null;
26     private static HashMap JavaDoc typesMap;
27
28     // Merge with realizer.attr.Decoder.typesMap()
29
static {
30         typesMap = new HashMap JavaDoc();
31         typesMap.put("boolean", java.lang.Boolean JavaDoc.class);
32         typesMap.put("date", java.util.Date JavaDoc.class);
33         typesMap.put("double", java.lang.Double JavaDoc.class);
34         typesMap.put("float", java.lang.Float JavaDoc.class);
35         typesMap.put("integer", java.lang.Integer JavaDoc.class);
36         typesMap.put("string", java.lang.String JavaDoc.class);
37     }
38
39     public HierarchicalDataMetaData(Element JavaDoc metaDataElement) {
40         super((metaDataElement == null) ?
41             0 : Integer.parseInt(metaDataElement.getAttribute("columnCount")));
42         columns = metaDataElement;
43         init();
44     }
45
46     protected void init() {
47         if (columns != null) {
48             NodeList JavaDoc list = ( (Element JavaDoc) columns).getChildNodes();
49             int i = 0, k = 0, max = list.getLength();
50             Node JavaDoc node;
51             Element JavaDoc elem;
52             while (i < max) {
53                 node = list.item(i++);
54                 if (node instanceof Element JavaDoc) {
55                     elem = (Element JavaDoc) node;
56                     if (elem.getLocalName().equals("columnMetaData")) {
57                         setColumnName(++k, elem.getAttribute("name"));
58                         String JavaDoc type = elem.getAttribute("type");
59                         if (type.length() > 0) {
60                             Class JavaDoc klass = decodeType(type);
61                             if (klass != null) {
62                                 setColumnClass(k, klass);
63                                 if (klass != String JavaDoc.class) {
64                                     Converter converter =
65                                         Converters.get(klass);
66                                     if (converter == null) {
67                                         System.err.println(
68                                             "warning: couldn't find converter for " +
69                                             klass.getName() +
70                                             ". Reseting class of column " +
71                                             k + "to String.class");
72                                         setColumnClass(k, String JavaDoc.class);
73                                     }
74                                     else {
75                                         setColumnConverter(k, converter); //stash it
76
}
77                                 }
78                             }
79                         }
80                     }
81                 }
82             }
83         }
84     }
85
86     // Merge with realizer.attr.Decoder.decodeType()
87
private Class JavaDoc decodeType(String JavaDoc value) {
88         Class JavaDoc klass = (Class JavaDoc)typesMap.get(value);
89         if (klass == null) {
90             try {
91                 klass = Class.forName(value);
92             } catch (ClassNotFoundException JavaDoc e) {
93                 System.out.println("Could not convert type: " + value + " to a java type or class");
94             }
95         }
96         return klass;
97     }
98
99 }
100
Popular Tags