1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.model;
20
21 import java.util.List;
22
23
24
25
26
27
28
29 public class ListDataModel extends DataModel
30 {
31
32
33 private int _rowIndex = -1;
34 private List _data;
35
36
37 public ListDataModel()
38 {
39 super();
40 }
41
42 public ListDataModel(List list)
43 {
44 if (list == null) throw new NullPointerException("list");
45 setWrappedData(list);
46 }
47
48
49 public int getRowCount()
50 {
51 if (_data == null)
52 {
53 return -1;
54 }
55 return _data.size();
56 }
57
58 public Object getRowData()
59 {
60 if (_data == null)
61 {
62 return null;
63 }
64 if (!isRowAvailable())
65 {
66 throw new IllegalArgumentException("row is unavailable");
67 }
68 return _data.get(_rowIndex);
69 }
70
71 public int getRowIndex()
72 {
73 return _rowIndex;
74 }
75
76 public Object getWrappedData()
77 {
78 return _data;
79 }
80
81 public boolean isRowAvailable()
82 {
83 if (_data == null)
84 {
85 return false;
86 }
87 return _rowIndex >= 0 && _rowIndex < _data.size();
88 }
89
90 public void setRowIndex(int rowIndex)
91 {
92 if (rowIndex < -1)
93 {
94 throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
95 }
96 int oldRowIndex = _rowIndex;
97 _rowIndex = rowIndex;
98 if (_data != null && oldRowIndex != _rowIndex)
99 {
100 Object data = isRowAvailable() ? getRowData() : null;
101 DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
102 DataModelListener[] listeners = getDataModelListeners();
103 for (int i = 0; i < listeners.length; i++)
104 {
105 listeners[i].rowSelected(event);
106 }
107 }
108 }
109
110 public void setWrappedData(Object data)
111 {
112 if (data == null)
113 {
114 setRowIndex(-1);
115 _data = null;
116 }
117 else
118 {
119 _data = (List)data;
120 _rowIndex = -1;
121 setRowIndex(0);
122 }
123 }
124
125 }