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.util.StringTokenizer ; 64 65 100 public class LayoutConverter implements Converter { 101 102 public static final Class TEMPLATE = LayoutManager.class; 103 104 public static final String FLOW_LAYOUT = "flowlayout"; 105 public static final String BORDER_LAYOUT = "borderlayout"; 106 public static final String GRID_LAYOUT = "gridlayout"; 107 public static final String GRIDBAG_LAYOUT = "gridbaglayout"; 108 public static final String CARD_LAYOUT = "cardlayout"; 109 110 156 public Object convert( final Class type, final Attribute attr, final Localizer localizer ) { 157 LayoutManager lm = null; 158 StringTokenizer st = new StringTokenizer ( attr.getValue(), "(,)" ); 159 String s = st.nextToken().trim(); if (s != null) { 161 s = s.toLowerCase(); 162 if (s.equals( LayoutConverter.FLOW_LAYOUT )) { 163 try { 167 if (st.hasMoreTokens()) { 168 Object o = PrimitiveConverter.conv( null, new Attribute( "NA", st.nextToken() ), localizer ); 172 int[] para = Util.ia( st ); 173 if (para.length < 2) 177 lm = new FlowLayout( Integer.valueOf( o.toString() ).intValue() ); 178 else 179 lm = new FlowLayout( Integer.valueOf( o.toString() ).intValue(), para[ 0 ], para[ 1 ] ); 180 } 181 } catch (Exception e) { 182 } 183 if (lm == null) { 184 lm = new FlowLayout(); 185 } 186 } else if (s.equals( LayoutConverter.BORDER_LAYOUT )) { 187 int[] para = Util.ia( st ); 191 if (para.length < 2) 192 lm = new BorderLayout(); 193 else 194 lm = new BorderLayout( para[ 0 ], para[ 1 ] ); 195 } else if (s.equals( LayoutConverter.GRID_LAYOUT )) { 196 int[] para = Util.ia( st ); 200 if (4 <= para.length) 201 lm = new GridLayout( para[ 0 ], para[ 1 ], para[ 2 ], para[ 3 ] ); 202 else if (2 <= para.length) 203 lm = new GridLayout( para[ 0 ], para[ 1 ] ); 204 else 205 lm = new GridLayout(); 206 } else if (s.equals( LayoutConverter.CARD_LAYOUT )) { 207 int[] para = Util.ia( st ); 212 if (para.length < 2) 213 lm = new CardLayout(); 214 else 215 lm = new CardLayout( para[ 0 ], para[ 1 ] ); 216 } else if (s.equals( LayoutConverter.GRIDBAG_LAYOUT )) { 217 lm = new GridBagLayout(); 223 224 if (st.hasMoreTokens()) { 225 try { 226 String fieldname = st.nextToken(); 227 Field field = GridBagLayout.class.getField( fieldname ); 228 if (field != null) { 229 Class fieldtype = field.getType(); 230 231 if (int[].class.equals( fieldtype )) { 232 field.set( lm, Util.ia( st ) ); 233 } else if (double[].class.equals( fieldtype )) { 234 field.set( lm, Util.da( st ) ); 235 } 236 237 } 238 } catch (NoSuchFieldException e) { 239 if (SwingEngine.DEBUG_MODE) 240 System.err.println( e.getMessage() ); 241 } catch (SecurityException e) { 242 if (SwingEngine.DEBUG_MODE) 243 System.err.println( e.getMessage() ); 244 } catch (IllegalArgumentException e) { 245 if (SwingEngine.DEBUG_MODE) 246 System.err.println( e.getMessage() ); 247 } catch (IllegalAccessException e) { 248 if (SwingEngine.DEBUG_MODE) 249 System.err.println( e.getMessage() ); 250 } 251 } 252 } 253 } 254 return lm; 255 } 256 257 258 263 public Class convertsTo() { 264 return TEMPLATE; 265 } 266 } 267 | Popular Tags |