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 /**
033 * Entry.
034 * @author Howard Lewis Ship
035 */
036 private static class Entry
037 {
038 String _label;
039
040 String _value;
041
042 Entry(String term)
043 {
044 Defense.notNull(term, "term");
045
046 int equalx = term.indexOf('=');
047
048 if (equalx < 0)
049 {
050 _label = term.trim();
051 _value = _label;
052 }
053 else
054 {
055 _label = term.substring(0, equalx).trim();
056 _value = term.substring(equalx + 1).trim();
057 }
058 }
059 }
060
061 private final List _entries;
062
063 public StringConvertedPropertySelectionModel(String[] terms)
064 {
065 Defense.notNull(terms, "terms");
066
067 _entries = new ArrayList(terms.length);
068
069 for (int i = 0; i < terms.length; i++)
070 {
071 _entries.add(new Entry(terms[i]));
072 }
073 }
074
075 public int getOptionCount()
076 {
077 return _entries.size();
078 }
079
080 private Entry getEntry(int index)
081 {
082 return (Entry) _entries.get(index);
083 }
084
085 public Object getOption(int index)
086 {
087 return getValue(index);
088 }
089
090 public String getLabel(int index)
091 {
092 return getEntry(index)._label;
093 }
094
095 public String getValue(int index)
096 {
097 return getEntry(index)._value;
098 }
099
100 public boolean isDisabled(int index)
101 {
102 return false;
103 }
104
105 public Object translateValue(String value)
106 {
107 // Values are the same on the client and the server, so no translation needed.
108 return value;
109 }
110
111 }