KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
19   * @author Thomas Spiegl (latest modification by $Author: mwessendorf $)
20   * @version $Revision: 1.5 $ $Date: 2004/07/01 22:01:10 $
21 */

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