001: /*
002: * @(#) ObjectConverterManager.java
003: *
004: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
005: */
006: package com.jidesoft.converter;
007:
008: import java.awt.*;
009: import java.io.File;
010: import java.util.Calendar;
011: import java.util.Date;
012:
013: /**
014: * A global object that can register converter with a type and a ConverterContext.
015: */
016: public class ObjectConverterManager {
017:
018: private static CacheMap<ObjectConverter, ConverterContext> _cache = new CacheMap<ObjectConverter, ConverterContext>(
019: ConverterContext.DEFAULT_CONTEXT);
020:
021: private static ObjectConverter _defaultConverter = new DefaultObjectConverter();
022:
023: /**
024: * Registers a converter with the type specified as class and a converter context specified as context.
025: *
026: * @param clazz the type of which the converter will registered.
027: * @param converter the converter to be registered
028: * @param context the converter context.
029: */
030: public static void registerConverter(Class<?> clazz,
031: ObjectConverter converter, ConverterContext context) {
032: if (clazz == null) {
033: throw new IllegalArgumentException(
034: "Parameter class cannot be null");
035: }
036:
037: if (context == null) {
038: context = ConverterContext.DEFAULT_CONTEXT;
039: }
040: _cache.register(clazz, converter, context);
041: }
042:
043: /**
044: * Registers as the default converter with type specified as clazz.
045: *
046: * @param clazz the type of which the converter will be registered.
047: * @param converter the converter to be registered
048: */
049: public static void registerConverter(Class<?> clazz,
050: ObjectConverter converter) {
051: registerConverter(clazz, converter,
052: ConverterContext.DEFAULT_CONTEXT);
053: }
054:
055: /**
056: * Unregisters converter associated with clazz and context.
057: *
058: * @param clazz the type of which the converter will be unregistered.
059: * @param context the converter context.
060: */
061: public static void unregisterConverter(Class<?> clazz,
062: ConverterContext context) {
063: if (context == null) {
064: context = ConverterContext.DEFAULT_CONTEXT;
065: }
066: _cache.unregister(clazz, context);
067: }
068:
069: /**
070: * Unregisters converter associated with clazz.
071: *
072: * @param clazz the type of which the converter will be unregistered.
073: */
074: public static void unregisterConverter(Class<?> clazz) {
075: unregisterConverter(clazz, ConverterContext.DEFAULT_CONTEXT);
076: }
077:
078: /**
079: * Unregisters all the converters which registered before.
080: */
081: public static void unregisterAllConverters() {
082: _cache.clear();
083: }
084:
085: /**
086: * Gets the registered converter associated with class and context.
087: *
088: * @param clazz the type of which the converter will be registered.
089: * @param context the converter context.
090: * @return the registered converter.
091: */
092: public static ObjectConverter getConverter(Class<?> clazz,
093: ConverterContext context) {
094: if (isAutoInit()) {
095: initDefaultConverter();
096: }
097:
098: if (context == null) {
099: context = ConverterContext.DEFAULT_CONTEXT;
100: }
101: ObjectConverter converter = _cache.getRegisteredObject(clazz,
102: context);
103: if (converter != null) {
104: return converter;
105: } else {
106: return _defaultConverter;
107: }
108: }
109:
110: /**
111: * Gets the converter associated with the type.
112: *
113: * @param clazz type
114: * @return the converter
115: */
116: public static ObjectConverter getConverter(Class<?> clazz) {
117: return getConverter(clazz, ConverterContext.DEFAULT_CONTEXT);
118: }
119:
120: /**
121: * Converts an object to string using default converter context.
122: *
123: * @param object object to be converted.
124: * @return the string
125: */
126: public static String toString(Object object) {
127: if (object != null)
128: return toString(object, object.getClass(),
129: ConverterContext.DEFAULT_CONTEXT);
130: else
131: return "";
132: }
133:
134: /**
135: * Converts an object to string using default converter context.
136: *
137: * @param object object to be converted.
138: * @param clazz type of the object
139: * @return the string
140: */
141: public static String toString(Object object, Class<?> clazz) {
142: return toString(object, clazz, ConverterContext.DEFAULT_CONTEXT);
143: }
144:
145: /**
146: * Converts an object to string using converter context sepcified.
147: *
148: * @param object object to be converted.
149: * @param clazz type of the object
150: * @param context converter context
151: * @return the string converted from object
152: */
153: public static String toString(Object object, Class<?> clazz,
154: ConverterContext context) {
155: ObjectConverter converter = getConverter(clazz, context);
156: if (converter != null
157: && converter.supportToString(object, context)) {
158: return converter.toString(object, context);
159: } else if (object == null) {
160: return "";
161: } else {
162: return object.toString();
163: }
164: }
165:
166: /**
167: * Converts from a string to an object with type class.
168: *
169: * @param string the string to be converted
170: * @param clazz the type to be converted to
171: * @return the object of type class which is converted from string
172: */
173: public static Object fromString(String string, Class<?> clazz) {
174: return fromString(string, clazz,
175: ConverterContext.DEFAULT_CONTEXT);
176: }
177:
178: /**
179: * Converts from a string to an object with type class using the converter context.
180: *
181: * @param string the string to be converted
182: * @param clazz the type to be converted to
183: * @param context converter context to be used
184: * @return the object of type class which is converted from string
185: */
186: public static Object fromString(String string, Class<?> clazz,
187: ConverterContext context) {
188: ObjectConverter converter = getConverter(clazz, context);
189: if (converter != null
190: && converter.supportFromString(string, context)) {
191: return converter.fromString(string, context);
192: } else {
193: return null;
194: }
195: }
196:
197: private static boolean _inited = false;
198: private static boolean _autoInit = true;
199:
200: /**
201: * Checks the value of autoInit.
202: *
203: * @return true or false.
204: * @see #setAutoInit(boolean)
205: */
206: public static boolean isAutoInit() {
207: return _autoInit;
208: }
209:
210: /**
211: * Sets autoInit to true or false. If autoInit is true, whenever someone tries to call methods like
212: * as toString or fromString, {@link #initDefaultConverter()} will be called if it has never be called.
213: * By default, autoInit is true.
214: * <p/>
215: * This might affect the behavior if users provide their own converters and want to overwrite
216: * default converters. In this case, instead of depending on autoInit to initialize default converters,
217: * you should call {@link #initDefaultConverter()} first, then call registerConverter to add your own converters.
218: *
219: * @param autoInit true or false.
220: */
221: public static void setAutoInit(boolean autoInit) {
222: _autoInit = autoInit;
223: }
224:
225: /**
226: * Adds a listener to the list that's notified each time a change
227: * to the manager occurs.
228: *
229: * @param l the RegistrationListener
230: */
231: public static void addRegistrationListener(RegistrationListener l) {
232: _cache.addRegistrationListener(l);
233: }
234:
235: /**
236: * Removes a listener from the list that's notified each time a
237: * change to the manager occurs.
238: *
239: * @param l the RegistrationListener
240: */
241: public static void removeRegistrationListener(RegistrationListener l) {
242: _cache.removeRegistrationListener(l);
243: }
244:
245: /**
246: * Returns an array of all the registration listeners
247: * registered on this manager.
248: *
249: * @return all of this registration's <code>RegistrationListener</code>s
250: * or an empty array if no registration listeners are currently registered
251: * @see #addRegistrationListener
252: * @see #removeRegistrationListener
253: */
254: public static RegistrationListener[] getRegistrationListeners() {
255: return _cache.getRegistrationListeners();
256: }
257:
258: /**
259: * Gets the available ConverterContexts registered with the class.
260: *
261: * @param clazz the class.
262: * @return the available ConverterContexts.
263: */
264: public static ConverterContext[] getConverterContexts(Class<?> clazz) {
265: return _cache.getKeys(clazz, new ConverterContext[0]);
266: }
267:
268: /**
269: * Initialize default converters. Please make sure you call this method before you use any
270: * converter related classes. By default we register following converters.
271: * <ul>
272: * <li> registerConverter(String.class, new DefaultObjectConverter());
273: * <li> registerConverter(Integer.class, new IntegerConverter());
274: * <li> registerConverter(int.class, new IntegerConverter());
275: * <li> registerConverter(Integer.class, new NaturalNumberConverter(), NaturalNumberConverter.CONTEXT);
276: * <li> registerConverter(int.class, new NaturalNumberConverter(), NaturalNumberConverter.CONTEXT);
277: * <li> registerConverter(Long.class, new LongConverter());
278: * <li> registerConverter(long.class, new LongConverter());
279: * <li> registerConverter(Double.class, new DoubleConverter());
280: * <li> registerConverter(double.class, new DoubleConverter());
281: * <li> registerConverter(Float.class, new FloatConverter());
282: * <li> registerConverter(float.class, new FloatConverter());
283: * <li> registerConverter(Short.class, new ShortConverter());
284: * <li> registerConverter(short.class, new ShortConverter());
285: * <li> registerConverter(Rectangle.class, new RectangleConverter());
286: * <li> registerConverter(Point.class, new PointConverter());
287: * <li> registerConverter(Insets.class, new InsetsConverter());
288: * <li> registerConverter(Dimension.class, new DimensionConverter());
289: * <li> registerConverter(Boolean.class, new BooleanConverter());
290: * <li> registerConverter(boolean.class, new BooleanConverter());
291: * <li> registerConverter(File.class, new FileConverter());
292: * <li> registerConverter(String.class, new FontNameConverter(), FontNameConverter.CONTEXT);
293: * <li> registerConverter(Date.class, new DateConverter());
294: * <li> registerConverter(Calendar.class, new CalendarConverter());
295: * <li> registerConverter(Calendar.class, new MonthConverter(), MonthConverter.CONTEXT_MONTH);
296: * <li> registerConverter(Color.class, new RgbColorConverter());
297: * <li> registerConverter(Color.class, new HexColorConverter(), ColorConverter.CONTEXT_HEX);
298: * <li> registerConverter(String[].class, new StringArrayConverter());
299: * </ul>
300: */
301: public static void initDefaultConverter() {
302: if (_inited) {
303: return;
304: }
305:
306: registerConverter(String.class, new DefaultObjectConverter());
307: registerConverter(char[].class, new PasswordConverter(),
308: PasswordConverter.CONTEXT);
309:
310: IntegerConverter integerConverter = new IntegerConverter();
311: registerConverter(Integer.class, integerConverter);
312: registerConverter(int.class, integerConverter);
313:
314: NaturalNumberConverter naturalNumberConverter = new NaturalNumberConverter();
315: registerConverter(Integer.class, naturalNumberConverter,
316: NaturalNumberConverter.CONTEXT);
317: registerConverter(int.class, naturalNumberConverter,
318: NaturalNumberConverter.CONTEXT);
319:
320: LongConverter longConverter = new LongConverter();
321: registerConverter(Long.class, longConverter);
322: registerConverter(long.class, longConverter);
323:
324: DoubleConverter doubleConverter = new DoubleConverter();
325: registerConverter(Double.class, doubleConverter);
326: registerConverter(double.class, doubleConverter);
327:
328: FloatConverter floatConverter = new FloatConverter();
329: registerConverter(Float.class, floatConverter);
330: registerConverter(float.class, floatConverter);
331:
332: ShortConverter shortConverter = new ShortConverter();
333: registerConverter(Short.class, shortConverter);
334: registerConverter(short.class, shortConverter);
335:
336: ByteConverter byteConverter = new ByteConverter();
337: registerConverter(Byte.class, byteConverter);
338: registerConverter(byte.class, byteConverter);
339:
340: registerConverter(Rectangle.class, new RectangleConverter());
341: registerConverter(Point.class, new PointConverter());
342: registerConverter(Insets.class, new InsetsConverter());
343: registerConverter(Dimension.class, new DimensionConverter());
344:
345: BooleanConverter booleanConverter = new BooleanConverter();
346: registerConverter(Boolean.class, booleanConverter);
347: registerConverter(boolean.class, booleanConverter);
348:
349: registerConverter(File.class, new FileConverter());
350: registerConverter(String.class, new FontNameConverter(),
351: FontNameConverter.CONTEXT);
352:
353: DateConverter dateConverter = new DateConverter();
354: registerConverter(Date.class, dateConverter);
355: registerConverter(Date.class, dateConverter,
356: DateConverter.DATETIME_CONTEXT);
357: registerConverter(Date.class, dateConverter,
358: DateConverter.TIME_CONTEXT);
359:
360: CalendarConverter calendarConverter = new CalendarConverter();
361: registerConverter(Calendar.class, calendarConverter);
362: registerConverter(Calendar.class, calendarConverter,
363: DateConverter.DATETIME_CONTEXT);
364: registerConverter(Calendar.class, calendarConverter,
365: DateConverter.TIME_CONTEXT);
366:
367: registerConverter(Calendar.class, new MonthConverter(),
368: MonthConverter.CONTEXT_MONTH);
369: registerConverter(Color.class, new RgbColorConverter());
370: registerConverter(Color.class, new HexColorConverter(),
371: ColorConverter.CONTEXT_HEX);
372:
373: registerConverter(String[].class, new StringArrayConverter());
374:
375: QuarterNameConverter quarterNameConverter = new QuarterNameConverter();
376: registerConverter(int.class, quarterNameConverter,
377: QuarterNameConverter.CONTEXT);
378: registerConverter(Integer.class, quarterNameConverter,
379: QuarterNameConverter.CONTEXT);
380:
381: registerConverter(Font.class, new FontConverter());
382:
383: CurrencyConverter currencyConverter = new CurrencyConverter();
384: registerConverter(Float.class, currencyConverter,
385: CurrencyConverter.CONTEXT);
386: registerConverter(float.class, currencyConverter,
387: CurrencyConverter.CONTEXT);
388: registerConverter(Double.class, currencyConverter,
389: CurrencyConverter.CONTEXT);
390: registerConverter(double.class, currencyConverter,
391: CurrencyConverter.CONTEXT);
392:
393: PercentConverter percentConverter = new PercentConverter();
394: registerConverter(Float.class, percentConverter,
395: PercentConverter.CONTEXT);
396: registerConverter(float.class, percentConverter,
397: PercentConverter.CONTEXT);
398: registerConverter(Double.class, percentConverter,
399: PercentConverter.CONTEXT);
400: registerConverter(double.class, percentConverter,
401: PercentConverter.CONTEXT);
402:
403: MonthNameConverter monthNameConverter = new MonthNameConverter();
404: registerConverter(Integer.class, monthNameConverter,
405: MonthNameConverter.CONTEXT);
406: registerConverter(int.class, monthNameConverter,
407: MonthNameConverter.CONTEXT);
408:
409: YearNameConverter yearNameConverter = new YearNameConverter();
410: registerConverter(Integer.class, yearNameConverter,
411: YearNameConverter.CONTEXT);
412: registerConverter(int.class, yearNameConverter,
413: YearNameConverter.CONTEXT);
414:
415: ObjectConverterManager.registerConverter(int[].class,
416: new DefaultArrayConverter("; ", int.class));
417: registerConverter(Object[].class, new DefaultArrayConverter(
418: "; ", Object.class));
419: registerConverter(String[].class, new DefaultArrayConverter(
420: "; ", String.class));
421:
422: _inited = true;
423: }
424:
425: /**
426: * If {@link #initDefaultConverter()} is called once, calling it again will have no effect because an internal flag is set.
427: * This method will reset the internal flag so that you can call {@link #initDefaultConverter()} in case you unresgister all
428: * converters using {@link #unregisterAllConverters()}.
429: */
430: public static void resetInit() {
431: _inited = false;
432: }
433: }
|