001: /*
002: * @(#)IntegerEnumConverter.java 4/1/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.converter;
007:
008: /**
009: * A typical way to define a constant is to use int as the value type. For example, in SwingConstants, the following values
010: * are defined.
011: * <code><pre>
012: * public static final int CENTER = 0;
013: * public static final int TOP = 1;
014: * public static final int LEFT = 2;
015: * public static final int BOTTOM = 3;
016: * public static final int RIGHT = 4;
017: * </pre></code>
018: * Before JDK1.5, there is no enum type, so this is one way to define enumeration.
019: * When you use it, you just need to define a int field say _locaton and the
020: * valid value for _location is one of the values above. If you want to display
021: * it in UI and allow user to specify the value of _location, problem comes.
022: * You don't want to use 0, 1, 2, 3, 4 as the value doesn't mean anything from user point of view.
023: * You want user to be able to use meanful names such as "Center", "Top", "Left", "Bottom", "Right".
024: * Obviously you need a converter here to convert from integer in an enum to string, such as converting from 0 to "Center" and vice versa.
025: * That's what <tt>EnumConverter</tt> for.
026: * <p/>
027: * Combining with EnumCellConverter, EnumCellEditor,
028: * you can easily use combobox to choose value for _location like the example above using
029: * meanful strings.
030: */
031: public class EnumConverter implements ObjectConverter {
032: private String _name;
033: private Object _default;
034: private Class<?> _type;
035: private Object[] _objects;
036: private String[] _strings;
037:
038: public EnumConverter(String name, Object[] values, String[] strings) {
039: this (name, values[0].getClass(), values, strings);
040: }
041:
042: public EnumConverter(String name, Class<?> type, Object[] values,
043: String[] strings) {
044: this (name, type, values, strings, null);
045: }
046:
047: /**
048: * Creates an EnumConverter.
049: *
050: * @param name the name of the converter. The name is used to create ConverterContext and later on the EditorContext.
051: * @param type the type of the element in <code>objects</code> array.
052: * @param objects the <code>objects</code> array. All elements in the <code>objects</code> array should have the same type.
053: * @param strings the <code>strings</code> array. It contains the meanful names for the elements in <code>objects</code> array.
054: * They should one to one match with each other. The length of <code>strings</code>
055: * array should be the same as that of <code>objects</code> array. Otherwise IllegalArgumentExceptio will be thrown.
056: * @param defaultValue the default value
057: */
058: public EnumConverter(String name, Class<?> type, Object[] objects,
059: String[] strings, Object defaultValue) {
060: if (name == null || name.trim().length() == 0) {
061: throw new IllegalArgumentException(
062: "The \"name\" parameter cannot be null or empty. Please use a unique string to represent the name of the converter.");
063: }
064: _name = name;
065: if (objects == null) {
066: throw new IllegalArgumentException(
067: "The \"objects\" parameter cannot be null.");
068: }
069: if (strings == null) {
070: throw new IllegalArgumentException(
071: "The \"strings\" parameter cannot be null.");
072: }
073: if (strings.length != objects.length) {
074: throw new IllegalArgumentException(
075: "The \"objects\" and \"strings\" parameters should have the same length.");
076: }
077: _type = type;
078: _objects = objects;
079: _strings = strings;
080: _default = defaultValue;
081: }
082:
083: transient private ConverterContext _conext;
084:
085: /**
086: * Gets the converter context of this converter. The name of the context
087: * is the name of the converter where you pass in to EnumConverter's constructor.
088: *
089: * @return the converter context of this converter.
090: */
091: public ConverterContext getContext() {
092: if (_conext == null) {
093: _conext = new ConverterContext(_name);
094: }
095: return _conext;
096: }
097:
098: /**
099: * Converts the object to string. It will find the object from the <code>objects</code> array and find the matching string
100: * from <code>strings</code> array.
101: *
102: * @param object the object to be converted.
103: * @param context the converter context.
104: * @return the string for the object.
105: */
106: public String toString(Object object, ConverterContext context) {
107: for (int i = 0; i < _objects.length; i++) {
108: if ((_objects[i] == null && object == null)
109: || (_objects[i] != null && _objects[i]
110: .equals(object))) {
111: if (i < _strings.length) {
112: return _strings[i];
113: }
114: }
115: }
116: return null;
117: }
118:
119: public boolean supportToString(Object object,
120: ConverterContext context) {
121: return true;
122: }
123:
124: /**
125: * Converts the string to the object. It will find the string from the <code>strings</code> array and find the matching
126: * object from <code>objects</code> array.
127: *
128: * @param string the string to be converted
129: * @param context the converter context.
130: * @return the object of the string.
131: */
132: public Object fromString(String string, ConverterContext context) {
133: for (int i = 0; i < _strings.length; i++) {
134: if (_strings[i].equals(string)) {
135: if (i < _objects.length) {
136: return _objects[i];
137: }
138: }
139: }
140: return _default;
141: }
142:
143: public boolean supportFromString(String string,
144: ConverterContext context) {
145: return true;
146: }
147:
148: /**
149: * Gets the name of the converter.
150: *
151: * @return the name of the converter.
152: */
153: public String getName() {
154: return _name;
155: }
156:
157: /**
158: * Gets the type of the converter.
159: *
160: * @return the type of the converter.
161: */
162: public Class<?> getType() {
163: return _type;
164: }
165:
166: /**
167: * Gets the default value of the converter if it failed to find the matching object for a particular string.
168: *
169: * @return the default value.
170: */
171: public Object getDefault() {
172: return _default;
173: }
174:
175: /**
176: * Gets the <code>objects</code> array.
177: *
178: * @return the <code>objects</code> array.
179: */
180: public Object[] getObjects() {
181: return _objects;
182: }
183:
184: /**
185: * Gets the <code>strings</code> array.
186: *
187: * @return the <code>strings</code> array.
188: */
189: public String[] getStrings() {
190: return _strings;
191: }
192: }
|