View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.keyvalues;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.WordUtils;
20  import org.kuali.rice.core.api.util.ConcreteKeyValue;
21  import org.kuali.rice.core.api.util.KeyValue;
22  import org.kuali.rice.kew.api.util.CodeTranslator;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * ValuesFinder that derives values directly from a Java enum.
29   * KeyValues are provided in enum definition order, enum name
30   * is the key, capitalized lowercase enum name is the label.
31   */
32  public class EnumValuesFinder extends KeyValuesBase {
33      private Class<? extends Enum> enumeration;
34  
35      public EnumValuesFinder(Class<? extends Enum> enumeration) {
36          this.enumeration = enumeration;
37      }
38  
39      @Override
40      public List<KeyValue> getKeyValues() {
41          List<KeyValue> labels = new ArrayList<KeyValue>();
42          for (Enum enumval: enumeration.getEnumConstants()) {
43              labels.add(new ConcreteKeyValue(getEnumKey(enumval), getEnumLabel(enumval)));
44          }
45          return labels;
46      }
47  
48      /**
49       * Derives a key value from an enum
50       */
51      protected String getEnumKey(Enum enm) {
52          return enm.name();
53      }
54  
55      /**
56       * Derives a label value from an enum
57       */
58      protected String getEnumLabel(Enum enm) {
59          return WordUtils.capitalize(enm.name().toLowerCase());
60      }
61  }