001: package org.conform;
002:
003: import org.conform.format.AbstractFormat;
004: import org.conform.format.FormatComparator;
005: import org.apache.commons.logging.LogFactory;
006:
007: import java.util.*;
008:
009: /**
010: * derived classed need to implement:
011: *
012: * 1) createValues() -> return the list of values for the combo box,
013: * the values must be of the same type as the property
014: *
015: * 2) formatValue() -> convert a value to a text string which is diplayed
016: * in the combo box
017: * or:
018: * setValue2String -> set all format strings for all values during initialization
019: *
020: * wilken.openshop.core.util.AbstractDomainProvider
021: */
022: public abstract class AbstractDomainProvider<T> extends AbstractFormat
023: implements DomainProvider, Modifier {
024: private static org.apache.commons.logging.Log LOG = LogFactory
025: .getLog(DomainProvider.class);
026:
027: protected String propertyName;
028:
029: protected List<T> values;
030:
031: private Map<T, String> value2String;
032:
033: protected AbstractDomainProvider() {
034: }
035:
036: protected AbstractDomainProvider(String propertyName) {
037: this .propertyName = propertyName;
038: }
039:
040: /**
041: * @param value2String The value2String to set.
042: */
043: public void setValue2String(Map<T, String> value2String) {
044: this .value2String = value2String;
045: }
046:
047: /**
048: * @inheritDoc
049: */
050: public final void modify(BeanMeta beanMeta)
051: throws ModifierException {
052: if (propertyName == null)
053: throw new IllegalStateException(
054: "Provide the propertyName, if you use it as a modifier");
055:
056: PropertyMeta property = beanMeta.getProperty(propertyName);
057: property.setDomainProvider(this );
058: property.setFormat(this );
059: }
060:
061: protected abstract List<T> createValues();
062:
063: /**
064: * @inheritDoc
065: */
066: public final String format(Object value) {
067: return formatValue((T) value);
068: }
069:
070: /**
071: * this method can be overriden, or the format mapping must be initialized
072: *
073: * @param value
074: * @return
075: */
076: protected String formatValue(T value) {
077: if (this .value2String == null) {
078: LOG.warn("No string for value " + value);
079: return String.valueOf(value);
080: }
081: String text = this .value2String.get(value);
082: if (text == null) {
083: LOG.warn("No string for value " + value);
084: return String.valueOf(value);
085: }
086: return text;
087: }
088:
089: /**
090: * @inheritDoc
091: */
092: public final List<T> getDomain() {
093: if (values == null) {
094: values = createValues();
095: if (values == null)
096: values = new ArrayList<T>(0);
097:
098: if (isSorted())
099: Collections.sort(values, createComparator());
100: }
101: return values;
102: }
103:
104: /**
105: * create the comparator for sorting
106: *
107: * @return
108: */
109: protected Comparator<T> createComparator() {
110: return new FormatComparator<T>(this );
111: }
112:
113: /**
114: * override this method to have an unsorted list
115: *
116: * @return
117: */
118: protected boolean isSorted() {
119: return true;
120: }
121: }
|