1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.model; |
17 | |
|
18 | |
import java.util.SortedMap; |
19 | |
|
20 | |
import javax.servlet.jsp.jstl.sql.Result; |
21 | |
|
22 | |
public class ResultDataModel extends DataModel { |
23 | |
|
24 | 14 | private Result result = null; |
25 | |
|
26 | 14 | private int index = -1; |
27 | |
|
28 | 14 | private SortedMap[] rows_ = null; |
29 | |
|
30 | |
public ResultDataModel() { |
31 | 6 | this(null); |
32 | 6 | } |
33 | |
|
34 | |
public ResultDataModel(Result result) { |
35 | 14 | super(); |
36 | 14 | setWrappedData(result); |
37 | 14 | } |
38 | |
|
39 | |
public int getRowCount() { |
40 | 2 | return (result != null) ? rows_.length : -1; |
41 | |
} |
42 | |
|
43 | |
public Object getRowData() { |
44 | 17 | if (result == null) { |
45 | 1 | return null; |
46 | |
} |
47 | |
|
48 | 16 | if (!isRowAvailable()) { |
49 | 1 | throw new IllegalArgumentException(); |
50 | |
} |
51 | 15 | return rows_[index]; |
52 | |
} |
53 | |
|
54 | |
public int getRowIndex() { |
55 | 3 | return index; |
56 | |
} |
57 | |
|
58 | |
public Object getWrappedData() { |
59 | 3 | return result; |
60 | |
} |
61 | |
|
62 | |
public boolean isRowAvailable() { |
63 | 36 | if (result == null) { |
64 | 1 | return false; |
65 | |
} |
66 | 35 | return (index >= 0 && index < rows_.length); |
67 | |
} |
68 | |
|
69 | |
public void setRowIndex(int rowIndex) { |
70 | |
|
71 | 24 | if (rowIndex < -1) { |
72 | 1 | throw new IllegalArgumentException(); |
73 | |
} |
74 | |
|
75 | 23 | int oldIndex = index; |
76 | 23 | index = rowIndex; |
77 | |
|
78 | 23 | if (result == null) { |
79 | 7 | return; |
80 | |
} |
81 | |
|
82 | 16 | DataModelListener[] listeners = getDataModelListeners(); |
83 | 16 | if ((oldIndex != index) && (listeners != null)) { |
84 | 16 | Object rowData = null; |
85 | 16 | if (isRowAvailable()) { |
86 | 13 | rowData = getRowData(); |
87 | |
} |
88 | |
|
89 | 16 | DataModelEvent event = new DataModelEvent(this, index, rowData); |
90 | 18 | for (int i = 0; i < listeners.length; i++) { |
91 | 2 | listeners[i].rowSelected(event); |
92 | |
} |
93 | |
} |
94 | |
|
95 | 16 | } |
96 | |
|
97 | |
public void setWrappedData(Object data) { |
98 | 15 | if (data == null) { |
99 | 6 | setRowIndex(-1); |
100 | |
} else { |
101 | 9 | result = (Result) data; |
102 | 9 | rows_ = result.getRows(); |
103 | 9 | setRowIndex(0); |
104 | |
} |
105 | 15 | } |
106 | |
|
107 | |
} |