Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
StringPropertySelectionModel |
|
| 1.5;1.5 |
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 | 2 | { |
40 | 2 | this._options = options; |
41 | 2 | } |
42 | ||
43 | /** |
|
44 | * Standard constructor. The options are retained (not copied). |
|
45 | */ |
|
46 | ||
47 | public StringPropertySelectionModel(String[] options, boolean[] disabled) |
|
48 | { |
|
49 | 1 | this(options); |
50 | 1 | _disabled = disabled; |
51 | 1 | } |
52 | ||
53 | public int getOptionCount() |
|
54 | { |
|
55 | 1 | return _options.length; |
56 | } |
|
57 | ||
58 | public Object getOption(int index) |
|
59 | { |
|
60 | 3 | return _options[index]; |
61 | } |
|
62 | ||
63 | /** |
|
64 | * Labels match options. |
|
65 | */ |
|
66 | ||
67 | public String getLabel(int index) |
|
68 | { |
|
69 | 3 | 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 | 3 | return Integer.toString(index); |
79 | } |
|
80 | ||
81 | public boolean isDisabled(int index) |
|
82 | { |
|
83 | 3 | return _disabled != null && _disabled[index]; |
84 | } |
|
85 | ||
86 | public Object translateValue(String value) |
|
87 | { |
|
88 | 2 | if (value == null) |
89 | 0 | return null; |
90 | ||
91 | int index; |
|
92 | ||
93 | 2 | index = Integer.parseInt(value); |
94 | ||
95 | 2 | if (index < 0 || index >= _options.length) |
96 | 0 | return null; |
97 | ||
98 | 2 | return _options[index]; |
99 | } |
|
100 | ||
101 | } |