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