Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ListDataModel |
|
| 2.5555555555555554;2.556 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package javax.faces.model; | |
20 | ||
21 | import java.util.List; | |
22 | ||
23 | /** | |
24 | * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a> | |
25 | * | |
26 | * @author Thomas Spiegl (latest modification by $Author: skitching $) | |
27 | * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $ | |
28 | */ | |
29 | public class ListDataModel extends DataModel | |
30 | { | |
31 | ||
32 | // FIELDS | |
33 | 0 | private int _rowIndex = -1; |
34 | private List _data; | |
35 | ||
36 | // CONSTRUCTORS | |
37 | public ListDataModel() | |
38 | { | |
39 | 0 | super(); |
40 | 0 | } |
41 | ||
42 | public ListDataModel(List list) | |
43 | 0 | { |
44 | 0 | if (list == null) throw new NullPointerException("list"); |
45 | 0 | setWrappedData(list); |
46 | 0 | } |
47 | ||
48 | // METHODS | |
49 | public int getRowCount() | |
50 | { | |
51 | 0 | if (_data == null) |
52 | { | |
53 | 0 | return -1; |
54 | } | |
55 | 0 | return _data.size(); |
56 | } | |
57 | ||
58 | public Object getRowData() | |
59 | { | |
60 | 0 | if (_data == null) |
61 | { | |
62 | 0 | return null; |
63 | } | |
64 | 0 | if (!isRowAvailable()) |
65 | { | |
66 | 0 | throw new IllegalArgumentException("row is unavailable"); |
67 | } | |
68 | 0 | return _data.get(_rowIndex); |
69 | } | |
70 | ||
71 | public int getRowIndex() | |
72 | { | |
73 | 0 | return _rowIndex; |
74 | } | |
75 | ||
76 | public Object getWrappedData() | |
77 | { | |
78 | 0 | return _data; |
79 | } | |
80 | ||
81 | public boolean isRowAvailable() | |
82 | { | |
83 | 0 | if (_data == null) |
84 | { | |
85 | 0 | return false; |
86 | } | |
87 | 0 | return _rowIndex >= 0 && _rowIndex < _data.size(); |
88 | } | |
89 | ||
90 | public void setRowIndex(int rowIndex) | |
91 | { | |
92 | 0 | if (rowIndex < -1) |
93 | { | |
94 | 0 | throw new IllegalArgumentException("illegal rowIndex " + rowIndex); |
95 | } | |
96 | 0 | int oldRowIndex = _rowIndex; |
97 | 0 | _rowIndex = rowIndex; |
98 | 0 | if (_data != null && oldRowIndex != _rowIndex) |
99 | { | |
100 | 0 | Object data = isRowAvailable() ? getRowData() : null; |
101 | 0 | DataModelEvent event = new DataModelEvent(this, _rowIndex, data); |
102 | 0 | DataModelListener[] listeners = getDataModelListeners(); |
103 | 0 | for (int i = 0; i < listeners.length; i++) |
104 | { | |
105 | 0 | listeners[i].rowSelected(event); |
106 | } | |
107 | } | |
108 | 0 | } |
109 | ||
110 | public void setWrappedData(Object data) | |
111 | { | |
112 | 0 | _data = (List)data; |
113 | 0 | int rowIndex = _data != null ? 0 : -1; |
114 | 0 | setRowIndex(rowIndex); |
115 | 0 | } |
116 | ||
117 | } |