Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EnumPropertySelectionModel |
|
| 2.2857142857142856;2.286 |
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 | package org.apache.tapestry.form; | |
15 | ||
16 | ||
17 | /** | |
18 | * Implementation of a property model that works off of native | |
19 | * java enum types. | |
20 | * | |
21 | * <p> | |
22 | * The enum label/values are all translated by calling Enum.toString() on your individual | |
23 | * enum types, so it may be a good idea to provide a toString() method in your Enums if the | |
24 | * types aren't what you would prefer to display. | |
25 | * </p> | |
26 | */ | |
27 | public class EnumPropertySelectionModel implements IPropertySelectionModel | |
28 | { | |
29 | private Enum[] _set; | |
30 | ||
31 | public EnumPropertySelectionModel(Enum[] set) | |
32 | 0 | { |
33 | 0 | _set = set; |
34 | 0 | } |
35 | ||
36 | /** | |
37 | * {@inheritDoc} | |
38 | */ | |
39 | public String getLabel(int index) | |
40 | { | |
41 | 0 | if (index == 0) |
42 | 0 | return "None"; |
43 | ||
44 | 0 | return _set[index - 1].toString(); |
45 | } | |
46 | ||
47 | /** | |
48 | * {@inheritDoc} | |
49 | */ | |
50 | public Object getOption(int index) | |
51 | { | |
52 | 0 | if (index == 0) |
53 | 0 | return null; |
54 | ||
55 | 0 | return _set[index - 1]; |
56 | } | |
57 | ||
58 | /** | |
59 | * {@inheritDoc} | |
60 | */ | |
61 | public int getOptionCount() | |
62 | { | |
63 | 0 | return _set.length + 1; |
64 | } | |
65 | ||
66 | /** | |
67 | * {@inheritDoc} | |
68 | */ | |
69 | public String getValue(int index) | |
70 | { | |
71 | 0 | if (index == 0) |
72 | 0 | return "None"; |
73 | ||
74 | 0 | return _set[index - 1].toString(); |
75 | } | |
76 | ||
77 | public boolean isDisabled(int index) | |
78 | { | |
79 | 0 | return false; |
80 | } | |
81 | ||
82 | /** | |
83 | * {@inheritDoc} | |
84 | */ | |
85 | public Object translateValue(String value) | |
86 | { | |
87 | 0 | for (Enum e : _set) { |
88 | 0 | if (e.toString().equals(value)) |
89 | 0 | return e; |
90 | } | |
91 | ||
92 | 0 | return null; |
93 | } | |
94 | } |