Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 140   Methods: 8
NCLOC: 57   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
EnumPropertySelectionModel.java 100% 100% 100% 100%
coverage
 1   
 // Copyright 2004, 2005 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.tapestry.form;
 16   
 
 17   
 import java.util.ResourceBundle;
 18   
 
 19   
 import org.apache.commons.lang.enum.Enum;
 20   
 
 21   
 /**
 22   
  *  Implementation of {@link IPropertySelectionModel} that wraps around
 23   
  *  a set of {@link Enum}s.
 24   
  * 
 25   
  *  <p>Uses a simple index number as the value (used to represent the option).
 26   
  *
 27   
  *  <p>The resource bundle from which labels are extracted is usually
 28   
  *  a resource within the Tapestry application.  Since 
 29   
  *  {@link ResourceBundle#getBundle(String, java.util.Locale)} uses its caller's class loader,
 30   
  *  and that classloader will be the Tapestry framework's classloader, the application's
 31   
  *  resources won't be visible.  This requires that the application resolve
 32   
  *  the resource to a {@link ResourceBundle} before creating this model.
 33   
  *  
 34   
  *  @author Howard Lewis Ship
 35   
  * 
 36   
  **/
 37   
 
 38   
 public class EnumPropertySelectionModel implements IPropertySelectionModel
 39   
 {
 40   
     private Enum[] _options;
 41   
     private String[] _labels;
 42   
 
 43   
     private String _resourcePrefix;
 44   
     private ResourceBundle _bundle;
 45   
 
 46   
     /**
 47   
      *  Standard constructor.
 48   
      *
 49   
      *  <p>Labels for the options are extracted from a resource bundle.  resourceBaseName
 50   
      *  identifies the bundle.  Typically, the bundle will be a <code>.properties</code>
 51   
      *  file within the classpath.  Specify the fully qualified class name equivalent, i.e.,
 52   
      *  for file <code>/com/example/foo/LabelStrings.properties</code> use
 53   
      *  <code>com.example.foo.LabelStrings</code> as the resource base name.
 54   
      *
 55   
      *  <p>Normally (when resourcePrefix is null), the keys used to extract labels
 56   
      *  matches the {@link Enum#getName() enumeration id} of the option.  By
 57   
      *  convention, the enumeration id matches the name of the static variable.
 58   
      *
 59   
      *  <p>To avoid naming conflicts when using a single resource bundle for multiple
 60   
      *  models, use a resource prefix.  This is a string which is prepended to
 61   
      *  the enumeration id (they prefix and enumeration id are seperated with a period).
 62   
      *
 63   
      *  @param   options The list of possible values for this model, in the order they
 64   
      *  should appear. This exact array is retained (not copied).
 65   
      *
 66   
      *  @param   bundle The {@link ResourceBundle} from which labels may be extracted.
 67   
      *
 68   
      *  @param   resourcePrefix An optional prefix used when accessing keys within the bundle. 
 69   
      *  Used to allow a single ResouceBundle to contain labels for multiple Enums.
 70   
      **/
 71   
 
 72  11
     public EnumPropertySelectionModel(Enum[] options, ResourceBundle bundle, String resourcePrefix)
 73   
     {
 74  11
         _options = options;
 75  11
         _bundle = bundle;
 76  11
         _resourcePrefix = resourcePrefix;
 77   
     }
 78   
 
 79   
     /**
 80   
      *  Simplified constructor using no prefix.
 81   
      *
 82   
      **/
 83   
 
 84  2
     public EnumPropertySelectionModel(Enum[] options, ResourceBundle bundle)
 85   
     {
 86  2
         this(options, bundle, null);
 87   
     }
 88   
 
 89  9
     public int getOptionCount()
 90   
     {
 91  9
         return _options.length;
 92   
     }
 93   
 
 94  39
     public Object getOption(int index)
 95   
     {
 96  39
         return _options[index];
 97   
     }
 98   
 
 99  39
     public String getLabel(int index)
 100   
     {
 101  39
         if (_labels == null)
 102  6
             readLabels();
 103   
 
 104  39
         return _labels[index];
 105   
     }
 106   
 
 107  39
     public String getValue(int index)
 108   
     {
 109  39
         return Integer.toString(index);
 110   
     }
 111   
 
 112  10
     public Object translateValue(String value)
 113   
     {
 114  10
         int index;
 115   
 
 116  10
         index = Integer.parseInt(value);
 117   
 
 118  10
         return _options[index];
 119   
     }
 120   
 
 121  6
     private void readLabels()
 122   
     {
 123  6
         _labels = new String[_options.length];
 124   
 
 125  6
         for (int i = 0; i < _options.length; i++)
 126   
         {
 127  20
             String enumerationId = _options[i].getName();
 128   
 
 129  20
             String key;
 130   
 
 131  20
             if (_resourcePrefix == null)
 132  12
                 key = enumerationId;
 133   
             else
 134  8
                 key = _resourcePrefix + "." + enumerationId;
 135   
 
 136  20
             _labels[i] = _bundle.getString(key);
 137   
         }
 138   
 
 139   
     }
 140   
 }