KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.jstl.sql.Result;
19 import java.util.SortedMap JavaDoc;
20
21 /**
22   * @author Thomas Spiegl (latest modification by $Author: mwessendorf $)
23   * @version $Revision: 1.6 $ $Date: 2004/07/01 22:01:09 $
24 */

25 public class ResultDataModel extends DataModel
26 {
27     // FIELDS
28
private int _rowIndex = -1;
29     private SortedMap JavaDoc[] _data;
30
31     // CONSTRUCTORS
32
public ResultDataModel()
33     {
34         super();
35     }
36
37     public ResultDataModel(Result result)
38     {
39         if (result == null) throw new NullPointerException JavaDoc("result");
40         setWrappedData(result);
41     }
42
43     // METHODS
44
public int getRowCount()
45     {
46         if (_data == null)
47         {
48             return -1;
49         }
50         return _data.length;
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[_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.length;
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         if (data == null)
108         {
109             setRowIndex(-1);
110         }
111         else
112         {
113             _data = ((Result)data).getRows();
114             setRowIndex(0);
115         }
116     }
117
118 }
119
Popular Tags