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.SortedMap;
22
23 import javax.servlet.jsp.jstl.sql.Result;
24
25
26
27
28
29 public class ResultDataModel extends DataModel<SortedMap<String, Object>>
30 {
31
32 private int _rowIndex = -1;
33 private Result _data;
34
35
36 public ResultDataModel()
37 {
38 super();
39 }
40
41 public ResultDataModel(Result result)
42 {
43 if (result == null)
44 {
45 throw new NullPointerException("result");
46 }
47 setWrappedData(result);
48 }
49
50
51 @Override
52 public int getRowCount()
53 {
54 if (getRows() == null)
55 {
56 return -1;
57 }
58 return getRows().length;
59 }
60
61 @Override
62 public SortedMap<String, Object> getRowData()
63 {
64 if (getRows() == null)
65 {
66 return null;
67 }
68 if (!isRowAvailable())
69 {
70 throw new IllegalArgumentException("row is unavailable");
71 }
72 return getRows()[_rowIndex];
73 }
74
75 @Override
76 public int getRowIndex()
77 {
78 return _rowIndex;
79 }
80
81 @Override
82 public Object getWrappedData()
83 {
84 return _data;
85 }
86
87 @Override
88 public boolean isRowAvailable()
89 {
90 return getRows() != null && _rowIndex >= 0 && _rowIndex < getRows().length;
91 }
92
93 @Override
94 public void setRowIndex(int rowIndex)
95 {
96 if (rowIndex < -1)
97 {
98 throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
99 }
100 int oldRowIndex = _rowIndex;
101 _rowIndex = rowIndex;
102 if (getRows() != null && oldRowIndex != _rowIndex)
103 {
104 SortedMap<String, Object> data = isRowAvailable() ? getRowData() : null;
105 DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
106 DataModelListener[] listeners = getDataModelListeners();
107 for (int i = 0; i < listeners.length; i++)
108 {
109 listeners[i].rowSelected(event);
110 }
111 }
112 }
113
114 private SortedMap<String, Object>[] getRows()
115 {
116 if (_data == null)
117 {
118 return null;
119 }
120
121 return _data.getRows();
122 }
123
124 @Override
125 public void setWrappedData(Object data)
126 {
127 if (data == null)
128 {
129 setRowIndex(-1);
130 _data = null;
131 }
132 else
133 {
134 _data = ((Result)data);
135 _rowIndex = -1;
136 setRowIndex(0);
137 }
138 }
139
140 }