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