| 1 |
/* |
| 2 |
* @(#)IntegerEnumConverter.java 4/1/2005 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2005 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import com.jidesoft.utils.ReflectionUtils; |
| 9 |
|
| 10 |
import java.lang.reflect.Array; |
| 11 |
|
| 12 |
/** |
| 13 |
* A typical way to define a constant is to use int as the value type. For example, in SwingConstants, the following |
| 14 |
* values are defined. |
| 15 |
* <code><pre> |
| 16 |
* public static final int CENTER = 0; |
| 17 |
* public static final int TOP = 1; |
| 18 |
* public static final int LEFT = 2; |
| 19 |
* public static final int BOTTOM = 3; |
| 20 |
* public static final int RIGHT = 4; |
| 21 |
* </pre></code> |
| 22 |
* Before JDK1.5, there is no enum type, so this is one way to define enumeration. When you use it, you just need to |
| 23 |
* define a int field say _locaton and the valid value for _location is one of the values above. If you want to display |
| 24 |
* it in UI and allow user to specify the value of _location, problem comes. You don't want to use 0, 1, 2, 3, 4 as the |
| 25 |
* value doesn't mean anything from user point of view. You want user to be able to use meaningful names such as |
| 26 |
* "Center", "Top", "Left", "Bottom", "Right". Obviously you need a converter here to convert from integer in an enum to |
| 27 |
* string, such as converting from 0 to "Center" and vice verse. That's what <tt>EnumConverter</tt> for. |
| 28 |
* <p/> |
| 29 |
* Combining with EnumCellConverter, EnumCellEditor, you can easily use combobox to choose value for _location like the |
| 30 |
* example above using meaningful strings. |
| 31 |
*/ |
| 32 |
public class EnumConverter implements ObjectConverter { |
| 33 |
private String _name; |
| 34 |
private Object _default; |
| 35 |
private Class<?> _type; |
| 36 |
private Object[] _objects; |
| 37 |
private String[] _strings; |
| 38 |
private boolean _strict = true; |
| 39 |
|
| 40 |
/** |
| 41 |
* The constructor to convert a enum type class. |
| 42 |
* <p/> |
| 43 |
* Reflection is used to invoke Enum#getValues(). Please consider make the enum class protected or public if you |
| 44 |
* want to release your version after obfuscated. Otherwise, this constructor may not be able to find correct class |
| 45 |
* method to work. |
| 46 |
* |
| 47 |
* @param enumType the enum type |
| 48 |
* @since 3.4.0 |
| 49 |
*/ |
| 50 |
public EnumConverter(Class<? extends Enum> enumType) { |
| 51 |
if (enumType == null || !enumType.isEnum()) { |
| 52 |
throw new IllegalArgumentException("To use this constructor, the type has to be an enum type."); |
| 53 |
} |
| 54 |
String name = enumType.getName(); |
| 55 |
if (name == null || name.trim().length() == 0) { |
| 56 |
throw new IllegalArgumentException("Empty enum type name."); |
| 57 |
} |
| 58 |
int index = name.lastIndexOf("$"); |
| 59 |
if (index >= 0) { |
| 60 |
_name = name.substring(index + 1); |
| 61 |
} |
| 62 |
else { |
| 63 |
_name = name; |
| 64 |
} |
| 65 |
_type = enumType; |
| 66 |
try { |
| 67 |
Object values = ReflectionUtils.callStatic(enumType, "values", null, null); |
| 68 |
if (!values.getClass().isArray()) { |
| 69 |
throw new IllegalArgumentException("Illegal enum type."); |
| 70 |
} |
| 71 |
int length = Array.getLength(values); |
| 72 |
_objects = new Object[length]; |
| 73 |
_strings = new String[length]; |
| 74 |
for (int i = 0; i < length; i++) { |
| 75 |
_objects[i] = Array.get(values, i); |
| 76 |
_strings[i] = "" + _objects[i]; |
| 77 |
} |
| 78 |
} |
| 79 |
catch (Exception e) { |
| 80 |
throw new IllegalArgumentException("Illegal enum type."); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public EnumConverter(String name, Object[] values, String[] strings) { |
| 85 |
this(name, values[0] == null ? (values.length > 1 ? values[1].getClass() : Object.class) : values[0].getClass(), values, strings); |
| 86 |
} |
| 87 |
|
| 88 |
public EnumConverter(String name, Class<?> type, Object[] values, String[] strings) { |
| 89 |
this(name, type, values, strings, null); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Creates an EnumConverter. |
| 94 |
* |
| 95 |
* @param name the name of the converter. The name is used to create ConverterContext and later on the |
| 96 |
* EditorContext. |
| 97 |
* @param type the type of the element in <code>objects</code> array. |
| 98 |
* @param objects the <code>objects</code> array. All elements in the <code>objects</code> array should have |
| 99 |
* the same type. |
| 100 |
* @param strings the <code>strings</code> array. It contains the meaningful names for the elements in |
| 101 |
* <code>objects</code> array. They should one to one match with each other. The length of |
| 102 |
* <code>strings</code> array should be the same as that of <code>objects</code> array. |
| 103 |
* Otherwise IllegalArgumentExceptio will be thrown. |
| 104 |
* @param defaultValue the default value |
| 105 |
*/ |
| 106 |
public EnumConverter(String name, Class<?> type, Object[] objects, String[] strings, Object defaultValue) { |
| 107 |
if (name == null || name.trim().length() == 0) { |
| 108 |
throw new IllegalArgumentException("The \"name\" parameter cannot be null or empty. Please use a unique string to represent the name of the converter."); |
| 109 |
} |
| 110 |
_name = name; |
| 111 |
if (objects == null) { |
| 112 |
throw new IllegalArgumentException("The \"objects\" parameter cannot be null."); |
| 113 |
} |
| 114 |
if (strings == null) { |
| 115 |
throw new IllegalArgumentException("The \"strings\" parameter cannot be null."); |
| 116 |
} |
| 117 |
if (strings.length != objects.length) { |
| 118 |
throw new IllegalArgumentException("The \"objects\" and \"strings\" parameters should have the same length."); |
| 119 |
} |
| 120 |
_type = type; |
| 121 |
_objects = objects; |
| 122 |
_strings = strings; |
| 123 |
_default = defaultValue; |
| 124 |
} |
| 125 |
|
| 126 |
transient private ConverterContext _context; |
| 127 |
|
| 128 |
/** |
| 129 |
* Gets the converter context of this converter. The name of the context is the name of the converter where you pass |
| 130 |
* in to EnumConverter's constructor. |
| 131 |
* |
| 132 |
* @return the converter context of this converter. |
| 133 |
*/ |
| 134 |
public ConverterContext getContext() { |
| 135 |
if (_context == null) { |
| 136 |
_context = new ConverterContext(_name); |
| 137 |
} |
| 138 |
return _context; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Converts the object to string. It will find the object from the <code>objects</code> array and find the matching |
| 143 |
* string from <code>strings</code> array. If {@link #isStrict()} is true, null will be returned if nothing matches. |
| 144 |
* Otherwise, it will return the string value of the object using toString. |
| 145 |
* |
| 146 |
* @param object the object to be converted. |
| 147 |
* @param context the converter context. |
| 148 |
* @return the string for the object. |
| 149 |
*/ |
| 150 |
public String toString(Object object, ConverterContext context) { |
| 151 |
for (int i = 0; i < _objects.length; i++) { |
| 152 |
if ((_objects[i] == null && object == null) || (_objects[i] != null && _objects[i].equals(object))) { |
| 153 |
if (i < _strings.length) { |
| 154 |
return _strings[i]; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
return isStrict() ? null : "" + object; |
| 159 |
} |
| 160 |
|
| 161 |
public boolean supportToString(Object object, ConverterContext context) { |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Converts the string to the object. It will find the string from the <code>strings</code> array and find the |
| 167 |
* matching object from <code>objects</code> array. If {@link #isStrict()} is true, the default value will be |
| 168 |
* returned if nothing matches. Otherwise, it will return the string itself that is passed in. |
| 169 |
* |
| 170 |
* @param string the string to be converted |
| 171 |
* @param context the converter context. |
| 172 |
* @return the object of the string. |
| 173 |
*/ |
| 174 |
public Object fromString(String string, ConverterContext context) { |
| 175 |
for (int i = 0; i < _strings.length; i++) { |
| 176 |
if (_strings[i].equals(string)) { |
| 177 |
if (i < _objects.length) { |
| 178 |
return _objects[i]; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
return isStrict() ? _default : string; |
| 183 |
} |
| 184 |
|
| 185 |
public boolean supportFromString(String string, ConverterContext context) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Gets the name of the converter. |
| 191 |
* |
| 192 |
* @return the name of the converter. |
| 193 |
*/ |
| 194 |
public String getName() { |
| 195 |
return _name; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Gets the type of the converter. |
| 200 |
* |
| 201 |
* @return the type of the converter. |
| 202 |
*/ |
| 203 |
public Class<?> getType() { |
| 204 |
return _type; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Gets the default value of the converter if it failed to find the matching object for a particular string. |
| 209 |
* |
| 210 |
* @return the default value. |
| 211 |
*/ |
| 212 |
public Object getDefault() { |
| 213 |
return _default; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Gets the <code>objects</code> array. |
| 218 |
* |
| 219 |
* @return the <code>objects</code> array. |
| 220 |
*/ |
| 221 |
public Object[] getObjects() { |
| 222 |
return _objects; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Gets a partial object array from the complete enum <code>objects</code> array. |
| 227 |
* |
| 228 |
* @param indices of the partial enum values. |
| 229 |
* @return the <code>objects</code> array. |
| 230 |
*/ |
| 231 |
public Object[] getObjects(int[] indices) { |
| 232 |
Object[] objects = new Object[indices.length]; |
| 233 |
for (int i = 0; i < objects.length; i++) { |
| 234 |
objects[i] = _objects[indices[i]]; |
| 235 |
} |
| 236 |
return objects; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Gets the <code>strings</code> array. |
| 241 |
* |
| 242 |
* @return the <code>strings</code> array. |
| 243 |
*/ |
| 244 |
public String[] getStrings() { |
| 245 |
return _strings; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Converts an object array to a String array using ObjectConverterManager. |
| 250 |
* <p/> |
| 251 |
* This method can be used, for example, for Enum type, to provide a default string representation of the enum |
| 252 |
* values. |
| 253 |
* <code><pre> |
| 254 |
* ObjectConverter converter = new EnumConverter("Rank", Rank.values(), |
| 255 |
* EnumConverter.toStrings(Rank.values())); |
| 256 |
* </pre></code> |
| 257 |
* Of course, you can still define your own string array for the enum values if the default one doesn't work well. |
| 258 |
* |
| 259 |
* @param values the object array. |
| 260 |
* @return the string array. |
| 261 |
*/ |
| 262 |
public static String[] toStrings(Object[] values) { |
| 263 |
return toStrings(values, null); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Converts an object array to a String array using ObjectConverterManager. |
| 268 |
* |
| 269 |
* @param values the object array. |
| 270 |
* @param converterContext the converter context used when calling ObjectConverterManager.toString. |
| 271 |
* @return the string array. |
| 272 |
*/ |
| 273 |
public static String[] toStrings(Object[] values, ConverterContext converterContext) { |
| 274 |
String[] s = new String[values.length]; |
| 275 |
for (int i = 0; i < s.length; i++) { |
| 276 |
s[i] = ObjectConverterManager.toString(values[i], values[i].getClass(), converterContext); |
| 277 |
} |
| 278 |
return s; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Checks if the EnumConverter is strict about the value that passed to fromString and toString. If true, fromString |
| 283 |
* will convert any String that doesn't match to the default value, toString will return null if the value doesn't |
| 284 |
* match. If false, the string itself will be return from fromString. Default is true. |
| 285 |
* |
| 286 |
* @return true or false. |
| 287 |
*/ |
| 288 |
public boolean isStrict() { |
| 289 |
return _strict; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Sets if the EnumConverter is strict about the value that passed to fromString and toString. Default is true. |
| 294 |
* |
| 295 |
* @param strict true or false. |
| 296 |
*/ |
| 297 |
public void setStrict(boolean strict) { |
| 298 |
_strict = strict; |
| 299 |
} |
| 300 |
} |