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
015package org.apache.tapestry5.util;
016
017import java.io.Serializable;
018import java.util.List;
019
020import org.apache.tapestry5.OptionGroupModel;
021import org.apache.tapestry5.OptionModel;
022import org.apache.tapestry5.internal.OptionModelImpl;
023import org.apache.tapestry5.internal.TapestryInternalUtils;
024import org.apache.tapestry5.ioc.Messages;
025import 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 provided 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 */
033public 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        if (!Enum.class.isAssignableFrom(enumClass))
049        {
050            throw new IllegalArgumentException("Cannot create " + this.getClass().getSimpleName()
051                + " from " + enumClass.getName() + ", not an enum class.");
052        }
053        String prefix = enumClass.getSimpleName();
054
055        for (T value : values)
056        {
057            String label = TapestryInternalUtils.getLabelForEnum(messages, prefix, value);
058
059            options.add(new OptionModelImpl(label, value));
060        }
061    }
062
063    /**
064     * Returns null.
065     */
066    public List<OptionGroupModel> getOptionGroups()
067    {
068        return null;
069    }
070
071    /**
072     * Returns the option groups created in the constructor.
073     */
074    public List<OptionModel> getOptions()
075    {
076        return options;
077    }
078
079}