Coverage Report - org.apache.tapestry5.util.EnumSelectModel
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumSelectModel
100%
13/13
100%
2/2
0
 
 1  
 // Copyright 2007, 2008 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.util;
 16  
 
 17  
 import org.apache.tapestry5.OptionGroupModel;
 18  
 import org.apache.tapestry5.OptionModel;
 19  
 import org.apache.tapestry5.internal.OptionModelImpl;
 20  
 import org.apache.tapestry5.internal.TapestryInternalUtils;
 21  
 import org.apache.tapestry5.ioc.Messages;
 22  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 23  
 import static org.apache.tapestry5.ioc.internal.util.Defense.notNull;
 24  
 
 25  
 import java.io.Serializable;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * A basic select model for a particular Enum type. The labels for each Enum are drawn from the Enum instance name and
 30  
  * the provides message catalog: <ul> <li>As key <em>ClassName</em>.<em>name</em> if present. The class name excludes
 31  
  * the package portion. Ex: "ElementType.LOCAL_VARIABLE" <li>As key <em>name</em> if present, i.e., "LOCAL_VARIABLE".
 32  
  * <li>As a user-presentable version of the name, i.e., "Local Variable". </ul>
 33  
  */
 34  
 public final class EnumSelectModel extends AbstractSelectModel implements Serializable
 35  
 {
 36  
     private static final long serialVersionUID = -3590412082766899684L;
 37  
 
 38  56
     private final List<OptionModel> options = CollectionFactory.newList();
 39  
 
 40  
     public <T extends Enum> EnumSelectModel(Class<T> enumClass, Messages messages)
 41  
     {
 42  56
         this(enumClass, messages, enumClass.getEnumConstants());
 43  56
     }
 44  
 
 45  
     public <T extends Enum> EnumSelectModel(Class<T> enumClass, Messages messages, T[] values)
 46  56
     {
 47  56
         notNull(enumClass, "enumClass");
 48  56
         notNull(messages, "messages");
 49  
 
 50  56
         String prefix = enumClass.getSimpleName();
 51  
 
 52  421
         for (T value : values)
 53  
         {
 54  365
             String label = TapestryInternalUtils.getLabelForEnum(messages, prefix, value);
 55  
 
 56  365
             options.add(new OptionModelImpl(label, value));
 57  
         }
 58  56
     }
 59  
 
 60  
     /**
 61  
      * Returns null.
 62  
      */
 63  
     public List<OptionGroupModel> getOptionGroups()
 64  
     {
 65  132
         return null;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Returns the option groupos created in the constructor.
 70  
      */
 71  
     public List<OptionModel> getOptions()
 72  
     {
 73  138
         return options;
 74  
     }
 75  
 
 76  
 }