01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
20: *
21: * @author Thomas Spiegl (latest modification by $Author: mbr $)
22: * @version $Revision: 512227 $ $Date: 2007-02-27 13:25:16 +0100 (Di, 27 Feb 2007) $
23: */
24: public class ArrayDataModel extends DataModel {
25: // FIELDS
26: private int _rowIndex = -1;
27: private Object[] _data;
28:
29: // CONSTRUCTORS
30: public ArrayDataModel() {
31: super ();
32: }
33:
34: public ArrayDataModel(Object[] array) {
35: if (array == null)
36: throw new NullPointerException("array");
37: setWrappedData(array);
38: }
39:
40: // METHODS
41: public int getRowCount() {
42: if (_data == null) {
43: return -1;
44: }
45: return _data.length;
46: }
47:
48: public Object getRowData() {
49: if (_data == null) {
50: return null;
51: }
52: if (!isRowAvailable()) {
53: throw new IllegalArgumentException("row is unavailable");
54: }
55: return _data[_rowIndex];
56: }
57:
58: public int getRowIndex() {
59: return _rowIndex;
60: }
61:
62: public Object getWrappedData() {
63: return _data;
64: }
65:
66: public boolean isRowAvailable() {
67: if (_data == null) {
68: return false;
69: }
70: return _rowIndex >= 0 && _rowIndex < _data.length;
71: }
72:
73: public void setRowIndex(int rowIndex) {
74: if (rowIndex < -1) {
75: throw new IllegalArgumentException("illegal rowIndex "
76: + rowIndex);
77: }
78: int oldRowIndex = _rowIndex;
79: _rowIndex = rowIndex;
80: if (_data != null && oldRowIndex != _rowIndex) {
81: Object data = isRowAvailable() ? getRowData() : null;
82: DataModelEvent event = new DataModelEvent(this , _rowIndex,
83: data);
84: DataModelListener[] listeners = getDataModelListeners();
85: for (int i = 0; i < listeners.length; i++) {
86: listeners[i].rowSelected(event);
87: }
88: }
89: }
90:
91: public void setWrappedData(Object data) {
92: _data = (Object[]) data;
93: int rowIndex = _data != null ? 0 : -1;
94: setRowIndex(rowIndex);
95: }
96:
97: }
|