001    package org.trails.component;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.apache.tapestry.form.IPropertySelectionModel;
007    import org.trails.exception.TrailsRuntimeException;
008    import ognl.Ognl;
009    
010    public abstract class AbstractPropertySelectionModel implements IPropertySelectionModel
011    {
012            protected List instances;
013            protected boolean allowNone;
014            protected String labelProperty = "toString()";
015            public static final String DEFAULT_NONE_LABEL = "None";
016            public static final String DEFAULT_NONE_VALUE = "none";
017            protected String noneLabel = DEFAULT_NONE_LABEL;
018    
019            public AbstractPropertySelectionModel(List instances)
020            {
021                    this(instances, false);
022            }
023    
024            public AbstractPropertySelectionModel(List instances, boolean allowNone)
025            {
026                    this.allowNone = allowNone;
027                    this.instances = new ArrayList();
028                    this.instances.addAll(instances);
029                    if (this.allowNone)
030                    {
031                            this.instances.add(0, null);
032                    }
033            }
034    
035            public String getNoneLabel()
036            {
037                    return noneLabel;
038            }
039    
040            public void setNoneLabel(String noneLabel)
041            {
042                    this.noneLabel = noneLabel;
043            }
044    
045            public String getLabelProperty()
046            {
047                    return labelProperty;
048            }
049    
050            public void setLabelProperty(String labelProperty)
051            {
052                    this.labelProperty = labelProperty;
053            }
054    
055            /**
056             * (non-Javadoc)
057             *
058             * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
059             */
060            public int getOptionCount()
061            {
062                    return instances.size();
063            }
064    
065            /**
066             * (non-Javadoc)
067             *
068             * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
069             */
070            public Object getOption(int index)
071            {
072                    return instances.get(index);
073            }
074    
075    
076            /**
077             * (non-Javadoc)
078             *
079             * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
080             */
081            public String getLabel(int index)
082            {
083                    if (allowNone && index == 0)
084                    {
085                            return getNoneLabel();
086                    }
087                    try
088                    {
089                            return Ognl.getValue(labelProperty, instances.get(index)).toString();
090                    } catch (Exception e)
091                    {
092                            throw new TrailsRuntimeException(e);
093                    }
094            }
095    
096            public boolean isDisabled(int i)
097            {
098                    return false;
099            }
100    }