Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ListDataModel |
|
| 2.4444444444444446;2.444 |
1 | /* | |
2 | * Copyright 2004-2011 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package javax.faces.model; | |
17 | ||
18 | import java.util.List; | |
19 | ||
20 | public class ListDataModel extends DataModel { | |
21 | ||
22 | 17 | private List list = null; |
23 | ||
24 | 17 | int index = -1; |
25 | ||
26 | public ListDataModel() { | |
27 | 6 | this(null); |
28 | 6 | } |
29 | ||
30 | public ListDataModel(List list) { | |
31 | 17 | super(); |
32 | 17 | setWrappedData(list); |
33 | 17 | } |
34 | ||
35 | public int getRowCount() { | |
36 | 2 | return (list != null) ? list.size() : -1; |
37 | } | |
38 | ||
39 | public Object getRowData() { | |
40 | 17 | if (list == null) { |
41 | 1 | return null; |
42 | } | |
43 | ||
44 | 16 | if (!isRowAvailable()) { |
45 | 1 | throw new IllegalArgumentException(); |
46 | } | |
47 | 15 | return list.get(index); |
48 | } | |
49 | ||
50 | public int getRowIndex() { | |
51 | 3 | return index; |
52 | } | |
53 | ||
54 | public Object getWrappedData() { | |
55 | 2 | return list; |
56 | } | |
57 | ||
58 | public boolean isRowAvailable() { | |
59 | 47 | if (list == null) { |
60 | 0 | return false; |
61 | } | |
62 | 47 | return (index >= 0 && index < list.size()); |
63 | } | |
64 | ||
65 | public void setRowIndex(int rowIndex) { | |
66 | ||
67 | 37 | if (rowIndex < -1) { |
68 | 1 | throw new IllegalArgumentException(); |
69 | } | |
70 | ||
71 | 36 | int oldIndex = index; |
72 | 36 | index = rowIndex; |
73 | ||
74 | 36 | if (list == null) { |
75 | 7 | return; |
76 | } | |
77 | ||
78 | 29 | DataModelListener[] listeners = getDataModelListeners(); |
79 | 29 | if ((oldIndex != index) && (listeners != null)) { |
80 | 27 | Object rowData = null; |
81 | 27 | if (isRowAvailable()) { |
82 | 14 | rowData = getRowData(); |
83 | } | |
84 | ||
85 | 27 | DataModelEvent event = new DataModelEvent(this, index, rowData); |
86 | 31 | for (int i = 0; i < listeners.length; i++) { |
87 | 4 | listeners[i].rowSelected(event); |
88 | } | |
89 | } | |
90 | ||
91 | 29 | } |
92 | ||
93 | public void setWrappedData(Object data) { | |
94 | 19 | list = (List) data; |
95 | 19 | int index = (list != null) ? 0 : -1; |
96 | 19 | setRowIndex(index); |
97 | 19 | } |
98 | ||
99 | } |