1 53 54 package org.swixml.converters; 55 56 import org.jdom.Attribute; 57 import org.jdom.DataConversionException; 58 import org.swixml.Converter; 59 import org.swixml.Localizer; 60 import org.swixml.Parser; 61 62 import javax.swing.*; 63 import javax.swing.tree.TreeSelectionModel ; 64 import java.awt.*; 65 import java.util.HashMap ; 66 import java.util.Map ; 67 68 77 78 public class PrimitiveConverter implements Converter, SwingConstants, KeyEvent, InputEvent { 79 80 81 public static final Class TEMPLATE = Object .class; 82 83 private static Map dictionaries = new HashMap (); 84 87 static { 88 89 PrimitiveConverter.addConstantProvider( JTabbedPane.class ); 90 PrimitiveConverter.addConstantProvider( JScrollPane.class ); 91 PrimitiveConverter.addConstantProvider( GridBagConstraints.class ); 92 PrimitiveConverter.addConstantProvider( FlowLayout.class ); 93 PrimitiveConverter.addConstantProvider( ListSelectionModel.class ); 94 PrimitiveConverter.addConstantProvider( TreeSelectionModel .class ); 95 PrimitiveConverter.addConstantProvider( WindowConstants.class ); 96 PrimitiveConverter.addConstantProvider( JFrame.class ); 97 98 }; 99 105 public static Object conv( final Class type, final Attribute attr, final Localizer localizer ) throws Exception { 106 Attribute a = (Attribute) attr.clone(); 107 Object obj = null; 108 if ( Parser.LOCALIZED_ATTRIBUTES.contains( a.getName().toLowerCase() )) 109 if (a.getAttributeType() == Attribute.CDATA_TYPE ) 110 a.setValue( localizer.getString( a.getValue() )); 111 112 try { 113 if (boolean.class.equals( type )) { 114 obj = new Boolean ( a.getBooleanValue() ); 115 } else if (int.class.equals( type )) { 116 obj = new Integer ( a.getIntValue() ); 117 } else if (long.class.equals( type )) { 118 obj = new Long ( a.getLongValue() ); 119 } else if (float.class.equals( type )) { 120 obj = new Float ( a.getFloatValue() ); 121 } else if (double.class.equals( type )) { 122 obj = new Double ( a.getDoubleValue() ); 123 } 124 } catch (DataConversionException e) { 125 } finally { 126 if (obj==null) { 127 try { 128 String s = a.getValue(); 129 int k = s.indexOf( '.' ) - 1; 130 Class pp = (Class ) dictionaries.get( s.substring( 0, s.indexOf( '.' ) ) ); 131 obj = pp.getField( s.substring( k + 2 ) ).get( pp ); 132 } catch (Exception ex) { 133 obj = PrimitiveConverter.class.getField( a.getValue() ).get( PrimitiveConverter.class ); 137 } 138 } 139 } 140 141 return obj; 142 } 143 144 151 public Object convert( final Class type, final Attribute attr, final Localizer localizer ) throws Exception { 152 return PrimitiveConverter.conv( type, attr, localizer ); 153 } 154 155 160 public Class convertsTo() { 161 return TEMPLATE; 162 } 163 164 168 public static void addConstantProvider(final Class type) { 169 String shortName = type.getName().substring(type.getName().lastIndexOf( '.' ) + 1 ); 170 dictionaries.put( shortName, type ); 171 } 172 } 173 | Popular Tags |