KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > component > html > ext > _SerializableDataModel


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 org.apache.myfaces.component.html.ext;
17
18 import javax.faces.model.DataModel;
19 import javax.faces.model.DataModelEvent;
20 import javax.faces.model.DataModelListener;
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * @author Manfred Geiler (latest modification by $Author: matze $)
27  * @version $Revision: 1.6 $ $Date: 2004/10/13 11:50:57 $
28  */

29 class _SerializableDataModel
30         extends DataModel
31         implements Serializable JavaDoc
32 {
33     //private static final Log log = LogFactory.getLog(_SerializableDataModel.class);
34

35     protected int _first;
36     protected int _rows;
37     protected int _rowCount;
38     protected List JavaDoc _list;
39     private transient int _rowIndex = -1;
40
41     public _SerializableDataModel(int first, int rows, DataModel dataModel)
42     {
43         _first = first;
44         _rows = rows;
45         _rowCount = dataModel.getRowCount();
46         if (_rows <= 0)
47         {
48             _rows = _rowCount - first;
49         }
50         _list = new ArrayList JavaDoc(rows);
51         for (int i = 0; i < _rows; i++)
52         {
53             dataModel.setRowIndex(_first + i);
54             if (!dataModel.isRowAvailable()) break;
55             _list.add(dataModel.getRowData());
56         }
57         _rowIndex = -1;
58
59         DataModelListener[] dataModelListeners = dataModel.getDataModelListeners();
60         for (int i = 0; i < dataModelListeners.length; i++)
61         {
62             DataModelListener dataModelListener = dataModelListeners[i];
63             addDataModelListener(dataModelListener);
64         }
65     }
66
67     protected _SerializableDataModel()
68     {
69     }
70
71     public int getFirst()
72     {
73         return _first;
74     }
75
76     public void setFirst(int first)
77     {
78         _first = first;
79     }
80
81     public int getRows()
82     {
83         return _rows;
84     }
85
86     public void setRows(int rows)
87     {
88         _rows = rows;
89     }
90
91     public boolean isRowAvailable()
92     {
93         return _rowIndex >= _first &&
94             _rowIndex < _first + _rows &&
95             _rowIndex < _rowCount &&
96             _list.size() > _rowIndex - _first;
97     }
98
99     public int getRowCount()
100     {
101         return _rowCount;
102     }
103
104     public Object JavaDoc getRowData()
105     {
106         if (!isRowAvailable())
107         {
108             throw new IllegalStateException JavaDoc();
109         }
110         return _list.get(_rowIndex - _first);
111     }
112
113     public int getRowIndex()
114     {
115         return _rowIndex;
116     }
117
118     public void setRowIndex(int rowIndex)
119     {
120         if (rowIndex < -1)
121         {
122             throw new IllegalArgumentException JavaDoc();
123         }
124
125         int oldRowIndex = _rowIndex;
126         _rowIndex = rowIndex;
127         if (oldRowIndex != _rowIndex)
128         {
129             Object JavaDoc data = isRowAvailable() ? getRowData() : null;
130             DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
131             DataModelListener[] listeners = getDataModelListeners();
132             for (int i = 0; i < listeners.length; i++)
133             {
134                 listeners[i].rowSelected(event);
135             }
136         }
137     }
138
139     public Object JavaDoc getWrappedData()
140     {
141         return _list;
142     }
143
144     public void setWrappedData(Object JavaDoc obj)
145     {
146         if (obj != null)
147         {
148             throw new IllegalArgumentException JavaDoc("Cannot set wrapped data of _SerializableDataModel");
149         }
150     }
151
152
153
154     /*
155     // StateHolder interface
156
157     public Object saveState(FacesContext context)
158     {
159         Object values[] = new Object[4];
160         values[0] = new Integer(_first);
161         values[1] = new Integer(_rows);
162         values[2] = new Integer(_rowCount);
163         values[3] = _list;
164         return ((Object) (values));
165     }
166
167     public void restoreState(FacesContext context, Object state)
168     {
169         Object values[] = (Object[])state;
170         _first = ((Integer)values[0]).intValue();
171         _rows = ((Integer)values[1]).intValue();
172         _rowCount = ((Integer)values[2]).intValue();
173         _list = (List)values[3];
174     }
175
176     public boolean isTransient()
177     {
178         return false;
179     }
180
181     public void setTransient(boolean newTransientValue)
182     {
183         throw new UnsupportedOperationException();
184     }
185     */

186 }
187
Popular Tags