| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| EnumPropertySelectionModel |
|
| 0.0;0 |
| 1 | package org.trails.component; |
|
| 2 | ||
| 3 | import java.util.Arrays; |
|
| 4 | import java.util.List; |
|
| 5 | ||
| 6 | import org.trails.exception.TrailsRuntimeException; |
|
| 7 | ||
| 8 | /** |
|
| 9 | * Implementation of a property model that works off of native |
|
| 10 | * java enum types. |
|
| 11 | ||
| 12 | * <p> |
|
| 13 | * The main diference with Tapestry's EnumPropertySelectionModel is that "allowNone" and the property used to get the |
|
| 14 | * label values (labelProperty) are both configurable. |
|
| 15 | * </p> |
|
| 16 | ||
| 17 | * <p> |
|
| 18 | * The enum label/values are all translated by calling the method indicated by "labelProperty" |
|
| 19 | * By default labelProperty = "toString()", so it may be a good idea to provide a toString() method in your Enums if the |
|
| 20 | * types aren't what you would prefer to display. |
|
| 21 | * </p> |
|
| 22 | */ |
|
| 23 | public class EnumPropertySelectionModel extends AbstractPropertySelectionModel |
|
| 24 | { |
|
| 25 | private Class type; |
|
| 26 | ||
| 27 | public EnumPropertySelectionModel(Class type) |
|
| 28 | { |
|
| 29 | 32 | this(type, false); |
| 30 | 32 | } |
| 31 | ||
| 32 | public EnumPropertySelectionModel(Enum[] set) |
|
| 33 | { |
|
| 34 | 0 | this(Arrays.asList(set), false); |
| 35 | 0 | } |
| 36 | ||
| 37 | public EnumPropertySelectionModel(Enum[] set, boolean allowNone) |
|
| 38 | { |
|
| 39 | 0 | this(Arrays.asList(set), allowNone); |
| 40 | 0 | } |
| 41 | ||
| 42 | public EnumPropertySelectionModel(List instances, boolean allowNone) |
|
| 43 | { |
|
| 44 | 4 | super(instances, allowNone); |
| 45 | 4 | } |
| 46 | ||
| 47 | public EnumPropertySelectionModel(Class type, boolean allowNone) |
|
| 48 | { |
|
| 49 | 84 | super(Arrays.asList(type.getEnumConstants()), allowNone); |
| 50 | 84 | this.type = type; |
| 51 | 84 | } |
| 52 | ||
| 53 | public EnumPropertySelectionModel(Class type, String labelProperty, boolean allowNone) |
|
| 54 | { |
|
| 55 | 0 | this(type, allowNone); |
| 56 | 0 | setLabelProperty(labelProperty); |
| 57 | 0 | } |
| 58 | ||
| 59 | /* |
|
| 60 | * (non-Javadoc) |
|
| 61 | * |
|
| 62 | * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) |
|
| 63 | */ |
|
| 64 | public String getValue(int index) |
|
| 65 | { |
|
| 66 | try |
|
| 67 | { |
|
| 68 | 12 | if (allowNone && index == 0) |
| 69 | { |
|
| 70 | 4 | return DEFAULT_NONE_VALUE; |
| 71 | } else |
|
| 72 | { |
|
| 73 | 8 | return instances.get(index).toString(); |
| 74 | } |
|
| 75 | 0 | } catch (Exception e) |
| 76 | { |
|
| 77 | 0 | throw new TrailsRuntimeException(e, type); |
| 78 | } |
|
| 79 | } |
|
| 80 | ||
| 81 | /* |
|
| 82 | * (non-Javadoc) |
|
| 83 | * |
|
| 84 | * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String) |
|
| 85 | */ |
|
| 86 | public Object translateValue(String value) |
|
| 87 | { |
|
| 88 | 16 | if (allowNone) |
| 89 | { |
|
| 90 | 8 | if (value.equals(DEFAULT_NONE_VALUE)) |
| 91 | 4 | return null; |
| 92 | } |
|
| 93 | 32 | for (Object enumElement : instances) |
| 94 | { |
|
| 95 | 20 | if (enumElement != null && enumElement.toString().equals(value)) |
| 96 | { |
|
| 97 | 12 | return enumElement; |
| 98 | } |
|
| 99 | } |
|
| 100 | 0 | return null; |
| 101 | } |
|
| 102 | } |