001    // Copyright 2004, 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.form;
016    
017    /**
018     *  Implementation of {@link IPropertySelectionModel} that allows one String from
019     *  an array of Strings to be selected as the property.
020     *
021     *  <p>Uses a simple index number as the value (used to represent the selected String).
022     *  This assumes that the possible values for the Strings will remain constant between
023     *  request cycles.
024     *
025     *  @author Howard Lewis Ship
026     * 
027     **/
028    
029    public class StringPropertySelectionModel implements IPropertySelectionModel
030    {
031        private String[] options;
032    
033        /**
034         * Standard constructor.
035         *
036         * The options are retained (not copied).
037         **/
038    
039        public StringPropertySelectionModel(String[] options)
040        {
041            this.options = options;
042        }
043    
044        public int getOptionCount()
045        {
046            return options.length;
047        }
048    
049        public Object getOption(int index)
050        {
051            return options[index];
052        }
053    
054        /**
055         *  Labels match options.
056         *
057         **/
058    
059        public String getLabel(int index)
060        {
061            return options[index];
062        }
063    
064        /**
065         *  Values are indexes into the array of options.
066         *
067         **/
068    
069        public String getValue(int index)
070        {
071            return Integer.toString(index);
072        }
073    
074        public Object translateValue(String value)
075        {
076            int index;
077    
078            index = Integer.parseInt(value);
079    
080            return options[index];
081        }
082    
083    }