KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > model > ListDataModel


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package javax.faces.model;
17
18 import java.util.List JavaDoc;
19
20 /**
21   * @author Thomas Spiegl (latest modification by $Author: mwessendorf $)
22   * @version $Revision: 1.6 $ $Date: 2004/07/01 22:01:10 $
23 */

24 public class ListDataModel extends DataModel
25 {
26
27     // FIELDS
28
private int _rowIndex = -1;
29     private List JavaDoc _data;
30
31     // CONSTRUCTORS
32
public ListDataModel()
33     {
34         super();
35     }
36
37     public ListDataModel(List JavaDoc list)
38     {
39         if (list == null) throw new NullPointerException JavaDoc("list");
40         setWrappedData(list);
41     }
42
43     // METHODS
44
public int getRowCount()
45     {
46         if (_data == null)
47         {
48             return -1;
49         }
50         return _data.size();
51     }
52
53     public Object JavaDoc getRowData()
54     {
55         if (_data == null)
56         {
57             return null;
58         }
59         if (!isRowAvailable())
60         {
61             throw new IllegalArgumentException JavaDoc("row is unavailable");
62         }
63         return _data.get(_rowIndex);
64     }
65
66     public int getRowIndex()
67     {
68         return _rowIndex;
69     }
70
71     public Object JavaDoc getWrappedData()
72     {
73         return _data;
74     }
75
76     public boolean isRowAvailable()
77     {
78         if (_data == null)
79         {
80             return false;
81         }
82         return _rowIndex >= 0 && _rowIndex < _data.size();
83     }
84
85     public void setRowIndex(int rowIndex)
86     {
87         if (rowIndex < -1)
88         {
89             throw new IllegalArgumentException JavaDoc("illegal rowIndex " + rowIndex);
90         }
91         int oldRowIndex = _rowIndex;
92         _rowIndex = rowIndex;
93         if (_data != null && oldRowIndex != _rowIndex)
94         {
95             Object JavaDoc data = isRowAvailable() ? getRowData() : null;
96             DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
97             DataModelListener[] listeners = getDataModelListeners();
98             for (int i = 0; i < listeners.length; i++)
99             {
100                 listeners[i].rowSelected(event);
101             }
102         }
103     }
104
105     public void setWrappedData(Object JavaDoc data)
106     {
107         _data = (List JavaDoc)data;
108         int rowIndex = _data != null ? 0 : -1;
109         setRowIndex(rowIndex);
110     }
111
112 }
113
Popular Tags