| 1 |
/* |
| 2 |
* @(#)RangeConverter.java 5/22/2014 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2014 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import com.jidesoft.range.Range; |
| 9 |
|
| 10 |
import java.text.MessageFormat; |
| 11 |
import java.util.Locale; |
| 12 |
|
| 13 |
/** |
| 14 |
* An abstract class that is extended by any converters that convert to/from an array-like format, such as 1, 2, 3. |
| 15 |
* Examples are Point. Point(100, 200) can convert to/from "100, 200" <br> You have the choice of what the separator is; |
| 16 |
* separator is the ", " in the Point example above. |
| 17 |
*/ |
| 18 |
public class RangeConverter implements ObjectConverter { |
| 19 |
private Class<?> _elementClass; |
| 20 |
|
| 21 |
public static final ConverterContext CONTEXT_RANGE = new ConverterContext("Range"); |
| 22 |
public static final ConverterContext CONTEXT_MULTIPLE = new ConverterContext("Multiple"); |
| 23 |
|
| 24 |
/** |
| 25 |
* Creates an ArrayConverter. |
| 26 |
* |
| 27 |
* @param elementClass class of the array element. Assume all elements have the same class type. If not, use the |
| 28 |
* constructor which takes Class<?>[] as parameter. |
| 29 |
*/ |
| 30 |
public RangeConverter(Class<?> elementClass) { |
| 31 |
_elementClass = elementClass; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Converts from array to string by concating them with separators. |
| 36 |
* |
| 37 |
* @param objects an array of objects |
| 38 |
* @param context converter context |
| 39 |
* @return string all objects concatenated with separators |
| 40 |
*/ |
| 41 |
public String arrayToString(Object[] objects, ConverterContext context) { |
| 42 |
if (objects.length > 1) { |
| 43 |
if (CONTEXT_RANGE.equals(context)) { |
| 44 |
String s = Resource.getResourceBundle(Locale.getDefault()).getString("Range.range"); |
| 45 |
return MessageFormat.format(s, toString(0, objects[0], context), toString(1, objects[1], context)); |
| 46 |
} |
| 47 |
else { |
| 48 |
return Resource.getResourceBundle(Locale.getDefault()).getString("Range.multiple"); |
| 49 |
} |
| 50 |
} |
| 51 |
else if (objects.length == 1) { |
| 52 |
return toString(0, objects[0], context); |
| 53 |
} |
| 54 |
return ""; |
| 55 |
} |
| 56 |
|
| 57 |
protected String toString(int i, Object o, ConverterContext context) { |
| 58 |
return ObjectConverterManager.toString(o, _elementClass, context); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Converts from string to an array of objects, using separator to separate the string. |
| 63 |
* |
| 64 |
* @param string string to be converted |
| 65 |
* @param context converter context |
| 66 |
* @return the array |
| 67 |
*/ |
| 68 |
public Object[] arrayFromString(String string, ConverterContext context) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
protected Object fromString(int i, String s, ConverterContext context) { |
| 73 |
return ObjectConverterManager.fromString(s, _elementClass, context); |
| 74 |
} |
| 75 |
|
| 76 |
public String toString(Object object, ConverterContext context) { |
| 77 |
if (object instanceof Range) { |
| 78 |
Range range = (Range) object; |
| 79 |
if (range.size() == 0) { |
| 80 |
return arrayToString(new Object[]{range.lower()}, context); |
| 81 |
} |
| 82 |
return arrayToString(new Object[]{range.lower(), range.upper()}, context); |
| 83 |
} |
| 84 |
else { |
| 85 |
return ""; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public boolean supportToString(Object object, ConverterContext context) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
public Object fromString(String string, ConverterContext context) { |
| 94 |
if (string == null || string.trim().length() == 0) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
Object[] objects = arrayFromString(string, context); |
| 98 |
if (objects == null) return null; |
| 99 |
else if (objects.length == 1) { |
| 100 |
return null; |
| 101 |
} |
| 102 |
else if (objects.length >= 2) { |
| 103 |
return null; |
| 104 |
} |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
public boolean supportFromString(String string, ConverterContext context) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |