KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > swixml > ConverterLibrary


1 /*--
2  $Id: ConverterLibrary.java,v 1.1 2004/03/01 07:56:05 wolfpaulus Exp $
3
4  Copyright (C) 2003-2004 Wolf Paulus.
5  All rights reserved.
6
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions, and the following disclaimer.
13
14  2. Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions, and the disclaimer that follows
16  these conditions in the documentation and/or other materials provided
17  with the distribution.
18
19  3. The end-user documentation included with the redistribution,
20  if any, must include the following acknowledgment:
21         "This product includes software developed by the
22          SWIXML Project (http://www.swixml.org/)."
23  Alternately, this acknowledgment may appear in the software itself,
24  if and wherever such third-party acknowledgments normally appear.
25
26  4. The name "Swixml" must not be used to endorse or promote products
27  derived from this software without prior written permission. For
28  written permission, please contact <info_AT_swixml_DOT_org>
29
30  5. Products derived from this software may not be called "Swixml",
31  nor may "Swixml" appear in their name, without prior written
32  permission from the Swixml Project Management.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
38  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  SUCH DAMAGE.
46  ====================================================================
47
48  This software consists of voluntary contributions made by many
49  individuals on behalf of the Swixml Project and was originally
50  created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
51  on the Swixml Project, please see <http://www.swixml.org/>.
52 */

53
54 package org.swixml;
55
56 import org.swixml.converters.*;
57
58 import javax.swing.*;
59 import javax.swing.border.Border JavaDoc;
60 import java.awt.*;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.*;
63
64 /**
65  * <p>The <code>ConverterLibrary</code> contains all available Coverters, converting Strings.</p>
66  * <p>General purpose data type converters are able to convert Strings into objects that are
67  * usually as parameters when setters are called on javax.swing objects.
68  * <pre>
69  * Available Converter include converters able to produce
70  * <ul>
71  * <li>Primitives</li>
72  * <li>Dimension</li>
73  * <li>Color</li>
74  * <li>Border</li>
75  * <li>etc.</li>
76  * </ul>
77  * </pre>
78  * <pre>
79  * Example String inputs could look like this:
80  * <ul>
81  * <li>MatteBorder(4,4,4,4,red)</li>
82  * <li>FFCCEE</li>
83  * <li>BorderLayout.CENTER</li>
84  * <li>2,2,2,2</li>
85  * </ul>
86  * </pre>
87  * Date: Dec 16, 2002
88  *
89  * @author <a HREF="mailto:wolf@paulus.com">Wolf Paulus</a>
90  * @version $Revision: 1.1 $
91  *
92  * @see org.swixml.Converter
93  */

94 public class ConverterLibrary {
95   private static ConverterLibrary instance = new ConverterLibrary();
96   private Map converters = new HashMap();
97
98   /**
99    * The only available Ctor is private to make this a Singleton
100    */

101   private ConverterLibrary() {
102     registerConverters();
103   }
104
105   /**
106    * @return <code>ConverterLibrary</code> the single INstacne of the ConverterLibarary.
107    */

108   public static synchronized ConverterLibrary getInstance() {
109     return instance;
110   }
111
112   /**
113    * @return <code>Map</code> - all registered converters.
114    * <pre>Use a class to get to the converters</pre>
115    */

116   public Map getConverters() {
117     return converters;
118   }
119
120   /**
121    * Registers <code>Converters</code> with the ConverterLibrary.
122    */

123   private void registerConverters() {
124     register( Action.class, new ActionConverter() );
125     register( Border JavaDoc.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 JavaDoc.class, new StringConverter() );
140     //
141
// Register the PrimitiveConverter class for java primitive types
142
//
143
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   /**
151    * Registers a Converter with the ConverterLibrary
152    * @param template <code>Class</code> type of the objects the Converter creates
153    * @param converter <code>Converter</code> Instance of Converter able to vconvert Strings into objects of the given type
154    */

155   public void register( Class JavaDoc template, Converter converter ) {
156     converters.put( template, converter );
157   }
158
159   /**
160    * Indicates if a the ConverterLibary has a Converter producing instances of the given Class.
161    * @param template <code>Class</code>
162    * @return <code>boolean</code> true, if the ConverterLibrary has a Converter to produce an instances of the gioven class.
163    */

164   public boolean hasConverter( Class JavaDoc 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   /**
174    * Returns a <code>Converter</code> instance, able to produce obejcts of the given <code>class</code>
175    * @param template <code>Class</code> Class of the object the <code>Converter</code> needs to produce.
176    * @return <code>Converter</code> - instance of the given Converter class.
177    */

178   public Converter getConverter( Class JavaDoc template ) {
179     return (Converter) converters.get( template );
180
181   }
182 }
183
Popular Tags