1 7 8 package org.jdesktop.swing.data; 9 10 import java.util.HashMap ; 11 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.Node ; 14 import org.w3c.dom.NodeList ; 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 24 public class HierarchicalDataMetaData extends TabularDataMetaData { 25 private Element columns = null; 26 private static HashMap typesMap; 27 28 static { 30 typesMap = new HashMap (); 31 typesMap.put("boolean", java.lang.Boolean .class); 32 typesMap.put("date", java.util.Date .class); 33 typesMap.put("double", java.lang.Double .class); 34 typesMap.put("float", java.lang.Float .class); 35 typesMap.put("integer", java.lang.Integer .class); 36 typesMap.put("string", java.lang.String .class); 37 } 38 39 public HierarchicalDataMetaData(Element 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 list = ( (Element ) columns).getChildNodes(); 49 int i = 0, k = 0, max = list.getLength(); 50 Node node; 51 Element elem; 52 while (i < max) { 53 node = list.item(i++); 54 if (node instanceof Element ) { 55 elem = (Element ) node; 56 if (elem.getLocalName().equals("columnMetaData")) { 57 setColumnName(++k, elem.getAttribute("name")); 58 String type = elem.getAttribute("type"); 59 if (type.length() > 0) { 60 Class klass = decodeType(type); 61 if (klass != null) { 62 setColumnClass(k, klass); 63 if (klass != String .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 .class); 73 } 74 else { 75 setColumnConverter(k, converter); } 77 } 78 } 79 } 80 } 81 } 82 } 83 } 84 } 85 86 private Class decodeType(String value) { 88 Class klass = (Class )typesMap.get(value); 89 if (klass == null) { 90 try { 91 klass = Class.forName(value); 92 } catch (ClassNotFoundException 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 |