001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.coerce;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    import org.apache.hivemind.util.Defense;
021    import org.apache.tapestry.form.IPropertySelectionModel;
022    
023    /**
024     * {@link org.apache.tapestry.form.IPropertySelectionModel} created from a comma-seperated string by
025     * {@link org.apache.tapestry.coerce.StringToPropertySelectionModelConverter}.
026     * 
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    public final class StringConvertedPropertySelectionModel implements IPropertySelectionModel
031    {
032        private static class Entry
033        {
034            String _label;
035    
036            String _value;
037    
038            Entry(String term)
039            {
040                Defense.notNull(term, "term");
041    
042                int equalx = term.indexOf('=');
043    
044                if (equalx < 0)
045                {
046                    _label = term.trim();
047                    _value = _label;
048                }
049                else
050                {
051                    _label = term.substring(0, equalx).trim();
052                    _value = term.substring(equalx + 1).trim();
053                }
054            }
055        }
056    
057        private final List _entries;
058    
059        public StringConvertedPropertySelectionModel(String[] terms)
060        {
061            Defense.notNull(terms, "terms");
062    
063            _entries = new ArrayList(terms.length);
064    
065            for (int i = 0; i < terms.length; i++)
066            {
067                _entries.add(new Entry(terms[i]));
068            }
069        }
070    
071        public int getOptionCount()
072        {
073            return _entries.size();
074        }
075    
076        private Entry getEntry(int index)
077        {
078            return (Entry) _entries.get(index);
079        }
080    
081        public Object getOption(int index)
082        {
083            return getValue(index);
084        }
085    
086        public String getLabel(int index)
087        {
088            // TODO Auto-generated method stub
089            return getEntry(index)._label;
090        }
091    
092        public String getValue(int index)
093        {
094            return getEntry(index)._value;
095        }
096    
097        public Object translateValue(String value)
098        {
099            // Values are the same on the client and the server, so no translation needed.
100            return value;
101        }
102    
103    }