KickJava   Java API By Example, From Geeks To Geeks.

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


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.4 $ $Date: 2004/07/01 22:01:10 $
21 */

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