1 53 54 package org.swixml.converters; 55 56 import org.jdom.Attribute; 57 import org.swixml.Converter; 58 import org.swixml.ConverterLibrary; 59 import org.swixml.Localizer; 60 import org.swixml.SwingEngine; 61 62 import javax.swing.*; 63 import javax.swing.border.Border ; 64 import java.lang.reflect.Method ; 65 import java.util.StringTokenizer ; 66 67 91 public class BorderConverter implements Converter { 92 93 public static final Class TEMPLATE = Border .class; 94 95 96 private static final Method [] METHODS = BorderFactory.class.getMethods(); 97 98 104 public Object convert( final Class type, final Attribute attr, Localizer localizer ) { 105 Border border = null; 106 StringTokenizer st = new StringTokenizer ( attr.getValue(), "(,)" ); int n = st.countTokens() - 1; String borderType = st.nextToken().trim(); 109 Method method = null; 110 ConverterLibrary cvtlib = ConverterLibrary.getInstance(); 111 115 if (n == 1) { 116 try { 117 method = BorderFactory.class.getMethod( "create" + borderType, new Class []{String .class} ); 118 } catch (Exception e) { 119 } 121 } 122 for (int i = 0; method == null && i < METHODS.length; i++) { 123 if (METHODS[i].getParameterTypes().length == n && METHODS[i].getName().endsWith( borderType )) { 124 method = METHODS[i]; 125 126 for (int j = 0; j < method.getParameterTypes().length; j++) { 127 if (String .class.equals( method.getParameterTypes()[j] )) { 128 continue; 129 } 130 if (null == cvtlib.getConverter( method.getParameterTypes()[j] )) { 131 method = null; 132 break; 133 } 134 } 135 } 136 } 137 try { 138 Object [] args = new Object [n]; 139 for (int i = 0; i < n; i++) { Converter converter = cvtlib.getConverter( method.getParameterTypes()[i] ); 141 Attribute attrib = new Attribute( String .class.equals(converter.convertsTo()) ? "title" : "NA", st.nextToken().trim(), Attribute.CDATA_TYPE ); 142 if (converter != null) { 143 args[i] = converter.convert( method.getParameterTypes()[i], attrib, localizer ); 144 } else { 145 args[i] = attrib.getValue(); 146 } 147 } 148 border = (Border ) method.invoke( null, args ); 149 } catch (Exception e) { 150 if (SwingEngine.DEBUG_MODE) System.err.println( "Couldn't create border, " + attr.getValue() + "\n" + e.getMessage() ); 151 } 152 return border; 153 } 154 155 160 public Class convertsTo() { 161 return TEMPLATE; 162 } 163 } 164 | Popular Tags |