1 53 54 package org.swixml.converters; 55 56 import org.jdom.Attribute; 57 import org.swixml.Converter; 58 import org.swixml.Localizer; 59 import org.swixml.SwingEngine; 60 61 import java.awt.*; 62 import java.lang.reflect.Field ; 63 import java.lang.reflect.Modifier ; 64 import java.util.StringTokenizer ; 65 66 84 public class ColorConverter implements Converter { 85 86 public static final Class TEMPLATE = Color.class; 87 88 94 public Object convert( final Class type, final Attribute attr, Localizer localizer ) { 95 return ColorConverter.conv(type,attr); 96 } 97 103 public static Object conv( final Class type, final Attribute attr ) { 104 if (attr != null) { 105 try { 106 Field field = Color.class.getField( attr.getValue() ); 107 if (Color.class.equals( field.getType() ) && Modifier.isStatic( field.getModifiers() )) 108 return field.get( Color.class ); 109 } catch (NoSuchFieldException e) { 110 } catch (SecurityException e) { 111 } catch (IllegalAccessException e) { 112 } 113 StringTokenizer st = new StringTokenizer ( attr.getValue(), "," ); 114 if (1 == st.countTokens()) { 115 try { 116 return new Color( Integer.parseInt( st.nextToken().trim(), 16 ) ); 117 } catch (NumberFormatException e) { 118 if (SwingEngine.DEBUG_MODE) System.err.println( e ); 119 return null; 120 } 121 } 122 int[] para = Util.ia( st ); 123 if (4 <= para.length) 124 return new Color( para[0], para[1], para[2], para[3] ); 125 if (3 <= para.length) 126 return new Color( para[0], para[1], para[2] ); 127 if (1 <= para.length) 128 return new Color( para[0] ); 129 } 130 return null; 131 } 132 133 134 139 public Class convertsTo() { 140 return TEMPLATE; 141 } 142 143 } 144 | Popular Tags |