Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultAutocompleteModel |
|
| 3.6;3.6 |
1 | // Copyright Jul 30, 2006 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.dojo.form; | |
15 | ||
16 | import org.apache.commons.beanutils.PropertyUtils; | |
17 | import org.apache.hivemind.ApplicationRuntimeException; | |
18 | import org.apache.hivemind.util.Defense; | |
19 | ||
20 | import java.util.ArrayList; | |
21 | import java.util.List; | |
22 | ||
23 | ||
24 | /** | |
25 | * Default simple implementation of {@link IAutocompleteModel}. This class relies | |
26 | * on the java beans specification to resolve key fields of an incoming | |
27 | * {@link List}. | |
28 | * | |
29 | * <p> | |
30 | * If you had an object type of <code>User</code>, with the primary/unique id of | |
31 | * each <code>User</code> object stored as a member with a name of <code>id</code> | |
32 | * you would pass something like this into the model(don't forget that javabeans syntax | |
33 | * requires a corresponding getId() for members): | |
34 | * </p> | |
35 | * | |
36 | * <pre> | |
37 | * IAutocompleteModel model = new DefaultAutocompleteModel(List users, "id", "name"); | |
38 | * </pre> | |
39 | * | |
40 | * @see "http://jakarta.apache.org/commons/beanutils/commons-beanutils-1.6.1/docs/api/org/apache/commons/beanutils/PropertyUtils.html" | |
41 | * @author jkuhnert | |
42 | */ | |
43 | public class DefaultAutocompleteModel implements IAutocompleteModel | |
44 | { | |
45 | ||
46 | private List _values; | |
47 | ||
48 | private String _keyExpression; | |
49 | ||
50 | private String _labelExpression; | |
51 | ||
52 | /** | |
53 | * Create a new model using java beans syntax to access the key/label | |
54 | * for the list using the specified bean expressions. | |
55 | * | |
56 | * @param values | |
57 | * The list of values to manage. | |
58 | * @param keyField | |
59 | * The java beans expression for getting the primary key of each object | |
60 | * in the list. {@link #getPrimaryKey(Object)}. | |
61 | * @param labelField | |
62 | * The java beans expression for getting the label of each object | |
63 | * in the list. {@link #getLabelFor(Object)}. | |
64 | */ | |
65 | public DefaultAutocompleteModel(List values, String keyField, String labelField) | |
66 | 0 | { |
67 | 0 | Defense.notNull(values, "Value list can't be null."); |
68 | 0 | Defense.notNull(keyField, "Model keyField java beans expression can't be null."); |
69 | 0 | Defense.notNull(labelField, "Model labelField java beans expression can't be null."); |
70 | ||
71 | 0 | _values = values; |
72 | 0 | _keyExpression = keyField; |
73 | 0 | _labelExpression = labelField; |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * {@inheritDoc} | |
78 | */ | |
79 | public List getValues(String match) | |
80 | { | |
81 | 0 | List ret = new ArrayList(); |
82 | ||
83 | 0 | if (match == null) |
84 | 0 | return ret; |
85 | ||
86 | 0 | String filter = match.trim().toLowerCase(); |
87 | ||
88 | 0 | for (int i = 0; i < _values.size(); i++) |
89 | { | |
90 | 0 | Object value = _values.get(i); |
91 | 0 | String label = getLabelFor(value); |
92 | ||
93 | 0 | if (label.toLowerCase().indexOf(filter) > -1) |
94 | 0 | ret.add(value); |
95 | } | |
96 | ||
97 | 0 | return ret; |
98 | } | |
99 | ||
100 | /** | |
101 | * {@inheritDoc} | |
102 | */ | |
103 | public String getLabelFor(Object value) | |
104 | { | |
105 | try | |
106 | { | |
107 | 0 | return PropertyUtils.getProperty(value, _labelExpression).toString(); |
108 | 0 | } catch (Exception e) |
109 | { | |
110 | 0 | throw new ApplicationRuntimeException(e); |
111 | } | |
112 | } | |
113 | ||
114 | /** | |
115 | * {@inheritDoc} | |
116 | */ | |
117 | public Object getPrimaryKey(Object value) | |
118 | { | |
119 | try | |
120 | { | |
121 | 0 | return PropertyUtils.getProperty(value, _keyExpression); |
122 | 0 | } catch (Exception e) |
123 | { | |
124 | 0 | throw new ApplicationRuntimeException(e); |
125 | } | |
126 | } | |
127 | ||
128 | /** | |
129 | * {@inheritDoc} | |
130 | */ | |
131 | public Object getValue(Object primaryKey) | |
132 | { | |
133 | 0 | for (int i = 0; i < _values.size(); i++) |
134 | { | |
135 | 0 | Object value = _values.get(i); |
136 | ||
137 | 0 | if (getPrimaryKey(value).toString().equals(primaryKey.toString())) |
138 | 0 | return value; |
139 | } | |
140 | ||
141 | 0 | return null; |
142 | } | |
143 | ||
144 | } |