01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * Resin Open Source is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License version 2
11: * as published by the Free Software Foundation.
12: *
13: * Resin Open Source is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
16: * of NON-INFRINGEMENT. See the GNU General Public License for more
17: * details.
18: *
19: * You should have received a copy of the GNU General Public License
20: * along with Resin Open Source; if not, write to the
21: *
22: * Free Software Foundation, Inc.
23: * 59 Temple Place, Suite 330
24: * Boston, MA 02111-1307 USA
25: *
26: * @author Scott Ferguson
27: */
28:
29: package javax.faces.model;
30:
31: public class ArrayDataModel extends DataModel {
32: private Object[] _array;
33: private int _rowIndex = -1;
34:
35: public ArrayDataModel() {
36: }
37:
38: public ArrayDataModel(Object[] array) {
39: _array = array;
40: setRowIndex(0);
41: }
42:
43: public int getRowCount() {
44: if (_array != null)
45: return _array.length;
46: else
47: return -1;
48: }
49:
50: public Object getRowData() {
51: if (_array == null)
52: return null;
53: else if (_rowIndex < _array.length)
54: return _array[_rowIndex];
55: else
56: throw new IllegalArgumentException("row " + _rowIndex
57: + " is not available in " + _array.length);
58: }
59:
60: public boolean isRowAvailable() {
61: return _array != null && _rowIndex < _array.length;
62: }
63:
64: public Object getWrappedData() {
65: return _array;
66: }
67:
68: public void setWrappedData(Object data) {
69: _array = (Object[]) data;
70: setRowIndex(0);
71: }
72:
73: public int getRowIndex() {
74: return _rowIndex;
75: }
76:
77: public void setRowIndex(int index) {
78: if (_array != null && index < -1)
79: throw new IllegalArgumentException("rowIndex '" + index
80: + "' cannot be less than -1.");
81:
82: DataModelListener[] listeners = getDataModelListeners();
83:
84: if (listeners.length > 0 && _array != null
85: && _rowIndex != index) {
86: DataModelEvent event = new DataModelEvent(this , index,
87: _array);
88:
89: for (int i = 0; i < listeners.length; i++) {
90: listeners[i].rowSelected(event);
91: }
92: }
93:
94: _rowIndex = index;
95: }
96: }
|