    001    // Copyright 2007, 2008 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.tapestry5.util;
    016
    017    import java.io.Serializable;
    018    import java.util.List;
    019
    020    import org.apache.tapestry5.OptionGroupModel;
    021    import org.apache.tapestry5.OptionModel;
    022    import org.apache.tapestry5.internal.OptionModelImpl;
    023    import org.apache.tapestry5.internal.TapestryInternalUtils;
    024    import org.apache.tapestry5.ioc.Messages;
    025    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    026
    027    /**
    028     * A basic select model for a particular Enum type. The labels for each Enum are drawn from the Enum instance name and
    029     * the provides message catalog: <ul> <li>As key <em>ClassName</em>.<em>name</em> if present. The class name excludes
    030     * the package portion. Ex: "ElementType.LOCAL_VARIABLE" <li>As key <em>name</em> if present, i.e., "LOCAL_VARIABLE".
    031     * <li>As a user-presentable version of the name, i.e., "Local Variable". </ul>
    032     */
    033    public final class EnumSelectModel extends AbstractSelectModel implements Serializable
    034    {
    035        private static final long serialVersionUID = -3590412082766899684L;
    036
    037        private final List<OptionModel> options = CollectionFactory.newList();
    038
    039        public <T extends Enum> EnumSelectModel(Class<T> enumClass, Messages messages)
    040        {
    041            this(enumClass, messages, enumClass.getEnumConstants());
    042        }
    043
    044        public <T extends Enum> EnumSelectModel(Class<T> enumClass, Messages messages, T[] values)
    045        {
    046            assert enumClass != null;
    047            assert messages != null;
    048            String prefix = enumClass.getSimpleName();
    049
    050            for (T value : values)
    051            {
    052                String label = TapestryInternalUtils.getLabelForEnum(messages, prefix, value);
    053
    054                options.add(new OptionModelImpl(label, value));
    055            }
    056        }
    057
    058        /**
    059         * Returns null.
    060         */
    061        public List<OptionGroupModel> getOptionGroups()
    062        {
    063            return null;
    064        }
    065
    066        /**
    067         * Returns the option groupos created in the constructor.
    068         */
    069        public List<OptionModel> getOptions()
    070        {
    071            return options;
    072        }
    073
    074    }