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 package org.apache.tapestry.form;
015
016
017 /**
018 * Implementation of a property model that works off of native
019 * java enum types.
020 *
021 * <p>
022 * The enum label/values are all translated by calling Enum.toString() on your individual
023 * enum types, so it may be a good idea to provide a toString() method in your Enums if the
024 * types aren't what you would prefer to display.
025 * </p>
026 */
027 public class EnumPropertySelectionModel implements IPropertySelectionModel
028 {
029 private Enum[] _set;
030
031 public EnumPropertySelectionModel(Enum[] set)
032 {
033 _set = set;
034 }
035
036 /**
037 * {@inheritDoc}
038 */
039 public String getLabel(int index)
040 {
041 if (index == 0)
042 return "None";
043
044 return _set[index - 1].toString();
045 }
046
047 /**
048 * {@inheritDoc}
049 */
050 public Object getOption(int index)
051 {
052 if (index == 0)
053 return null;
054
055 return _set[index - 1];
056 }
057
058 /**
059 * {@inheritDoc}
060 */
061 public int getOptionCount()
062 {
063 return _set.length + 1;
064 }
065
066 /**
067 * {@inheritDoc}
068 */
069 public String getValue(int index)
070 {
071 if (index == 0)
072 return "None";
073
074 return _set[index - 1].toString();
075 }
076
077 public boolean isDisabled(int index)
078 {
079 return false;
080 }
081
082 /**
083 * {@inheritDoc}
084 */
085 public Object translateValue(String value)
086 {
087 for (Enum e : _set) {
088 if (e.toString().equals(value))
089 return e;
090 }
091
092 return null;
093 }
094 }