| 1 |
/* |
| 2 |
* @(#) ConverterContext.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
/** |
| 9 |
* The context object used by ObjectConverter. For the same type, we may need different way to convert them. This |
| 10 |
* context is used so that user can register different converters for the same type. |
| 11 |
*/ |
| 12 |
public class ConverterContext extends AbstractContext { |
| 13 |
/** |
| 14 |
* Default converter context with empty name and no user object. |
| 15 |
*/ |
| 16 |
public static ConverterContext DEFAULT_CONTEXT = new ConverterContext(""); |
| 17 |
/** |
| 18 |
* Default converter context with dynamic value to work with EditorContext#DEFAULT_CONTEXT_DYNAMIC_VALUE. |
| 19 |
*/ |
| 20 |
@Deprecated |
| 21 |
public static ConverterContext DEFAULT_CONTEXT_DYNAMIC_VALUE = new ConverterContext("Dynamic Value"); |
| 22 |
private static final long serialVersionUID = 8015351559541303641L; |
| 23 |
|
| 24 |
/** |
| 25 |
* Creates a converter context with a name. |
| 26 |
* |
| 27 |
* @param name the name of the converter context |
| 28 |
*/ |
| 29 |
public ConverterContext(String name) { |
| 30 |
super(name); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Creates a converter context with a name and an object. |
| 35 |
* |
| 36 |
* @param name the name of the converter context |
| 37 |
* @param object the user object. It can be used as any object to pass information along. |
| 38 |
*/ |
| 39 |
public ConverterContext(String name, Object object) { |
| 40 |
super(name, object); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Checks if the context is for an array. By conversion, we put "[]" at the end of the converter context's name if |
| 45 |
* the context is for an array data type. Please note, this is a conversion only. If developer chooses to not put |
| 46 |
* "[]" at the end for their own customized context, this method will fail. |
| 47 |
* |
| 48 |
* @param context the context. |
| 49 |
* @return true or false. |
| 50 |
*/ |
| 51 |
public static boolean isArrayConverterContext(ConverterContext context) { |
| 52 |
return context != null && context.getName() != null && context.getName().endsWith("[]"); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Gets the converter context which removes the trailing "[]" from the context name. |
| 57 |
* |
| 58 |
* @param context the context for an array type. |
| 59 |
* @return the converter context for the element type of an array. |
| 60 |
*/ |
| 61 |
public static ConverterContext getElementConverterContext(ConverterContext context) { |
| 62 |
if (isArrayConverterContext(context)) { |
| 63 |
return new ConverterContext(context.getName().substring(0, context.getName().length() - 2)); |
| 64 |
} |
| 65 |
else { |
| 66 |
return context; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Gets the converter context which add a trailing "[]" to the context name. |
| 72 |
* |
| 73 |
* @param context the context for the element type of an array. |
| 74 |
* @return the converter context the array of the element type. |
| 75 |
*/ |
| 76 |
public static ConverterContext getArrayConverterContext(ConverterContext context) { |
| 77 |
if (!isArrayConverterContext(context)) { |
| 78 |
return new ConverterContext(context.getName() + "[]"); |
| 79 |
} |
| 80 |
else { |
| 81 |
return context; |
| 82 |
} |
| 83 |
} |
| 84 |
} |