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 java.util.ArrayList; |
|
17 | import java.util.List; |
|
18 | ||
19 | import org.apache.commons.beanutils.PropertyUtils; |
|
20 | import org.apache.hivemind.ApplicationRuntimeException; |
|
21 | import org.apache.hivemind.util.Defense; |
|
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 | 7 | { |
67 | 7 | Defense.notNull(values, "Value list can't be null."); |
68 | 7 | Defense.notNull(keyField, "Model keyField java beans expression can't be null."); |
69 | 7 | Defense.notNull(labelField, "Model labelField java beans expression can't be null."); |
70 | ||
71 | 7 | _values = values; |
72 | 7 | _keyExpression = keyField; |
73 | 7 | _labelExpression = labelField; |
74 | 7 | } |
75 | ||
76 | /** |
|
77 | * {@inheritDoc} |
|
78 | */ |
|
79 | public List getValues(String match) |
|
80 | { |
|
81 | 5 | List ret = new ArrayList(); |
82 | ||
83 | 5 | if (match == null) |
84 | 1 | return ret; |
85 | ||
86 | 4 | String filter = match.trim().toLowerCase(); |
87 | ||
88 | 16 | for (int i = 0; i < _values.size(); i++) { |
89 | ||
90 | 12 | Object value = _values.get(i); |
91 | 12 | String label = getLabelFor(value); |
92 | ||
93 | 12 | if (label.toLowerCase().indexOf(filter) > -1) |
94 | 10 | ret.add(value); |
95 | } |
|
96 | ||
97 | 4 | return ret; |
98 | } |
|
99 | ||
100 | /** |
|
101 | * {@inheritDoc} |
|
102 | */ |
|
103 | public String getLabelFor(Object value) |
|
104 | { |
|
105 | try { |
|
106 | ||
107 | 21 | return PropertyUtils.getProperty(value, _labelExpression).toString(); |
108 | ||
109 | 0 | } catch (Exception e) { |
110 | 0 | throw new ApplicationRuntimeException(e); |
111 | } |
|
112 | } |
|
113 | ||
114 | /** |
|
115 | * {@inheritDoc} |
|
116 | */ |
|
117 | public Object getPrimaryKey(Object value) |
|
118 | { |
|
119 | try { |
|
120 | ||
121 | 11 | return PropertyUtils.getProperty(value, _keyExpression); |
122 | ||
123 | 0 | } catch (Exception e) { |
124 | 0 | throw new ApplicationRuntimeException(e); |
125 | } |
|
126 | } |
|
127 | ||
128 | /** |
|
129 | * {@inheritDoc} |
|
130 | */ |
|
131 | public Object getValue(Object primaryKey) |
|
132 | { |
|
133 | 2 | for (int i = 0; i < _values.size(); i++) { |
134 | ||
135 | 2 | Object value = _values.get(i); |
136 | 2 | if (getPrimaryKey(value).toString().equals(primaryKey.toString())) |
137 | 2 | return value; |
138 | } |
|
139 | ||
140 | 0 | return null; |
141 | } |
|
142 | ||
143 | } |