Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringPropertySelectionModel |
|
| 1.75;1.75 |
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 | /** | |
18 | * Implementation of {@link IPropertySelectionModel} that allows one String from | |
19 | * an array of Strings to be selected as the property. | |
20 | * <p> | |
21 | * Uses a simple index number as the value (used to represent the selected | |
22 | * String). This assumes that the possible values for the Strings will remain | |
23 | * constant between request cycles. | |
24 | * | |
25 | * @author Howard Lewis Ship | |
26 | */ | |
27 | ||
28 | public class StringPropertySelectionModel implements IPropertySelectionModel | |
29 | { | |
30 | ||
31 | private String[] _options; | |
32 | ||
33 | private boolean[] _disabled; | |
34 | ||
35 | /** | |
36 | * Standard constructor. The options are retained (not copied). | |
37 | */ | |
38 | public StringPropertySelectionModel(String[] options) | |
39 | 0 | { |
40 | 0 | this._options = options; |
41 | 0 | } |
42 | ||
43 | /** | |
44 | * Standard constructor. The options are retained (not copied). | |
45 | */ | |
46 | ||
47 | public StringPropertySelectionModel(String[] options, boolean[] disabled) | |
48 | { | |
49 | 0 | this(options); |
50 | 0 | _disabled = disabled; |
51 | 0 | } |
52 | ||
53 | public int getOptionCount() | |
54 | { | |
55 | 0 | return _options.length; |
56 | } | |
57 | ||
58 | public Object getOption(int index) | |
59 | { | |
60 | 0 | return _options[index]; |
61 | } | |
62 | ||
63 | /** | |
64 | * Labels match options. | |
65 | */ | |
66 | ||
67 | public String getLabel(int index) | |
68 | { | |
69 | 0 | return _options[index]; |
70 | } | |
71 | ||
72 | /** | |
73 | * Values are indexes into the array of options. | |
74 | */ | |
75 | ||
76 | public String getValue(int index) | |
77 | { | |
78 | 0 | return Integer.toString(index); |
79 | } | |
80 | ||
81 | public boolean isDisabled(int index) | |
82 | { | |
83 | 0 | return _disabled != null && _disabled[index]; |
84 | } | |
85 | ||
86 | public Object translateValue(String value) | |
87 | { | |
88 | 0 | if (value == null) |
89 | 0 | return null; |
90 | ||
91 | int index; | |
92 | ||
93 | 0 | index = Integer.parseInt(value); |
94 | ||
95 | 0 | if (index < 0 || index >= _options.length) |
96 | 0 | return null; |
97 | ||
98 | 0 | return _options[index]; |
99 | } | |
100 | ||
101 | } |