Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
SimpleTableColumn |
|
| 1.8571428571428572;1.857 |
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.contrib.table.model.simple; |
|
16 | ||
17 | import org.apache.tapestry.IComponent; |
|
18 | import org.apache.tapestry.contrib.table.model.ITableRendererSource; |
|
19 | import org.apache.tapestry.contrib.table.model.common.AbstractTableColumn; |
|
20 | ||
21 | import java.io.Serializable; |
|
22 | import java.util.Comparator; |
|
23 | ||
24 | /** |
|
25 | * A simple minimal implementation of the |
|
26 | * {@link org.apache.tapestry.contrib.table.model.ITableColumn}interface that |
|
27 | * provides all the basic services for displaying a column. |
|
28 | * |
|
29 | * @author mindbridge |
|
30 | */ |
|
31 | public class SimpleTableColumn extends AbstractTableColumn |
|
32 | { |
|
33 | // TODO: Unify SimpleTableColumnRendererSource and |
|
34 | // SimpleTableColumnFormRendererSource |
|
35 | // and implement the configuration with HiveMind |
|
36 | ||
37 | 0 | public static final ITableRendererSource DEFAULT_COLUMN_RENDERER_SOURCE = new SimpleTableColumnRendererSource(); |
38 | ||
39 | 0 | public static final ITableRendererSource FORM_COLUMN_RENDERER_SOURCE = new SimpleTableColumnFormRendererSource(); |
40 | ||
41 | 0 | public static final ITableRendererSource DEFAULT_VALUE_RENDERER_SOURCE = new SimpleTableValueRendererSource(); |
42 | ||
43 | private static final long serialVersionUID = 1L; |
|
44 | ||
45 | private String m_strDisplayName; |
|
46 | ||
47 | private ITableColumnEvaluator m_objEvaluator; |
|
48 | ||
49 | /** |
|
50 | * Creates a SimpleTableColumn. |
|
51 | * |
|
52 | * @param strColumnName |
|
53 | * the identifying name and display name of the column |
|
54 | */ |
|
55 | public SimpleTableColumn(String strColumnName) |
|
56 | { |
|
57 | 0 | this(strColumnName, strColumnName); |
58 | 0 | } |
59 | ||
60 | /** |
|
61 | * Creates a SimpleTableColumn. |
|
62 | * |
|
63 | * @param strColumnName |
|
64 | * the identifying name and display name of the column |
|
65 | * @param bSortable |
|
66 | * whether the column is sortable |
|
67 | */ |
|
68 | public SimpleTableColumn(String strColumnName, boolean bSortable) |
|
69 | { |
|
70 | 0 | this(strColumnName, strColumnName, bSortable); |
71 | 0 | } |
72 | ||
73 | /** |
|
74 | * Creates a SimpleTableColumn. |
|
75 | * |
|
76 | * @param strColumnName |
|
77 | * the identifying name and display name of the column |
|
78 | * @param bSortable |
|
79 | * whether the column is sortable |
|
80 | * @param objEvaluator |
|
81 | * the evaluator to extract the column value from the row |
|
82 | */ |
|
83 | public SimpleTableColumn(String strColumnName, |
|
84 | ITableColumnEvaluator objEvaluator, boolean bSortable) |
|
85 | { |
|
86 | 0 | this(strColumnName, strColumnName, objEvaluator, bSortable); |
87 | 0 | } |
88 | ||
89 | /** |
|
90 | * Creates a SimpleTableColumn. |
|
91 | * |
|
92 | * @param strColumnName |
|
93 | * the identifying name of the column |
|
94 | * @param strDisplayName |
|
95 | * the display name of the column |
|
96 | */ |
|
97 | public SimpleTableColumn(String strColumnName, String strDisplayName) |
|
98 | { |
|
99 | 0 | this(strColumnName, strDisplayName, false); |
100 | 0 | } |
101 | ||
102 | /** |
|
103 | * Creates a SimpleTableColumn. |
|
104 | * |
|
105 | * @param strColumnName |
|
106 | * the identifying name of the column |
|
107 | * @param strDisplayName |
|
108 | * the display name of the column |
|
109 | * @param bSortable |
|
110 | * whether the column is sortable |
|
111 | */ |
|
112 | public SimpleTableColumn(String strColumnName, String strDisplayName, |
|
113 | boolean bSortable) |
|
114 | { |
|
115 | 0 | this(strColumnName, strDisplayName, null, bSortable); |
116 | 0 | } |
117 | ||
118 | /** |
|
119 | * Creates a SimpleTableColumn. |
|
120 | * |
|
121 | * @param strColumnName |
|
122 | * the identifying name of the column |
|
123 | * @param strDisplayName |
|
124 | * the display name of the column |
|
125 | * @param bSortable |
|
126 | * whether the column is sortable |
|
127 | * @param objEvaluator |
|
128 | * the evaluator to extract the column value from the row |
|
129 | */ |
|
130 | public SimpleTableColumn(String strColumnName, String strDisplayName, |
|
131 | ITableColumnEvaluator objEvaluator, boolean bSortable) |
|
132 | { |
|
133 | 0 | super(strColumnName, bSortable, null); |
134 | 0 | setComparator(new DefaultTableComparator()); |
135 | 0 | setDisplayName(strDisplayName); |
136 | 0 | setColumnRendererSource(DEFAULT_COLUMN_RENDERER_SOURCE); |
137 | 0 | setValueRendererSource(DEFAULT_VALUE_RENDERER_SOURCE); |
138 | 0 | setEvaluator(objEvaluator); |
139 | 0 | } |
140 | ||
141 | /** |
|
142 | * Returns the display name of the column that will be used in the table |
|
143 | * header. Override for internationalization. |
|
144 | * |
|
145 | * @return String the display name of the column |
|
146 | */ |
|
147 | public String getDisplayName() |
|
148 | { |
|
149 | 0 | return m_strDisplayName; |
150 | } |
|
151 | ||
152 | /** |
|
153 | * Sets the displayName. |
|
154 | * |
|
155 | * @param displayName |
|
156 | * The displayName to set |
|
157 | */ |
|
158 | public void setDisplayName(String displayName) |
|
159 | { |
|
160 | 0 | if (displayName != null) |
161 | 0 | displayName = displayName.replace("_", "."); |
162 | ||
163 | 0 | m_strDisplayName = displayName; |
164 | 0 | } |
165 | ||
166 | /** |
|
167 | * Returns the evaluator. |
|
168 | * |
|
169 | * @return ITableColumnEvaluator |
|
170 | */ |
|
171 | public ITableColumnEvaluator getEvaluator() |
|
172 | { |
|
173 | 0 | return m_objEvaluator; |
174 | } |
|
175 | ||
176 | /** |
|
177 | * Sets the evaluator. |
|
178 | * |
|
179 | * @param evaluator |
|
180 | * The evaluator to set |
|
181 | */ |
|
182 | public void setEvaluator(ITableColumnEvaluator evaluator) |
|
183 | { |
|
184 | 0 | m_objEvaluator = evaluator; |
185 | 0 | } |
186 | ||
187 | /** |
|
188 | * Sets a comparator that compares the values of this column rather than the |
|
189 | * objects representing the full rows. <br> |
|
190 | * This method allows easier use of standard comparators for sorting the |
|
191 | * column. It simply wraps the provided comparator with a row-to-column |
|
192 | * convertor and invokes the setComparator() method. |
|
193 | * |
|
194 | * @param comparator |
|
195 | * The column value comparator |
|
196 | */ |
|
197 | public void setColumnComparator(Comparator comparator) |
|
198 | { |
|
199 | 0 | setComparator(new ColumnComparator(this, comparator)); |
200 | 0 | } |
201 | ||
202 | /** |
|
203 | * Extracts the value of the column from the row object. |
|
204 | * |
|
205 | * @param objRow |
|
206 | * the row object |
|
207 | * @return Object the column value |
|
208 | */ |
|
209 | public Object getColumnValue(Object objRow) |
|
210 | { |
|
211 | 0 | ITableColumnEvaluator objEvaluator = getEvaluator(); |
212 | 0 | if (objEvaluator != null) |
213 | 0 | return objEvaluator.getColumnValue(this, objRow); |
214 | ||
215 | // default fallback |
|
216 | 0 | return objRow.toString(); |
217 | } |
|
218 | ||
219 | /** |
|
220 | * Use the column name to get the display name, as well as the column and |
|
221 | * value renderer sources from the provided component. |
|
222 | * |
|
223 | * @param objSettingsContainer |
|
224 | * the component from which to get the settings |
|
225 | */ |
|
226 | public void loadSettings(IComponent objSettingsContainer) |
|
227 | { |
|
228 | 0 | String strDisplayName = objSettingsContainer.getMessages().getMessage(getDisplayName()); |
229 | ||
230 | // Hack! the Messages inteface needs to restore the getMessage(key, |
|
231 | // default), or needs |
|
232 | // to add a containsKey(key) method. Looking for the '[' used with |
|
233 | // invalid/unknown keys. |
|
234 | ||
235 | 0 | if (!strDisplayName.startsWith("[")) |
236 | { |
|
237 | 0 | setDisplayName(strDisplayName); |
238 | } |
|
239 | ||
240 | 0 | super.loadSettings(objSettingsContainer); |
241 | 0 | } |
242 | ||
243 | /** |
|
244 | * |
|
245 | * @author mb |
|
246 | */ |
|
247 | 0 | public class DefaultTableComparator implements Comparator, Serializable |
248 | { |
|
249 | ||
250 | private static final long serialVersionUID = 1L; |
|
251 | ||
252 | public int compare(Object objRow1, Object objRow2) |
|
253 | { |
|
254 | 0 | Object objValue1 = getColumnValue(objRow1); |
255 | 0 | Object objValue2 = getColumnValue(objRow2); |
256 | ||
257 | 0 | if (objValue1 == objValue2) return 0; |
258 | ||
259 | 0 | boolean bComparable1 = objValue1 instanceof Comparable; |
260 | 0 | boolean bComparable2 = objValue2 instanceof Comparable; |
261 | ||
262 | // non-comparable values are considered equal |
|
263 | 0 | if (!bComparable1 && !bComparable2) |
264 | 0 | return 0; |
265 | ||
266 | // non-comparable values (null included) are considered smaller |
|
267 | // than the comparable ones |
|
268 | 0 | if (!bComparable1) |
269 | 0 | return -1; |
270 | ||
271 | 0 | if (!bComparable2) |
272 | 0 | return 1; |
273 | ||
274 | 0 | return ((Comparable) objValue1).compareTo(objValue2); |
275 | } |
|
276 | } |
|
277 | ||
278 | } |