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 private boolean[] _disabled;
034
035 /**
036 * Standard constructor. The options are retained (not copied).
037 */
038 public StringPropertySelectionModel(String[] options)
039 {
040 this._options = options;
041 }
042
043 /**
044 * Standard constructor. The options are retained (not copied).
045 */
046
047 public StringPropertySelectionModel(String[] options, boolean[] disabled)
048 {
049 this(options);
050 _disabled = disabled;
051 }
052
053 public int getOptionCount()
054 {
055 return _options.length;
056 }
057
058 public Object getOption(int index)
059 {
060 return _options[index];
061 }
062
063 /**
064 * Labels match options.
065 */
066
067 public String getLabel(int index)
068 {
069 return _options[index];
070 }
071
072 /**
073 * Values are indexes into the array of options.
074 */
075
076 public String getValue(int index)
077 {
078 return Integer.toString(index);
079 }
080
081 public boolean isDisabled(int index)
082 {
083 return _disabled != null && _disabled[index];
084 }
085
086 public Object translateValue(String value)
087 {
088 if (value == null)
089 return null;
090
091 int index;
092
093 index = Integer.parseInt(value);
094
095 if (index < 0 || index >= _options.length)
096 return null;
097
098 return _options[index];
099 }
100
101 }