1 53 54 package org.swixml; 55 56 import org.swixml.converters.*; 57 58 import javax.swing.*; 59 import javax.swing.border.Border ; 60 import java.awt.*; 61 import java.awt.event.ActionListener ; 62 import java.util.*; 63 64 94 public class ConverterLibrary { 95 private static ConverterLibrary instance = new ConverterLibrary(); 96 private Map converters = new HashMap(); 97 98 101 private ConverterLibrary() { 102 registerConverters(); 103 } 104 105 108 public static synchronized ConverterLibrary getInstance() { 109 return instance; 110 } 111 112 116 public Map getConverters() { 117 return converters; 118 } 119 120 123 private void registerConverters() { 124 register( Action.class, new ActionConverter() ); 125 register( Border .class, new BorderConverter() ); 126 register( Color.class, new ColorConverter() ); 127 register( Component.class, new ComponentConverter() ); 128 register( Dimension.class, new DimensionConverter() ); 129 register( Font.class, new FontConverter() ); 130 register( Image.class, new ImageConverter() ); 131 register( Icon.class, new ImageIconConverter() ); 132 register( ImageIcon.class, new ImageIconConverter() ); 133 register( Insets.class, new InsetsConverter() ); 134 register( KeyStroke.class, new KeyStrokeConverter() ); 135 register( LayoutManager.class, new LayoutConverter() ); 136 register( Locale.class, new LocaleConverter() ); 137 register( Point.class, new PointConverter() ); 138 register( Rectangle.class, new RectangleConverter() ); 139 register( String .class, new StringConverter() ); 140 register( boolean.class, new PrimitiveConverter() ); 144 register( int.class, new PrimitiveConverter() ); 145 register( long.class, new PrimitiveConverter() ); 146 register( float.class, new PrimitiveConverter() ); 147 register( double.class, new PrimitiveConverter() ); 148 } 149 150 155 public void register( Class template, Converter converter ) { 156 converters.put( template, converter ); 157 } 158 159 164 public boolean hasConverter( Class template ) { 165 boolean found = converters.keySet().contains( template ); 166 Iterator it = converters.keySet().iterator(); 167 while (!found && it!=null && it.hasNext()) { 168 found= template.isAssignableFrom( ((Converter)converters.get(it.next())).convertsTo()); 169 } 170 return found; 171 } 172 173 178 public Converter getConverter( Class template ) { 179 return (Converter) converters.get( template ); 180 181 } 182 } 183 | Popular Tags |