01: package org.conform;
02:
03: import org.conform.format.FormatFactory;
04: import org.conform.format.ResourceProvider;
05:
06: import java.util.*;
07:
08: /**
09: * A domain provider for java 5 enumerations.
10: * It is using <code>type.getClass().getName() + "." + o.toString()</code> as the key to ResourceProvider
11: * and a java.text.Collator for locale-sensitive sorting.
12: */
13: public class EnumDomainProvider extends AbstractDomainProvider {
14: protected Class type;
15:
16: public EnumDomainProvider(Class type) {
17: super (null);
18: this .type = type;
19: }
20:
21: public EnumDomainProvider(String propertyName, Class type) {
22: super (propertyName);
23: this .type = type;
24: }
25:
26: @Override
27: protected List createValues() {
28: try {
29: return Arrays.asList(type.getEnumConstants());
30: } catch (Exception e) {
31: throw new RuntimeException("" + propertyName, e);
32: }
33: }
34:
35: @Override
36: protected String formatValue(Object object) {
37: FormatFactory formatFactory = FormatFactory.getInstance();
38: Locale locale = formatFactory.getLocale();
39: ResourceProvider resourceProvider = formatFactory
40: .getResourceProvider();
41: return resourceProvider.getMessage(locale, type.getName() + "."
42: + ((Enum) object).name());
43: }
44: }
|